Force VBox to shutdown/save state on system kill signal

Discussions related to using VirtualBox on Windows hosts.
Post Reply
wdolek
Posts: 29
Joined: 11. Apr 2008, 00:33

Force VBox to shutdown/save state on system kill signal

Post by wdolek »

I'm using virtualized machine as development box, apache server etc. Because I need it most of the time, I scripted auto start of virtual machine just after I log on system. This is working perfectly. Problem is, that I'm not able to do the same for shutdown even. When I want to restart or shutdown host machine, virtual box "hangs" with prompt what to do (restart, ACPI shutdown, save state).

---

I have found these htreads:

1) how to get rid of savestate confirmation window: viewtopic.php?f=6&t=41520&p=215563
2) Windows 7 Host - Restart/Shutdown: viewtopic.php?f=6&t=46908&p=211790

#1 is not working for me - I posted reply there, #2 without any response.

---

So I'm curious, is there any way:

1) how to script shutdown/save state of virtual machine - just after pressing "shutdown"/"restart" on host Start menu (before Windows hangs on "Some tasks are not responding" dialog) (...this is more Windows related, because it involves knowledge of how processes are started when/on which event - shutdown scripts in group policy are started, but not in time when user hits "shutdown" button in Start menu)

2) how to make VirtualBox smarter and set default action, when system kill signal is caught - set somewhere something like "when OS sends kill signal, save state and don't ask anything"
Tiddo
Posts: 9
Joined: 2. Mar 2011, 12:54
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: CentOS

Re: Force VBox to shutdown/save state on system kill signal

Post by Tiddo »

1. I don't know
2. You can use the VBoxManage.exe, located in your virtualbox installation folder. See http://www.kernelhardware.org/virtualbo ... adless-vm/ for the commands

Edit: Sorry, I didn't click your links, and I know see that this was exactly what the other thread suggested. Do you get an error message? Could you also try sending this command manually using the command line. I also noticed that the guest name is case sensitive, so that might be a problem too.
wdolek
Posts: 29
Joined: 11. Apr 2008, 00:33

Re: Force VBox to shutdown/save state on system kill signal

Post by wdolek »

There is no problem with "how to control virtualbox by script", problem is, how to invoke such script when user is ending his session in host system (#1). Another problem is, how to make VBox to handle kill signal from OS (not from user) (#2).

VBoxManage has to be run just after user clicks on "Shutdown"/"Restart" button on Windows Start menu (or confirms shutdown dialog). When user does this, every running process in Windows receives kill signal (which is telling application "Hey, System wants you to stop").
Problem is - when Virtualbox receives such signal, it just displays prompt, which requires user manual action - instead of doing "default" action. (I wrote example of such prefered behavior: System: "Hey VBox process, please, kill yourself", VBox: "Ok, I wont prompt user, but I will choose to save state and then commit suicide", User: "Oh, I don't have to click anything and System can continue in shutting down" *).

So, #1 is about finding place, when/on which event/how to run some script (**); point #2 is kind of Feature request - to have option to set default action when system sends kill signal to VBox (in case, there is no such thing yet).

I hope it is now more clear, what I want to achieve.

(* todays behavior: System: "VBox, please, kill yourself", VBox: "NO! I want user to click on MY dialog! STFU!", User: "Arrrgh, application is not responding again!")

(** adding script to Group Policy to Shutdown script is not working - these scripts are invoked after OS enters shutting down state - not just after "clicking on button, sending kill to processes and wait whether there will be any not stopped process")
Tiddo
Posts: 9
Joined: 2. Mar 2011, 12:54
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: CentOS

Re: Force VBox to shutdown/save state on system kill signal

Post by Tiddo »

Perhaps you could script that VBox will run in system (link). In that case you could create a script which runs the vbox shutdown script on logoff instead of shutdown. Perhaps that will work since it wouldn't surprise me if the logoff scripts are called before the shutdown procedure is started for the system processes.

Unfortunally I can't help you with the default action, but I'd love to see that feature as well!
wdolek
Posts: 29
Joined: 11. Apr 2008, 00:33

Re: Force VBox to shutdown/save state on system kill signal

Post by wdolek »

I'm afraid I don't understand much that `run as system` (service) thingy. It won't help me much, because I still need to setup it (service) somehow - and I'm not able to do it, because all events I know are fired in shutdown/log off process, not before. Or did I miss something?

Only one thing came to my mind: create dummy service (application which would do absolutely nothing) - let it start at log on, and when kill signal is received, run system command - "VBoxManage controlvm ..."... But I'm not sure, how is this handled, when application is running as Windows Service (and I'm far away from being Windows developer).
Tiddo
Posts: 9
Joined: 2. Mar 2011, 12:54
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: CentOS

Re: Force VBox to shutdown/save state on system kill signal

Post by Tiddo »

If you run VBox as a service (SYSTEM process), then VBox won't shut down on logoff, since the process belongs to SYSTEM instead of your user account. When you shutdown the pc, you're first logged off, and then Windows is shutdown. But if VBox is running as a service, then VBox isn't shut down when you're logged off, but when Windows is shutdown. So if you run your script on the log off event, then VBox hasn't received a kill signal from Windows, so in theory you could still kill the process.

In short, this is what happens on shutdown if I understand everything correctly (I'm not an expert, this just seems most likely if I read posts about this subject)

Click shutdown -> USER processes are killed -> logoff events are fired -> SYSTEM processes are killed -> shutdown events are fired -> computer is shut down

Usually VBox runs as a User process, so it will be killed before any event is fired. However, if you make it a system process, the logoff event is fired before the process is killed. (Again, this is what I deduce from observations of others, I haven't tested it yet). The link I posted in my previous comment showed how to launch a program as a service using a .BAT script.

At the moment I can't test this for you, it's 2 am here so I'm going to sleep;) If you can work this out, please let me know, it will be useful for me as well! Otherwise I will try to do this tomorrow.
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: Force VBox to shutdown/save state on system kill signal

Post by Sasquatch »

If you always want to be able to control the VM from it's console, or have the VM available only when you are logged on, then leave the startup as you had and change the Windows environment a bit. Use GPO to remove the shutdown button from the start menu or simply teach yourself not to use it. Then create a shortcut on your taskbar/quicklaunch or desktop which will invoke a script. This script will run through a list of all running VMs, send the save state signal to them and then log you off or shut down the system. Just run it and walk away, the system will shut down or log off once the VMs are in saved state.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
General Foch
Posts: 1
Joined: 26. Apr 2014, 21:40

Re: Force VBox to shutdown/save state on system kill signal

Post by General Foch »

I know this is an old thread but but its still and issue. when running a guest vm on a win 7 host and the win 7 host is told to shutdown the Vm still gives a popup requiring manual intervention and seems to block the shutdown for a while before windows chooses to kill it and then after boot all my VM's are in the "aborted" state.

I have tried adding a shutdown batch file to the log out policy and the shutdown policy but in both cases the bat script cant get executed before the vm throws up the "what do you want to do " popup rending the bat script moot.

I have not yet tried to run the vm as a service but I don't know if that would solve the issue for shutdown since all the services are sent the kill sig when shutdown is run.

I have looked into creating a VB shutdown button that would first stop the vm's and then inti the system shutdown but that will not help in the event windows installs something or the AV installs something and chooses to reboot. I think a more elegant solution would be to have a settable default action that would override the popup. It would not interfere with the shutdown but would allow the guests to be shutdown without manual intervention.

total vbox N00b,
GF
Post Reply