なにかの技術メモ置き場

なにかの技術メモ置き場

@インフラエンジニア

/etc/rc.d/rc.local@CentOS7の落とし穴 case2

以前とは別件の、rc.localで苦戦した話。
テーマは「systemdにおけるサービスの起動順序」。

事象

OS起動時に/etc/fstabに記載したディレクトリがマウントされていない。

補足
・マウント元のディレクトリを公開しているサーバは、自分自身。
・sambaにより公開しているため、ファイルシステムはcifs。

分析

・CentOS7では/etc/fstabに記載しただけでは自動マウントしないため、/etc/rc.d/rc.localに「/usr/bin/mount -a」と追記している。
・/etc/rc.d/rc.localに記載した他のコマンドは正常に実行されている。

原因

sambaサービスが起動完了する前にrc.localが実行されているため。
そのため、"/usr/bin/mount -a"は正常に実行されているが、マウント元が存在しないためマウントされていなかった。

解決方法

サービスの起動順序を定める。

まずは現状確認。

[prompt]# cat /lib/systemd/system/smb.service
[Unit]
Description=Samba SMB Daemon
After=syslog.target network.target nmb.service winbind.service

[Service]
Environment=KRB5CCNAME=/run/samba/krb5cc_samba
Type=notify
NotifyAccess=all
PIDFile=/run/smbd.pid
LimitNOFILE=16384
EnvironmentFile=-/etc/sysconfig/samba
ExecStart=/usr/sbin/smbd $SMBDOPTIONS
ExecReload=/usr/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[prompt]# cat /lib/systemd/system/rc-local.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.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99

「Unit」セクション内の「After」オプションに着目する。
ここには、このUnitより先に起動している必要があるUnitを記載する。例えばrc-localの場合、
 network起動 → rc-local起動
という起動順序となっている。

さて、今回やりたいことは、
 smb起動 → rc-local起動
なので、rc-local.serviceを編集する。

[prompt]# vi /lib/systemd/system/rc-local.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.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.d/rc.local is executable.
[Unit]
Description=/etc/rc.d/rc.local Compatibility
ConditionFileIsExecutable=/etc/rc.d/rc.local
After=network.target smb.service

[Service]
Type=forking
ExecStart=/etc/rc.d/rc.local start
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99

これで設定は終了。OSを再起動し、無事に自動マウントされていた。

補足

「After」とは逆に「Before」パラメータもある。