Note: This requires libguestfs ≥ 1.4
If files/
is a directory containing some files, you can create a new disk image populated with those files using the command below (virt-make-fs can also be used for this):
$ guestfish -N fs:ext2:400M -m /dev/sda1 tar-in <(tar -C files/ -cf - .) /
Explanation:
-
guestfish -N fs:ext2:400M
creates a prepared disk image which is 400MB in size with a single partition formatted as ext2. The new disk image is calledtest1.img
. -
-m /dev/sda1
mounts the new filesystem when guestfish starts up. -
tar-in [...] /
uploads and unpacks the tar file into the root directory of the mounted disk image. -
<(tar -C files/ -cf - .)
is a bash process substitution which runs the tar command and supplies the output (ie. the tar file) as the input on the command line.
This is a quick way to create “appliances” using febootstrap and libguestfs, although you should note that I don’t think these appliances would really work, I just use them for testing our virtualization management tools, like the ability to detect and manage disk images:
$ febootstrap -i fedora-release -i bash -i setup -i coreutils fedora-13 f13 $ echo '/dev/sda1 / ext2 defaults 1 1' > fstab $ febootstrap-install f13 fstab /etc 0644 root.root $ febootstrap-minimize f13 $ guestfish -N fs:ext2:40M -m /dev/sda1 tar-in <(tar -C f13 -cf - .) / $ guestfish --ro -i -a test1.img Welcome to guestfish, the libguestfs filesystem interactive shell for editing virtual machine filesystems. Type: 'help' for a list of commands 'man' to read the manual 'quit' to quit the shell Operating system: Fedora release 13 (Goddard) /dev/vda1 mounted on / ><fs> cat /etc/fstab /dev/sda1 / ext2 defaults 1 1
Sounds very useful! I am thinking in particular in backup/restore situations.
I e.g. use BackupPC for backing up my KVM guests.
Could guestfish now be used to create a bootable image?
And could it be a LVM LV instead of a file?
Can guestfish be used for backups is a pretty common question. I should add it to the FAQ. The short answer is no. The longer answer is here:
https://www.redhat.com/archives/libguestfs/2010-August/msg00024.html
Also you shouldn’t use this to create appliances. Do it properly and use something like puppet and kickstart.
And could it be a LVM LV instead of a file?
Yes. The guestfish -N option can only create files, so it’s a (very little) bit more effort.
I wasn’t thinking about using guestfish for the backup. My back ups are done correctly with BackupPC, so I was wondering if guestfish can create a bootable image, where I have restored a boot partition from BackupPC?
About LVM:
So you would let guestfish create the file, and use
dd bs=1M if=/dev/zero of=/dev/vg_server1/vm10
qemu-img convert ~/vm10.qcow2 -O raw /dev/vg_server1/vm10
?
Yes, guestfish should be able to create a bootable partition (eg. by installing grub or by copying in NTLDR/BOOTMGR). Why doesn’t BackupPC make your restored images bootable?
To create an LV, you can do it directly (tested):
Unlike the plain file case, you need to run guestfish as root in this case (so that it can write to the LV). Note that /dev/sda* refers to the partition names on the disk image, inside the LV, not on the host.
About BackupPC: It uses rsync to do the backup and restore, so it is completely file based back up.
Very cool script you gave me there! I am definitely going to try that tomorrow.
How would you install grub?
Right now I read device type from /etc/grub.conf and do:
grub-install –recheck –root-directory=/ /dev/xvda
grub –device-map=/dev/null –batch <<EOT
device (hd0) /dev/xvda
root (hd0,0)
setup –stage2=/boot/grub/stage2 (hd0)
EOT
Is there a better and perhaps automated way?
guestfish has a grub-install command. Whether it is sufficient to do the rather complex installation that you need above is another question. You can also write arbitrary files using the write command.