VirtualBox – How to Automatically Start and Shut Down Virtual Machines

servicesvirtual machinevirtualbox

I need to run a software system that is intended to be installed as an appliance on a dedicated machine. In order to save energy, I plan to run the system on a VirtualBox VM instead.

The host is a standard Linux box with a SysV-Init system, the guest is a heavily modified Linux and I would prefer not to have to alter it further. VirtualBox is used in the OSE version.

I have already figured out how to start the VM when the host boots (Edit: this is done, as Nikhil mentioned below, through the command VBoxManager startvm), but how can I gracefully shut down the VM? Any script running on the host would need to wait until the guest has fully shut down.

Can anyone suggest how, for example, a service file doing this would have to look?

Best Answer

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.