How to automatically start and shut down VirtualBox machines?
Have you tried acpipowerbutton
from this command set?
VBoxManage controlvm <uuid>|<name>
pause|resume|reset|poweroff|savestate|
acpipowerbutton|acpisleepbutton|
Edit after reading the comments:
You can use acpid
or other acpi utilities to make it graceful. Also, can you provide more information about how do you shutdown the machine at the moment?
Plain shutdown
wouldn't wait for unfinished jobs, a time delay may be too long.
I assume you aren't using a window manager so try this tool.
Just seen this daemon. You might find it useful.
Rather than code this up yourself, consider using Vagrant, which is built to instantiate and control virtualbox instances. The documentation is excellent and I suggest that you check it out rather than attempting to roll your own.
The long and short of it is that you create a simple control file and then run vagrant up
to start as many VirtualBox instances as you want. You can use vagrant ssh
to log into the hosts and vagrant halt
to shut the host down (without terminating). vagrant destroy
will get rid of the instances.
It supports provisioning with puppet, Ansible or Chef and allows you to control most of the exposed VBox configuration settings.
I have similar application as you, with one difference: I need to restart system and recover from snapshot.
What you are interested in is headless-mode.
I have a few of such services so I use following script:
VBox_StopRestoreStart.sh
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 VMNAME_or_UUID"
exit 1
fi
set -x
VBoxManage controlvm "$1" poweroff #enforce turnoff
VBoxManage snapshot "$1" restorecurrent #retore state
VBoxManage showvminfo "$1" | grep State #display state to ensure
VBoxHeadless -s "$1" #run in headless mode in background
how can I gracefully shut down the VM?
IF you want to turn off VM gracefully, you have two options, depending on your application:
- Emulate "shut-down button" or "sleep button" and prepare VM to react on it (to close gracefully)
VBoxManage controlvm <uuid>|<VMname> acpipowerbutton
VBoxManage controlvm <uuid>|<VMname> acpisleepbutton
- Save VM state in order to restore afterwards
VBoxManage controlvm <uuid>|<VMname> savestate
TIPS: You might find useful:
VBoxManage list vms
- list of available vmsrdesktop IP-ADDR:3389
orrdesktop-vrdp IP-ADDR:3389
- when you would like a GUI (even remotely) when you run in headless mode :VBoxHeadless -s <uuid>|<VMname>
VBoxManage startvm
- start with GUI for local debugging
Related VirtualBox manual's chapter: Chapter 7. Remote virtual machines - Step by step: creating a virtual machine on a headless server
P.S. If you are interested in full featured already implemented solutions, OpenStack seems interesting choice.