0

I just backed up a remote system using rsync. I have a directory that I can chroot into and would now like to boot it up as a VM. I know that qemu has -kernel capability, as well as -drive file=fat: virtual FAT feature. I was hoping that maybe with -append and a bit of modifying of /etc/fstab I could boot the thing up, but unfortunately the early tests suggest a problem with device files in the backup directory:

# kvm -kernel boot/vmlinuz-4.15.0-99-generic -drive file=fat:`pwd`
qemu-system-x86_64: -drive file=fat:/home/d33tah/workspace/hakierspejs/backup/mounted: Could not read directory /home/d33tah/workspace/hakierspejs/backup/mounted/dev/fd/12

What other options do I have if I want to have the image bootable with minimum hassle after each iteration of backing up? I'm looking for a "set up once, use often" use case.

d33tah
  • 301
  • 4
  • 15

2 Answers2

0

Virtual FAT is just a FAT filesystem type indeed. Its not a linux ext filesystem so it's not really possible to boot it from there. Not at least without a lot of modifications.

You can follow this step by step to prepare new image in a file container:

  1. dd if=/dev/zero of=/some/path/some/file.dat bs=1M count=[SIZE_IN_MB]M This will create a new empty file of SIZE_IN_MB Mb size.
  2. mke2fs /some/path/some/file.dat This will create ext filesystem inside that file.
  3. mkdir /mnt/newimage && sudo mount -o loop /some/path/some/file.dat /mnt/newimage This will mount your file with ext fs to /mnt/newimage.

Then you can rsync/copy your backup data there. Once done, you can umount that image. Also check to detach loop bind with losetup -d /some/path/some/file.dat. Now you have a proper ext fs image inside /some/path/some/file.dat, you can try booting with QEMU of it (it's of the type RAW).

Another (and better way) is to migrate your original system to QEMU VM. Backing up & restoring VMs is much easier and add more consistency to your backups.

NStorm
  • 1,248
  • 7
  • 18
0

I asked around and it looks like there's no easy way out of the situation - except for maybe booting via NFS or other similar hacks. Here's how to quickly create an ext4 drive from a source directory:

#!/bin/bash

set -euo pipefail
set -x

FNAME=ext4image.bin
SRCDIR=source-directory
DSTDIR=temporary-empty-directory

mkdir -pv $DSTDIR
truncate -s 20G $FNAME
mkfs.ext4 -F $FNAME
mount $FNAME $DSTDIR
tar cf - $SRCDIR | pv | tar xf - --strip=1 -C $DSTDIR
umount $DSTDIR

Assuming that you have all of the mounts in a single filesystem now, the first step is to modify /etc/fstab so that it only contains the following:

/dev/root / ext4 defaults 0 0

Now you can use the script above to create an ext4 image. You can boot it the following way:

kvm ext4image.bin -kernel mounted/boot/vmlinuz-4.15.0-99-generic -append root=/dev/sda -initrd mounted/boot/initrd.img-4.15.0-99-generic -m 2G

Kernel version may obviously vary.

d33tah
  • 301
  • 4
  • 15