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:

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

Advertisements

6 Responses to “Mounting a KVM Disk Image”

  1. Ted Says:

    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

  2. Dustin Kirkland Says:

    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

  3. lukas Says:

    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 …

  4. Dustin Kirkland Says:

    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

  5. Christian Theune Says:

    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

  6. Christian Theune Says:

    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


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: