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.

My homeserver runs several other servers via VirtualBox in VM's.

In particular, I have a web server that for some reason, if it isn't shut down properly, doesn't autoselect on the grub menu, meaning it hangs until I manually reset it on the server.

How can I make sure that my VM's shutdown properly whenever my server shuts down?

Additional info: The "server" is actually an old laptop, and it is running Ubuntu Desktop 11.04 and isn't headless (also runs the media center).

share|improve this question
    
So your host is Ubuntu, but what is the guest VM with the web server? –  slhck Jul 29 '11 at 19:55
    
Also Ubuntu, Ubuntu Server 11.04 –  Malfist Jul 29 '11 at 20:00

2 Answers 2

up vote 1 down vote accepted

You can use the VBoxManage command to control your virtual machines from the command line. This allows you to create a script that can shutdown your vm before powering down.

You could run a very basic script to shut down the vm & host...

#!/bin/bash
# poff.sh - poweroff the vm, then the host...

VBoxManage controlvm VMNAME acpipowerbutton && sudo shutdown -P 1

Or, you could use the same commands as an alias added to your .bashrc file...

alias poff='VBoxManage controlvm VMNAME acpipowerbutton && sudo shutdown -P 1' # poweroff the vm, then the host...

These are very basic examples that can be built upon to fit your exact requirements. They may also need to be modified to work correctly with your OS configuration.

share|improve this answer
    
How can I make sure this runs and halts the shutdown until completion when the computer shuts down? –  Malfist Aug 1 '11 at 13:24
    
Right now, the shutdown command give the vm 1 minute to power down. You would need to test this with your setup to see if it needs adjustment. For more info, look at man shutdown for those options, and VBoxManage --help to see more options for controlvm. You can also add conditional logic to check the state of the vm process before calling shutdown. –  Joe Internet Aug 1 '11 at 19:47
    
Use VBoxManage showvminfo MyVM --machinereadable and look for the property VMState to become "poweroff" (or anything other than "running") –  RolKau Jan 5 '12 at 23:31

I did this using qemu running on Debian hosting an Windows Server 2003 VM. I enabled "Emergency Management Services" on the Win2k3 VM. This gives you the "Special Administration Console" on the serial port. qemu lets you connect a virtual serial port to a "telnet" port, so I gave it a localhost address. The SAC allows you to shutdown the system cleanly through issuing a command via the serial port, and since it's something like an "emergency back door" it tends to be very reliable regardless of what else might be going on in the VM.

I then created a brief expect script to "login" to the serial-based SAC and issue the shutdown command. This was wrapped in another script that would call this expect script and wait 2 minutes for the qemu process to die. If the process didn't die, then it would forcefully kill it. Setting Linux to call this script on shutdown ensured the VM always shutdown cleanly if possible.

So you might be able to adapt this method to your situation.

If you have a Linux VM then you really can just create a small script on your Ubuntu system that logs into the VM via ssh and calls a script that shuts down the system properly.

share|improve this answer

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.