Accessing the contents of a KVM disk image file

KVM can use either an entire partition to simulate a disk for the guest operating system, or a single file. By default the file type used is a "raw" file compatible with other virtualization systems, like Xen.

In order to mount the image file of the guest OS we need to first map a loop device to it.
The command losetup is used to associate files with loop devices. First check what the next available loopback device is with the -f argument which is used to find the next available device:


In this case there are no other loop devices and /dev/loop7 is available. This can then be associated with the VM image file:


Within a single image file there may be multiple partitions (e.g. /, /boot, etc.). The partition table can be read from the new (loop) device and individual devices for each partition created. This is done with the kpartx command (it creates map devices from device partition tables):


This created the following files in /dev/mapper directory:


Now we can mount the files as we would normally do:


To revert back execute the following in this order:


1 comment:

  1. Hi,
    My guest not starting. I have backup before 8 hours so it's not bad. What can i do if kpartx -av /dev/loop7 do nothing?

    ReplyDelete