Zhigang Wang
  • Home
  • Blog
  • Wiki
  • LDP
  • Planet

Accessing Data in RAW Disk Image of Xen Guest

Last modified on 2009-12-01

There are several ways to access the data in RAW disk image of Xen guest. But 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!

The following examples use an image (system.img) formatted as a default Fedora install, so it has two partitions: one /boot, and one LVM volume containing everything else.

Access it within a running domain

The simplest way is to start a guest and attach it to the domain:

# xm block-attach <domid> file:/system.img xvdb w 0

or:

# xm block-attach <domid> tap:aio:/system.img xvdb w 0

In fact, you can attach it to dom0 as well:

# xm block-attach 0 file:/system.img xvdb w 0

or:

# xm block-attach 0 tap:aio:/system.img xvdb w 0

Then the blkfront driver and udev in the running domain should initial it and make it ready for your access:

# mount /dev/xvdb1 /mnt/boot

And we can scan out the lvm volume:

# pvscan 
  PV /dev/xvdb2    VG VolGroup00     lvm2 [5.88 GB / 0    free]

lomount

"lomount" is tool a shipped with Xen, but maybe missing in some specific Xen distribution.

# lomount -t ext3 -diskimage system.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

kpartx 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 /dev/loop0 system.img
# kpartx -av /dev/loop0
add map loop0p1 : 0 208782 linear /dev/loop0 63
add map loop0p2 : 0 12370050 linear /dev/loop0 208845
# mount /dev/mapper/loop0p1 /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":

# 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/sys
always remember to deactivate the logical volumes after you are finished:

# umount /mnt/sys
# vgchange -an VolGroup00
# kpartx -d /dev/loop0
# losetup -d /dev/loop0

There are two reasons:

  • First of all, the default volume group name for a Fedora 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.

Hack it yourself

To mount and manipulate the partitions in the disk image file, we can use:

# losetup -o offset /dev/loop[n] [imgfile]

The option -o offset means the data start is moved [offset] bytes into the specified file or device. But how to calculate the value?

We know that the first track is reserved for the MBR. By the output of the following command:

# sfdisk -l system.img  2>/dev/null

Disk system.img: 783 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

     Device Boot Start     End   #cyls    #blocks   Id  System
system.img1   *      0+     12      13-    104391   83  Linux
system.img2         13     782     770    6185025   8e  Linux LVM
system.img3          0       -       0          0    0  Empty
system.img4          0       -       0          0    0  Empty

we can get:

offset_of_partition1 = sector_size * sectors_per_track = 512 * 63 = 32256
offset_of_partition2 = offset_of_partition1 + partition1_size = 32256 + 104391 * 1024 = 106928640

So we can use the following command to setup the partitions:

# losetup -o 32256 /dev/loop0 system.img
# losetup -o 106928640 /dev/loop1 system.img

Then we can mount the boot partition:

# mount /dev/loop0 /mnt/boot

And we can scan out the lvm volume:

# pvscan 
  PV /dev/loop1    VG VolGroup00    lvm2 [5.88 GB / 0    free]
 

Categories

  • All contents
  • English contents
  • Chinese contents

Feeds

  • AtomAll contents
  • AtomEnglish contents
  • AtomChinese contents

Tags

  • gtd
  • syslog
  • twiki
  • virtualizaion
  • wiki
  • xen

Copyright © 2012 Zhigang Wang. Some right reserved.

The views expressed on this web site are my own and do not necessarily reflect the views of Oracle.