Mount a KVM image (.img) file directly

Step-by-Step descriptions of how to do things.
Post Reply
User avatar
^rooker
Site Admin
Posts: 1481
Joined: Fri Aug 29, 2003 8:39 pm

Mount a KVM image (.img) file directly

Post 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.
Jumping out of an airplane is not a basic instinct. Neither is breathing underwater. But put the two together and you're traveling through space!
Post Reply