So earlier today, I had a need to mount a disk image from a virtual machine on the host, and discovered a “new” method before remembering I’d made notes on this in the past. So I’m recording the details in the probably vain hope that I’ll remember this post in the future.
The first thing to do is to add an option to include partition support in the relevant kernel module, which I’ve done by adding a line to /etc/modprobe.d/etc-modules-parameters.conf :-
options nbd max_part=63
The next step is to load the module:
# modprobe nbd
The next is to use a Qemu tool to connect a disk image to a network block device :-
# qemu-nbd -r -c /dev/nbd0 /home/mike/lib/virtual-machine-disks/W10.vdi
# ls /dev/nbd0*
/dev/nbd0 /dev/nbd0p1 /dev/nbd0p2 /dev/nbd0p3
And next mount the relevant partition :-
# mount -o ro /dev/nbd0p2 /mnt
All done! Except for un-mounting it and finally disconnecting the network block device :-
# umount /mnt
# ls /dev/nbd0*
/dev/nbd0 /dev/nbd0p1 /dev/nbd0p2 /dev/nbd0p3
# qemu-nbd -d /dev/nbd0
/dev/nbd0 disconnected
# ls /dev/nbd0*
/dev/nbd0
The trickiest part is the qemu-nbd command (so not very tricky at all).
The “-r” option specifies that the disk image should be connected read-only, which seems to be sensible when you’re working with a disk image that “belongs” to another machine. Obviously if you need to write to the disk image then you should drop the “-r” (but consider cloning or taking a snapshot).
The “-c” option connects the disk image to a specific device and the “-d” option disconnects the specific device.