libguestfs is a C library tool for creating, accessing and modifying virtual machine disk images. You can look inside the disk images, modify the files they contain, create them from scratch, resize them, and much more.
The easy way to modify files in a VM’s disk image without powering on the VM, you could “mount” the disk image. You then would be able to modify the content of the disk image before unmounting it. libguestfs works with any disk image, including ones created in VMware, KVM, qemu, VirtualBox, Xen, and many other hypervisors, and ones which you have created from scratch.
Overview of libguestfs
There are many tools which use libguestfs:
guestfish – Interactive and scriptable shell.
guestmount – Mount filesystems from any guest or disk image on the host.
guestunmount – unmount guest filesystem
virt-alignment – scan Check alignment of partitions in guests.
virt-builder – quick image builder
virt-cat – Display a file from a guest.
virt-copy-in – Copy files and directories into a guest.
virt-copy-out – Copies files and directories out of a virtual machine disk image or named libvirt domain.
virt-customize – customize virtual machines
virt–df Display – disk usage of a guest.
virt-diff – Differences between files in two virtual machines
virt-edit – Edit a file in a guest.
virt–filesystems – Display the partitions, filesystems, logical volumes etc. in a guest.
virt-inspector – Inspect a guest and produce a report detailing the operating system, version, applications installed and more.
virt-format – erase and make blank disks
virt-ls – List files and directories in a guest.
virt-log – display log files
virt-make-fs – Make a new filesystem.
virt-rescue – Rescue mode for guests.
virt-resize – Resize a guest.
virt-sparsify – Make a disk sparse.
virt–sysprep – Reset a guest to “factory configuration”.
virt-tar-in – Copy files from a tarball into a guest.
virt-tar-out – Copy files out of a guest into a tarball.
virt-win-reg – Export and merge Windows Registry entries from a Windows guest
libguestfs-test-tool — test libguestfs
libguestfs-make-fixed-appliance — make libguestfs fixed appliance
BoxGrinder & Oz Projects that can create guests from scratch.
OpenStack Uses libguestfs to inject files into guests.
virt-manager Uses libguestfs to display icons and applications in guests.
virt-v2v & virt-p2v Migrate between physical machines and different hypervisors
How to install libguestfs toolset on Linux
On Debian-based system:
# sudo apt-get install libguestfs-tools
On Red Hat-based system:
# sudo yum install libguestfs-tools
To mount qcow2 image volume at localfolder.
guestmount -a /path/to/qcow2/image -m <device> /local_path
The “-m <device>” option is used to specify a partition within the disk image.
run the following command.
#guestmount -a /images/testcentos.qcow2 -m /dev/sda1 /mnt
By default, the disk image is mounted in “read-write” mode. So you can modify any file in /mnt after mounting it.