Lately, I’ve been creating, deleting and recreating a lot of virtual machines with Oracle VirtualBox. So today, when I needed to start with a clean environment, I decided to create a virtual machine using the command line instead of the GUI.
Clicking through the wizard is a lot easier than creating the virtual machine from the command line, but there are some options I always change after creating the guest. For example, to install Windows, you need to mount a CD. To access other installation files on my laptop, I need to share a local folder. And I like to enable drag ‘n’ drop. So why not automate the creation process, including these settings? This is the script I created, calling VBoxManage.exe:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
REM General SET "vmname=DC01" SET "memory=2048" SET "description=Windows 2008 64 bit domain controller" "C:\Program Files\Oracle\VirtualBox\Vboxmanage" createvm --name %vmname% --register --ostype Windows2008_64 "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname%--clipboard bidirectional "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname%--draganddrop hosttoguest "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname%--description %description% REM System "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname% --memory %memory% "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname% --cpus 2 REM Display "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname% --vram 30 REM Storage "C:\Program Files\Oracle\VirtualBox\Vboxmanage" storagectl %vmname% --name IDE --add IDE --controller PIIX4 --bootable on "C:\Program Files\Oracle\VirtualBox\Vboxmanage" storageattach %vmname% --storagectl IDE --port 0 --device 0 --type dvddrive --medium "D:\Install\Microsoft\Trial software\Windows 2008R2 x64 EVAL_EN_DVD.iso" "C:\Program Files\Oracle\VirtualBox\Vboxmanage" storagectl %vmname% --name SATA --add SATA --controller IntelAHCI --portcount 1 "C:\Program Files\Oracle\VirtualBox\Vboxmanage" createhd --filename "D:\VirtualBox VMs\%vmname%\disk1.vdi" --size 125000 --format vdi --variant Standard "C:\Program Files\Oracle\VirtualBox\Vboxmanage" storageattach %vmname% --storagectl SATA --port 0 --device 0 --type hdd --medium "D:\VirtualBox VMs\%vmname%\disk1.vdi" --nonrotational on REM network "C:\Program Files\Oracle\VirtualBox\Vboxmanage" modifyvm %vmname% --nic2 intnet REM Shared folder "C:\Program Files\Oracle\VirtualBox\Vboxmanage" sharedfolder add %vmname% --name install --hostpath d:\install --readonly --automount REM starten maar! "C:\Program Files\Oracle\VirtualBox\Vboxmanage" startvm %vmname% |
Update: in version 4.3.20, the program files directory has been changed from:
C:\Program Files\Oracle\VirtualBox
to:
C:\Program Files\Oracle VM VirtualBox
Update: for reusability, I’ve created variables for some parameters.