I'm running Windows 7 inside Virtualbox on Ubuntu 11.10. Everything works fine. I'm running it at startup, but I have a problem with rebooting.
When I type sudo reboot now
the state of the virtual Windows 7 isn't saved. After the reboot the virtualbox starts, but instead of the running Windows I get the Windows' 7 crash boot menu and the windows is booting again.
Is there an option that Ubuntu could send some signal to the virtual box to safely close the instance before the host reboot?
In case you really need to shutdown while a virtual machine in Virtual Box is running you could define your own script for a manual shutdown where you place a command to save the machine state before the shutdown process starts:
Alternatively you could also generate a script that always runs at shutdown.
If you use
sudo reboot
programs are given the kill signal ending them automatically without giving an application time to act on such situation. This is not a bug, it has always worked the same way and that is the expected behaviour.There is a similar question where you can see which commands are given when you press the
shutdown
,reboot
,suspend
, etc button on the user menu, such solution should ask you what to do when trying to close a window with a running application and its preferable (in your case) to thesudo shutdown
approach. Have a lookI would recommend a more sophisticated approach including an upstart job, a start and stop script. As example I am using Windows XP, as my home directory lets use tombert ... which you should change accordingly. It has the advantage of whatever you do (reboot, shutdown, pressing the power button) it handles your virtual machine nicely.
First the upstart job, put into /etc/init/winxpvm.conf:
The upstart job starts the virtual machine in runlevel 2 (which is in graphical mode), and in my case it increases the priority with
nice
. In order to nicely shutdown the virtual machine I need to "disable" the upstart termination using thekill signal SIGCONT
statement. This leaves the virtual machine running at first (avoiding the defaultSIGTERM
). After 120 seconds theSIGKILL
is send anyhow. Instead I am running thewinxpvm-stop.sh
script.Side-Note 1: The stanzas
start on started runlevel [2]
andstop on starting runlevel [!2]
do not work. One has to specifically mention the jobrc
.Side-Note 2: What is confusing also from the upstart manual: The
kill signal
stanza specifies the signal sent after 5 seconds. In this example I set it fromSIGTERM
(default) to SIGCONT - but the 5 seconds timeout I was unable to change. Thekill timeout
stanza specifies the timeout after which theSIGKILL
is sent - which signal one cannot change. An improvement therefore would be to define new stanzasterm signal
andterm timeout
.Here the start script winxpvm-start.sh:
Since all the settings etc. are done in user mode (as my login is tombert), even when run as root I change the account to tombert. The user of course could be changed in the upstart configuration but this solution leaves me the option to start/stop the virtual machine "by hand" from the console.
The more interesting is the shutdown script in winxpvm-stop.sh:
First I do the same as in the start script - I am changing the user from root to my account tombert. Now lets look at the function
dostop
. First I am checking if the virtual machine is even running. Then I am trying to "softly" shutdown by sending a shutdown directly to WinXP usingguestcontrol
. Here you must provide the credentials for the WinXP account, which in my case is tombert and a password. The Windowsshutdown
will gracefully close all applications and power off the operating system (normally). Then lets check the virtual machine state continuously usingshowvminfo
. Doing this at least 60 times with 1 second timeout (do whatever you think appropiate is here) should leave the virtual machine enough time to shutdown gracefully. Note that the call toshowvminfo
also takes a little bit less than a second (at least on my computer) so this gives it ~120 seconds in my case. If everything brakes we can forcefully shutdown using thepoweroff
statement.You also should see the
acpipowerbutton
, but unused. This is because it does not work reliable. If you are logged on to Windows, or even worse multiple users, Windows will show a confirmation shutdown dialog preventing the system from shutdown. This is also the reason why theacpibutton
in the/etc/default/virtualbox
will not work 100% reliable. Also thepoweroff
will forcefully shutdown the virtual machine - same as a long-press power button. Therefore it is best to set this to empty:Excerpt from /etc/default/virtualbox:
To make it perfect you might want to change the power button behaviour:
Excerpt from /etc/acpi/powerbtn.sh:
There is one little drawback left. When the virtual machine is still booting and the guest control service is not up (in the virtual machine) it will not receive the shutdown command. A rare case ... but think about it.
Thats it, hope it helps.
Follow this answer to change your system policy for rebooting
You can't streamline this into
reboot
. AFAIKinit.d
scripts will not work because it takes too much time, but you can run the command like this:where
<vm>
is the name of the Virtual MachineYou can send a shutdown request to the virtual machine with:
But if you do this in an init script, the script should not exit until the shutdown has completed. We may be able to detect that by polling the VM's drive file (.vdi) with
lsof
orfuser
in a loop. Or as a cheap workaround,sleep 20
may suffice.Here is what I am currently using in the close block of my init script:
Near the top of the file I defined:
This may not actually close the VirtualBox app itself, but it does wait for the VM to complete shutdown. Also it does not work if the virtual machine is still in the process of booting up (many operating systems ignore the power-off button during this phase), or if you are emulating an old system with no ACPI support.