BootCamp Partition Virtual Machine with VirtualBox

I’ve had a BootCamp partition on my Macbook since it bought it; I waited specifically for the Intel CoreDuo Macbooks. Sometimes I don’t want to restart just to run an app or test something out, so developed this bash script to boot it using Virtual Box.

VirtualBox is an extremely powerful virtualisation platform that is amazingly free. I’ve used the more Mac like VMs (and pricey) but have found VirtualBox to trump them in term of features and control with command line interaction. Like more open source and powerful programs, it requires a fair bit of research to use it beyond a basic VM. Hopefully this post will help others tackle the useful ability of booting a hard partition like a virtual disk, giving you the best of both worlds and not requiring management of two installations. It should work on any other Unix platform too.

I used the info from a few other blogs for this and most permanently unmounted the Windows volume or required user interaction. I made a script to avoid this and enable me to simply enter bootcamp into Terminal.

  1. Get the partition of your BootCamp install using diskutil list, it’s the /dev/disk* listed under what your named your partition.

  2. sudo chmod 777 /dev/disk0s3 using the correct partition numbering you found above (replace /dev/disk0s3 onwards).

  3. diskutil umount /dev/disk0s3 – ejects the Windows partition.

  4. sudo VBoxManage internalcommands createrawvmdk -rawdisk /dev/disk0 -filename bootcamp.vmdk -partitions 3 – creates the virtual disk to boot the partition, the number after the ‘partitions’ argument must match the partition on your disk (number after s).

  5. sudo chown YOUR-USERNAME *.vmdk – ensures you own rights to the files created.

  6. Run VirtualBox and create a new Windows machine. Set the settings as you want but when asked to select a hard drive, choose ‘existing disk’ and browse for the ‘bootcamp.vmk’ created in the above steps (it will be in your ‘Home’ directory if you just opened Terminal fresh).

  7. Highlight the machine, choose ‘Settings’. In ‘Storage’ change the type to “ICH6” – this caused me problems at the default.

The virtual machine should now be booting your BootCamp installation just fine. The problem comes when you restart, the partition will auto-mount and permissions will be lost. This is where my script comes in.

26/02/2016 UPDATE: After a Virtual Box update my bootcamp stopped working with ‘VERR_NOT_SUPPORTED’ error. After some digging, I found you have to enable ‘Use Host I/O Cache’ in Settings -> Storage -> Controller:SATA.

Second to the permission problem, I was recieving the error ‘VERR_FILE_NOT_FOUND’ every few boots. Many attribute this to the permissions not being set but even after chmod I would still get the error. It turned out that since I now have in my Macbook, sometimes the mount points would change at boot (SDD disk1 not disk0) leaving the virtual disk referring to a non-existent partition.

The .vmdk can be edited using a text editor and contains the line RW 78125000 FLAT "/dev/disk1s3" 0, clearly referring to the physical disk. Correcting this stopped the error. So my script is a little more complex than some but makes it almost flawless. The operation is:

  • Find and set the variable disk as the correct BootCamp mount point (using mountgrep Windows
  • Set full permissions to $disk
  • Unmount $disk
  • Using awk look at line 12 of bootcamp.vmdk to check if it is referring to the right place.
  • Boot the virtual machine if it is, otherwise signal it is not
  • Plenty of ASCII like any good bash script!

I also included the additional remount arg to remount the volume once I’ve finished using machine.

Copy and paste the below into a text file, then save it in /usr/local/sbin

printf '\e[31m
 ______     ______     ______   ______   __     __   __     ______        __  __     ______      ______     ______     __    __     ______     
/\  ___\   /\  ___\   /\__  _\ /\__  _\ /\ \   /\ "-.\ \   /\  ___\      /\ \/\ \   /\  == \    /\  ___\   /\  __ \   /\ "-./  \   /\  == \    
\ \___  \  \ \  __\   \/_/\ \/ \/_/\ \/ \ \ \  \ \ \-.  \  \ \ \__ \     \ \ \_\ \  \ \  _-/    \ \ \____  \ \  __ \  \ \ \-./\ \  \ \  _-/    
 \/\_____\  \ \_____\    \ \_\    \ \_\  \ \_\  \ \_\\"\_\  \ \_____\     \ \_____\  \ \_\       \ \_____\  \ \_\ \_\  \ \_\ \ \_\  \ \_\      
  \/_____/   \/_____/     \/_/     \/_/   \/_/   \/_/ \/_/   \/_____/      \/_____/   \/_/        \/_____/   \/_/\/_/   \/_/  \/_/   \/_/      

if [ "$1" == "remount" ]; then
  diskutil mount /dev/disk0s4
  #Get mount point of bootcamp
  disk=$(mount | grep 'Windows' | cut -d" " -f1)
  printf "\e[1;31m\e[47mSetting up permissions....\e[0m\n"
  chmod 777 $disk
  printf "\e[1;31m\e[47mUnmounting Bootcamp parition ($disk)....\e[0m\n"
  diskutil umount $disk
  line=$(awk 'NR==12' ~/VirtualBox\ VMs/Bootcamp/bootcamp.vmdk)
  if [ "$line" != "RW 78125000 FLAT \"$disk\" 0" ]; then
    printf "\e[1;37m\e[41mThe virtual machine is refering to the wrong partition and will not boot!!\e[0m\n"
  printf "\e[1;31m\e[47mStarting virtual machine....\e[0m\n"
  sudo -u John vboxmanage startvm 'VirtualCamp'
  export disk=$disk

How it looks when it runs:

BootCamp VM Bash Script

9 replies on “BootCamp Partition Virtual Machine with VirtualBox”

It all worked but I get ‘VERR_FILE_NOT_FOUND’ and I don’t quite understand how you are explaining to fix it any help?

I found this happened due to the virtual machine referring to the wrong partition. Do you have you Boot Camp on a separate drive?

Open ‘bootcamp.vmdk’, does the line RW ... FLAT "/dev/disk0s4" refer to the same mount point as the Boot Camp partition when you run diskutil list from Terminal?

Hi John, once i try to run the bootcamp vm I receive a Missing operating system?? My ..FLAT.. refers to the same mount point as diskutil list. Thanks in advance for any suggestions 🙂

Hi Doug. I’ve found the script can be a little particular and needs running twice. Once as root ‘sudo bootcamp’ (to set permission and dismount) and then once as standard user ‘bootcamp’ to launch. It needs to be done in that order otherwise the permissions won’t set but it will still dismount.

Let me know if that works.

HI john, thanks for the quick reply. This happens right after step 7, should i run the script before I start the vbox?

Yes, you have to run the script before starting the vbox; it sets the permissions to access the partition (these change back to read only at boot) and dismounts the partition from Finder.

Hi John, Everything is simple, but the process does not work with windows 8.1 because of efi bootloader. Well when I mount only bootcamp partition, vm rightly gives error that there is no boot partition. However if I also mount boot partition I think it tries to load the osx again and is lost somewhere. Do you happen to know if we can specify a different startup partition to boot when booting from vm. parallels and vmware surely does that somehow.

When i try and select the disk file in virtualbox i get this error:

Permission problem accessing the file for the medium ‘/Users/jordan/win10raw.vmdk’ (VERR_ACCESS_DENIED).

Result Code:
IMedium {4afe423b-43e0-e9d0-82e8-ceb307940dda}
IVirtualBox {0169423f-46b4-cde9-91af-1e9d5b6cd945}
Callee RC:

Sounds like you did not perform Step. 5 correctly:

sudo chown YOUR-USERNAME *.vmdk

Where YOUR_USERNAME is replaced with the username for your account.

Leave a Reply