我可以强制 systemd 中的服务顺序吗?

我可以强制 systemd 中的服务顺序吗?

我正在开发一个通过 PXE 启动的无盘系统,其只读根目录通过 NFS 挂载。我已经设置好了它,因此它会在启动期间配置其主机名,并在稍后挂载和配置一些系统目录。这是配置主机名的服务:

root@node080027eb24b8:~# cat /lib/systemd/system/sethostname.service
[Unit]
Wants=network-pre.target
Before=network-pre.target
After=local-fs.target
DefaultDependencies=false

[Service]
Type=oneshot
ExecStart=/usr/bin/sethostname
RemainAfterExit=yes

[Install]
WantedBy=network.target

和:

root@node080027eb24b8:~# ll /etc/systemd/system/network.target.wants
total 0
lrwxrwxrwx 1 root root 43 Feb 19 11:39 sethostname.service -> /usr/lib/systemd/system/sethostname.service

可写入文件/home由 挂载/etc/fstab。在稍后阶段,我通过创建amd并使用将rsp绑定到这些目录来提供可写入文件/var和目录。负责此操作的服务是:/tmp/home/var/[hostname]/home/tmp/[hostname]mount -o bind/var/tmpprepsysdirs

cat /lib/systemd/system/prepsysdirs.service
[Unit]
Description=Script to run after fstab mount for /home/
Requires=home.mount
After=home.mount
RequiresMountsFor=/home
Before=munge.service slurmd.service
 
[Service]
ExecStart=/usr/bin/prepsysdirs
 
[Install]
WantedBy=munge.service

和:

root@node080027eb24b8:~# ll /etc/systemd/system/munge.service.wants
total 0
lrwxrwxrwx 1 root root 39 Feb 26 15:16 prepsysdirs.service -> /lib/systemd/system/prepsysdirs.service

到目前为止,这一切都正常。但是,看起来munge.service之前启动的程序prepsysdirs.service已经完成了它的工作:

root@node080027eb24b8:~# systemctl status munge
● munge.service - MUNGE authentication service
     Loaded: loaded (/lib/systemd/system/munge.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Mon 2024-02-26 16:15:30 UTC; 30s ago
       Docs: man:munged(8)
    Process: 375 ExecStart=/usr/sbin/munged $OPTIONS (code=exited, status=1/FAILURE)
        CPU: 4ms

Feb 26 16:15:30 node080027eb24b8 systemd[1]: Starting MUNGE authentication service...
Feb 26 16:15:30 node080027eb24b8 munged[381]: munged: Error: Failed to check logfile "/var/log/munge/munged.log": Permission denied
Feb 26 16:15:30 node080027eb24b8 systemd[1]: munge.service: Control process exited, code=exited, status=1/FAILURE
Feb 26 16:15:30 node080027eb24b8 systemd[1]: munge.service: Failed with result 'exit-code'.
Feb 26 16:15:30 node080027eb24b8 systemd[1]: Failed to start MUNGE authentication service.

如果我尝试登录后手动启动它,则不会出现此问题。

所以,问题是:我如何确保在运行munge之前不会启动?prepsysdirs

答案1

事实证明,问题在于Type=...缺少cat /lib/systemd/system/prepsysdirs.service

cat /lib/systemd/system/prepsysdirs.service
[Unit]
Description=Script to run after fstab mount for /home/
Requires=home.mount
After=home.mount
RequiresMountsFor=/home
Before=munge.service slurmd.service
 
[Service]
Type=oneshot
ExecStart=/usr/bin/prepsysdirs
 
[Install]
WantedBy=munge.service

添加该行之后,它就起作用了!

相关内容