Wanted to get at a file in a VirtualBox guest OS without starting up the system in question. The data was stored in a .vdi file (dynamic). It turns out that one can mount such .vdi files using some qemu tools.

First you make sure that the nbd kernel module is loaded with the max_part option set to a number high enough to accommodate all the partitions in your .vdi image:

# rmmod nbd# modprobe nbd max_part=16

Then you use qemu-nbd to attach the image:

# qemu-nbd -c /dev/nbd0 test.vdi

You will get a /dev/nbd0 block device, along with several /dev/nbd0p* partition device nodes. You use them as any other disk. Once you are done, you unmount everything, and disconnect the device:

# qemu-nbd -d /dev/nbd0

To determine the relevant partition device node I used cfdisk on /dev/nbd0, but it may be that a simple use of the blkid command may be enough. partprobe may or may not be necessary after the first qemu-nbd invocation.

I first learned about this approach via How to mount a VirtualBox VDI image, but the specific instructions in the main post didn't quite work (though with modifications suggested in the comments things worked out).

libguestfs is a set of tools for accessing and modifying virtual machine (VM) disk images. You can use this for viewing and editing files inside guests, scripting changes to VMs, monitoring disk used/free statistics, creating guests, P2V, V2V, performing backups, cloning VMs, building VMs, formatting disks, resizing disks, and much more.

libguestfs can access almost any disk image imaginable. It can do it securely — without needing root and with multiple layers of defence against rogue disk images. It can access disk images on remote machines or on CDs/USB sticks. It can access proprietary systems like VMware and Hyper-V.

All this functionality is available through a scriptable shell called guestfish, or an interactive rescue shell virt-rescue.

This is slightly off-topic, but I was surprised to discover recently that since Windows 7 (or so) you can mount virtual disks (VHDs) natively in Windows. Just open Disk Management and use Action -> Attach VHD. (You can even configure Windows to boot from VHDs somehow, but I haven't looked into it enough to learn how to do that.)