BootCamp VM Bash Script

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

How it looks when it runs:

BootCamp VM Bash Script

cropped-JBRLogo.png

9 thoughts on “BootCamp Partition Virtual Machine with VirtualBox”

  1. 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?

    1. 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?

  2. 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 🙂

    1. 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.

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

    1. 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.

  4. 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.

  5. 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:
    VBOX_E_FILE_ERROR (0x80BB0004)
    Component:
    MediumWrap
    Interface:
    IMedium {4afe423b-43e0-e9d0-82e8-ceb307940dda}
    Callee:
    IVirtualBox {0169423f-46b4-cde9-91af-1e9d5b6cd945}
    Callee RC:
    VBOX_E_OBJECT_NOT_FOUND (0x80BB0001)

    1. 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