Mounting partitions stored in a logical volume (or a disk image)

With the increased use of virtualization has come the increased use of disk images (.iso, .vmdl, .qcow2, .vdi, etc) and LVM’s logical volumes (LV), as dedicated storage for Virtual Machines (VM). Frequently, when setting up a VM, several partitions are created within these disk images.

So, you end up with a machine (the virtualization host), usually installed across several partitions, one of which contains a filesystem containing a disk image or an LVM physical volume with some logical volumes, which in turn contain some more partitions, maybe containing a simple filesystem, or again containing a LVM or RAID volume, that again contains more partitions, etc. If this reminds you of Inception, then you already realize how hard it might be if you want to access on of those interior partitions directly from the host. However, sometimes you need to do just that, be it for disaster recovery or just plain backups.

This post show the method I successfully used to access such partitions. I had to find this out quickly recently, and couldn’t find any one blog post that covered this steps, so here it comes.

Making the partitions from your target image/LV visible

Our friend here is the utility kpartx. It will read the partition table from your device and add mappings to the partitions it discovers.

On Debian/Ubuntu, you can install it with

apt-get install kpartx

Just run kpartx on your target image/LV, with the -a option to add partitions:

kpartx -a /dev/VM/foo

You should now have some new devices under /dev/mapper. If they contain ordinary filesystems, just mount them as you normally would:

mount /dev/mapper/VM-foo1 /mnt

Making LVM logical volumes visible

If one of the partitions that you just created a device node for in /dev/mapper is actually a LVM physical volume, you need to tell LVM about it:

pvscan

After this, vgdisplay should show a new volume group, but it is disabled. Get it’s name from the vgdisplay output (VolGroupBar in this example) and activate it:

vgchange -a y VolGroupBar

You should now find the logical volumes from VolGroupBar under /dev/VolGroupBar/, and you can mount them as you normally would.

Accessing software RAID partitions with mdadm

Mounting ext4 filesystems on a Debian Lenny host

OK, so this is suddenly a lot less generic, but I stumbled into this problem at the same time. The VM whose partition I needed to mount was installed with Ubuntu, and using ext4, but my host was Debian Lenny, which doesn’t contain ext4 support in it’s kernel.

However, the Debian Wiki has a method to access ext4 partitions anyway. Assuming you want to mount /dev/VolGroupBar/root: