在启动时使用 systemd 挂载网络驱动器

在启动时使用 systemd 挂载网络驱动器

我正在尝试使用由 systemd-service 调用的脚本在启动时挂载本地 NAS。昨天我在 Debian 11-Install 上做了同样的事情,它成功了。今天在Arch上遇到了执行脚本时网络不可达的问题。这是服务:

[Unit]
Description=mount FRITZ.NAS network share
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/etc/systemd/system/connect_fritz_nas

[Install]
WantedBy=multi-user.target

这是执行脚本:

#!/bin/bash

### connecting to fritz nas
### cannot copy without "noserverino"-option

sudo mount -t cifs -o credentials=/home/user/.smbcredentials,vers=2.1,_netdev,noserverino,uid=1000,gid=1000 //192.168.178.1/FRITZ.NAS /home/user/networkshare

正如我在不同的帖子和文章中读到的那样(拱门维基,免费桌面维基),systemd-networkd.service需要启用,因为systemd-networkd-wait-online.service为了达到network-online.target.

systemctl status connect-fritz-nas.service尽管如此,我在重新启动系统后运行时收到此输出:

× connect-fritz-nas.service - mount FRITZ.NAS network share
     Loaded: loaded (/etc/systemd/system/connect-fritz-nas.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Tue 2022-01-04 09:24:08 CET; 17min ago
    Process: 513 ExecStart=/etc/systemd/system/connect_fritz_nas (code=exited, status=32)
   Main PID: 513 (code=exited, status=32)
        CPU: 19ms

Jan 04 09:24:08 wooden-arch-box systemd[1]: Starting mount FRITZ.NAS network share...
Jan 04 09:24:08 wooden-arch-box sudo[517]:     root : PWD=/ ; USER=root ; COMMAND=/usr/bin/mount -t cifs -o credentials=/home/hendrik/.smbcredentials,vers=2.1,_netdev,noserverino,uid=1000,gi>
Jan 04 09:24:08 wooden-arch-box sudo[517]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 04 09:24:08 wooden-arch-box connect_fritz_nas[532]: mount error(101): Network is unreachable
Jan 04 09:24:08 wooden-arch-box connect_fritz_nas[532]: Refer to the mount.cifs(8) manual page (e.g. man mount.cifs) and kernel log messages (dmesg)
Jan 04 09:24:08 wooden-arch-box sudo[517]: pam_unix(sudo:session): session closed for user root
Jan 04 09:24:08 wooden-arch-box systemd[1]: connect-fritz-nas.service: Main process exited, code=exited, status=32/n/a
Jan 04 09:24:08 wooden-arch-box systemd[1]: connect-fritz-nas.service: Failed with result 'exit-code'.
Jan 04 09:24:08 wooden-arch-box systemd[1]: Failed to start mount FRITZ.NAS network share.

告诉我网络无法访问。我知道我可以用作fstab替代方案,但在 fstab 上我遇到了同样的问题。我是否监督了什么或者我做错了什么?

答案1

我没有使用由 systemd 单元执行的挂载脚本,而是使用了 systemd.automount,您需要为其创建两个文件(挂载单元)。 Systemd.automount 使用自动挂载单元来引用挂载单元。两个文件需要具有相同的名称(扩展名除外)。一种叫做home-user-networkshare.automount

[Unit]
Description=Automount networkshare

[Automount]
Where=/home/user/networkshare

[Install]
WantedBy=multi-user.target

另一个叫做home-user-networkshare.mount

[Unit]
Description=mounting the fritz nas
Wants=network-online.target
After=network-online.target

[Mount]
What=//192.168.178.1/FRITZ.NAS
Where=/home/user/networshare
Type=cifs
Options=credentials=/home/user/.smbcredentials,vers=2.1,noserverino,uid=1000,gid=1000

[Install]
WantedBy=multi-user.target

然后我启用了systemctl enable home-user-networkshare.mountsystemctl enable home-user-networkshare.mount。下次重新启动后,将安装网络共享。欲了解更深入的信息,请挖掘手动的

相关内容