How to mount a VirtualBox VDI image

Don’t believe the hype! It is entirely possible to mount a VirtualBox VDI image, just like a loopback filesystem… all you need are the right tools and know-how. Allow me to illustrate.

My apologies, that was the wrong illustration. Onward!

Before we start, it should be noted that you don’t want to do this while your disk image is already in use. That is to say, if you’re running a virtualised host using this image, GTFO.

First, install the QEMU tools. In Ubuntu, you’ll find them in the qemu-kvm package. Whatever package your distribution ships which contains the qemu-nbd binary should be fine.

Load the nbd kernel module. Yes, I’m serious, the network block device module! (Note: All of the following commands require superuser permissions, so escalate your privileges in whatever way makes you most comfortable.)

modprobe nbd

Then run qemu-nbd, which is a user space loopback block device server for QEMU-supported disk images. Basically, it knows all about weird disk image formats, and presents them to the kernel via nbd, and ultimately to the rest of the system as if they were a normal disk.

qemu-nbd -c /dev/nbd0 <vdi-file>

That command will expose the entire image as a block device named /dev/nbd0, and the partitions within it as subdevices. For example, the first partition in the image will appear as /dev/nbd0p1.

Now you could, for instance, run cfdisk on the block device, but you will most likely want to mount an individual partition.

mount /dev/nbd0p1 /mnt

Gadzooks! Now you can muck around inside the filesystem to your heart’s content. Go ahead and copy stuff in or out, or if you’re feeling fruity, have some chrooty: chroot /mnt.

When you’re done, unmount the filesystem and shut down the qemu-nbd service.

umount /mnt
qemu-nbd -d /dev/nbd0
  • email
  • Print
  • Twitter
  • Identi.ca
  • del.icio.us
  • Facebook
This entry was posted in General and tagged Linux, nbd, qemu, Ubuntu, vdi, VirtualBox. Bookmark the permalink.

10 Responses to How to mount a VirtualBox VDI image

  1. unity fan says:

    hehe ;) lol

  2. dremon says:

    Thanks a lot. I think there must be a “-c” parameter specified to qemu-nbd (at least on my machine).

  3. Paul Goller says:

    I also have to run partprobe (from parted package) before I’m able to access the partitions (tested under Debian Squeeze RC). After that the partitions will accessible through /dev/disk/by-id/*nbd*part[0-9]*.

    Sadly the nbd module seems to be very unstable for this kind of operation. After copying ~1.5GB of data from a VDI-File the module dies and the copying process (cp and rsync tested) will produce I/O Errors.

    • Jeff Waugh says:

      The versions of qemu and nbd worked well for me on Ubuntu 10.10.

      In fact, I was meant to be fetching packaging stuff off the disk image (because the new VirtualBox was wedging the machine) when I found myself doing the packaging and building while chrooted in it! Not a terrible stress test. ;-)

  4. aperson says:

    I believe this to only be applicable to fixed-size images. Dynamically-sized images cannot be mounted, IIRC.

  5. Ulli Hochholdinger says:

    Hi,
    Many thanks for the Tip.

    I use debian squeeze. To get this to work there you have to insert the nbd module with the paramter max_part with appropriate number of partitions to use:

    rmmod nbd
    modprobe nbd max_part=16

    without this Parameter you won’t get the partitions. partprobe gives a weird Error:
    “Error: Error informing the kernel about modifications to partition /dev/nbd0p1 — Invalid argument. This means Linux won’t know about any changes you made to /dev/nbd0p1 until you reboot — so you shouldn’t mount it or use it in any way before rebooting.”

    Cheers Ulli

  6. Erik N says:

    Works like a charm on Ubuntu 10.10

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments will be sent to the moderation queue.