paulgorman.org

Making systemd supervise qemu-guest-agent

I’m something of a systemd curmudgeon, though (non-unixy design aside) its reliability and documentation has improved. This is a case where systemd won by making something I expected to be annoying relatively painless.

Debian Stretch still ships qemu-guest-agent with a SysV init script. The agent sometimes dies, breaking VM backups. So, I went looking for a way to make systemd supervise it.

For anything systemd finds in /etc/init.d, its systemd-sysv-generator creates a unit file.

~ #  systemctl status qemu-guest-agent.service
● qemu-guest-agent.service - LSB: QEMU Guest Agent startup script
   Loaded: loaded (/etc/init.d/qemu-guest-agent; generated; vendor preset: enabled)
   Active: active (exited) since Fri 2019-04-12 10:02:44 EDT; 1 weeks 0 days ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0 (limit: 4915)
   Memory: 0B
      CPU: 0
   CGroup: /system.slice/qemu-guest-agent.service

~ $  cat /run/systemd/generator.late/qemu-guest-agent.service
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/qemu-guest-agent
Description=LSB: QEMU Guest Agent startup script
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
After=remote-fs.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/qemu-guest-agent start
ExecStop=/etc/init.d/qemu-guest-agent stop

So, the generated unit file defaults to not restarting a service, or at least not a forking one. How do we override this?

systemd.unit(5) says:

There are two methods of overriding vendor settings in unit files: copying the unit file from /lib/systemd/system to /etc/systemd/system and modifying the chosen settings. Alternatively, one can create a directory named unit.d/ within /etc/systemd/system and place a drop-in file name.conf there that only changes the specific settings one is interested in. Note that multiple such drop-in files are read if present.

[…]

Note that for drop-in files, if one wants to remove entries from a setting that is parsed as a list (and is not a dependency), such as ConditionPathExists= (or e.g. ExecStart= in service units), one needs to first clear the list before re-adding all entries except the one that is to be removed. See below for an example.

Does this work for unit files generated by systemd-sysv-generator? Yes!

~ #  mkdir /etc/systemd/system/qemu-guest-agent.service.d
~ #  vi /etc/systemd/system/qemu-guest-agent.service.d/local.conf
~ #  cat /etc/systemd/system/qemu-guest-agent.service.d/local.conf
[Service]
PIDFile=
PIDFile=/var/run/qemu-ga.pid
RemainAfterExit=
RemainAfterExit=no
Restart=
Restart=always
RestartSec=20

~ #  systemctl daemon-reload
~ #  systemctl stop qemu-guest-agent.service
~ #  systemctl start qemu-guest-agent.service
~ #  kill $(sudo cat /var/run/qemu-ga.pid) && sleep 60 && sudo systemctl status qemu-guest-agent.service
● qemu-guest-agent.service - LSB: QEMU Guest Agent startup script
   Loaded: loaded (/etc/init.d/qemu-guest-agent; generated; vendor preset: enabled)
  Drop-In: /etc/systemd/system/qemu-guest-agent.service.d
           └─local.conf
   Active: active (running) since Fri 2019-04-26 12:02:21 EDT; 39s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 364 ExecStop=/etc/init.d/qemu-guest-agent stop (code=exited, status=0/SUCCESS)
  Process: 392 ExecStart=/etc/init.d/qemu-guest-agent start (code=exited, status=0/SUCCESS)
 Main PID: 398 (qemu-ga)
    Tasks: 1 (limit: 4915)
   Memory: 484.0K
      CPU: 9ms
   CGroup: /system.slice/qemu-guest-agent.service
           └─398 /usr/sbin/qemu-ga --daemonize -m virtio-serial -p /dev/virtio-ports/org.qemu.guest_agent.0

Apr 26 12:02:21 box systemd[1]: qemu-guest-agent.service: Service hold-off time over, scheduling restart.
Apr 26 12:02:21 box systemd[1]: Stopped LSB: QEMU Guest Agent startup script.
Apr 26 12:02:21 box systemd[1]: Starting LSB: QEMU Guest Agent startup script...
Apr 26 12:02:21 box systemd[1]: qemu-guest-agent.service: PID file /var/run/qemu-ga.pid not readable (yet?) after start: No such file or directory
Apr 26 12:02:21 box systemd[1]: Started LSB: QEMU Guest Agent startup script.

#systemd #linux

⬅ Older Post Newer Post ➡