Mounting a KVM Disk Image
October 22, 2008
Martin Pitt pinged me in #ubuntu-devel earlier today asking how to mount a KVM/QEMU/Virt-Manager disk. It’s occasionally useful crack open a .img disk file, and read/write/modify files there.
For the last couple of years, I’ve used a rather manual process, involving losetup, fdisk, calculating the byte offset into the image of each partition, losetup again, and then mount.
Many times, it crossed my mind that there must be an easier way. But, meh, it worked 😉
In the process of answering pitti’s question, I stumbled across:
Much cleaner! It amounts to:
# losetup /dev/loop0 foo.img
# kpartx -av /dev/loop0
# mount /dev/mapper/loop0p1 /mnt
# unmount /mnt
# kpartx -dv /dev/loop0
# losetup -d /dev/loop0