[TIP] Virtualbox from the command line

coffee

Well-Known Member
Reaction score
1,832
Location
United States
I am posting this to help others that might find this information useful.

I am currently building a server for a client based on Centos7. This server will be running virtualbox and a guest operating system of windows7. One of the requirements it make sure the windows vm comes up on boot and can be administered via remote (either ssh or teamviewer). Therefore, I have put together some helpful things to share to others here that might be administering a linux server. When doing this make sure you have the user automatically login and if you use teamviewer have it set to unattended access.

Consider a vm named 'win-vm'

Show available vm's:
vboxmanager list vms

Show running vm's:
vboxmanager list runningvms

Start a vm:
vboxmanager startvm win-vm

Shutdown a vm:
vboxmanager controlvm win-vm acpipowerbutton

One thing that you might run into is that perhaps you su'ed to root do do some work and that when you go to work with a vm it does not show up. Infact, Running 'vboxmanager list vms' shows none at all. This is because virtualbox was installed under another user account. In our case a user account at install time was created with admin permissions called 'admin'. So, We need to be 'admin' to work with the vm's. This can be an issue when your writing a script to automate say a cloning of the vm for backup. The way around this is simple. Simply tell VB to run the command under the admin account.

su -c "vboxmanager list runningvms" admin

This will show the running vm's and can be also used from the command line if you wish. If your writing a script I always include a wait period between commands. For instance, It takes windows time to shutdown before you can backup the vm.

I use a script to start the vm and run it from the user .bash_profile file located in /home/$USER . You could run the startup command directly from the .bash_profile if you want however, If you have some other services starting from the bash_profile then it can get a bit sticky. I like to add a sleep statement to the boot script for VB so that the desktop has time to 'settle down' before you start the VM. I prefer auto login on the user and then starting VB as if you did it system wide then you could run into trouble if you have more than one person logging in (ouch!). Also, In your startup script for VB you should have a statement to check if its already running before it tries to start another instance again.

VBoxSVC is the process name you will see when VB is started. This bit of scripting will check to see if its running and just output "Virtualbox is running" if it still is. You can modify this to skip starting VB if its already running and add it to your .bash_profile . ;)

if pidof -s "VBoxSVC" > /dev/null; then
echo "Virtualbox is running"
fi


Here is a script that will shutdown the running vm, backup the vm to a mounted raid and then restart it.

#!/bin/bash
# Define some variables to make customization easier
OF="/mnt/raidset/vmbackups/myvmbackup.ova"
VM="win-vm"

# Power down VM
su -c "vboxmanager controlvm $VM apcipowerdown" admin
sleep 60
# Export the VM as an appliance
su -c "vboxmanager export $VM -o $OF" admin
sleep 10
# Start the VM again
su -c "vboxmanager startvm $VM" admin

Perhaps you would feel more comfortable storing a few appliances in case you need to fall back on a previous one. Just assign a date to the output name.

OF=/mnt/raidset/vmbackups/$(date+%d%m%y)winvm.ova

[Just dont let them build up ;) ]

Just adjust your sleep statements to what you feel comfortable with. I added the sleep statements because if you start a export before windows shuts down then its going to fail. You can build on this script to check if the vm has actually shutdown before doing the export and just cleanly exiting or just looping at some point in the script until it does shutdown. I have not tested that yet so I will not bother posting it.

I hope some here find this useful in some way. There are many options when working in the command line with VB.

Have a great day.

coffee
 
Back
Top