[clug] Fun with VirtualBox... Mounting partitions within .VDI files

steve jenkin sjenkin at canb.auug.org.au
Sun Dec 29 17:29:27 MST 2013


Thought some of you might be interested in this, I'd not seen it before.

For images of whole disks, it's straight forward to mount a partition
(given you've got the filesystem driver).

sudo mount -o loop,offset=32256 [disk.img] /mnt

where 32256 = 63 * 512, for common size of MBR in 'track' 0.

If you've not copied the whole disk, fdisk _will_ fail because
partitions point past the end of the image.
These can be fixed with "sfdisk -d [disk.img] >sf.txt" (an ascii file),
edit  and then apply with "sfdisk [disk.img] < sf.txt". '-n' is a
dry-run to check.

Make a VDI file, a 'allocate on use' container by:

vboxmange convertfromraw [disk.img] [disk.vdi]

Unless the VDI file is 100% allocated, mount-with-offset fails, it can't
seek to the end.
It's annoying, because you _know_ there's no data in these omitted
blocks and read-only access is safe.

I came across this article that uses the Network Block Driver (nbd) from
Qemu that allows you to mount (read-write or read-only) any of their
support virtual disk formats.

Apparently those good folk support a very large number of virtual disk
formats, works for more than VDI.

The usual caveats: don't mount an active filesystem read-write.
Nor expect a read-only mount to reflect changes in a filesystem mounted
elsewhere.

I've not tested NBD's ability to update the VDI file if you _add_ files.
My interest has been reading, renaming & deleting files.

There was a problem for me with a solution in the comments.
The Debian kernel has 'max_part' set to zero, so that needs setting when
loading the kernel module.
For non-Deb systems, try without 'max_part' first.

Hope that's useful to some of you.

cheers
steve

<http://bethesignal.org/blog/2011/01/05/how-to-mount-virtualbox-vdi-image/>

# install package

sudo aptitude install qemu

# setup framework

sudo modprobe nbd max_part=16		# not obvious, needed for Debian
sudo qemu-nbd -c /dev/nbd0 [disk.vdi]	# connect block driver

# show contents of VDI file

sudo cfdisk /dev/nbd0
ls /dev/nbd0*

# Mount a partition

sudo mount /dev/nbd0p1 /mnt		# mount partition 1
ls /dev/nbd0*

# use mounted partition

ls /mnt
df -h /mnt

## undo mount & framework

sudo umount /mnt
sudo qemu-nbd -d /dev/nbd0	# disconnect block driver
sudo modprobe -r nbd		# remove kernel module.
				# Can leave loaded.

-- 
Steve Jenkin, Info Tech, Systems and Design Specialist.
0412 786 915 (+61 412 786 915)
PO Box 48, Kippax ACT 2615, AUSTRALIA

sjenkin at canb.auug.org.au http://members.tip.net.au/~sjenkin


More information about the linux mailing list