简体   繁体   English

在systemd中停止服务之前卸载filesystem

[英]filesystem are unmounted before the services are stopped in systemd

I am debugging systemd shutdown issue. 我正在调试systemd shutdown问题。 Here the problem is some of the filesystems are unmounted while the services are still running. 这里的问题是在服务仍在运行时卸载了一些文件系统。

In general, we want systemd to shutdown the services first and then umount the mount points. 通常,我们希望systemd先关闭服务然后卸载挂载点。

But here, umount and stopping the services are happening in parallel. 但在这里,umount和停止服务正在并行发生。 (See below). (见下文)。 Also unmounting of root filesystem at first. 首先还要卸载根文件系统。

#        Unmounting /root...
         Unmounting /var/lib/ntp...
         Unmounting /etc/cron.d/local...
[  OK  ] Stopped Apply Kernel Variables.
         Unmounting /proc/fs/nfsd...
         Unmounting /tmp/rshell/trace...
         Stopping Availability of block devices...
         Unmounting /etc/timestamp...
         Unmounting /var/lib/nfs/rpc_pipefs...
         Unmounting /etc/sysconfig/clock...
[  OK  ] Removed slice system-getty.slice.
[  OK  ] Stopped Load Kernel Modules.
         Unmounting /etc/ssh/ssh_external_host_rsa_key...
[  OK  ] Stopped Create Static Device Nodes in /dev.
         Unmounting /mnt/log...
[  OK  ] Stopped Resets System Activity Logs.
         Stopping Crond Periodic Command Scheduler...
[  OK  ] Stopped Mount Restricted SFTP Jail Folders.
[  OK  ] Stopped Mount Restricted Shell Folders.
         Stopping Runs processtat...
         Unmounting /etc/ssh/ssh_external_host_ecdsa_key.pub...
[  OK  ] Stopped target RPC Port Mapper.
         Unmounting /boot...
         Unmounting /srv...
         Stopping Initializes network console logging...
[  OK  ] Stopped Crond Periodic Command Scheduler.
[FAILED] Failed unmounting /root.
[  OK  ] Unmounted /var/lib/ntp.
[  OK  ] Unmounted /etc/cron.d/local.
**[FAILED] Failed unmounting /mnt/sysimg.**
[  OK  ] Unmounted /etc/sysconfig/clock.
[  OK  ] Unmounted /usr/share/cracklib.
**[FAILED] Failed unmounting /run/netns.**
[  OK  ] Unmounted /tftpboot/state.
**[FAILED] Failed unmounting /tmp/ldap.**

How do we synchronise this in systemd? 我们如何在systemd中同步这个?

In general, systemd-reboot.service depends on final.target, shutdown.target and umount.target. 通常,systemd-reboot.service依赖于final.target,shutdown.target和umount.target。

Here looks like, umount.target and shutdown.target are executed in parallel. 这看起来像是,umount.target和shutdown.target是并行执行的。

# cat /usr/lib/systemd/system/systemd-reboot.service 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Reboot
Documentation=man:systemd-halt.service(8)
DefaultDependencies=no
Requires=shutdown.target umount.target final.target
After=shutdown.target umount.target final.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl --force reboot

I tried, umount.target being dependent on shutdown.target but that did not help. 我试过,umount.target依赖于shutdown.target,但没有帮助。 Always these umount and service shutdown seems to be happening in parallel. 总是这些umount和服务关闭似乎并行发生。 If my understanding is wrong please correct. 如果我的理解有误,请更正。

Please give some tips/suggestions to properly shutdown the services first and then unmount the mount points. 请提供一些提示/建议,以便首先正确关闭服务,然后卸载挂载点。

In your service unit, try the following 在您的服务单元中,尝试以下操作

BindsTo=mymount.mount
After=mymount.mount

mymount.mount must already exist. mymount.mount必须已经存在。 In my case I let systemd generate /var/run/systemd/generator/mymount.mount based on /etc/fstab contents. 在我的例子中,我让systemd根据/etc/fstab内容生成/var/run/systemd/generator/mymount.mount

BindsTo= means the mount must be started for the service to start and the service must be stopped if the mount is stopped. BindsTo=表示必须启动挂载才能启动服务,并且如果挂载已停止,则必须停止服务。 But both units can still be started and stopped (almost) in parallel. 但两个单位仍然可以并行(几乎)并行启动和停止。 You need another constraint. 你需要另一个约束。 The mount must reach active state before the service can start. 在服务启动之前,安装必须达到活动状态。 You'll achieve this with After= . 你会用After=实现这个目标。 As with other systemd directives, what After= does during start is inverted during stop: the service will have to be fully stopped before the mount is stopped. 与其他systemd指令一样,启动期间After= do在停止期间被反转:服务必须在挂载停止之前完全停止。

The relevant documentation with emphasis added. 重点补充了相关文件。 The doc does not mention what happens during the stop but I confirmed it works as we expect.. 该文件没有提到停止期间发生的事情,但我确认它的工作正如我们所期望的那样......

BindsTo= BindsTo =

Configures requirement dependencies, very similar in style to Requires=. 配置需求依赖项, 风格与Requires =非常相似。 However, this dependency type is stronger: in addition to the effect of Requires= it declares that if the unit bound to is stopped, this unit will be stopped too . 但是,这种依赖类型更强:除了Requires =的效果之外,它声明如果绑定的单位被停止,该单位也将被停止 This means a unit bound to another unit that suddenly enters inactive state will be stopped too. 这意味着绑定到突然进入非活动状态的另一个单元的单元也将被停止。 Units can suddenly, unexpectedly enter inactive state for different reasons: the main process of a service unit might terminate on its own choice, the backing device of a device unit might be unplugged or the mount point of a mount unit might be unmounted without involvement of the system and service manager. 由于不同的原因,单元可能会突然意外地进入非活动状态:服务单元的主进程可能会根据自己的选择终止,可能会拔出设备单元的后备设备,或者可能卸载安装单元的安装点而不涉及系统和服务经理。

When used in conjunction with After= on the same unit the behaviour of BindsTo= is even stronger. 当与After =在同一单位上使用时,BindsTo =的行为甚至更强。 In this case, the unit bound to strictly has to be in active state for this unit to also be in active state. 在这种情况下,严格必须处于活动状态的单元也必须处于活动状态。 This not only means a unit bound to another unit that suddenly enters inactive state, but also one that is bound to another unit that gets skipped due to a failed condition check (such as ConditionPathExists=, ConditionPathIsSymbolicLink=, … — see below) will be stopped, should it be running. 这不仅意味着一个单元绑定到另一个突然进入非活动状态的单元,而且还绑定到另一个单元,该单元由于条件检查失败而被跳过(例如ConditionPathExists =,ConditionPathIsSymbolicLink =,...... - 见下文)将是它应该停止运行。 Hence, in many cases it is best to combine BindsTo= with After=. 因此,在许多情况下,最好将BindsTo =与After =结合使用。

When BindsTo=b.service is used on a.service, this dependency will show as BoundBy=a.service in property listing of b.service. 当在a.service上使用BindsTo = b.service时,此依赖关系将在b.service的属性列表中显示为BoundBy = a.service。 BoundBy= dependency cannot be specified directly. BoundBy =依赖项不能直接指定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM