VirtualBox: Automate Virtual Machine Creation Process through VBoxManage + PowerShell

Automation isn’t a silver bullet, and it won’t fix your broken processes for you.

This article is to tell how to create Virtual Machine with VBoxManage commands. By PowerShell, the whole process could be automatic and might save you much time 😃 even somebody said that automation isn’t a silver bullet.

Story Preview

VBoxManage Commands Introduction

Before checking VirtualBox help document and installation directory, I didn’t know the existence of VBoxManage — the powerful command line interface of VirtualBox.

The first command of VBoxManage I used is startvm which is used to start given Virutal Machine. I created one PowerShell alias for VBoxManage: vbm

"vbm","C:\Program Files\Oracle\VirtualBox\VBoxManage.exe","","None"

And load it in PowerShell profile file. Then, defined one simple function:

function vbf {vbm startvm Fedora_64}

As you see, Fedora_64 is the Virtual Machine name. I created one PowerShell script to run this function and call the script with one scheduled task in Windows. The scheduled task will run after login, so the Virtual Machine will start automatically after I login my laptop. It brings good experience to me. Check article Windows: Customize Autostart Programs and Tasks with schtasks.exe to know more about auto-start program setting.

VBoxManage has lots of commands and most commands have lots of options.

> vbm startvm
Usage:
VBoxManage startvm <uuid|vmname>...
[--type gui|sdl|headless|separate]
[-E|--putenv <NAME>[=<VALUE>]]

Gradually, I start to learn and use more commands for my own purpose. For example, the command below is to capture the screenshot of the running Virtual Machine and save it as one .png file:

vbm controlvm $vm_name screenshotpng "$target_file_name"

It’s very helpful when I prepare tutorial materials.

Virtual Machine Creation by VBoxManage

I am an automation testing developer and took lot of time on scripts learning in past 3 years. Recently, I got one need to re-create all Virtual Machines. You know, for one automation developer, to create even 2 Virtual Machine by manually would be a waste of life.

That’s why I start to learn how to create Virtual Machine by VBoxManage commands.

To create a Virtual Machine to be ready to install OS, we need the following steps:

Figure 1: Steps (VBoxManage Commands) to Create Virtual Machine

📔 Two storagectl and storageattach are for hard disk and DvD drive respectively. Check more in below section: commands details for each step.

Commands details for each Step:

vbm createvm --name "${vm_name}" --groups "$group_id" --ostype "$ostype" --register --basefolder "$vms_home"
vbm createmedium disk --filename "$disk_medium_path" --size $disk_size
vbm storagectl "$vm_name" --name "$disk_sc_name" --add $disk_sb_type --controller "$disk_sc_type" --portcount $sc_port_count --hostiocache off --bootable on
vbm storageattach "$vm_name" --storagectl "$disk_sc_name" --port $disk_sc_attach_port --device $disk_sc_attach_device --type "$disk_drive_type" --medium "$disk_medium_path"
  • Create Storage Control for DvD Drive
vbm storagectl "$vm_name" --name "$dvd_sc_name" --add "$dvd_sb_type" --controller "$dvd_sc_type" --portcount $sc_port_count --bootable on --hostiocache off
  • Attach image file (*.iso) to DvD Drive
vbm storageattach "$vm_name" --storagectl "$dvd_sc_name" --port $dvd_sc_attach_port --device $dvd_sc_attach_device --type "$dvd_drive_type" --medium "$iso_uri"
  • Configure CPU, Memory, Network, Boot Sequence for the Virtual Machine (Refer to Help Document)
vbm modifyvm “$vm_name” --cpus $cpu_count --memory $memory_size --vram $vram_size --nic1 $network_mode --boot1 dvd --boot2 disk --boot3 none --boot4 none

Then, we can start the Virtual Machine to install the OS.

vbm startvm "$vm_name"

There are something else need to mention:

  • Host disk space checking. Before create new virtual disk, we need to make sure there is enough Host disk space for new virtual disk.
  • OS Image file preparation. We need download *.iso files in advance. For example, download *.iso file for ArchLinux OS from here. Then, we can attach them to DvD drive (storage controller — Attach image file (*.iso) to DvD Drive above).
  • Random names. As we want to create new Virtual Machine by automation script, we will use timestamp in Virtual Machine name, storage controller name, and so on. Otherwise, we have to remove existing Virtual Machine before creating new one.

Host disk space checking

By get-disk cmdlet of PowerShell, we can get disk total size data.

> (get-disk).AllocatedSize
512109142016

By get-psdrive cmdlet of PowerShell, we can get how much disk space has been used and how much is still free:

> (get-psdrive).used
396510621696
> (get-psdrive).free
113945985024

To make the disk size more readable, we can convert the bytes to Megabytes:

> $one_mb = 1024*1024
> (get-psdrive).free / $one_mb
108663,2578125

And then round the result value to remove extra decimals:

> $free_size = (get-psdrive).free / $one_mb
> [Math]::Round($free_size, 2)
108663,25

Put all together to create one function to fetch free disk space:

function get_free_disk_size ($return_values = $false) {
$one_mb = 1024*1024;
$free_space = (get-psdrive).free/$one_mb;
$free_space = [Math]::Round($free_space, 2)
return $free_space
}

Random names

We can use one simple function to return random string by time-stamp:

function get_timestamp(){
Get-Date -Format o | ForEach-Object { $_ -replace ":", "." }
}

Whole PowerShell Function

Finally, let see how the whole PowerShell function looks like. And please feel free to try it and enjoy!

That’s all about how to automate the VM creation process and hope it can help you in some degree. Don’t hesitate to leave your comments and advice. Thanks!

Happy coding!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store