How to use libguestfs tools to Edit Virtual machine (VM) Disk Images in CentOS/RHEL/Fedora?

Updated on September 2, 2017

Question: By mistake I had disabled ‘root’ account in a CentOS Virtual machine running in VMWare. Thereafter, I tried all possible ways to recover ‘root’ account and finally decided to reboot the VM in a single user mode. Unfortunately, the VM failed to boot in single user mode and the error messages indicated that the boot process is expecting root account and the process hung indefinitely. Below is the screenshot explaining the scenario:

vm root account recovery

In order to fix the issue, I have to somehow edit /etc/passwd file and enable root account. To that, I need the Virtual Machine disk image – in my case, the VM was running on VMWare, so I need the vmdk file.

Solution:

In this tutorial, I’ll show you how to use libguestfs tools to access and edit Virtual Machine (VM) Disk Image file.  The libguestfs is a C library with set of tools to view and edit files inside guests, make changes to VMs, monitor disk usage, clone VMs, format disks, resize disks and many more. The tool is capable of reading VM disk image from CDs, USB disks and remotely via SSH as well.

Before using libguestfs tools, get ready with your virtual machine disk image file. The libguestfs supports proprietary systems like VMware and Hyper-V as well.

Install libguestfs tools:

On CentOS/Fedora/RHEL:

sudo yum install libguestfs-tools

On Debian/Ubuntu:

sudo apt-get install libguestfs-tools

As I told earlier, libguestfs tools comes with many commands such as guestfish, guestmount, guestunmount, virt-alignment-scan, virt-builder, virt-cat, virt-copy, virt-edit, virt-diff, virt-customize, virt-filesystems, virt-format, virt-get-kernel, virt-inspector, virt-list-filesystems, virt-list-partitions, virt-log, virt-ls, virt-make-fs, virt-p2v, virt-p2v-make-disk, virt-rescue, virt-tar and many more. But we’ll be using guestfish command – which allows you to access and modify virtual machine images.

guestfish:

The guestfish command can be used as both interactive shell and command-line tool. The command allows you to open a disk image in both read-only (–ro option) and read-write (–rw option) modes.

Caution: Make sure to shutdown the VM before using guesfish to access/modify the image.

Using guestfish as an interactive shell:

# guestfish
Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.
Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell

Let’s now access the vmdk image file (say test-flat.vmdk).

><fs> add-ro test-flat.vmdk

Note: Lookout for the command add-ro which means, adding or accessing the image in read-only mode. If you want to make changes to the image, then you need to open the image with read-write privilege (we will see this later).

Always execute run command before listing the file systems in the disk image as shown below:

><fs> run
â 25% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠--:-â 50% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠00:3â 75% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠00:1 100% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠00:00

The above command will take a while to read the image. Once done, you can list the available file systems in the disk as shown below:

><fs> list-filesystems
/dev/vda1: ext4
/dev/VolGroup/lv_home: ext4
/dev/VolGroup/lv_root: ext4
/dev/VolGroup/lv_swap: swap

Now, I need to mount /dev/VolGroup/lv_root for accessing /etc/passwd.

><fs> mount /dev/VolGroup/lv_root /

As I have opened the image in read-only mode, I can only view /etc/passwd.

> cat /etc/passwd
::::::::::::::::::::::

Here’s the complete snapshot:

# guestfish
Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.
Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell
><fs> add-ro test-flat.vmdk
><fs> run
â 50% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠--:-â 75% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠00:0 100% â¦âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ⧠00:00
><fs> mount /dev/VolGroup/lv_root /
><fs> cat /etc/passwd
::::::::::::::::::::::::::::::
><fs> exit

Using guestfish as a command and edit a file:

# guestfish --rw --add test-flat.vmdk -i edit /etc/passwd

Lookout for the options

  • –rw – to open the VM disk image in read-write mode
  • –add – adds the ‘test-flat.vmdk’ image
  • -i – automatically mounts the disks from a virtual machine image
  • edit – command to edit the file

The above command will allow you to edit a file directly from the image and saves it as well.

Few more examples:

Assume, you want to update /etc/resolv.conf in a guest, then here’s how you can do that.

# guestfish \
   add disk.vmdk : run : mount /dev/VolGroup/lv_root / : \
   write /etc/resolv.conf "nameserver 8.8.8.8"

Using guestfish to access a remote disk image using ssh:

# guestfish -a ssh://somedomain.com/path/to/disk.vmdk

guestfish can help you to do wonders, so it’s a good idea to look at its help page.

# guestfish -N help

That’s it. Checkout libguestfs.org for more information.

Was this article helpful?

Related Articles

Leave a Comment