Skip to content

Mounting VirtualBox VDI images on a MacOS X host

During all this VirtualBox hackery stuff I came across an interesting blogpost on how to mount a VirtualBox VDI in MacOS X. That is, we don't really want to mount it, we merely want to access the VDI file via a blockdevice. In GNU/Linux or Solaris one would use losetup resp. lofiadm to attach any file to a blockdevice.
In MacOS X there's hdid. By default, hdid not only tries to assign a blockdevice to the file but it tries to mount it too. We don't want this, so we use -nomount:
$ hdid -nomount linux.vdi 
hdid: attach failed - not recognized
Still, hdid failed. The blogpost above helped, we have to use the magic .img extension for the filename, oh well:
$ ln linux.vdi linux.img
$ hdid -nomount linux.img
/dev/disk3
However, we're still not entirely satisfied. Our linux.vdi contains a whole virtual disk (partition table + partion), so let's apply the blogpost above to our disk. Read the post again to understand what we do here:
$ hexdump -C linux.vdi | grep -m1 ^00000150
00000150  00 00 00 00 00 02 00 00  00 22 00 00 00 00 00 00  |........."......|
$ echo 'obase=16; 512; ibase=16; 2200 / 200' | bc
200
11
Now that we have the offset to our disk, we can instruct hdid to just attach this disk (minus the VDI header):
$ hdid -section 0x11 -nomount linux.img 
/dev/disk3              FDisk_partition_scheme
/dev/disk3s1            Linux
$ file -s /dev/disk3*
/dev/disk3:   x86 boot sector; partition 1: ID=0x83, starthead 1, startsector 63 [...]
/dev/disk3s1: Linux rev 1.0 ext4 filesystem data (extents) (large files) (huge files)
Now we could even fsck our virtual Linux partion from MacOS, hey! :-)