Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
fdisk is only showing you what the partition table has. The kernel does not know of these partitions because it does not normally scan LVM volumes for partitions. –  Skaperen Oct 19 '12 at 16:40

2 Answers 2

up vote 6 down vote accepted

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
    
share|improve this answer

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
share|improve this answer
    
Interesting, but choose kpartx (see the other answer) instead. –  cstamas Oct 19 '12 at 17:24
    
I don't like reinventing the wheel. I do like tools which do grunt work for me. –  Michael Hampton Oct 19 '12 at 17:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.