pan*_*ita 7 shutdown networking debian unmounting systemd
我的关机需要很长时间(我使用的是 Debian 8.1),我发现它可以通过在关闭系统之前卸载网络驱动器来修复。显然,在卸载所有驱动器之前,网络已断开连接。
为了自动执行此操作,我尝试创建一个 systemd 服务,但它不起作用,即它似乎没有及时执行卸载并且关闭过程仍然需要很长时间。我的方法受到这些问题的答案以及对 systemd.service 手册页的一些浏览的启发...
[unit]
description=Unmount network drives on shutdown
Before=shutdown.target reboot.target halt.target network.target
[Service]
type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/sh umount /media/networkdrive1 /media/networkdrive2
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
编辑
网络驱动器通过以下几行安装在 /etc/fstab 中:
//192.168.1.5/networkdrive1 /media/nw1 cifs _netdev,uid=myuser,credentials=/home/myuser/.credfile
//192.168.1.5/networkdrive2 /media/nw2 cifs _netdev,uid=myuser,credentials=/home/myuser/.credfile
Run Code Online (Sandbox Code Playgroud)
经过 3 天的搜索和测试,我开发了一个可行的解决方案——这是针对 Debian-Jessie、Linux Mint、i386(32 位)安装的。我有网络 cifs 共享,如果没有先卸载它们,它会挂起关闭或重新启动 120 秒。使用此脚本,我不必在重新启动或关闭之前手动卸载共享。
自动 cifs 卸载脚本,在关机和重启时运行
创建以下脚本/etc/init.d/aaaumount (脚本按编号和 alpha 顺序执行,这就是aaa, 所以它首先运行):
#!/bin/sh
#
# aaaumount initscript
#
### BEGIN INIT INFO
# Provides: aaaumount
# Required-Start: $local_fs $remote_fs
# Required-Stop: $remote_fs
# Default-Start: S
# Default-Stop: 0 1 6
# Short-Description: umounts cifs shares
# Description: This script unmounts cifs shares
### END INIT INFO
case "$1" in
stop)
umount -t cifs -af
esac
Run Code Online (Sandbox Code Playgroud)使其可执行:
sudo chmod 755 /etc/init.d/aaaumount
Run Code Online (Sandbox Code Playgroud)在 /lib/systemd/system/ 中建立到服务的符号链接:
cd /lib/systemd/system/
sudo ln -s /dev/null aaaumount.service
Run Code Online (Sandbox Code Playgroud)在/etc/rc0.d和/etc/rc6.d (0并且6是关闭/重启运行级别) 中创建符号链接:
sudo ln -s /etc/init.d/aaaumount /etc/rc0.d/K01aaaumount
sudo ln -s /etc/init.d/aaaumount /etc/rc6.d/K01aaaumount
Run Code Online (Sandbox Code Playgroud)激活它:
sudo systemctl enable aaaumount.service
Run Code Online (Sandbox Code Playgroud)将_netdev挂载选项添加到 中的远程文件系统/etc/fstab。之后,这应该使您的网络安装;systemctl daemon-reload的依赖项 remote-fs.target用 来检查systemctl list-dependencies remote-fs.target。在网络关闭之前,此类文件系统将被卸载。