How to Automate Virtual Machine Installation on VirtualBox

3
6327

Hello there, today we are going to learn how to automate virtual machine creation on VirtualBox. Many times, not once, not twice when you would want to create virtual machine on VirtualBox. So just imagine how tedious and time consuming it is to create a VM manually every time you need a virtual lab to play with. No, that manual procedure is not for you.

To make this a realization, we are going to use the unattended install feature of VBoxManage command. With this feature, you can get the VM automatically installed with very little human interaction while setting up installation variables.

You can also script the whole of this procedure to achieve full blown automation.

Create and Register a Virtual Machine

As a first step, run the command below to create and register the vm.

$ VBoxManage createvm --name ubuntu18server --ostype Ubuntu_64 --register
Virtual machine 'ubuntu18server' is created and registered.
UUID: 41d12b9c-10b2-46a1-8710-7f00a9a02de7
Settings file: '/home/mibeyki/VirtualBox VMs/ubuntu18server/ubuntu18server.vbox'
  • The createvm command creates a new XML virtual machine definition file.
  • The –ostype option specifies the guest operating system to run in the VM. Run the command
    • $ VBoxManage list ostypes | less
  • The –register option imports the virtual machine definition in an XML file into VirtualBox.

Setup the Virtual Machine Storage Medium

Once you have registered a VM, create a storage medium as shown below.

$ VBoxManage createmedium --filename /home/mibeyki/VirtualBox\ VMs/ubuntu18server/ubuntu18server.vdi --size 10240
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: 854c2296-28b9-4d6e-b6a7-c148ee823823
  • The createmedium command creates a new medium
  • The –filename option specifies the location of the VM. As shown above, the format of the medium is VDI. Other formats exist; VMDK, VHD.
  • The –size option specifies the size of the storage medium in megabytes.

Add and Attach SATA and IDE Storage Controllers

VirtualBox usually uses SATA controller as the default for newly created virtual machines. To add a SATA controller to VM, run the command below.

$ VBoxManage storagectl ubuntu18server --name SATA --add SATA --controller IntelAhci
$ VBoxManage storageattach ubuntu18server --storagectl SATA --port 0 --device 0 --type hdd --medium /home/mibeyki/VirtualBox\ VMs/ubuntu18server/ubuntu18server.vd

IDE ATA provides support CD-ROM drives and other types of removable media. It enables you to attach upto four virtual storage devices to your vm. One of these four virtual storages can be preconfigured to be the machine’s virtual CD/DVD drive.

$ VBoxManage storagectl ubuntu18server --name IDE --add ide
$ VBoxManage storageattach ubuntu18server --storagectl IDE --port 0 --device 0 --type dvddrive --medium /home/mibeyki/Downloads/iso/Debian/ubuntu-18.04-server-amd64.iso
  • The storagectl command defines the storage controller for the VM
  • The –name parameter specifies the name of the storage controller. It is mandatory.
  • The –add parameter specifies the type of the system bus to which the storage controller must be connected.
  • The –controller parameter enables a choice of chipset type being emulated for the given storage controller.
  • The storageattach command attaches the storage medium to the controller.
  • The –port parameter specifies the number of the storage controller’s port which is to be modified.
  • The –type parameter defines the type of the drive to which the medium is being attached
  • The –medium parameter specifies what is to be attached.

Define the VM General Settings

Next, we are going to define various properties of the registered VM. To achieve this, we are going to us the modifyvm command. Most of the properties that this command makes available correspond to the VM settings that VirtualBox graphical user interface displays in each VM’s “Settings” dialog.

Set the VM RAM and Virtual graphics card RAM size

Allocate the vm a specific RAM as well as nvram size as shown below;

$ VBoxManage modifyvm ubuntu18server --memory 1024 --vram 16

Enable IO APIC

With IO APIC enabled, the OS can use more than 16 interrupt requests (IRQs) thus avoiding IRQ sharing for improved reliability.

$ VBoxManage modifyvm ubuntu18server --ioapic on

Define the boot order for the virtual machine

Run the command below to define the vm boot order.

$ VBoxManage modifyvm ubuntu18server --boot1 dvd --boot2 disk --boot3 none --boot4 none

Define the number of virtual CPUs for the VM

$ VBoxManage modifyvm ubuntu18server --cpus 2

Disable Audio for the VM

Since this is a server, there is no point of having audio enabled.

$ VBoxManage modifyvm ubuntu18server --audio none

Disable USB. USB2.0, USB3.0 controllers.

Since I am not planning on using USB with my VM, am choosing to disable this. The case may be different for you.

$ VBoxManage modifyvm ubuntu18server --usb off
$ VBoxManage modifyvm ubuntu18server --usbehci off
$ VBoxManage modifyvm ubuntu18server --usbxhci off

Define the Networking settings for the VM

In this case, am going to assign my VM two NICs, NAT and Bridged for NIC1 and NIC2 respectively.

$ VBoxManage modifyvm ubuntu18server --nic1 bridged --bridgeadapter1 wlan0 --nic2 nat

You can define as many settings as it pleases you.

Virtual Machine Unattended Installation

Once you are done, it is time to install the VM unattended. Run the following   command to start the VM auto-installation.

$ VBoxManage unattended install ubuntu18server --user=amos [email protected] --country=KE --time-zone=EAT --hostname=server01.example.com --iso=/home/mibeyki/Downloads/iso/Debian/ubuntu-18.04-server-amd64.iso --start-vm=gui

What the command does is that, it launches and configure the VM with all the variable set. The breakdown of the same command is as in below;

  • The unattended install configures the VM with the defined variables non-interactively.
  • –user sets the login username. If not specified, the default user set will be vboxuser.
  • –password sets the password for the defined login user
  • –country speficies the two letter country code.
  • –time-zone specifies the time zone to set up the guest OS with.
  • –hostname sets the VM hostname to the defined fqdn.
  • –iso specifies the installation ISO to run the detection on.
  • –start-vm starts the VM using the front end given by session type such as headless, gui

That is all it takes to automate VirtualBox guest OS installation. The process may not seem automated due to various settings being made. Therefore, consider putting all these steps in bash script to achieve full automation.

We hope this helped. Thank you for visiting kifarunix.com.

3 COMMENTS

    • On the guide, we attached a bridged interface for the wireless interface as the 2nd interface, which means, on the VM created, you can assign the 2nd interface an IP address on the range of your wlan adapter on your host.
      You can instead attach an host-only interface and create the host-only networks on VirtualBox Host Network Manager and assign the IP to the vm in the range of the host-only network created. I hope am not confusing further, 🙂

  1. Hi.. I am trying to run the unattened install command, but getting the below message. I am able to install the same ISO manually through Virtual box though. Can someone please help.

    VBoxManage.exe: info: Starting unattended installation of Ubuntu_64 in machine ‘EnergyManager’ (98ed9b8d-e1af-4346-8aec-d15d65f8e6c8).
    VBoxManage.exe: error: ISO reader fail to open ‘C:\Program Files\Oracle\VirtualBox\ENL_release_1404_64Bit_7955_20160617002125.iso’ (VERR_VFS_UNSUPPORTED_FORMAT): Unsupported supplemental volume descriptor version: 0x2
    VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component UnattendedWrap, interface IUnattended, callee IUnknown
    VBoxManage.exe: error: Context: “ConstructMedia()” at line 1796 of file VBoxManageMisc.cpp

LEAVE A REPLY

Please enter your comment!
Please enter your name here