大网数据广告活动
服务器运维问答

Systemd - running custom VM shutdown script before the standard libvirt-guests.service

· 52CE 我爱测

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 ord…

问题原文

原帖链接 · LetMeSOThat4U · CC BY-SA 4.0

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?

已采纳解答

原帖链接 · Thomas · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-11-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。