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
:
$ file linux.vdi linux.vdi: VDI Image version 1.1 (<<< Oracle VM VirtualBox Disk Image >>>), \ 2147483648 bytes $ hdid -nomount linux.vdi hdid: attach failed - not recognizedStill,
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/disk5However, we're still not entirely satisfied. Our
linux.vdi
contains a whole virtual disk (partition table + data), so let's apply the blogpost above to our disk. Read the post again to understand what we do here:
$ hdiutil detach disk5 $ hexdump -C linux.vdi | grep -m1 ^00000150 00000150 00 4e 88 00 00 10 00 00 00 50 10 00 00 00 00 00 |.N.......P......| $ echo 'obase=16; 512; ibase=16; 00015000 / 200' | bc 200 A8Now that we have the offset to our disk, we can instruct
hdid
to just attach this disk (minus the VDI header):
$ hdid -section 0xa8 -nomount linux.img /dev/disk5 GUID_partition_scheme /dev/disk5s1 EFI $ file -Ls /dev/disk5* /dev/disk5: x86 boot sector /dev/disk5s1: Linux rev 1.0 ext4 filesystem data, [...]Now we could even
fsck
our virtual Linux partion from MacOS, hey! :-)
Update: Two mindful readers noted that my calculation was incorrect. This should now be fixed in the article.