Opensuse 12.2 / VB 4.2.6_OSE cant auto shutdown cleanly VMs on shutdown/reboot

Hello,

I am relatively experienced with Virtualbox on window$ but I am now working them on linux, the problem is that I cant figure out how to make the virtualbox VMs to shutdonw cleanly (gracefully) when I shutdown/reboot my linux server.

The thing is every time I reboot the VMs windows I have running tellme the havent shutdown properly (so its like the VMs never savestate or acpishutdown… they just poweroff like plugin the electricity cord out)

I read virtualbox main forums and opensuse virtualization forums looking for everything like “shutdown” but cant get with a solution or hint that helps my way with this.

I am testing vboxtool, I can autostart the VMs but on shutdown nothing changes. Also I read vbox manual pages CH9 about autostart, but too little info.

So, if someone can give me a hint or experiences about this… thank you very much!

S.

I could make work the vboxes service that gets installed by virtualbox opensuse default distro, so everytime I start the server it starts my VMs… but on shutdown, it do not shutdown gracefully, so its just like if you turn off the electricity, so vboxes service is not doing a savestate (even if I tell it to do so with /etc/defaults/virtualbox config file)

Any idea what would be the problem? because if /etc/init.d/vboxes service starts okay my vms, it “should” shutdown them the same way, I mean if my parameters are correct.

I have a user created called vbox for all my vms, from where I have running the virtual machine and also the /etc/init.d/vboxweb-service so phpvirtualbox web works fine.

Here I post my configuration files that “have” to be configured as I understood from virtualbox manual, some forum posts on virtualbox forum and some google (I could find anything just for my OpenSuse, but I think its is almost fine the config… just it doesnt shutdown gracefully and I dont know how to trace the problem)

quahog:/ # cat /etc/sysconfig/vbox
# Virtual box machines to autostart
# Example to start 2 machines
# VBOX_AUTOSTART = "MachineName1 MachineName2"
# VBOX_AUTOSTART=""
VBOX_AUTOSTART="machine1;vbox"



quahog:/ # ls -la /etc/vbox/
total 12
-rw-rw-r-- 1 root vboxusers  63 Jan 22 15:10 vbox.cfg
-rw-rw-r-- 1 vbox vboxusers   1 Jan 14 21:43 vbox.start
-rw-rw-r-- 1 root vboxusers 179 Jan 14 21:35 vboxauto.cfg

quahog:/ # cat /etc/vbox/vbox.cfg
#settings for vboxwebsrv
VBOXWEB_USER="vbox"
VBOX_USER="vbox"

quahog:/ # cat /etc/vbox/vboxauto.cfg
# Default policy is to deny starting a VM, the other option is "allow".


default_policy = deny


# El usuario vbox esta autorizado para autoiniciar VMs
vbox = {
    allow = true
}

quahog:/ # cat /etc/default/virtualbox
# /etc/default/virtualbox
#
# -------------------------------------------------------------------------------------------------
# In the "SHUTDOWN_USERS" list all users for which a check for runnings VMs should be done during
# shutdown of "vboxdrv" resp. the server:
# SHUTDOWN_USERS="foo bar"
#
# Set "SHUTDOWN" to one of "poweroff", "acpibutton" or "savestate" depending on which of the
# shutdown methods for running VMs are wanted:
# SHUTDOWN="poweroff"
# SHUTDOWN="acpibutton"
# SHUTDOWN="savestate"
# -------------------------------------------------------------------------------------------------
#
#SHUTDOWN_USERS="foo bar"
#SHUTDOWN="savestate"
SHUTDOWN_USERS="vbox"
SHUTDOWN="savestate"
VBOXAUTOSTART_DB="/etc/vbox"
VBOXAUTOSTART_CONFIG="/etc/vbox/vboxauto.cfg"


What is the result of the command:

chkconfig -l vboxes?

Get some more information, but running the vboxes script from /var/tmp, this prevents that systemd is involved:

cp /usr/sbin/rcvboxes /var/tmp/vboxes

Get info:

/var/tmp/vboxes status

At line 52 you can add the command: set -x, to obtain runtime information:

  • edit the file you just copied, e.g.: vi /var/tmp/vboxes
  • at line 52 add: set -x
    51 # considered a success.
    52 set -x
    53 VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage

Now run the script again:

/var/tmp/vboxes stop

Have a good look at the output and try to discover why your VM does not stop. Perhaps you can execute the commands that the script executes manually and see what happens.

Am guessing a bit because it looks like the man init has been completely replaced by the man systemd.

Still, I’d recommend you take a look at the contents and possibly add your service as a target in


/etc/init.d/.depend.boot
/etc/init.d/.depend.halt
/etc/init.d/.depend.stop

Also for your consideration, you created your service using the legacy systemVinit way, moving towards the future you might consider creating a unit service “the systemd way.”

Another possible approach might be to script you VM startup and shutdown, so even if you can’t find a way to invoke, it’s only a 1-click operation. I did that years ago before VMware implemented that feature, it also permitted me to control the order of startup and shutdown which can be critical for a full blown LDAP network.

HTH,
TSU

HTH,
TSU

Thanks Richard! here I answer your questions… (sorry I coulnd answer before but too much work at job:|)

Here it is:

chkconfig -l vboxes

Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.

vboxes                    0:off  1:off  2:off  3:on   4:off  5:on   6:off


Done…

Remember that I have an user I create called vbox where I run the machines, so, I think… the script should be run as this user?

Done…
I have vboxes service running since the server boot up, so… is it alright to run the /var/tmp/vboxes status?
What user should I use to test the /var/temp/vboxes status?
(also I have two machines now configured to autostartup with vboxes)

quahog:~ # cat /etc/sysconfig/vbox
# Virtual box machines to autostart
# Example to start 2 machines
# VBOX_AUTOSTART = "MachineName1 MachineName2"
# VBOX_AUTOSTART=""
VBOX_AUTOSTART="win2003;vbox winxpfirmas;vbox"

I tried to run it with both root and vbox user, here is the output:


**quahog:~ # /var/tmp/vboxes status**
redirecting to systemctl
+ VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage
+  ! -x /usr/lib/virtualbox/VBoxManage ]]
+ VBOXHeadLess_BIN=/usr/lib/virtualbox/VBoxHeadless
+  ! -x /usr/lib/virtualbox/VBoxHeadless ]]
++ basename /var/tmp/vboxes
+ PRG=vboxes
+ SERVICE='Virtualbox machines'
+ '' -r /etc/sysconfig/vbox ']'
+ . /etc/sysconfig/vbox
++ VBOX_AUTOSTART='win2003;vbox winxpfirmas;vbox'
+ case "$1" in
+ status
+ for VBOX in '$VBOX_AUTOSTART'
+ grep -q ';'
++ cut '-d;' -f2
+ VBOX_USER=vbox
+ grep -qw vbox
+ VBOX_USERS=' vbox'
+ for VBOX in '$VBOX_AUTOSTART'
+ grep -q ';'
++ cut '-d;' -f2
+ VBOX_USER=vbox
+ grep -qw vbox
+ N=1
+ for VBOX_USER in '$VBOX_USERS'
++ su vbox -c '/usr/lib/virtualbox/VBoxManage list --long runningvms'
++ sed -n 's/^Name::blank:]]\{7\} *//p'
+ VBOX_RUNNING='win2003
winxpfirmas'
+ for VBOX in '$VBOX_RUNNING'
+ VBOX_NAME$N]=win2003
+ VBOX_USER$N]=vbox
+ N=2
+ for VBOX in '$VBOX_RUNNING'
+ VBOX_NAME$N]=winxpfirmas
+ VBOX_USER$N]=vbox
+ N=3
+ VBOXES=2
+  2 -eq 0 ]]
+ N=1
+  1 -le 2 ]]
++ su vbox -c '/usr/lib/virtualbox/VBoxManage showvminfo win2003'
++ sed 's/\([0-9][0-9]\)\.[0-9]\{9\}/\1/;s/\([0-9][0-9]\)T\([0-9][0-9]\)/\1 \2/'
++ sed -n 's/State: *//p'
+ STATE='running (since 2013-01-30 21:06:55)'
+ printf ' %-56s %s' 'win2003 (user: vbox):' 'running (since 2013-01-30 21:06:55)'
 win2003 (user: vbox):                                    running (since 2013-01-30 21:06:55)+ rc_status -v
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ _rc_status_ret=0
+ local i
+ for i in '"$@"'
+ case "$i" in
+ local vrt=
+ local out=1
+ local opt=en
+ test -n ''
+ opt=e
+ case "$_rc_status" in
+ vrt='                                                              running'
+ echo -e ''                                                         running
                                                                     running
+ _rc_status=0
+ return 0
+ N=2
+  2 -le 2 ]]
++ su vbox -c '/usr/lib/virtualbox/VBoxManage showvminfo winxpfirmas'
++ sed 's/\([0-9][0-9]\)\.[0-9]\{9\}/\1/;s/\([0-9][0-9]\)T\([0-9][0-9]\)/\1 \2/'
++ sed -n 's/State: *//p'
+ STATE='running (since 2013-01-30 21:06:55)'
+ printf ' %-56s %s' 'winxpfirmas (user: vbox):' 'running (since 2013-01-30 21:06:55)'
 winxpfirmas (user: vbox):                                running (since 2013-01-30 21:06:55)+ rc_status -v
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ _rc_status_ret=0
+ local i
+ for i in '"$@"'
+ case "$i" in
+ local vrt=
+ local out=1
+ local opt=en
+ test -n ''
+ opt=e
+ case "$_rc_status" in
+ vrt='                                                              running'
+ echo -e ''                                                         running
                                                                     running
+ _rc_status=0
+ return 0
+ N=3
+  3 -le 2 ]]

(OK! I understand, if I run it as vbox… it will ask for root password somewhere in the middle of the vboxes script because it is supposed to be run as root, so I wont post the output of running as vbox user…)

OK, this is the output: (da#n this way it works wonderful )

quahog:~ # /var/tmp/vboxes stop
redirecting to systemctl
+ VBOXMGR_BIN=/usr/lib/virtualbox/VBoxManage
+  ! -x /usr/lib/virtualbox/VBoxManage ]]
+ VBOXHeadLess_BIN=/usr/lib/virtualbox/VBoxHeadless
+  ! -x /usr/lib/virtualbox/VBoxHeadless ]]
++ basename /var/tmp/vboxes
+ PRG=vboxes
+ SERVICE='Virtualbox machines'
+ '' -r /etc/sysconfig/vbox ']'
+ . /etc/sysconfig/vbox
++ VBOX_AUTOSTART='win2003;vbox winxpfirmas;vbox'
+ case "$1" in
+ stop
+ for VBOX in '$VBOX_AUTOSTART'
+ grep -q ';'
++ cut '-d;' -f2
+ VBOX_USER=vbox
+ grep -qw vbox
+ VBOX_USERS=' vbox'
+ for VBOX in '$VBOX_AUTOSTART'
+ grep -q ';'
++ cut '-d;' -f2
+ VBOX_USER=vbox
+ grep -qw vbox
+ N=1
+ for VBOX_USER in '$VBOX_USERS'
++ su vbox -c '/usr/lib/virtualbox/VBoxManage list --long runningvms'
++ sed -n 's/^Name::blank:]]\{7\} *//p'
+ VBOX_RUNNING='win2003
winxpfirmas'
+ for VBOX in '$VBOX_RUNNING'
+ VBOX_NAME$N]=win2003
+ VBOX_USER$N]=vbox
+ N=2
+ for VBOX in '$VBOX_RUNNING'
+ VBOX_NAME$N]=winxpfirmas
+ VBOX_USER$N]=vbox
+ N=3
+ VBOXES=2
+  2 -eq 0 ]]
+ echo -n 'Shutting down Virtualbox machines: '
Shutting down Virtualbox machines: + N=1
+  1 -le 2 ]]
+  1 -lt 2 ]]
+ echo -n 'win2003 (user: vbox), '
win2003 (user: vbox), + N=2
+  2 -le 2 ]]
+  2 -lt 2 ]]
+ echo 'winxpfirmas (user: vbox)'
winxpfirmas (user: vbox)
+ N=3
+  3 -le 2 ]]
+ N=1
+  1 -le 2 ]]
+ echo -n ' win2003: '
 win2003: + su vbox -c '/usr/lib/virtualbox/VBoxManage -q controlvm win2003 savestate'
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
+ RETVAL=0
+ echo -n ' Shutting down virtual machine: win2003 (user: vbox)'
 Shutting down virtual machine: win2003 (user: vbox)+  0 == 0 ]]
+ rc_status -v -r
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ test 0 -gt 7
+ _rc_status_ret=0
+ case "$_rc_todo" in
+ test 0 -eq 7
+ local i
+ for i in '"$@"'
+ case "$i" in
+ local vrt=
+ local out=1
+ local opt=en
+ test -n ''
+ opt=e
+ case "$_rc_status" in
+ vrt='                                                              done'
+ echo -e ''                                                         done
                                                                     done
+ _rc_status=0
+ for i in '"$@"'
+ case "$i" in
+ rc_reset
+ _rc_status=0
+ _rc_status_all=0
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ return 0
+ return 0
+ N=2
+  2 -le 2 ]]
+ echo -n ' winxpfirmas: '
 winxpfirmas: + su vbox -c '/usr/lib/virtualbox/VBoxManage -q controlvm winxpfirmas savestate'
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
+ RETVAL=0
+ echo -n ' Shutting down virtual machine: winxpfirmas (user: vbox)'
 Shutting down virtual machine: winxpfirmas (user: vbox)+  0 == 0 ]]
+ rc_status -v -r
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ test 0 -gt 7
+ _rc_status_ret=0
+ case "$_rc_todo" in
+ test 0 -eq 7
+ local i
+ for i in '"$@"'
+ case "$i" in
+ local vrt=
+ local out=1
+ local opt=en
+ test -n ''
+ opt=e
+ case "$_rc_status" in
+ vrt='                                                              done'
+ echo -e ''                                                         done
                                                                     done
+ _rc_status=0
+ for i in '"$@"'
+ case "$i" in
+ rc_reset
+ _rc_status=0
+ _rc_status_all=0
+ rc_check
+ _rc_status_ret=0
+ test 0 -eq 0
+ test 0 -eq 0
+ return 0
+ return 0
+ return 0
+ N=3
+  3 -le 2 ]]

Both virtual machines savestate fine now! but if I shutdonw or reboot the server, both machines wont savestate. could it be what **Tsu2 **says? (will try to undertand that :sarcastic:)

Thank you very much for the free help you are giving!

tsu2 wrote:

>
> Am guessing a bit because it looks like the man init has been completely
> replaced by the man systemd.
>
> Still, I’d recommend you take a look at the contents and possibly add
> your service as a target in
>
>
> Code:
> --------------------
>
> /etc/init.d/.depend.boot
> /etc/init.d/.depend.halt
> /etc/init.d/.depend.stop
>
> --------------------
>
>
> Also for your consideration, you created your service using the legacy
> systemVinit way, moving towards the future you might consider creating a
> unit service “the systemd way.”
>
> Another possible approach might be to script you VM startup and
> shutdown, so even if you can’t find a way to invoke, it’s only a 1-click
> operation. I did that years ago before VMware implemented that feature,
> it also permitted me to control the order of startup and shutdown which
> can be critical for a full blown LDAP network.
>

I frequently see a failure of the VBox VM to shutdown immediately after an
update - the VM just hangs. I usually give it about as long as it takes to
get and drink a fresh cup of coffee then use the power off option to whack
the session. I have yet to see any problem from doing that in this
particular situation and the problem has yet to repeat until the next
update. Not quite the same as you describe but it may suggest something for
your case.


Will Honea

Here are some thoughts…

  • How does copying a command to /tmp somehow prevent systemd from being involved? Does not make sense. The way systemd works is that it is the “master process, task and services manager” and it is only by itself instantiating systemv ini scripts that systemv continues to be supported. SystemV init scripts are usually not located in the /tmp folder, but I cannot see how that makes any difference, once you define and run any kind of service, systemd has to has some control over it.

One way to verify systemd can see your service is to run the following which should list all loaded (not necessarily running) services on your machine

 
# systemctl type=service
 

From there, once you know the full name of the service, you can always get the existing status of that service at any time by running the following


# systemctl status servicename.service
 

So,
Some reading I’ve done over the past few days about systemd may be relevant to you… The method you’ve chosen using systemV init scripts uses BASH scripts which are daemonized, but in so doing detaches the original script from the process, hence creating the “service” that continues to run. Besides detachment, apparently the instantated service can spawn other processes and instantiate other child services but in doing so the child services may/will detach from the parent and if re-instantiate may/will even acquire new names or use different PiDs
(See for reference Wunschkonzert, Ponyhof und Abenteuerspielplatz)

Systemd attempts to resolve this legacy problem by automatically placing all spawned processes within a cgroup so that no matter what, when and how the child processes are spawned they continue to be tracked and managed.

The result and possible relevance to your problem is that if you create your service as a systemd “service” unit if any of these issues affect you then systemd will be able to properly manage your machines and properly shut them down.

This blog entry in the same series describes how to convert init scripts to systemd units but IMO is far more complicated than what your situation, if you start from scratch you’ll likely find it’ll be much more easy
Wunschkonzert, Ponyhof und Abenteuerspielplatz

In fact, I would recommend you follow the same general structure I created running my VMware boxes years ago…

  • Create individual Services for each box instead of what I think you are doing which is to create one massive script that manages all your boxes at once. These scripts for each box should define the default paramerters of the VM instance, ie file locations, default config files or define the default parameters.
  • Then create a script or manually instantiable service that will execute against specified (maybe all) your machines’ services.This can be defined as automatically loading or disabled but can be loaded or simply a service that is not immediately accessible (non-running). With this master script you should be able to start/stop/restart all your VMs.

HTH,
TSU

addendum to the above:
The Blog series also has a section on “Killing Services” - ie different ways of terminating running services ungracefully.

Note that if you configure as Service Units, you ordinarily should not need to forcefully shutdown services, but again the idea is that if you really have to, you really should be terminating all related processes and there shouldn’t be any orphans left running.

Wunschkonzert, Ponyhof und Abenteuerspielplatz

TSU

Minor update to my last post,

As we approach the launch of 12.3, you might consider trialing deploying on it.

Have been reading about recent, new systemd features and one supports the system recognizing, identifying and managing VMs automatically.

I only know that the version of systemd in12.3 should have that capability, without actually testing on a machine I wouldn’t know if the features really are enabled and not just latent, whether existing sysV init methods need to be modified.

HTH,
TSU