Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Fedora Core 7 (FC7) Xen/KVM Guide
Previous Page Home Next Page

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 it could corrupt the filesystem:

* 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, set up a loopback device for the file first:

  • # losetup -f
    # 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, 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 performing the above steps. The default volume group name for a Fedora install is always the same, it is important to avoid activating two volume group of the same name at the same time. LVM will cope as best it can, but it is not possible to distinguish between these two groups on the command line. In addition, if the volume group is active on the host and the guest at the same time, it can cause filesystem corruption.

Fedora Core 7 (FC7) Xen/KVM Guide
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire