Accessing data on xen guest lvm image

Accessing data on xen lvm guest image

Accessing xen guest image is very easy if the image is not lvm partitioned. But the main problem arise when the image is of lvm format and normal mount command cannot be used. Here I will show both the way. The first is when ext filesystem is used, and the second is when lvm is used.

To mount xen guest image (without lvm)

1. check the partition on the image
# fdisk -lu
The result will be something like this:



2. Mount using offset option
# mount -o loop,offset=106929152 /path/to/image /mnt
where 106929152=208846*512, 208846 is the start of the partition. Using this way, you only mount the second partition and not the whole image

3. You can now access your image at /mnt

Accessing data on a guest disk image

There are two tools which can help greatly in accessing data within a guest disk image: lomount and kpartx. Remember never to do this while the guest is up and running, as you could corrupt the filesystem if you try to access it from the guest and dom0 at the same time!

* lomount
# lomount -t ext3 -diskimage /xen/images/fc5-file.img -partition 1 /mnt/boot

lomount only works with small disk images and cannot deal with LVM volumes, so for more complex cases, kpartx (from the device-mapper-multipath RPM) is preferred:

* kpartx
# yum install device-mapper-multipath
# kpartx -av /dev/xen/guest1
add map guest1p1 : 0 208782 linear /dev/xen/guest1 63
add map guest1p2 : 0 16563015 linear /dev/xen/guest1 208845

Note that this only works for block devices, not for images installed on regular files. To use file images, you'll need to set up a loopback device for the file first:
# losetup -f
/dev/loop0
# losetup /dev/loop0 /xen/images/fc5-file.img
# kpartx -av /dev/loop0
add map loop0p1 : 0 208782 linear /dev/loop0 63
add map loop0p2 : 0 12370050 linear /dev/loop0 208845

In this case we have added an image formatted as a default Fedora install, so it has two partitions: one /boot, and one LVM volume containing everything else. They are accessible under /dev/mapper:
# ls -l /dev/mapper/ | grep guest1
brw-rw---- 1 root disk 253, 6 Jun 6 10:32 xen-guest1
brw-rw---- 1 root disk 253, 14 Jun 6 11:13 guest1p1
brw-rw---- 1 root disk 253, 15 Jun 6 11:13 guest1p2
# mount /dev/mapper/guest1p1 /mnt/boot/

To access LVM volumes on the second partition, we'll need to rescan LVM with "vgscan" and activate the volume group on that partition (named "VolGroup00" by default) with "vgchange -ay":
# kpartx -a /dev/xen/guest1
# vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
# vgchange -ay VolGroup00
2 logical volume(s) in volume group "VolGroup00" now active
# lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
LogVol00 VolGroup00 -wi-a- 5.06G
LogVol01 VolGroup00 -wi-a- 800.00M
# mount /dev/VolGroup00/LogVol00 /mnt/
...
# umount /mnt
# vgchange -an VolGroup00
# kpartx -d /dev/xen/guest1

Note: always remember to deactivate the logical volumes with "vgchange -an", remove the partitions with "kpartx -d", and (if appropriate) delete the loop device with "losetup -d" after you are finished. There are two reasons: first of all, the default volume group name for a FC install is always the same, so if you end up activating two disk images at the same time you'll end up with two separate LVM volume groups with the same name. LVM will cope as best it can, but you won't be able to distinguish between these two groups on the command line.

And secondly, if you don't deactivate it, then if the guest is started up again, you might end up with the LVM being active in both the guest and the dom0 at the same time, and this may lead to VG or filesystem corruption.