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.
- Get the partition of your BootCamp install using
diskutil list, it’s the /dev/disk* listed under what your named your partition.</p>
sudo chmod 777 /dev/disk0s3using the correct partition numbering you found above (replace /dev/disk0s3 onwards).
diskutil umount /dev/disk0s3– ejects the Windows partition.
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).
sudo chown YOUR-USERNAME *.vmdk– ensures you own rights to the files created.
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).
- 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
diskas the correct BootCamp mount point (using
mount</code piped with <code>grep Windows
- Set full permissions to
awklook 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
#!/bin/sh printf '\e[31m ______ ______ ______ ______ __ __ __ ______ __ __ ______ ______ ______ __ __ ______ /\ ___\ /\ ___\ /\__ _\ /\__ _\ /\ \ /\ "-.\ \ /\ ___\ /\ \/\ \ /\ == \ /\ ___\ /\ __ \ /\ "-./ \ /\ == \ \ \___ \ \ \ __\ \/_/\ \/ \/_/\ \/ \ \ \ \ \ \-. \ \ \ \__ \ \ \ \_\ \ \ \ _-/ \ \ \____ \ \ __ \ \ \ \-./\ \ \ \ _-/ \/\_____\ \ \_____\ \ \_\ \ \_\ \ \_\ \ \_\\"\_\ \ \_____\ \ \_____\ \ \_\ \ \_____\ \ \_\ \_\ \ \_\ \ \_\ \ \_\ \/_____/ \/_____/ \/_/ \/_/ \/_/ \/_/ \/_/ \/_____/ \/_____/ \/_/ \/_____/ \/_/\/_/ \/_/ \/_/ \/_/ ' if [ "$1" == "remount" ]; then diskutil mount /dev/disk0s4 else #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" else printf "\e[1;31m\e[47mStarting virtual machine....\e[0m\n" sudo -u John vboxmanage startvm 'VirtualCamp' export disk=$disk fi
How it looks when it runs: