I run a Xen vm system which in turn hosts all of my linux servers. Recently, I had reason to mount the disk image from one of the vm’s on the xen host.
I’ve found little documentation on this, so I thought i’d post what I ended up doing.
First, make sure the vm is down. Then you can mount it’s disk image.
I used kpartx to mount the image as a loop.
[root@chaos ~]# kpartx -av /var/lib/xen/images/interchange_os.img
add map loop0p1 : 0 401562 linear /dev/loop0 63
add map loop0p2 : 0 2104515 linear /dev/loop0 401625
add map loop0p3 : 0 18458685 linear /dev/loop0 2506140
The VM uses LVM for its disk configuration, so I needed to them work some disk magic to get it to load the PV, VG, and finally LV’s.
First, scan for new PV’s.
[root@chaos ~]# pvscan /dev/mapper/loop0p3
PV /dev/mapper/loop0p3 VG vg_root lvm2 [8.78 GB / 0 free]
PV /dev/sda3 VG VolGroup00 lvm2 [269.16 GB / 0 free]
Total: 2 [277.94 GB] / in use: 2 [277.94 GB] / in no VG: 0 [0 ]
Then scan for new VG’s.
[root@chaos ~]# vgscan
Reading all physical volumes. This may take a while…
Found volume group “vg_root” using metadata type lvm2
Found volume group “VolGroup00” using metadata type lvm2
Now, scan for new LV’s.
[root@chaos ~]# lvscan
inactive ‘/dev/vg_root/lv_slash’ [3.00 GB] inherit
inactive ‘/dev/vg_root/lv_home’ [1.00 GB] inherit
inactive ‘/dev/vg_root/lv_tmp’ [512.00 MB] inherit
inactive ‘/dev/vg_root/lv_var’ [3.28 GB] inherit
inactive ‘/dev/vg_root/lv_var_run’ [1.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_root’ [8.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_var’ [256.16 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_tmp’ [1.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_home’ [4.00 GB] inherit
And now, activate the VG’s
[root@chaos ~]# vgchange -ay
5 logical volume(s) in volume group “vg_root” now active
4 logical volume(s) in volume group “VolGroup00” now active
Now, you see that /dev/mapper contains my vg’s, and their lv’s.
[root@chaos ~]# ls /dev/mapper/
control loop0p2 vg_root-lv_home vg_root-lv_tmp vg_root-lv_var_run VolGroup00-lv_root VolGroup00-lv_var
loop0p1 loop0p3 vg_root-lv_slash vg_root-lv_var VolGroup00-lv_home VolGroup00-lv_tmp
All that’s neft to do now is mount them like any other local disk.
[root@chaos ~]# mount /dev/mapper/vg_root-lv_slash /mnt/
[root@chaos ~]# ls /mnt
bin boot dev etc home lib lost+found media misc mnt opt proc root sbin selinux srv sys tmp usr var
[root@chaos ~]#
To dismount, simply unmount the volume, disable the VG, and then use kpartx to remove the loop.
[root@chaos ~]# umount /mnt/
[root@chaos ~]# vgchange -an vg_root
0 logical volume(s) in volume group “vg_root” now active
[root@chaos ~]# kpartx -d /dev/loop0
Now, your image should be clean for your vm once more.