Systemd

在啟動時使用 systemd 掛載網路驅動器

  • February 28, 2022

我正在嘗試使用 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

正如我在不同的文章和文章(arch wikifreedesktop wiki)中閱讀的那樣,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 上我遇到了同樣的問題。我監督了什麼還是我做錯了什麼?

我沒有使用由 systemd-unit 執行的掛載腳本,而是使用了 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/share/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。下次重新啟動後,將安裝網路共享。有關更深入的資訊,請閱讀手冊

引用自:https://unix.stackexchange.com/questions/684937