How to mount partition from xen instance

This below describes the way to mount an LVM partition that exists within a partition from a multi-partition disk image.

Let assume our image is called System.img

We will need losetup, kpartx and vgscan,vgchange commands.

With losetup -f you will identify free loopback devices. let say this is loop6.

  1. losetup /dev/loop6 ./System.img
    

This will mount the disk image in the loop device.

You can see partition info with

fdisk -l /dev/loop6

Disk /dev/loop6: 82.6 GB, 82678120448 bytes
255 heads, 63 sectors/track, 10051 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

      Device Boot      Start         End      Blocks   Id  System
/dev/loop6p1   *           1          66      524288   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/loop6p2              66       10051    80209345+  8e  Linux LVM

You could possibly remove the loopback mount and remount only the partition you want from the total disk by using the offset ‘’’-o’’’ option.

losetup -d /dev/loop6

offset is the start cylinder multiplied by the size of each, here 512, so (start of p2) 66* (512 bytes) 512 = offset is 33792

losetup /dev/loop6 System.img -o 33792

However, we will not use that option.

Instead:

  1.  kpartx -a /dev/loop6
    

which will create the /dev/mapper/loopXpY devices for all partitions of image

  1. vgscan
    

this will show the volume groups that exists in all the devices (so eventually search those created above). In our example it returned (in addition to others) the vg_apol

  1. vgchange -ay vg_apol
    

This creates the /dev/<vgroup>/<logical_volume> devices so we can mount.

  1. finally mount the one we need
mount /dev/vg_apol/lv_root ./second/

After doing what we want with the mounted filesystem, we need to do reverse order, so

  1. umount /dev/vg_apol/lv_root unmount volume

  2. vgchange -an vg_apol remove logical volume devices 0 logical volume(s) in volume group “vg_apol” now active

  3. kpartx -d /dev/loop6 remove /dev mapper partitions

  4. losetup -d /dev/loop6 “dettach” the loopback device