Mounting QEMU / KVM images manually

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

Mounting raw disk images[edit]

If your disk image is available as a raw file, you can mount it with kpartx utility, which creates device maps from partition tables.

Example kpartx usage[edit]

# kpartx -a vm-106-disk-1.raw

Then, device nodes will be added to /dev/mapper/ directory (loop* files below):

# ls /dev/mapper/
control  loop0p1  loop0p2  loop0p5

Viewing available partitions[edit]

You can see what partition is what with file utility:

# file -s /dev/mapper/loop0p*
/dev/mapper/loop0p1: symbolic link to `../dm-0'
/dev/mapper/loop0p2: symbolic link to `../dm-1'
/dev/mapper/loop0p5: symbolic link to `../dm-2'

# file -s /dev/dm-*
/dev/dm-0: sticky Linux rev 1.0 ext4 filesystem data, UUID=bc3764a0-6e8c-4907-b9d2-791dbffd3588 (extents) (large files) (huge files)
/dev/dm-1: sticky x86 boot sector; partition 1: ID=0x82, starthead 254, startsector 2, 4188160 sectors, extended partition table (last)\011, code offset 0x0
/dev/dm-2: sticky Linux/i386 swap file (new style), version 1 (4K pages), size 523519 pages, no label, UUID=61b62dbb-b7b8-4d72-b928-f24b2786ff73


You can mount the partitions "added" with kpartx as below:

# mkdir /mnt/loop0p1

# mount /dev/mapper/loop0p1 /mnt/loop0p1/

# ls /mnt/loop0p1/
bin  boot  dev  etc  home  initrd.img  initrd.img.old  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  vmlinuz  vmlinuz.old

Please note that disk image must not be used (i.e. by qemu/kvm) if you intend to mount it as above.

removing the mount and partition devices[edit]

To reverse the operation, unmount the partition and use kpartx to delete partition devmappings:

# umount /mnt/loop0p1/

# kpartx -d vm-106-disk-1.raw-20151023
loop deleted : /dev/loop0

# ls /dev/mapper/

You will see that loop* devices are gone from /dev/mapper/ directory.