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:
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:
--name :: the name of the VM
--ostype :: the type of the guest OS to run on the VM
--register :: registers the VM definition in VirtualBox
--basefolder :: specifies the base folder directory where the VM definition be stored
The following would be the typical output:
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:
--snapshotfolder :: specifies the directory where one can snapshot the state of a VM for testing purposes
--clipboard :: indicates how the host clipboard is shared with the VM. The specified value of bidirectional means it is shared between the host and the VM both ways
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:
--memory :: specifies the amount of RAM (in MB) to allocate for the VM. The specified value of 4096 means 4 GB
--boot1 :: indicates the first boot device for the VM. The specified value of dvd means the first boot device is the DVD
--boot2 :: indicates the second boot device for the VM. The specified value of diskdvd means the second boot device is the HDD
--boot3 :: indicates the third boot device for the VM. The specified value of none means no third boot device
--boot1 :: indicates the fourth boot device for the VM. The specified value of none means no fourth boot device
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:
--mouse :: specifies the type of mouse used with the VM. The specified value of ps2 means it is a PS2 mouse
--keyboard :: specifies the type of keyboard used with the VM. The specified value of ps2 means it is a PS2 keyboard
--apic :: specifies the option to auto discovery and configuration of hardware in the VM. The specified value of on enables this feature
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:
--cpus :: specifies the number of virtual CPUs used for the VM. The specified value of 1 means a single virtual CPU
--cpuexecutioncap :: controls the percentage of the virtual CPU usage in the VM. The specified value of 100 means 100% of usage
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:
--hwvirtex :: indicates if hardware virtualization extensions are enabled for the VM. The specified value of on enables the feature
--paravirtprovider :: indicates the type of paravirtualization interface to present to the guest OS in the VM. The specified value of kvm means the interface type is KVM
--nestedpaging :: indicates the availability of the nested paging feature of the host processor to the hardware virtualization for the VM. This option is applicable only of the hardware vistualization option is enabled. The specified value of on enables the feature
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:
--vram :: specifies the amount of RAM (in MB) to use for the virtual graphics card in the VM. The specified value of 128 means 128 MB
--monitorcount :: specifies the number of monitor to use for the VM. The specified value of 1 means a single monitor
--graphicscontroller :: specifies the type of graphics controller to use for the VM. The specified value of vmsvga means emulate the VMWare SVGA device
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:
--audioout :: indicates the audio out behavior from the VM. The specified value of on means audio is enabled
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:
--nic1 :: specifies the type of networking used for the VM. The specified value of bridged means we are using a bridged network, which bypasses the host OS networking stack and uses the underlying hardware NIC directly
--bridgeadapter1 :: used only for bridged network and specifies the host network adapter (NIC) to use. The specified value of enp6s0 means use the default configured NIC from the host machine
--nictype1 :: specifies the type of network hardware to use for the VM. The specified value of 82540EM means emulate the Intel PRO/1000 MT device
--nicpromisc1 :: used only for bridged network and specifies the promiscuous network mode for the VM. The specified value of deny means promiscuous mode is disabled
--macaddress1 :: specifies how the MAC address for the VM is set. The specified value of auto means generate a random MAC address and use it
--cableconnected1 :: specifies the reset behavior of the network adapter for the VM. The specified value of on means allow resetting of the network device
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:
--usb :: specifies the availability of the USB interface in the VM. The specified value of on means USB is enabled
--usbohci :: specifies the use of USB 1.1 interface in the VM. The specified value of on means USB 1.1 is enabled
--usbehci :: specifies the use of USB 2.0 interface in the VM. The specified value of on means USB 2.0 is enabled
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:
--filename :: specifies the absolute path to the virtual disk file for the VM
--size :: specifies the size of the virtual disk (in MB) for the VM
--format :: specifies the virtual disk format to use for the VM. The specified value of VDI means use the virtual disk image (VDI) format
--variant :: specifies how the virtual disk space of the VM be managed. The specified value of fixed means the allocated virtual disk space is fixed
The following would be the typical output:
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:
--name :: specifies the name of the storage controller for the VM
--add :: specifies the type of the storage controller being added to the VM. The specified value of ide means an IDE storage controller
--controller :: specifies the controller chipset to emulate for the VM. The specified value of PIIX4 means use a PCI IDE ISA Xcelerator intel chipset
--hostiocache :: specifies the use of the host I/O cache for the VM. The specified value of on means host caching is enabled
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:
--name :: specifies the name of the storage controller for the VM
--add :: specifies the type of the storage controller being added to the VM. The specified value of sata means a SATA storage controller
--controller :: specifies the controller chipset to emulate for the VM. The specified value of IntelAhci means use a Serial ATA ACHI chipset
--portcount :: specifies the number of ports on the storage controller for the VM. The specified value of 1 means a single port
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:
--port :: specifies the port number on the storage controller of the VM where this storage must be attached
--device :: specifies the device number on the storage controller of the VM where this storage must be attached
--type :: specifies the type of the storage being attached to the VM. The specified value of hdd means it is a hard disk
--medium :: specifies the absolute path to the virtual disk image we created above to be attached as the storage for the VM
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:
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% Machine has been successfully cloned as 'test-vm-2'
References