Mounting a File System on a Partition Inside of an LVM Volume

Tags: , , , , , , , , , , ,

In my linux virtual environment I am using LVM volumes as the backing devices for virtual machines. Each of these LVM volumes contains a partition table splitting the LVM volume into at least one linux partition and one swap partition. In order to access these partitions from the dom0 host itself we can use the kpartx command to create device mapper entries which correspond to each of the partitions.

In this example we want to access the ext3 filesystem contained on the first partition of the “vm_example” logical volume.

[root@vm ~]# lvs
  LV              VG   Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  vm_example      vg0  -wi-ao  6.00G                                      
[root@vm ~]# fdisk -l /dev/vg0/vm_example 
Disk /dev/vg0/vm_example: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
              Device Boot      Start         End      Blocks   Id  System
/dev/vg0/vm_example1               1         653     5245191   83  Linux
/dev/vg0/vm_example2             654         783     1044225   82  Linux swap / Solaris

As you can see, partition 1 is type linux and partition 2 is type linux swap. Now we use kpartx with the -a flag to create the device mapper entries for the partitions displayed above.

[root@vm ~]# kpartx -a /dev/vg0/vm_example

And now we can interact with the /dev/mapper devices as you normally would to mount, fsck, etc.

[root@vm ~]# file -s /dev/mapper/vm_example1 
/dev/mapper/vm_example1: Linux rev 1.0 ext3 filesystem data (large files)
[root@vm ~]# file -s /dev/mapper/vm_example2
/dev/mapper/vm_example2: Linux/i386 swap file (new style) 1 (4K pages) size 261055 pages

Then, when you’re finished, clean up with the kpartx -d command. The logical volume will remain in-use until this is done.

[root@vm ~]# kpartx -d /dev/vg0/vm_example