PolarSPARC

VirtualBox VMs using CLI


Bhaskar S 11/06/2021


Overview

For demonstrations, where there is a need for one or more virtual machines (VMs) on the Linux desktop, typically make use of VirtualBox (for creating the VMs) using the installed GUI.

One could also use the CLI utility vboxmanage (or VBoxManage) to achieve the same goal of creating VMs.

Without further ado, we will jump in for a hands-on demonstartion of using the command-line for VM creation.

Hands-on vboxmanage

The demonstration will be on a Ubuntu 20.04 LTS based Linux desktop. Ensure a compatible version of the VirtualBox software is installed on the Linux desktop.

To find the version of VirtualBox installed, execute the following command in a Terminal window:

$ vboxmanage --version

The following would be the typical output:

Output.1

6.1.26_Ubuntur145957

We will create a base directory where all the files realated to our VM instance(s) will be stored. To create a base folder directory, execute the following command in the Terminal window:

$ mkdir -p $HOME/Downloads/VirtualBox


VM Definition

The first step in creating in VM VirtualBox is to create a VM definition file. To do that, execute the following command in the Terminal window:

$ vboxmanage createvm --name 'test-vm' --ostype Ubuntu_64 --register --basefolder $HOME/Downloads/VirtualBox

The following is the brief description of the options used:

The following would be the typical output:

Output.2

Virtual machine 'test-vm' is created and registered.
UUID: 0913e84b-3118-4f65-b990-4d92510bab29
Settings file: '/home/polarsparc/Downloads/VirtualBox/test-vm/test-vm.vbox'

VM Hardware Options

The next series of step(s) is to modify the properties (related to the processor, the video, etc) of the VM definition we just created.

To enable clipboard and specify the VM snapshot directory, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --snapshotfolder $HOME/Downloads/VirtualBox/test-vm/Snapshots --clipboard bidirectional

The following is the brief description of the options used:

To specify the RAM size and the boot order for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --memory 4096 --boot1 dvd --boot2 disk --boot3 none --boot4 none

The following is the brief description of the options used:

To specify the mouse/keyboard type and enable hardware detection for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --mouse ps2 --keyboard ps2 --apic on

The following is the brief description of the options used:

To enable and configure the virtual CPU for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --cpus 1 --cpuexecutioncap 100

The following is the brief description of the options used:

To enable and configure hardware virtualization for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --hwvirtex on --paravirtprovider kvm --nestedpaging on

The following is the brief description of the options used:

To enable and configure video for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --vram 128 --monitorcount 1 --graphicscontroller vmsvga

The following is the brief description of the options used:

To enable and configure audio output for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --audioout on

The following is the brief description of the options used:

To enable and configure the network adapter for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --nic1 bridged --bridgeadapter1 enp6s0 --nictype1 82540EM --nicpromisc1 deny --macaddress1 auto --cableconnected1 on

Notice the use of the suffix 1 at the end of all the options. This means we are configuring the first network adapter.

The following is the brief description of the options used:

To enable and configure USB support for the VM, execute the following command in the Terminal window:

$ vboxmanage modifyvm 'test-vm' --usb on --usbohci on --usbehci on

The following is the brief description of the options used:

VM Storage

The next step is to create the virtual storage (storage disk) for the VM definition we created and modified above.

To create the virtual storage, execute the following command in the Terminal window:

$ vboxmanage createmedium disk --filename $HOME/Downloads/VirtualBox/test-vm/test-vm.vdi --size 16384 --format VDI --variant fixed

The following is the brief description of the options used:

The following would be the typical output:

Output.3

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: 6c3340f7-530a-4f8a-b865-1cd869a02ff0

VM Storage Options

The next set of step(s) is to setup the storage controller(s) (such as IDE, SATA, etc) for the VM definition we created.

To create an IDE disk controller for the dvd drive, execute the following command in the Terminal window:

$ vboxmanage storagectl 'test-vm' --name 'IDE' --add ide --controller PIIX4 --hostiocache on

The following is the brief description of the options used:

To create a SATA disk controller for the hard drive, execute the following command in the Terminal window:

$ vboxmanage storagectl 'test-vm' --name 'SATA' --add sata --controller IntelAhci --portcount 1

The following is the brief description of the options used:

VM Storage Attach

The next set of step(s) is to attach the appropriate virtual storage to the storage controller(s) in the VM definition we created.

To attach the VDI disk image to the SATA disk controller, execute the following command in the Terminal window:

$ vboxmanage storageattach 'test-vm' --storagectl 'SATA' --port 0 --device 0 --type hdd --medium $HOME/Downloads/VirtualBox/test-vm/test-vm.vdi

The following is the brief description of the options used:

In order to create a VM image with a guest OS, download the latest version of Ubuntu 20.04 ISO. For this demonstration, we downloaded the latest Xubuntu 20.04.3 ISO image and stored it in the directory $HOME/Downloads/VirtualBox. To attach the ISO image to the IDE disk controller, execute the following command in the Terminal window:

$ vboxmanage storageattach 'test-vm' --storagectl 'IDE' --port 0 --device 0 --type dvddrive --medium $HOME/Downloads/VirtualBox/xubuntu-20.04.3-desktop-amd64.iso


VM Install Guest OS

Finally, it is time to start the VM to complete the installation of the guest OS onto the VM.

To complete the installation of the guest OS, execute the following command in the Terminal window:

$ vboxmanage startvm 'test-vm'

Once the installation completes, we will have a VM machine image that can be started and stopped at will.

VM Clone Image

To create more instances of the VM from a given machine image, one can create VM clones.

To create a clone of a machine image, execute the following command in the Terminal window:

$ vboxmanage clonevm 'test-vm' --name 'test-vm-2' --register --basefolder $HOME/Downloads/VirtualBox --mode machine

The following would be the typical output:

Output.4

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Machine has been successfully cloned as 'test-vm-2'

References

VirtualBox createvm

VirtualBox modifyvm

VirtualBox createmedium

VirtualBox storagectl

VirtualBox storageattach

VirtualBox startvm

VirtualBox clonevm



© PolarSPARC