Skip to main content

Error while creating the raw disk VMDK

I have been attaching raw disks to VirtualBox before but I missed one particular detail: apparently one cannot attach disks readonly. But that's exactly what I wanted to do, I did not want the VM to be able to alter the disk in any way:

$ ls -l ../disk3.img
-r--------  1 christian  staff  3965190144 Nov 26 12:18 ../disk3.img

$ hdid -nomount ../disk3.img
/dev/disk3              FDisk_partition_scheme          
/dev/disk3s1            DOS_FAT_32 

$ diskutil list disk3
/dev/disk3
   #:                       TYPE NAME              SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                   *4.0 GB     disk3
   1:                 DOS_FAT_32 CANON_DC           4.0 GB     disk3s1

$ ls -l /dev/disk3*
br--r-----  1 christian  staff   14,   9 Nov 28 18:20 /dev/disk3
br--r-----  1 christian  staff   14,  10 Nov 28 18:20 /dev/disk3s1

$ VBoxManage internalcommands createrawvmdk -filename ../disk3.vmdk \
             -rawdisk /dev/disk3 -partitions 1 -register
ERROR: VMDK: could not open raw partition file '/dev/disk3s1'
Error code VERR_ACCESS_DENIED at \
/Users/vbox/tinderbox/3.2-mac-rel/src/VBox/Devices/Storage/VmdkHDDCore.cpp(3661) \
in function int vmdkCreateRawImage(VMDKIMAGE*, VBOXHDDRAW*, uint64_t)
Error while creating the raw disk VMDK: VERR_ACCESS_DENIED
The raw disk vmdk file was not created
Even making the blockdevice and/or its backing store writable (with the disk still attached to Virtualbox), did not help. The trick is to unregister the disk, eject it from the OS, make it writable and then reattach it to VirtualBox again:
$ diskutil eject disk3
Disk disk3 ejected

$ chmod u+w ../disk3.img
$ hdid -nomount ../disk3.img

$ ls -l /dev/disk3*
brw-r-----  1 christian  staff   14,   9 Nov 28 18:42 /dev/disk3
brw-r-----  1 christian  staff   14,  10 Nov 28 18:42 /dev/disk3s1

$ VBoxManage internalcommands createrawvmdk -filename ../disk3s1.vmdk \
             -rawdisk /dev/disk3 -partitions 1 -register
RAW host disk access VMDK file ../disk3.vmdk created successfully.
To make the disk readonly after all, we can mark it immutable:
$ VBoxManage modifyhd ../disk3.vmdk --type immutable