Create VirtualBox VM from the command line

Sep 21, 2011 · virtualbox

[ Note that I am in the process of migrating these blog posts from Wordpress, and as such it may contain incorrect formatting, missing links, etc. My old site is available at http://www-old.perkin.org.uk/ so if you find something obviously broken please let me know. Thanks! ]

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!


All blog posts

Feb 20, 2012 » New blog design
Jan 11, 2012 » Set up anonymous FTP upload on Oracle Linux
Jan 09, 2012 » Kickstart Oracle Linux in VirtualBox
Jan 09, 2012 » Kickstart Oracle Linux from Ubuntu
Dec 22, 2011 » Last day at MySQL
Dec 15, 2011 » Installing OpenBSD with softraid
Sep 21, 2011 » Create VirtualBox VM from the command line
Sep 14, 2011 » Creating chroots for fun and MySQL testing
Jun 30, 2011 » Graphing memory usage during an MTR run
Jun 29, 2011 » Fix input box keybindings in Firefox
Jun 24, 2011 » How to lose weight
Jun 23, 2011 » How to fix stdio buffering
Jun 13, 2011 » Serving multiple DNS search domains in IOS DHCP
Jun 13, 2011 » Fix Firefox URL double click behaviour
Apr 20, 2011 » SSH via HTTP proxy in OSX
Nov 09, 2010 » How to build MySQL releases
Apr 29, 2010 » 'apt-get' and 5,000 packages for Solaris10/x86
Sep 16, 2009 » ZFS+NFS vs OSX
Sep 12, 2009 » pkgsrc on Solaris
Dec 09, 2008 » Jumpstart from OSX