VirtualBox Scripting Tutorial
This article will be focused on how to build, launch and delete a VirtualBox virtual machine using the command line interface on a GNU\Linux operating system with Virtualbox 5.1.6 installed. The majority of the commands should still work on older versions of VirtualBox.
The VirtualBox CLI is really useful, for example, when automated testing or unattended virtual machines deployment are needed.
The article requires as a pre-requisite the raw image of an hard disk, the one that will become the hard disk of the virtual machine. In case we do not have available an image of this kind, it is sufficient to create a zeroed file that will become a not formatted virtual hard drive. Later, we will need to install a new Operating System by adding a proper install medium to the virtual machine.
To create a zeroed file in raw format:
#dd if=/dev/zero of=hdimage.raw bs=1M count=<size in MB>
Obviously, the system installed on the virtual hard drive should contain all kernel modules or drivers needed to boot on the virtual hardware. The most simple way to boot from our hard disk image is to convert it to the .vdi format using the VBoxManage utility:
VBoxManage convertdd <image.raw> <image.vdi> –format VDI
The command copies, byte-wise, all the content of the raw image to the .vdi files. Since the file is usually very large, the operation could take a long time to run. Moreover, at the end of the operation we will have two large files (doubling the required space) and the operations on one of them are not carried out on the second one.
An alternative to “VBoxManage convertdd” is to create a VMDK image file which leverages on our disk image. The advantage is that it is no more needed to copy byte-wisely the file content resulting in a much faster execution time. To create the vmdk file:
#VBoxManage internalcommands createrawvmdk -filename <output.vmdk> -rawdisk <absolute image path>
Unluckily, the disadvantage is that the vmdk descriptor requires the absolute path of the disk images. Once the vmdk file has been created, it will not be possible anymore to move the disk image without creating a new vmdk image. The limitation is due to the main purpose of the vmdk linked to a file: to connect a real physical disk to a virtual machine.
Virtual machine creation:
The main VirtualBox command line interface utility to manage virtual machines is VBoxManage. The program is very powerful and is very well documented. Please check the manual of VirtuaBox for the cases not covered in this tutorial.
First of all, we need to create a Virtual Machine named Thinox1:
#VBoxManage createvm –name “Thinox1” –ostype Linux –register
The output of the command will be something like:
Virtual machine ‘Thinox1’ is created.
Settings file: ‘/mnt/thrdhdd/virtualbox/Thinox1/Thinox1.vbox’
As we can see above, to the machine is assigned an UUID that could be used to execute the following commands in place of the machine name. After the execution of the above command, the machine will be showed by the VirtualBox GUI. In case we forgot the “–register” switch, it is still possible to register the machine with:
VBoxManage registervm /mnt/thrdhdd/virtualbox/Thinox1/Thinox1.vbox
Only after the registration the machine will be editable with the following commands.
Now we can set the memory size of the machine, the virtual network card, etc.
#VBoxManage modifyvm “Thinox1” –memory 1024 –acpi on –nic1 bridged –nictype1 82540EM –bridgeadapter1 <ethX>
<ehtX> is the network interface of the physical host bridged with the one of the guest machine.
Let’s create the vmdk image format linked to our virtual hard disk as explained above with the command:
#VBoxManage internalcommands createrawvmdk -filename thinox1.vmdk -rawdisk <abspath>/thinox.raw
Before adding a virtual hard disk to a machine we need to add a virtual hard disk controller:
#VBoxManage storagectl “Thinox1” –name “IDE Controller” –add ide –controller PIIX4
Now we can attach the vmdk hard disk to the virtual machine:
#VBoxManage storageattach “Thinox1” –storagectl “IDE Controller” –port 0 –device 0 –type hdd –medium thinox1.vmdk
If we need to add other medium to the VM, for example an installation medium we can use this command:
VBoxManage.exe storageattach “Thinox1” –storagectl “IDE Controller” –port 0 –device 0 –type dvddrive –medium <iso path>
Now we can start the machine:
#VBoxSDL –startvm Thinox1
To delete a virtual machine, for example because we performed all the test and already sent the report, we first need to de-register it:
#VBoxManage unregistervm Thinox1
Then we can delete the files from our hard drive:
#rm -rf /mnt/thrdhdd/virtualbox/Thinox3/
and, if needed, the raw image files.