Libguestfs provides an easy way to access data in virtual machine disk image files, even if you cannot power on the virtual machine. Libguestfs consists of a rich set of tools that allows administrators to access virtual machine files and make modifications or even rescue the files.
One reason that libguestfs is popular is that it can access any virtual machine (VM) file as long as the VM is managed by libvirt, meaning you can manage KVM, Xen, QEMU and other types of VMs. It can also access many types of file systems, including
After installing the libguestfs package on your Linux distribution, there are two ways to gain VM access. You can either use guestfish, a shell environment that offers commands to modify VM disk files, or you can use one of the tools installed as binaries on the host operating system.
Let's start with an easy example. The virt-df tool can show available disk space in a VM. You can use it on running VMs, but also on VMs that haven't been started yet. To request information about an active domain, use virt-df -d sles11 -h, which gives information in human readable format (-h) about the domain sles11. The advantage of this and other libguestfs utilities is that they can access the VM files as if they were on a locally mounted file system.
Instead of accessing files based on the name of the VM, you can also work directly on the image file. Although this is a more direct VM access method, the chance of an error increases. If the VM from the previous example is using a storage back end on /dev/vgvms/lvsles11-1, you could also use the command virt-edit -a /dev/vgvms/lvsles11-1 /etc/hosts to access and modify the file directly. This access to VM files gives you a lot of flexibility to fix broken VMs.
Files starting with virt- in libguestfs are developed for easy access to VMs, and are oriented towards specific tasks, as you have seen with the virt-edit and virt-df tools. If you need more direct access to files within a VM disk image, you can use guestfish or guestmount as an alternative.
With guestfish, you open an interactive shell to the VM disk image, which allows you to work on the VM files directly, but only with tools from the libguestfs environment. That means that in a guestfish environment you won't be able to use normal Linux tools like ls to get a listing of files. With guestmount, you can mount a VM disk image into the file system of the computer you're currently working on, which makes it easy to perform tasks like copying files between the host and guest machine.
Both guestfish and guestmount allow you to work on either the complete VM or just a specific disk image file. You could use guestfish -i -a /dev/vgvms/lvsles11-1 to access files on the lvsless11-1 image file, or guestmount -i -a /dev/vgvms/lvsles11-1 /mnt to mount the contents of the lvsles1101 disk image on the /mnt directory. From there you can perform any VM management task even if you cannot power on the VM.
The last tool worth mentioning is virt-rescue. With virt-rescue, you can start a rescue environment on a VM. It works like all the other tools in the libguestfs tools, either working directly on the domain using the -d option, or in the image files using the -a option. After booting into the rescue environment, you first need to mount the partition in the VM, using the mount-rootfs-and-do-chroot.sh command. Once you've done that, all files in the VM are available in the rescue shell and you can start analyzing and repairing them.
Libguestfs tools provide an easy and convenient way to access VM disk image files directly. Using these tools makes it easy to perform common tasks without hassle and to repair VMs that you cannot start.
This was first published in February 2014