A script to ease the process of creation and registering of raw disk .vmdk files that are used by Virtualbox to launch os from that hard drive in virtual environment.
You need to ensure following
- Virtualbox is installed.
- Virtual machine for guest os is created. Name can be set in script.
- VM has an empty IDE controller. It does not work with SATA for some reason.
- Go to VM Settings > Storage
- Click 'Add new storage controller' below the 'Storage Devices' list
- Select 'Add IDE controller'
- Save
- System Integrity Protection needs to be disabled for Virtualbox to be able to mount drives.
- Restart…
- Hold down Command-R to reboot into Recovery Mode.
- launch Utilities > Terminal.
- run
csrutil disable
- Restart…
- vboxmanage reference page
- diskutil reference. Official apple page was taken down, or moved :/
This script can optionally attach/clear vmdk file to existing VM. Since to use drives directly Virtualbox requires owner permissions and drives in /dev/
are recreated every boot, this script needs to be rerun before starting VM.
bash ./create_vmdk.sh "VOLUME_NAME_1,VOLUME_NAME_2" --post 0 --device 0 --vm-name "VM_NAME" --vbox-man "PATH_TO_VBOXMANAGE" --attach --clear --efi
arguments | description |
---|---|
$1 | Name of the volume to make .vmdk of. Accept comma separated list of volume names. All volumes must be located on one physical drive. |
--port | Port of the IDE controller, default 0. |
--device | Device on the port of the IDE controller, default 0. |
--vm-name | Name of the virtual machine as it is registered in Virtualbox. You can check it by running vboxmanage list vms . Default Bootcamp . |
--vbox-man | Alternative path to vboxmanage tool. Full path is usually in Virtualbox.app: /Applications/VirtualBox.app/Contents/MacOS/VBoxManage . Default vboxmanage . |
--attach | Attaches created .vmdk to existing virtualbox. --vm-name should be specified. |
--clear | Clears .vmdk with same parameters from existing vm. Since this script has to be run every time or after every boot, you will need this if you use --attach . |
--efi | Tries to find EFI partition on the same disk and add it to the list of partitions for .vmdk file. |
To creates vmdk file for windows installed on Bootcamp
volume and include EFI
partition in it:
bash ./create_vmdk.sh "BOOTCAMP" --efi
Same as above but also register vmdk with VM and clear existing one if previously registered:
bash ./create_vmdk.sh "BOOTCAMP" --efi --attach --clear --vm-name "Bootcamp"
To create vmdk with vboxmanage
from custom path:
bash ./create_vmdk.sh "BOOTCAMP" --efi --vbox-man "/Applications/VMTools/VirtualBox.app/Contents/MacOS/VBoxManage"
Heres an example of a script to automate creation, removal, registration of vmdk, launch of VM, and mounting back volumes after VM closes and Virtualbox quits.
run.command
#!/bin/bash
source ./create_vmdk.sh
cd "$(dirname "$0")"
echo "working in $PWD"
vm_name="Windows10"
create_vmdk "BOOTCAMP" --device 0 --vm-name $vm_name --attach --clear --efi
create_vmdk "STORAGE_1,STORAGE_2" --device 1 --vm-name $vm_name --attach --clear
# this will wait for VirtualBox.app to quit, not just VM to close
open -g -W ./${vm_name}.vbox -a "/Applications/VirtualBox.app"
# after vbox quit it will mount back volumes
mount_volume "BOOTCAMP"
mount_volume "STORAGE_1,STORAGE_2"
Dont forget to make it executable sudo chmod +x ./run.command
efficient and seamless.. thanks for sharing