From the Canyon Edge -- :-Dustin

Wednesday, October 22, 2008

Mounting a KVM Disk Image

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:
http://equivocation.org/node/107
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
:-Dustin

11 comments:

  1. Hey Dustin,

    Would it be possible to set up a handler for that so that people could just double click on .img files and have them mounted on their desktop? That would be very cool.

    Ted

    ReplyDelete
  2. Hi Ted-

    Personally, I'm not too keen on that. Editing .img files is dangerous, and not for the faint of heart. Just my two cents...

    :-Dustin

    ReplyDelete
  3. I don't really get the file-image-fuzz - why not just use 'storage' for the guests? LVM Volumes, iSCSI targets, whatever. IO performace with that (plus virtio) is so much higher ...

    ReplyDelete
  4. Lukas-

    I like disk image files because they're easier to move around, from one system to another. You can use virtio with image files, but you're right, you will get better performance out of 'storage'.

    :-Dustin

    ReplyDelete
  5. Note: The 'losetup' cycle can be ommitted. kpartx will be happy to add and remove loopback devices for you:

    # kpartx -av foo.img
    # kpartx -dv foo.img

    ReplyDelete
  6. Note: The 'losetup' cycle can be ommitted. kpartx will be happy to add and remove loopback devices for you:

    # kpartx -av foo.img
    # kpartx -dv foo.img

    ReplyDelete
  7. Thanks for posting! Very helpful. My VM would not boot, but this allowed me to pull my files out of the image.

    ReplyDelete
  8. wonderful post. This helps me a lot to work with LTSP clusters

    ReplyDelete
  9. I ran into something nasty with kpartx (version 0.4.8). I had LVM based images named the following:

    /dev/main/guest (containing 3 partitions)
    /dev/main/guest2 (containing 3 partitions)

    guest2 was running when I wanted to mount guest's partitions. I did:

    #kpartx -a /dev/main/guest

    Lo and behold, I had mappings for

    /dev/main/guest1
    /dev/main/guest2
    /dev/main/guest5

    The original guest2 mapping was replaced with /dev/main/guest's second partition. Worse yet, the running guest was writing on that partition, corrupting "guest". Admittedly, my naming convention caused this corruption, but it also appears that kpartx doesn't check for naming collisions.

    ReplyDelete
  10. Is it working with qcow v1&2 ?

    ReplyDelete
  11. This was awesome - thanx !
    Now, I won't have to power-up a VM to get/set/edit/put/../../ something :)
    Thanks again.
    All best,
    Stoyan

    ReplyDelete

Please do not use blog comments for support requests! Blog comments do not scale well to this effect.

Instead, please use Launchpad for Bugs and StackExchange for Questions.
* bugs.launchpad.net
* stackexchange.com

Thanks,
:-Dustin

Printfriendly