You are not logged in.

#1 2020-12-29 16:07:40

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

systemd service ExecStop suddenly not executing...?

Hi, very happy with a systemd service that starts and stops (savestate) one/some VirtualBox vm's at logon/startup. But now, after playing around with a little change and reverting back to the always working script, somehow the ExecStop doesn't trigger anymore. In other words: my vm's get started at logon but when I shutdown they aren't nicely savedstated anymore (but aborted). The service seems to be running fine. Can't figure out what happened/changed? Perfomed all kinds of sudo systemctl daemon-reloads, re-enabling of the service files, ...??? Any help much appreciated, Zach!

The (for months working) contents of the service file in /etc/systemd/system/:

[Unit]
Description=VM autostart WinarchFamily
After=network.target vboxdrv.service
Before=runlevel2.target shutdown.target

[Service]
User=zach
Group=vboxusers
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
#KillMode=process
KillMode=control-group
GuessMainPID=no
RemainAfterExit=yes

ExecStart=/usr/bin/VBoxManage startvm WinarchFamily --type headless
ExecStop=/usr/bin/VBoxManage controlvm WinarchFamily savestate
ExecStop=/usr/bin/VBoxManage controlvm Winarch savestate

[Install]
WantedBy=multi-user.target

Offline

#2 2020-12-29 16:35:26

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,456

Re: systemd service ExecStop suddenly not executing...?

Please edit your first post and use code tags for the unit file.

That is not the documented way to run a VM with a service as described in the Wiki. The Wiki method ensures that each VM is a separate service instance managed by systemd. You are managing two different VMs in a single service and there's also no corresponding ExecStart for the 'Winarch' guest. Try using the documented method first.

Offline

#3 2020-12-29 17:01:51

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi twelveeighty,

Tried your advice, it works again for my "first" vm. What worked in the passed (not compliant to the Wiki advice I have to admit) was that when I also manually started the vm Winarch, it (when i forgot) nicely savestated that vm too (and later also others). Now I'm gonna create a separate service/unit file for every vm and follow the scripting advice of the Wiki. Thx! Regards, Zach

Offline

#4 2020-12-29 17:37:09

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, I've created a separate service/unit systemd file for all four vm's I frequently use. Still, now, only my first vm gets savedstate cleanly. The others are aborted (or powered off). Do I need to use TimeoutSec as an option or anything else in the additional service/unit files for them to close these vm's cleanly too? Thx again, regards, Zach

[Unit]
Description=WinarchFamily autostart and autosavestate
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
User=zach
Group=vboxusers
ExecStart=/usr/bin/VBoxManage startvm WinarchFamily --type headless
ExecStop=/usr/bin/VBoxManage controlvm WinarchFamily savestate
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Offline

#5 2020-12-30 07:12:24

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, got it working again by adding KillMode=none and TimeoutStopSec=40 (as stated in the wiki :-), sorry). Now when I check the status of one of the services I get a warning about KillMode=none (being deprecated) and that I should switch to a more safe KillMode. Any advice on this? How does this work? Regards, Zach

Offline

#6 2020-12-30 08:25:56

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,645

Re: systemd service ExecStop suddenly not executing...?

man systemd.kill wrote:

       KillMode=
           Specifies how processes of this unit shall be killed. One of control-group, mixed, process, none.

           If set to control-group, all remaining processes in the control group of this unit will be killed on unit stop (for services: after the stop
           command is executed, as configured with ExecStop=). If set to mixed, the SIGTERM signal (see below) is sent to the main process while the
           subsequent SIGKILL signal (see below) is sent to all remaining processes of the unit's control group. If set to process, only the main
           process itself is killed (not recommended!). If set to none, no process is killed (strongly recommended against!). In this case, only the
           stop command will be executed on unit stop, but no process will be killed otherwise. Processes remaining alive after stop are left in their
           control group and the control group continues to exist after stop unless empty.

           Note that it is not recommended to set KillMode= to process or even none, as this allows processes to escape the service manager's lifecycle
           and resource management, and to remain running even while their service is considered stopped and is assumed to not consume any resources.

           Processes will first be terminated via SIGTERM (unless the signal to send is changed via KillSignal= or RestartKillSignal=). Optionally, this
           is immediately followed by a SIGHUP (if enabled with SendSIGHUP=). If processes still remain after the main process of a unit has exited or
           the delay configured via the TimeoutStopSec= has passed, the termination request is repeated with the SIGKILL signal or the signal specified
           via FinalKillSignal= (unless this is disabled via the SendSIGKILL= option). See kill(2) for more information.

           Defaults to control-group.

Online

#7 2020-12-30 15:08:03

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,456

Re: systemd service ExecStop suddenly not executing...?

zachalexy wrote:

... that I should switch to a more safe KillMode.

First, time how long it takes to gracefully shut down the VM and save its state. That can take a while, especially if you have more than one VM being shut down and all have their states saved simultaneously. So also make sure you time it when shutting down all VMs at the same time and wait for all of them to gracefully stop. I'd say to be safe, double that and use that as the TimeoutStopSec= parameter. Then use the default 'control-group' for KillMode. If you configured it properly, the timeout would never be reached and if there's something hung on the VM, you would want systemd to clean up.

I would also not recommend using TimeoutSec, but both TimeoutStopSec and TimeoutStartSec individually since startup and shutdown could have very different durations, but that's just personal preference.

Offline

#8 2020-12-30 18:09:42

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, aha great info! Thx. I'm also diving in man systemd.service now...interesting. Regards, Zach

Offline

#9 2020-12-31 13:04:51

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, still struggling to get the second vm also nicely savedstated at shutdown. When I do a status check on the services I find that both vm's become part of one control group. As if both services are combined in one? Maybe this can explain why I can't get the second vm to close via the ExecStop (savestate)? Regards, Zach

CGroup: /system.slice/WinarchFamily_vm_autostart_savestate.service
             ├─876 /usr/lib/virtualbox/VBoxXPCOMIPCD
             ├─891 /usr/lib/virtualbox/VBoxSVC --auto-shutdown
             ├─915 /usr/lib/virtualbox/VBoxHeadless --comment WinarchFamily --startvm f0293c9f-087b-4dd>
             └─916 /usr/lib/virtualbox/VBoxHeadless --comment Winarch --startvm 3a80bb2c-35be-41b6-a548>

Offline

#10 2020-12-31 14:20:31

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,456

Re: systemd service ExecStop suddenly not executing...?

Please post your full current service file in [ code ] tags.

Offline

#11 2020-12-31 15:32:28

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, i have created 2 separate service files and enabled them both. One for vm WinarchFamily and one for vm Winarch. Only the first is correctly savedstated via the ExecStop command. Both start correct all the time. Also read this thread (dated), seems to hint in the direction I thought too: https://bbs.archlinux.org/viewtopic.php?id=162671...

[Unit]
Description=Winarch autostart and autosavestate
Requires=systemd-modules-load.service
After=systemd-modules-load.service

[Service]
User=zach
Group=vboxusers
TimeoutStopSec=60
RemainAfterExit=yes
 
ExecStart=/usr/bin/VBoxManage startvm Winarch --type headless
ExecStop=/usr/bin/VBoxManage controlvm Winarch savestate 

[Install]
WantedBy=multi-user.target

Regards, Zach

Last edited by zachalexy (2020-12-31 15:36:36)

Offline

#12 2020-12-31 17:09:32

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, if I exactly follow the Wiki with the not adviced KillMode=none, it works... Both vm's are being savedstated at shutdown and brought up again when I start/logon. Gonna test this for a few days...

[Unit]
Description=Winarch autostart and autosavestate 
Requires=systemd-modules-load.service
After=systemd-modules-load.service
 
[Service]
User=zach
Group=vboxusers
ExecStart=/usr/bin/VBoxManage startvm Winarch --type headless
ExecStop=/usr/bin/VBoxManage controlvm Winarch savestate 
RemainAfterExit=yes
KillMode=none
TimeoutStopSec=60

[Install]
WantedBy=multi-user.target

Offline

#13 2020-12-31 17:35:08

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, rebooted several times, perfect. Seems like KillMode=none is the magic parameter!? Joohoo... regards, Zach

Offline

#14 2021-01-03 14:44:19

zachalexy
Member
From: The Netherlands
Registered: 2020-01-08
Posts: 52

Re: systemd service ExecStop suddenly not executing...?

Hi, in terms of savestating running VM's without manual interaction at Shutdown...fine. Get a message at shutdown that /tmp can't be unmounted. No big issue I guess, has to do with the KillMode probably. Any tips on a different solution to savestate two or more VM's at shutdown more than welcome. Regards, Zach

Offline

Board footer

Powered by FluxBB