Accessing the partitions on an LVM volume

Suppose you have an LVM volume /dev/vg0/mylv. You have presented this as a virtual disk to a virtualised or emulated guest system. During installation the guest system sees it as /dev/sda and partitions it into /dev/sda{1,2,5,6} and completes the installation. Now at some point you need to access those filesystems from within the host system, without running the guest system. fdisk sees these partitions just fine:

# fdisk -l /dev/vg0/mylv
           Device Boot      Start         End      Blocks   Id  System
/dev/vg0/mylv1            2048      684031      340992   83  Linux
/dev/vg0/mylv2          686078    20969471    10141697    5  Extended
/dev/vg0/mylv5          686080     8290303     3802112   83  Linux
/dev/vg0/mylv6         8292352    11980799     1844224   83  Linux

However, the devices such as /dev/vg0/mylv1 do not actually exist. I guess that because they're within an LV, the OS does not recognise this type of nesting by default. Is there any way I can prod Linux so that /dev/vg0/mylv1 or equivalent appears and thus becomes mountable within the host system?

I understand that it's possible with qemu-nbd, and will use this if necessary. However, I was hoping for something more direct if possible, rather than simulating a network block device and attaching that.


I believe the tool you're looking for is kpartx.

The general procedure is:

  1. List partitions in the disk image:

    kpartx -l /dev/vg0/mylv
    
  2. Add the partitions to device-mapper:

    kpartx -a /dev/vg0/mylv
    
  3. Mount the partition you're interested in:

    mount -o ro /dev/mapper/loop0p5 /mnt
    

You may be able to access the partitioned filesystems via loopback using offsets. Multiply the sector start by the sector size.

mkdir /mnt/mylv{1,5,6}
mount -o ro,loop,offset=$((512*2048)) /dev/vg0/mylv /mnt/mylv1
mount -o ro,loop,offset=$((512*686080)) /dev/vg0/mylv /mnt/mylv5
mount -o ro,loop,offset=$((512*8292352)) /dev/vg0/mylv /mnt/mylv6