Why and How

I use https://www.virtualbox.org for all of my home computer virtualization needs. I have a number of virtual machine guests which spend most of their time off, and when I do power them on I have to spend some time updating them to bring them current with software and security updates. What I would like to do eventually is integrated these virtual machines with my regular https://www.saltstack.com/ based updates of my computers which are on-line most of the time.

Before I can fully automate things I need to figure out how to list all available virtual guest, identify whether they are currently on or off, power them on and shut them down from the command line. VirtualBox provides the VBoxMange for this purpose and this post is simply to put the commands all in one place where I can find them.

List Current Guest VMs

 $ VBoxManage list vms

Returns a list of VM names and UUIDs:

"Vista" {9e365a07-eae1-42f0-a574-716dd107ac09}
"vagrant-gentoo64" {56e77b0e-d3e3-4693-97f1-d2d94233d934}
"PXEBooter" {b79173c8-75b6-460a-bffd-21237338c96f}
"ArcaOS" {444c2999-96da-4099-bd1b-bb3f4b07a5d5}
"spyonmeplz" {85cda49b-c862-47c7-83ba-e0ee8de0ab14}
"Whonix-Gateway-CLI" {273fdecd-8e2f-45d9-b32b-5c0f8e82d939}
"Whonix-Workstation-CLI" {b7fe14cc-8037-41ff-88c7-4a180b5d89ed}
"Trisquel8" {92aea4db-5977-4fdb-83c0-f09a3c56f1d8}
"Whonix-Workstation-XFCE" {24017c4f-3827-4ace-a33f-58910d5912b7}
 $ VBoxManage list runningvms

Returns a list of powered on VMs and UUIDs:

"Vista" {9e365a07-eae1-42f0-a574-716dd107ac09}

Get Power State of VMs

VBoxManage list vms |cut -d" " -f1 |xargs -L 1 VBoxManage showvminfo --machinereadable|grep -e ^name -e ^VMState

Returns a listing of power states of the VMs.

name="Vista"
VMState="poweroff"
VMStateChangeTime="2019-02-17T20:35:01.388000000"
name="vagrant-gentoo64"
VMState="poweroff"
VMStateChangeTime="2018-10-21T00:43:14.000000000"
name="PXEBooter"
VMState="poweroff"
VMStateChangeTime="2019-02-02T18:28:29.000000000"
name="ArcaOS"
VMState="poweroff"
VMStateChangeTime="2019-02-02T18:48:49.000000000"
name="spyonmeplz"
VMState="poweroff"
VMStateChangeTime="2019-02-16T11:12:23.000000000"
name="Whonix-Gateway-CLI"
VMState="poweroff"
VMStateChangeTime="2019-02-16T16:31:24.000000000"
name="Whonix-Workstation-CLI"
VMState="poweroff"
VMStateChangeTime="2019-02-02T22:50:26.000000000"
name="Trisquel8"
VMState="poweroff"
VMStateChangeTime="2019-02-17T12:14:35.337000000"
name="Whonix-Workstation-XFCE"
VMState="poweroff"
VMStateChangeTime="2019-02-16T16:31:12.000000000"

Power On VMs

 $ VBoxManage startvm "Vista" --type separate

Resulting output:

Waiting for VM "Vista" to power on...
VM "Vista" has been successfully started.

Alternate –types for start up include headless and gui.

Pause VMs

 $ VBoxManage controlvm "Vista" pause

Resume VMs

 $ VBoxManage controlvm "Vista" resume --type gui

Power Off VMs

 $ VBoxManage controlvm "Vista" acpipowerbutton

In order for the above to work, the guest must be configured to respond to ACPI power button presses by shutting down gracefully.

VM metricts

To collect and display metrics for a given vm, specify period in seconds for the collection and number of last samples to retain. The following exampl callects all metrics for a vm every three seconds and reains the five most recent sample values.

 $ VBoxManage metrics collect -period 3 -samples 5 "Trisquel8"

The following example collects and displays only a subset of available metrics related to CPU usage and RAM.

 $ VBoxManage metrics collect -period 3 -samples 5 "Trisquel8" CPU/Load/User,RAM/Usage/Used
 ------------ ---------- -------------------- --------------------
 11:05:25.738 Trisquel8  CPU/Load/User        0.04%, 0.16%, 0.12%, 0.04%, 0.16%
 11:05:25.738 Trisquel8  RAM/Usage/Used       913716 kB, 913716 kB, 913716 kB, 913716 kB, 913716 kB
 ------------ ---------- -------------------- --------------------