Mounting / Unmounting KVM Image

From Leo's Notes
Last edited on 1 September 2019, at 06:21.

Suppose you have a KVM virtual machine using a virtual disk located in a LV group. To gain access to the data, use kpartx.

To list partitions in the disk image:

# kpartx -l /dev/vol_vm1/kvm101_img
vol_vm1-kvm101_img1 : 0 61861441 /dev/vol_vm1/kvm101_img 63
vol_vm1-kvm101_img2 : 0 1048576 /dev/vol_vm1/kvm101_img 61861504

Knowing that the first partition is / and the second is the swap, we will attempt to mount the first partition.

To map the parititions:

# kpartx -av /dev/vol_vm1/kvm101_img
add map vol_vm1-kvm101_img1 (253:6): 0 61861441 linear /dev/vol_vm1/kvm101_img 63
add map vol_vm1-kvm101_img2 (253:7): 0 1048576 linear /dev/vol_vm1/kvm101_img 61861504

To mount the newly mapped partitions:

# mount -t ext4 -o ro /dev/mapper/vol_vm1-kvm101_img1 /mnt/vm101

Once completed, simply unmount and unmap the image:

# umount /mnt/vm101
# kpartx -d /dev/vol_vm1/kvm101_img