Ubuntu – How to shut down Ubuntu 11.10 host from within a Windows 7 VirtualBox guest

shutdownvirtualbox

I have installed virtual box 4.2 on a Ubuntu 11.10 host.

I have Windows 7 as the only guest OS.

I'm able to auto start Windows 7 virtual when Ubuntu boots by adding it in start application with the command "VBoxMange startvm Win7" in the command option.

Now when I shutdown Windows 7 , the virtualbox program is getting closed, but I go back to Ubuntu, when I want to have Ubuntu shut down as well when I click Shutdown in Windows 7 in Virtualbox.

This is an assignment for me. Can anybody please help me on this?

Also, when virtual box starts up automatically, Ubuntu's desktop should not be visible.

Best Answer

The way to do this is to write a script to start your VM, wait until it terminates, and then issue the shut-down command.

Create a folder bin in your home folder (if it does not exist already).

Now create a file with the following contents. Let's call it win7vmshutdown, and save it in your bin folder.

#!/bin/bash

VBoxManage startvm 'Win7'                           # Start the VM.

sleep 10s                                           # Give it time to start up. 

while pgrep VirtualBox >/dev/null                   # As long as the VM is running...
do
        sleep 5s                                    # Wait a bit before trying again.
done

gnome-session-quit --power-off                      # Issue the command to shut down.

Make the script executable; enter the following in a Terminal.

chmod +x ~/bin/win7vmshutdown

In your Startup Applications, remove the VBoxManage ... command and replace it with win7vmshutdown.

The shutdown command will prompt you for shutdown; if you don't answer, it will shut down in 60 seconds. This gives you time to interrupt the shutdown in case of unexpected problems.

Reboot your computer to test.

You said:

Also, when virtual box starts up automatically, Ubuntu's desktop should not be visible.

To do that, you want to run your VM in full-screen. Install the Guest Additions in your VM, reboot it, and run it full-screen.