1

On a KVM host I have a few VMs with legacy OSes that do not listen to ACPI shutdown event. As it happens on Debian 9.5 host, on shutdown the libvirt-guests.service waits for 5 minutes for each such VM and then destroys it.

In order to avoid that and shut them down cleanly, I have created custom VM shutdown service, with a script that uses special methods to shut them down:

% cat /etc/systemd/system/multi-user.target.wants/vm_stop.service 

[Unit]
Description=vm_shutdown
Before=libvirt-guests.service

[Service]
ExecStart=/bin/true
ExecStop=/usr/local/bin/vm_shutdown_all.sh

[Install]
WantedBy=multi-user.target

However, on shutdown the service appears to run after libvirt-guests.service in spite of the Before= settings in above service.

Now, I have tested that the custom service actually does run on shutdown - it touches a test file where I can verify it.

The problem: how do I ensure that it runs before libvirt-guests.service?

1

The Before= statement relates to the start up of the service. From man systemd.unit.

If a unit foo.service contains a setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up.
...
Note that when two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied. i.e. if a unit is configured with After= on another unit, the former is stopped before the latter if both are shut down.

So you want to configure your service with After=libvirt-guests.service as the service then is started after libvirt-guests.service is started and your service is stopped before libvirt-guests.service is stopped.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.