Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Virtualbox has the ability to issue a command to a running vm:

vboxmanage controlvm NameOfRunningVM acpipowerbutton

However this command returns immediately which results in non-graceful shutdown for my situation.

The situation: I plan on using this in an /etc/init.d script. This would allow for graceful shutdown of all the running VMs. Currently when I issue the vboxmanage controlvm NameOfRunningVM acpipowerbutton command the shutdown gets cutoff because the command doesn't wait for the VM to shutdown.

I need a Bash script that takes as input the name of the Virtualbox machine and a timeout in seconds then waits for the VM to return to "poweroff" state or the timeout occurs?

I'm not sure what is the best way to go about doing this.


I was thinking of checking the state of the VM with the following command:

[user@machine ~]$ vboxmanage list runningvms
"VirtualMachineName" {65c93f1f-4508-4119-b07d-ce9e89b23b8e}

The bash script would maybe be polling for a list of running VMs. Once the machine name stops being listed, the VM would be considered finished.

share|improve this question
    
I'm not sure I understand. If the command returns immediately, what would the BASH script be waiting for? –  terdon Feb 7 '13 at 17:44
    
Could you ping the VM(s), and wait for that to fail before allowing the host to shut down? (You might want to allow a few seconds after ping failure for the VM to completely shut down.) Or is there some command specific to Virtualbox that would let you query the status of a VM? –  Scott Feb 7 '13 at 20:33
    
Polling is probably the best you can do. The problem is the command you're running is simulating pushing the power button on the machine. Different OSes handle this action differently. Ubuntu Desktop for example gives you a dialog asking you what you'd like to do. Others don't handle it at all so VirtualBox can't know if the system is actually shutting down. The other power down options (PowerOff or SaveState) are controlled completely by VirtualBox and will wait until the action is completed. –  heavyd Feb 11 '13 at 17:52
add comment

1 Answer 1

Using polling, it could be done like this:

#!/bin/bash
MACHINE=$1
echo "Waiting for machine $MACHINE to poweroff..."

until $(VBoxManage showvminfo --machinereadable $MACHINE | grep -q ^VMState=.poweroff.)
do
  sleep 1
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.