Page 1 of 1

Mount a KVM image (.img) file directly

Posted: Tue Aug 13, 2013 12:52 am
by ^rooker
This HowTo describes the steps necessary to mount a GNU/Linux virtual machine image file created by "virt-install" and used by KVM.

1) Connect the image as "loop" device:

Code: Select all

$ losetup <image_filename.img>
returns:
Loop device is /dev/loop0
If this doesn't work (for whatever reason), you can do it manually.
Check the next available/free loop device:

Code: Select all

$ losetup -f
For example, on a Ubuntu live distro it will return
/dev/loop2
(Because loop0 and loop1 are taken by the live distro).
Then assign the image to that loop device:

Code: Select all

$ losetup /dev/loop2 <image_filename.img>
2) Map the partitions which are inside the image:

Code: Select all

$ sudo kpartx -av /dev/loop0
returns:
add map loop0p1 (253:0): 0 20967424 linear /dev/loop0 2048
3) Mount the desired partition(s):

Code: Select all

$ mount /dev/mapper/loop0p1 <mountpoint>
Voila! :)

Unmounting is basically the same in reverse order with "delete" arguments:

Code: Select all

$ unmount /mnt
$ kpartx -dv /dev/loop0
$ losetup -d /dev/loop0
Thanks to Kirk Dougland's article "Mounting a KVM Disk Image" and David Champion's page Mounting a KVM disk image without KVM.