33.7. Accessing data from a guest disk image
kpartx tool, covered by this section, to mount the guest file system as a loop device which can then be accessed.
kpartx command creates device maps from partition tables. Each guest storage image has a partition table embedded in the file.
Warning
Procedure 33.1. Accessing guest image data
- Install the kpartx package.
yum install kpartx
# yum install kpartxCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Use kpartx to list partition device mappings attached to a file-based storage image. This example uses a image file named guest1.img.
kpartx -l /var/lib/libvirt/images/guest1.img
# kpartx -l /var/lib/libvirt/images/guest1.img loop0p1 : 0 409600 /dev/loop0 63 loop0p2 : 0 10064717 /dev/loop0 409663Copy to Clipboard Copied! Toggle word wrap Toggle overflow guest1 is a Linux guest. The first partition is the boot partition and the second partition is an EXT3 containing the root partition. - Add the partition mappings to the recognized devices in
/dev/mapper/.kpartx -a /var/lib/libvirt/images/guest1.img
# kpartx -a /var/lib/libvirt/images/guest1.imgCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Test that the partition mapping worked. There should be new devices in the /dev/mapper/ directory
ls /dev/mapper/
# ls /dev/mapper/ loop0p1 loop0p2Copy to Clipboard Copied! Toggle word wrap Toggle overflow The mappings for the image are named in the formatloopXpY.
- Mount the loop device which to a directory. If required, create the directory. This example uses
/mnt/guest1for mounting the partition.mkdir /mnt/guest1 mount /dev/mapper/loop0p1 /mnt/guest1 -o loop,ro
# mkdir /mnt/guest1 # mount /dev/mapper/loop0p1 /mnt/guest1 -o loop,roCopy to Clipboard Copied! Toggle word wrap Toggle overflow - The files are now available for reading in the
/mnt/guest1directory. Read or copy the files. - Unmount the device so the guest image can be reused by the guest. If the device is mounted the guest cannot access the image and therefore cannot start.
umount /mnt/tmp
# umount /mnt/tmpCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Disconnect the image file from the partition mappings.
kpartx -d /var/lib/libvirt/images/guest1.img
# kpartx -d /var/lib/libvirt/images/guest1.imgCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Many Linux guests use Logical Volume Management (LVM) volumes. Additional steps are required to read data on LVM volumes on virtual storage images.
- Add the partition mappings for the guest1.img to the recognized devices in the
/dev/mapper/directory.kpartx -a /var/lib/libvirt/images/guest1.img
# kpartx -a /var/lib/libvirt/images/guest1.imgCopy to Clipboard Copied! Toggle word wrap Toggle overflow - In this example the LVM volumes are on a second partition. The volumes require a rescan with the
vgscancommand to find the new volume groups.vgscan
# vgscan Reading all physical volumes . This may take a while... Found volume group "VolGroup00" using metadata type lvm2Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Activate the volume group on the partition (called
VolGroup00by default) with thevgchange -aycommand.vgchange -ay VolGroup00
# vgchange -ay VolGroup00 2 logical volumes in volume group VolGroup00 now active.Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Use the
lvscommand to display information about the new volumes. The volume names (theLVcolumn) are required to mount the volumes.lvs
# lvs LV VG Attr Lsize Origin Snap% Move Log Copy% LogVol00 VolGroup00 -wi-a- 5.06G LogVol01 VolGroup00 -wi-a- 800.00MCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Mount
/dev/VolGroup00/LogVol00in the/mnt/guestboot/directory.mount /dev/VolGroup00/LogVol00 /mnt/guestboot
# mount /dev/VolGroup00/LogVol00 /mnt/guestbootCopy to Clipboard Copied! Toggle word wrap Toggle overflow - The files are now available for reading in the
/mnt/guestbootdirectory. Read or copy the files. - Unmount the device so the guest image can be reused by the guest. If the device is mounted the guest cannot access the image and therefore cannot start.
umount /mnt/
# umount /mnt/Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Disconnect the volume group VolGroup00
vgchange -an VolGroup00
# vgchange -an VolGroup00Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Disconnect the image file from the partition mappings.
kpartx -d /var/lib/libvirt/images/guest1.img
# kpartx -d /var/lib/libvirt/images/guest1.imgCopy to Clipboard Copied! Toggle word wrap Toggle overflow