>

VBoxManage is the command-line interface (CLI) to VirtualBox. With it, we can completely control VirtualBox from the command line of our host operating system. VBoxManage supports all the features that the graphical user interface gives you access to, but it supports a lot more than that. It exposes really all the features of the virtualization engine, even those that cannot (yet) be accessed from the GUI.

It is extremely helpful when we are manage the VirtualBox VMs from remotely but only have access to a terminal on a remote host machine.

Other reasons that we may need VBoxManage is:

  • Use a different user interface than the main GUI (for example, VBoxSDL or the VBoxHeadless server);
  • Control some of the more advanced and experimental configuration settings for a VM.

VBoxManage is provided as an executable ("C:\Program Files\Oracle\VirtualBox\VBoxManage.exe") in Windows, we can execute this program like any other utility in Windows Command Line. By default, it is not in PATH environment variable, so we should add it to PATH so that we can execute it directly.

VBoxManage syntax
$ VBoxManage [-options] subcommand target

Observing Existing Virtual Machines

Assume we already have VMs registered in VirtualBox (say, with the help of the GUI), the first thing you we most likely want to do with VBoxManage is look through the list of those VMs:

$ VBoxManage list vms
"Ubuntu 12.04" {774c5e04-a35b-49c5-a406-305f1a887295}
"Ubuntu 17.10" {0d41ebff-4c96-48ec-a65a-233b4e21c10e}
"CentOS 7" {ad697671-713f-4704-9c46-882dd931c254}

If the command outputs nothing, you either currently have no VMs registered with Oracle VM VirtualBox or you’re running VBoxManage as a user different from the user who created those VMs.

To view the VMs that are running at the moment, you might use the runningvms subcommand of VBoxManage list:

$ VBoxManage list runningvms
"Oracle Developer Days" {c465cc52-dc11-4153-82d8-b5718182c117}

The VBoxManage list command can be used with option -l, which instructs the list command to output a detailed list (truncated below to save space):

$ VBoxManage list -l running vms
Name:            Ubuntu_16.10
Groups:          /
Guest OS:        Ubuntu (64-bit)
UUID:            ad697671-713f-4704-9c46-882dd931c254
Config file:     C:\Users\shichunhui\VirtualBox VMs\Ubuntu_16.10\Ubuntu_16.10.vbox
Snapshot folder: C:\Users\shichunhui\VirtualBox VMs\Ubuntu_16.10\Snapshots
Log folder:      C:\Users\shichunhui\VirtualBox VMs\Ubuntu_16.10\Logs
Hardware UUID:   ad697671-713f-4704-9c46-882dd931c254
Memory size:     4096MB
Page Fusion:     off
VRAM size:       16MB
CPU exec cap:    100%
HPET:            off
Chipset:         piix3
Firmware:        BIOS
Number of CPUs:  3
PAE:             off
...

Creating a VM


Use VBoxManage to create a VM in VirtualBox not only free us from manage the GUI interface, but also provides us an automated way to simplify the process.

$ VBoxManage createvm --name "Ubuntu17.10" --ostype Ubuntu --register

The process of creating a VM is to build a VM definition in XML file. We can add the –register option to register the VM’s definition into the VirtualBox directly, or we can register it later with the help of the VBoxManage registervm command.

In fact, the name of the VM is the only mandatory parameter of the createvm command. The details can be specified later through the various parameters of the modifyvm command. Both modifyvm and createvm, however, support the ostype optional parameter that you can use to give VirtualBox an idea of what guest operating system will be installed into the VM.

To get a complete list of supported operating systems we can do:

$ VBoxManage list ostypes

Setting Up a VM’s Properties

Once the VM is created, we may want to inspect its current properties:

$ VBoxManage showvminfo Ubuntu17.10
Name:            Ubuntu_16.10
Groups:          /
Guest OS:        Ubuntu (64-bit)
UUID:            ad697671-713f-4704-9c46-882dd931c254
Config file:     C:\Users\juniway\VirtualBox VMs\Ubuntu17.10\Ubuntu17.10.vbox
Snapshot folder: C:\Users\juniway\VirtualBox VMs\Ubuntu17.10\Snapshots
Log folder:      C:\Users\juniway\VirtualBox VMs\Ubuntu17.10\Logs
Hardware UUID:   ad697671-713f-4704-9c46-882dd931c254
Memory size:     4096MB
Page Fusion:     off
VRAM size:       16MB
CPU exec cap:    100%
HPET:            off
Chipset:         piix3
Firmware:        BIOS
Number of CPUs:  3
PAE:             off
Long Mode:       on
Triple Fault Reset: off
APIC:            on
X2APIC:          on
CPUID Portability Level: 0
CPUID overrides: None
Boot menu mode:  message and menu
Boot Device (1): Floppy
Boot Device (2): DVD
Boot Device (3): HardDisk
Boot Device (4): Not Assigned
ACPI:            on
IOAPIC:          on
BIOS APIC mode:  APIC

Beside, we might need to modify its properties to meet our personal needs and to comply with the requirements of the guest OS that we are going to install in the VM.

$ VBoxManage modifyvm Ubuntu17.10 --cpus 2 --memory 2048 --vram 12 --acpi on --boot1 dvd --nic1 nat

When setting the amount of RAM and CPUs for a VM, we should take into account the requirements of the guest OS, make sure that the host will have enough memory left to operate properly after we start all the VMs that are supposed to work simultaneously. Don’t assign more virtual CPUs than the number of physical cores on the host computer.

Since VBoxManage showvminfo displays VM’s complete infomation (too much), we can use grep to filter the output:

$ VBoxManage showvminfo Ubuntu17.10 | grep "Memory size"
Memory size:     2048MB

Configuring a Virtual Network Adapter

Network setting is very important when we set up a VM, we can configure a virtual network card over a physical network interface of the host system to provide your VM with the ability to interact with the outside world.
By default, VirtualBox configures one virtual adapter for a VM, setting it to operate in the NAT networking mode, in most case, this will satisfiy our need, the former command with option --nic1 nat explicitly specifies the networking mode to be NAT.

If we want to the mode of that adapter, nic1, to another mode (say bridged, for bridged networking), we can do this:

$ VBoxManage modifyvm Ubuntu17.10 --nic1 bridged --bridgeadapter1 eth0

To list the available interfaces on the host system, we can use ipconfig in Windows.

Attaching Virtual Media to a VM

Like a real computer, VM needs a storage device, such as a hard disk, to boot from and for storing and retrieving system and user data. it’s time to create a virtual hard disk for the VM.

This is actually a three-step process.
As the first step, you create a new virtual hard disk image. With the following command, you create a disk image file in VirtualBox’s VDI format.

$ VBoxManage createhd --filename /path/to/hard_drive_image/Ubuntu17.10.vdi --size 100000 --variant Standard
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Disk image created. 
UUID: c69e22d2-1560-44cc-85f6-d81979c81990

Here we create a virtual hard disk image with the size of 10 GB, the option –variant Standard means we create a dynamically allocated image file, if we want a fixed-size image, we could use –variant Fixed. In whichever case, we’ll be able to expand the image capacity anytime later with the VBoxManage modifyhd command.

The next step is to add a storage controller to be used with that hard disk:

$ VBoxManage storagectl Ubuntu17.10 --name "SATA Controller" --add sata --bootable on

Note: When choosing Serial ATA (SATA) as the controller type, make sure the guest OS has device support for Advanced Host Controller Interface (AHCI), which is the standard interface for SATA controllers. Be warned that older operating systems, such as Windows XP, do not support AHCI. In that case, use an Integrated Drive Electronics (IDE) controller.

Finally, we need to attach the hard disk to the controller:

$ VBoxManage storageattach Ubuntu17.10 --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium /path/to/hard_drive_image/Ubuntu17.10.vdi

We can connect up to 30 virtual hard drives to a VM through a single SATA controller. In the case of an IDE controller, you’re limited to just 4 virtual storage devices, where one of them is normally the virtual CD/DVD drive.

Now a VM and the correspoding hardware device are set up completely, we are ready to install a Guest OS into the VM.

Install Guest OS in VM

Assume we have already have a ISO image file for the OS we want to install, we need to configure the VM to boot from the it, to do that we need to create a virtual CD/DVD drive and connect it to the installation media. Like virtual hard drives, virtual CD/DVD drives also require a storage controller. While this can be done using a SATA controller for many operating systems, the following example illustrates adding an IDE controller for the CD/DVD drive.

$ VBoxManage storagectl Ubuntu17.10 --name "IDE Controller" --add ide --controller PIIX4

Suppose we have a installation DVD disk. Insert the installation disk into the host’s CD/DVD drive and then issue the following command, connecting the VM’s virtual CD/DVD drive to the physical CD/DVD drive on the host computer:

$ VBoxManage storageattach Ubuntu17.10 --storagectl "IDE Controller" --port 0  --device 0 --type dvddrive --medium /full/path/to/unbuntu17.10_server.iso

We can use vboxmanage showviminfo command to inspect whether we have IDE or SATA controller installed correctly:

$ VBoxManage showvminfo Ubuntu17.10 | grep "IDE"
Storage Controller Name (0):            IDE
IDE (0, 0): D:\Program Files\VBoxGuestAdditions_5.0.17.iso (UUID: 94ae2d78-c3b3-4d7f-86f7-389311e8d9fa)

$ VBoxManage showvminfo Ubuntu17.10 | grep "SATA"
Storage Controller Name (1):            SATA
SATA (0, 0): D:\Program Files\VDIs\ub1710.vdi (UUID: 31482d54-a73e-42ed-84be-ddde20546d9c)

Now you can boot the VM and start the guest installation using the following command, which starts in GUI mode by default, taking you through a standard guest OS installer:

$ VBoxManage startvm Ubuntu17.10 

Things to do after we have successfully installed the Guest OS

Once we have completed the OS installation, we shut down the VM from within the guest, which causes VirtualBox to power off the VM was well.
The below command is equivalent to briefly pressing the power button on a physical computer.

$ VBoxManage controlvm Ubuntu17.10 acpipowerbutton

We can forcibly shut down a VM using the following command, which is equivalent to pressing and holding a computer’s power button:

$ VBoxManage controlvm Ubuntu17.10 poweroff

Finally, since the OS is already installed in the guest, we can remove the DVD from the VM configuration:

$ VBoxManage storageattach Ubuntu17.10 --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium none
$ VBoxManage modifyvm "Ubuntu17.10" --dvd none

Guest Additions and Shared Folders

(1) Mount Guest Addition

$ VBoxManage storageattach "Ubuntu17.10" --storagectl "IDE Controller" --port 1 --device 0 --type hdd --medium /path/to/VBoxGuestAdditions.iso

(2) Adding/removing shared folders

$ vboxmanage sharedfolder add "Ubuntu17.10" --name shared_dir --hostpath /path/to/folder/ --automount
$ vboxmanage sharedfolder remove "io" --name share_dir

Export and Import VM

Oracle offers a number of prebuilt developer VMs for Oracle VM VirtualBox, providing a quick way to install and experience entire software stacks packaged into deployable appliances. The VBoxManage import command allows you to import such appliances packaged in OVA format into Oracle VM VirtualBox.
Also, if we want clone/package our local VM onto other Host Computer (System), we can export the VM out to a VirtualBox .ovf file and then import this file on other Host Computer.

Now, suppose we want to import Oracle Linux 7.2 into our VirtualBox from the OracleLinux72.ova package, which you have downloaded to your system from Oracle Linux Hands-on Labs. First, we might want to check the options available for the appliance to be imported. This can be done with the --dry-run option of the VBoxManage import command:

$ VBoxManage import /path/to/OracleLinux72.ova --dry-run 

Below command import an VM and pecify the amount of RAM and the number of CPUs to be allocated, also, use --eula accept option to accept the license agreement.

$ VBoxManage import /path/to/OracleLinux72.ova --vsys 0 --memory 1024 --cpus 2 --eula accept 

Once the import has successfully been completed, the Oracle Virtual Sysadmin Days VM icon should appear in the left pane of VirtualBox Manager. And, of course, you’ll be able to manipulate the VM from the command line using the VBoxManage commands.

Export has similar syntax, we can use --output to specify a file name

$ VBoxManage export "Ubuntu17.10" --output ub1710_clone.ovf

Reference
https://www.virtualbox.org/manual/ch08.html
https://community.oracle.com/docs/DOC-890092
http://nakkaya.com/2012/08/30/create-manage-virtualBox-vms-from-the-command-line/