0. 前言
由于 transmission 与 samba 等服务在启动时不考虑启动顺序,可能会出现硬盘还未挂载但服务已经启动的状况,造成找不到路径等问题。
通过设置 systemd 的 After 项,可以实现在硬盘挂载后再启动相应的服务。
1. 列出由 systemd 生成的挂载服务
执行 systemctl list-units --type=mount
,列出所有与 mount 相关的服务:
$ systemctl list-units --type=mount
UNIT LOAD ACTIVE SUB DESCRIPTION
-.mount loaded active mounted Root Mount
dev-mqueue.mount loaded active mounted POSIX Message Queue File System
mnt-backup.mount loaded active mounted /mnt/backup
mnt-storage.mount loaded active mounted /mnt/storage
run-user-1000.mount loaded active mounted /run/user/1000
sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System
sys-kernel-config.mount loaded active mounted Kernel Configuration File System
sys-kernel-debug.mount loaded active mounted Kernel Debug File System
sys-kernel-tracing.mount loaded active mounted Kernel Trace File System
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
9 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
其中,以 mnt-*.mount
命名的服务,即为硬盘挂载服务
2. 找到相应的 systemd 配置文件的路径
以 transmission-daemon
为例,
执行 systemctl status transmission-daemon
命令:
$ systemctl status transmission-daemon
● transmission-daemon.service - Transmission BitTorrent Daemon
Loaded: loaded (/lib/systemd/system/transmission-daemon.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-01-24 08:37:49 UTC; 1h 26min ago
Main PID: 636 (transmission-da)
Status: "Uploading 0.11 KBps, Downloading 0.00 KBps."
Tasks: 4 (limit: 8904)
Memory: 2.4G
CGroup: /system.slice/transmission-daemon.service
└─636 /usr/bin/transmission-daemon -f --log-error
Warning: some journal files were not opened due to insufficient permissions.
可以看到,transmission 的 systemd 文件的路径为 /lib/systemd/system/transmission-daemon.service
3. 修改 systemd 配置
打开 systemd 配置文件,并填写 After 项:
sudo vim /lib/systemd/system/transmission-daemon.service
在 After=
后,添加查询到的硬盘挂载服务,如:
After=network.target mnt-storage.mount
保存文件并退出。
4. 保存 systemd 配置
执行 sudo systemctl daemon-reload
命令即可
$ sudo systemctl daemon-reload