Turning host OS off automatically when guest OS turned off/closed

Discussions related to using VirtualBox on Linux hosts.
Post Reply
noname65535
Posts: 2
Joined: 18. Oct 2016, 10:20

Turning host OS off automatically when guest OS turned off/closed

Post by noname65535 »

Hello, everyone!

I've got kind of a rare question (at least I could't find anything similar in google):
How to turn off Host as soon as Guest was turned off?

A little bit of description: I am using Ubuntu 16.04 as host OS, Windows 7 Pro as guest OS, and VirtualBox 5.024 as virtualization software. Guest OS is launched automatically on startup, in fullscreen mode. But I could not find a way to send a "shutdown -P now" to host OS when guest OS was just turned off or closed.

At first, I tried to launch a virtual machine with a command, merged with a shutdown command by using ";", "&&", or "||", like this:
virtualbox /home/username/VirtualBoxVMs/myVM/myVM.vbox && shutdown -P now
Because if you type in terminal, for example, "vitualbox && gedit", gedit will be launched exactly after closing the virtualbox window, neither earlier, nor later.
But when I try this approach with virtual machine, shutdown command is either not executed, or executed together with virtual machine.

I understand that I can go a long way and write a program that will hang in the background waiting for a moment when virtual machine window is closed, then sending a shutdown command. I just want to know, is there any easier way to reach my goal?
noname65535
Posts: 2
Joined: 18. Oct 2016, 10:20

Re: Turning host OS off automatically when guest OS turned off/closed

Post by noname65535 »

Ok, I found the solution. I don't really think that anyone else will ever need this, but anyway.
So, the first thing is the way to launch virtual machine. In case of using "virtualbox /home/username/VirtualBosVMs/myVM/myVM.vbox && shutdown -P now", then virtual machine does not depend from the terminal, so shutdown command will be executed simultaneously with starting virtual machine. But if a "startvm" parameter is used, like this: "virtualbox --startvm myVM && shutdown -P now" then shutdown command will wait for closing of virtual machine window. This solution works fine when launched manually from terminal or shell script, but as soon as shellscript with this command is added to "Startup Applications" in Ubuntu, things go wrong. During startup instead of launching the VM, error message will be displayed: "Kernel driver not installed", suggesting to reinstall kernel module. This makes no sense, because if VM is launched manually, it works. Obviously, problem is that VM attemts to start before "vboxdrv" was loaded, so it would be logical to add into shell script a command to load "vboxdrv" before starting the VM: "modprobe vboxdrv". For some reason this also does not work, but if this command is executed with terminal, result is better: "gnome-terminal -x modprobe vboxdrv". Now VM is started with a host OS, and as soon as VM is turned of, host pc is also turned off. But there is one problem: sometimes you can still get an error. The solution is to write command for loading vboxdrv ten times. Now works.
Post Reply