Create VirtualBox VM from the command line

Sep 21, 2011
tags: virtualbox

As something of a follow-up post to the previous entry, here’s a quick recipe for creating a Virtual Machine using the VirtualBox command line tools:

We’re using Windows Server 2008 64bit as an example, modify to taste.

$ VM='Windows-2008-64bit'

Create a 32GB “dynamic” disk.

$ VBoxManage createhd --filename $VM.vdi --size 32768

You can get a list of the OS types VirtualBox recognises using:

$ VBoxManage list ostypes

Then copy the most appropriate one into here.

$ VBoxManage createvm --name $VM --ostype "Windows2008_64" --register

Add a SATA controller with the dynamic disk attached.

$ VBoxManage storagectl $VM --name "SATA Controller" --add sata \
>  --controller IntelAHCI
$ VBoxManage storageattach $VM --storagectl "SATA Controller" --port 0 \
>  --device 0 --type hdd --medium $VM.vdi

Add an IDE controller with a DVD drive attached, and the install ISO inserted into the drive:

$ VBoxManage storagectl $VM --name "IDE Controller" --add ide
$ VBoxManage storageattach $VM --storagectl "IDE Controller" --port 0 \
>  --device 0 --type dvddrive --medium /path/to/windows_server_2008.iso

Misc system settings.

$ VBoxManage modifyvm $VM --ioapic on
$ VBoxManage modifyvm $VM --boot1 dvd --boot2 disk --boot3 none --boot4 none
$ VBoxManage modifyvm $VM --memory 1024 --vram 128
$ VBoxManage modifyvm $VM --nic1 bridged --bridgeadapter1 e1000g0

Configuration is all done, boot it up! If you’ve done this one a remote machine, you can RDP to the console via vboxhost:3389.

$ VBoxHeadless -s $VM

Once you have configured the operating system, you can shutdown and eject the DVD.

$ VBoxManage storageattach $VM --storagectl "IDE Controller" --port 0 \
>  --device 0 --type dvddrive --medium none

Finally, it’s a good idea to take regular snapshots so that you can always revert back to a known-good state rather than having to completely re-install.

$ VBoxManage snapshot $VM take <name of snapshot>

And, if you need to revert back to a particular snapshot:

$ VBoxManage snapshot $VM restore <name of snapshot>

Enjoy!

Share this post on Twitter, HackerNews, Facebook or Google+

All Posts