VirtualBox Headless Script to Create/Delete VM

So creating a virtual machine using VBoxManage isn't like what it used to be. VirtualBox 3.1 changed around a lot of the options. I believe the changes are for the better, but they aren't well documented at all. The following python script (which isn't complete and is tailored to my OpenSolaris server) easily creates and deletes VMs for headless VirtualBox installations:

#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-

import os, sys

def usage():
	print "USAGE: " + sys.argv[0] + " [createvm|deletevm]"
	exit(1)

def createvm():
	name = raw_input('[+] VM Name:\t')
	base = raw_input('[*] VM Base:\t')
	memory = raw_input('[*] Memory:\t')
	hdsize = raw_input('[*] HD Size:\t')
	iso = raw_input('[*] ISO Image:\t')
	bridge = raw_input('[*] bNIC:\t')
	
	fullfilename =  base + "/" + name + ".vdi"
	
	print "----\t----"
	print "VBoxManage registerimage dvd \"" + iso + "\""
	os.system("VBoxManage registerimage dvd \"" + iso + "\"")
	
	print "----\t----"
	print "VBoxManage createvm -name \"" + name + "\" -register"
	os.system("VBoxManage createvm -name \"" + name + "\" -register")
	
	print "----\t----"
	print "VBoxManage modifyvm \"" + name + "\" --memory " + memory + " --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 " + bridge
	os.system("VBoxManage modifyvm \"" + name + "\" --memory " + memory + " --acpi on --boot1 dvd --boot2 disk --nic1 bridged --bridgeadapter1 " + bridge)
	
	print "----\t----"
	print "VBoxManage createvdi --filename \"" + fullfilename + "\" --size " + hdsize + " --register"
	os.system("VBoxManage createvdi --filename \"" + fullfilename + "\" --size " + hdsize + " --register")
	
	print "----\t----"
	print "VBoxManage storagectl \"" + name + "\" --add sata --name \"SATA Controller\""
	os.system("VBoxManage storagectl \"" + name + "\" --add sata --name \"SATA Controller\"")
	
	print "----\t----"
	print "VBoxManage storagectl \"" + name + "\" --add ide --name \"IDE Controller\""
	os.system("VBoxManage storagectl \"" + name + "\" --add ide --name \"IDE Controller\"")
	
	print "----\t----"
	print "VBoxManage storageattach \"" + name + "\" --storagectl \"IDE Controller\" --port 0 --device 0 --type dvddrive --medium \"" + iso + "\""
	os.system("VBoxManage storageattach \"" + name + "\" --storagectl \"IDE Controller\" --port 0 --device 0 --type dvddrive --medium \"" + iso + "\"")
	
	print "----\t----"
	print "VBoxManage storageattach \"" + name + "\" --storagectl \"SATA Controller\" --port 0 --device 0 --type hdd --medium \"" + fullfilename + "\""
	os.system("VBoxManage storageattach \"" + name + "\" --storagectl \"SATA Controller\" --port 0 --device 0 --type hdd --medium \"" + fullfilename + "\"")
	
	print "----\t----"
	print "[!] Called all VBoxManage entries, please double-check output."

def deletevm():
	name = raw_input("[+] VM Name: ")
	base = raw_input("[*] VM Base: ")
	
	print "----\t----"
	os.system("VBoxManage storagectl \"" + name + "\" --name \"SATA Controller\" --remove")
	os.system("VBoxManage unregistervm \"" + name + "\" --delete")
	os.system("VBoxManage unregisterimage disk \"" + base + "/" + name + ".vdi\" --delete")
	print "----\t----"
	
	print "[!] Deleted Virtual Machine \"" + name + "\", please double-check output."

if __name__ == '__main__':
	if len(sys.argv) < 2:
		usage()
	
	if (sys.argv[1] == 'createvm'):
		createvm()
	elif (sys.argv[1] == 'deletevm'):
		deletevm()

AddToAny

Share/Save