Mount kvm file based image (disk.img) on host computer

From Wiki

Jump to: navigation, search

Xen based disk images are of course easy to mount as they don't have a mbr or partitions. KVM however requires a disk image with boot, swap and extended partitions. As a result mounting a KVM disk based image is a little different, but as it turns out, quite easy.

There are loads of different ways of doing this. Many of them didnt work for me. lomount which was mentioned a lot, but wasn't in debian (as far as I could see). losetup was but there were extra steps involved which were not needed.

The commands below work for ext3 and ntfs, no problem! The only requirement is that it is a file based disk image.

Commands =

#on the host computer with the VM shutdown
kpartx -av disk.img
mount /dev/mapper/loop0p1 /mnt
umount /mnt
kpartx -dv disk.img

Example Mount

Debian-50-lenny-32-minimal:~# kpartx -av /xens/vm03/disk-vm03.img
add map loop1p1 (253:2): 0 9687132 linear /dev/loop1 63
add map loop1p2 (253:3): 0 546210 linear /dev/loop1 9687195
add map loop1p5 : 0 546147 linear 253:3 63
Debian-50-lenny-32-minimal:~# mount /dev/mapper/loop1p1 /mnt
Debian-50-lenny-32-minimal:~# ls /mnt
bin  boot  cdrom  dev  etc  home  initrd.img  lib  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var  vmlinuz
Debian-50-lenny-32-minimal:~# umount /mnt
Debian-50-lenny-32-minimal:~# kpartx -dv /xens/vm03/disk-vm03.img
del devmap : loop1p5
del devmap : loop1p2
del devmap : loop1p1
loop deleted : /dev/loop1

Ref: http://www.linuxquestions.org/questions/linux-virtualization-90/how-to-mount-a-kvm-image-file-787557/

Personal tools