Automate Virtual Machine Installation on VirtualBox

|
Last Updated:
|
|

In this tutorial, you will learn how to automate virtual machine installation 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.

Automating Virtual Machine Installation on VirtualBox

To automate virtual machine creation on VirtualBox, 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.

Install VirtualBox on your Host system

To begin with, you need to have installed VirtualBox on your host system. You can check the link below on how to install VirtualBox;

How to install VirtualBox on Linux system

Create and Register a Virtual Machine

As a first step on how to automatatically install a virtual machine on VirtualBox, run the command below to create and register the vm.

VBoxManage createvm --name ubuntu18server --ostype Ubuntu_64 --register

Sample output;

Virtual machine 'ubuntu18server' is created and registered.
UUID: 41d12b9c-10b2-46a1-8710-7f00a9a02de7
Settings file: '/home/kifarunix/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/kifarunix/VirtualBox\ VMs/ubuntu18server/ubuntu18server.vdi \
--size 10240
  • 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/kifarunix/VirtualBox\ VMs/ubuntu18server/ubuntu18server.vdi

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/kifarunix/Downloads/iso/Debian/ubuntu-22.04-desktop-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 wlp0s20f3 --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=kifarunix --password=P@SSWORD \
--country=US --time-zone=EST \
--language=en-US \
--hostname=ubuntu18server.kifarunix.com \
--iso=/home/kifarunix/Downloads/iso/Debian/ubuntu-22.04-desktop-amd64.iso \
--start-vm=gui

Sample command output;


VBoxManage: info: Starting unattended installation of Ubuntu_64 in machine 'ubuntu18server' (0875d510-dbe1-456f-8e72-ee2ee28757ad).
VBoxManage: info: Using values:
                           isoPath = /home/kifarunix/Downloads/iso/Debian/ubuntu-22.04-desktop-amd64.iso
                              user = kifarunix
                          password = P@SSWORD
                      fullUserName = 
                        productKey = 
                  additionsIsoPath = /usr/share/virtualbox/VBoxGuestAdditions.iso
             installGuestAdditions = false
              validationKitIsoPath = 
            installTestExecService = false
                            locale = en_US
                           country = US
                          timeZone = EST
                             proxy = 
                          hostname = ubuntu18server.kifarunix.com
       packageSelectionAdjustments = 
                 auxiliaryBasePath = /home/kifarunix/vol01/VirtualBox VMs/ubuntu18server/Unattended-0875d510-dbe1-456f-8e72-ee2ee28757ad-
                        imageIndex = 1
                scriptTemplatePath = /usr/share/virtualbox/UnattendedTemplates/ubuntu_preseed.cfg
     postInstallScriptTemplatePath = /usr/share/virtualbox/UnattendedTemplates/debian_postinstall.sh
                postInstallCommand = 
      extraInstallKernelParameters =  auto=true preseed/file=/cdrom/preseed.cfg priority=critical quiet splash noprompt noshell automatic-ubiquity debian-installer/locale=en_US keyboard-configuration/layoutcode=us languagechooser/language-name=English localechooser/supported-locales=en_US.UTF-8 countrychooser/shortlist=US --
                          language = en-US
                  detectedOSTypeId = Ubuntu_64
                 detectedOSVersion = 18.04.1 LTS "Jammy Jellyfish"
                  detectedOSFlavor = Ubuntu-Server 
               detectedOSLanguages = en-US
                   detectedOSHints = 
VBoxManage: info: Waiting for VM 'ubuntu18server' to power on...
VBoxManage: info: VM 'ubuntu18server' (0875d510-dbe1-456f-8e72-ee2ee28757ad) has been successfully started.

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 virtual machine installation on VirtualBox. 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.

Other Tutorials

Install KVM on Oracle Linux

Change Windows Boot Device from IDE to Virtio on KVM

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

3 thoughts on “Automate Virtual Machine Installation on VirtualBox”

    • 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, 🙂

      Reply
  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

    Reply

Leave a Comment