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


hehe
lol
Thanks a lot. I think there must be a “-c” parameter specified to qemu-nbd (at least on my machine).
Oops, crucial missing parameter! Thanks for pointing that out, I’ll fix the post.
I’ve noticed that “max_part” parameter for nbd kernel module in my distribution is 0, so for this to work, one has to do “modprobe nbd max_part=16″ or similar
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.
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.
I believe this to only be applicable to fixed-size images. Dynamically-sized images cannot be mounted, IIRC.
Not so! All of my disk images are dynamic. Hooray!
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
Works like a charm on Ubuntu 10.10