我的笔记本电脑有一个 USB 以太网适配器。显然,网络直到启动过程的后期才会出现,比正常情况要晚。我的 NFS 共享未在启动时挂载。但是,一旦笔记本电脑启动,我就可以使用以下命令挂载所有共享:
mount -a -t nfs,nfs4
没有错误,此时一切都很好。但是,我希望共享在启动时自动安装。因此,这是不是NFS 配置问题。我相信这个问题与 systemd、启动过程以及 USB 以太网适配器可用性的时间有关。
这是我所知道的要检查的所有内容,包括日志的相关部分,显示网络在 NFS 共享放弃尝试挂载后出现。我包含了相关的配置文件,但它们没有从默认值更改(据我所知)。
Jul 31 21:22:32 host systemd[1]: Mounted /var/log.
...
Jul 31 21:22:33 host mount[898]: mount.nfs: Network is unreachable
...
Jul 31 21:22:34 host mount[880]: mount.nfs: Resource temporarily unavailable
Jul 31 21:22:34 host systemd[1]: share1.mount: Mount process exited, code=exited status=32
Jul 31 21:22:34 host systemd[1]: share1.mount: Failed with result 'exit-code'.
Jul 31 21:22:34 host autossh[896]: starting ssh (count 9)
Jul 31 21:22:34 host autossh[896]: ssh child pid is 1033
Jul 31 21:22:34 host systemd[1]: Failed to mount /share1.
...
Jul 31 21:22:37 host autossh[899]: ssh: connect to host 10.10.0.201 port 22: Network is unreachable
Jul 31 21:22:37 host autossh[899]: ssh exited with error status 255; restarting ssh
...
Jul 31 21:22:38 host NetworkManager[792]: <info> [1533086558.3237] manager: NetworkManager state is now CONNECTING
...
Jul 31 21:22:38 host NetworkManager[792]: <info> [1533086558.3487] manager: NetworkManager state is now CONNECTED_LOCAL
Jul 31 21:22:38 host NetworkManager[792]: <info> [1533086558.3492] manager: NetworkManager state is now CONNECTED_SITE
Jul 31 21:22:38 host NetworkManager[792]: <info> [1533086558.3493] policy: set 'Wired connection 1' (eth1) as default for IPv4 routing and DNS
Jul 31 21:22:38 host NetworkManager[792]: <info> [1533086558.3497] device (eth1): Activation: successful, device activated.
Jul 31 21:22:38 host nm-dispatcher[1017]: req:3 'up' [eth1]: new request (0 scripts)
# systemctl status NetworkManager.service
● NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/NetworkManager.service.d
└─NetworkManager-ovs.conf
Active: active (running) since Tue 2018-07-31 21:22:33 EDT; 42min ago
Docs: man:NetworkManager(8)
Main PID: 792 (NetworkManager)
Tasks: 3 (limit: 4915)
Memory: 33.4M
CGroup: /system.slice/NetworkManager.service
└─792 /usr/bin/NetworkManager --no-daemon
Jul 31 22:00:59 host NetworkManager[792]: <info> [1533088859.6259] dhcp4 (eth1): gateway 192.168.0.1
Jul 31 22:00:59 host NetworkManager[792]: <info> [1533088859.6334] dhcp4 (eth1): state changed bound -> bound
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7755] dhcp4 (eth1): address 192.168.0.237
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7756] dhcp4 (eth1): plen 24
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7756] dhcp4 (eth1): expires in 300 seconds
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7757] dhcp4 (eth1): nameserver '192.168.0.1'
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7758] dhcp4 (eth1): domain name 'oaks'
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7758] dhcp4 (eth1): hostname 'host'
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7759] dhcp4 (eth1): gateway 192.168.0.1
Jul 31 22:03:12 host NetworkManager[792]: <info> [1533088992.7769] dhcp4 (eth1): state changed bound -> bound
# systemctl status share1.mount
● share1.mount - /share1
Loaded: loaded (/etc/fstab; generated)
Active: failed (Result: exit-code) since Tue 2018-07-31 21:22:34 EDT; 25min ago
Where: /share1
What: server:/share1/
Docs: man:fstab(5)
man:systemd-fstab-generator(8)
Jul 31 21:22:33 host systemd[1]: Mounting /share1...
Jul 31 21:22:34 host mount[880]: mount.nfs: Resource temporarily unavailable
Jul 31 21:22:34 host systemd[1]: share1.mount: Mount process exited, code=exited status=32
Jul 31 21:22:34 host systemd[1]: share1.mount: Failed with result 'exit-code'.
Jul 31 21:22:34 host systemd[1]: Failed to mount /share1.
# cat /usr/lib/systemd/system/NetworkManager.service
[Unit]
Description=Network Manager
Documentation=man:NetworkManager(8)
Wants=network.target
After=network-pre.target dbus.service
Before=network.target
[Service]
Type=dbus
BusName=org.freedesktop.NetworkManager
ExecReload=/usr/bin/dbus-send --print-reply --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.Reload uint32:0
#ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/bin/NetworkManager --no-daemon
Restart=on-failure
# NM doesn't want systemd to kill its children for it
KillMode=process
CapabilityBoundingSet=CAP_NET_ADMIN CAP_DAC_OVERRIDE CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SETGID CAP_SETUID CAP_SYS_MODULE CAP_AUDIT_WRITE CAP_KILL CAP_SYS_CHROOT
# ibft settings plugin calls iscsiadm which needs CAP_SYS_ADMIN
#CapabilityBoundingSet=CAP_SYS_ADMIN
ProtectSystem=true
ProtectHome=read-only
[Install]
WantedBy=multi-user.target
Alias=dbus-org.freedesktop.NetworkManager.service
Also=NetworkManager-dispatcher.service
# We want to enable NetworkManager-wait-online.service whenever this service
# is enabled. NetworkManager-wait-online.service has
# WantedBy=network-online.target, so enabling it only has an effect if
# network-online.target itself is enabled or pulled in by some other unit.
Also=NetworkManager-wait-online.service
# cat /usr/lib/systemd/system/NetworkManager.service.d/NetworkManager-ovs.conf
[Unit]
After=openvswitch.service
# cat /etc/NetworkManager/NetworkManager.conf
# Configuration file for NetworkManager.
# See "man 5 NetworkManager.conf" for details.
# tree /etc/NetworkManager/conf.d/
/etc/NetworkManager/conf.d/
0 directories, 0 files
# cat /usr/lib/systemd/system/NetworkManager-wait-online.service
[Unit]
Description=Network Manager Wait Online
Documentation=man:nm-online(1)
Requires=NetworkManager.service
After=NetworkManager.service
Before=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/nm-online -s -q --timeout=30
RemainAfterExit=yes
[Install]
WantedBy=network-online.target
Arch Linux 4.17.11-arch1 #1 SMP PREEMPT Sun Jul 29 10:11:16 UTC 2018 x86_64 GNU/Linux
KDE
更新:以下是有关 fstab 的请求信息:
所有 nfs 挂载都有以下选项:
nfs _netdev,defaults,noatime,nodiratime,soft,retrans=6,timeo=20,retry=0,rsize=32768,wsize=32768,proto=tcp 0 0
我确实将 _netdev 与 systemd 一起使用。读完 Filipe Brandenburger 的评论后,我不确定这个选项在 systemd 下是无意义还是有害。
systemctl cat share1.mount
# /run/systemd/generator/share1.mount
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=remote-fs.target
[Mount]
Where=/share1
What=server:/share1/
Type=nfs
Options=_netdev,defaults,noatime,nodiratime,soft,retrans=6,timeo=20,retry=0,rsize=32768,wsize=32768,proto=tcp
# systemctl show -p Wants network-online.target
Wants=NetworkManager-wait-online.service
这是一个更新以及@sourcejedi 要求的信息
sudo systemctl list-dependencies share1.mount
share1.mount
● ├─-.mount
● ├─home.mount
● ├─system.slice
● └─network-online.target
sudo systemctl list-dependencies --after share1.mount
share1.mount
● ├─-.mount
● ├─home.mount
● ├─system.slice
● ├─systemd-journald.socket
● ├─network-online.target
● │ └─network.target
● │ ├─dhcpcd.service
● │ ├─NetworkManager.service
● │ ├─wpa_supplicant.service
● │ └─network-pre.target
● ├─network.target
● │ ├─dhcpcd.service
● │ ├─NetworkManager.service
● │ ├─wpa_supplicant.service
● │ └─network-pre.target
● └─remote-fs-pre.target
# sudo journalctl -b -u share1.mount -u network-online.target -u NetworkManager-wait-online -u NetworkManager --no-pager
-- Logs begin at Sun 2018-06-10 17:09:08 EDT, end at Sat 2018-12-22 19:07:16 EST. --
Dec 22 17:58:38 laptop1 systemd[1]: Starting Network Manager...
Dec 22 17:58:38 laptop1 NetworkManager[463]: <info> [1545519518.3825] NetworkManager (version 1.14.5dev+17+gba83251bb-1) is starting... (for the first time)
Dec 22 17:58:38 laptop1 NetworkManager[463]: <info> [1545519518.3826] Read config: /etc/NetworkManager/NetworkManager.conf (lib: 20-connectivity.conf)
Dec 22 17:58:38 laptop1 NetworkManager[463]: <info> [1545519518.3836] wifi-nl80211: (wlan0): using nl80211 for WiFi device control
Dec 22 17:58:38 laptop1 NetworkManager[463]: <info> [1545519518.3895] bus-manager: acquired D-Bus service "org.freedesktop.NetworkManager"
Dec 22 17:58:38 laptop1 systemd[1]: Started Network Manager.
Dec 22 17:58:38 laptop1 NetworkManager[463]: <info> [1545519518.3900] manager[0x55b8543f4060]: monitoring kernel firmware directory '/lib/firmware'.
Dec 22 17:58:38 laptop1 systemd[1]: Reached target Network is Online.
Dec 22 17:58:38 laptop1 systemd[1]: share1.mount: Directory /share1 to mount over is not empty, mounting anyway.
Dec 22 17:58:38 laptop1 systemd[1]: Mounting /share1...
Dec 22 17:58:39 laptop1 mount[546]: mount.nfs: Network is unreachable
Dec 22 17:58:39 laptop1 systemd[1]: share1.mount: Mount process exited, code=exited status=32
Dec 22 17:58:39 laptop1 systemd[1]: share1.mount: Failed with result 'exit-code'.
Dec 22 17:58:39 laptop1 systemd[1]: Failed to mount /share1.
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3695] hostname: hostname: using hostnamed
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3696] hostname: hostname changed from (none) to "laptop1"
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3698] dns-mgr[0x98059644079A]: init: dns=default, rc-manager=symlink
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3708] rfkill2: found WiFi radio killswitch (at /sys/devices/pci0000:00/0000:00:1c.1/0000:03:00.0/ieee80211/phy0/rfkill2) (driver iwlwifi)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3711] manager[0x55b8543f4060]: rfkill: WiFi hardware radio set disabled
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3711] manager[0x55b8543f4060]: rfkill: WWAN hardware radio set enabled
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3926] settings: Loaded settings plugin: NMSKeyfilePlugin (internal)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3933] settings: Loaded settings plugin: NMSIbftPlugin ("/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-settings-plugin-ibft.so")
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3958] keyfile: new connection /etc/NetworkManager/system-connections/USB Ethernet Adapter 1 (fc9310,"USB Ethernet Adapter 1")
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3975] keyfile: new connection /etc/NetworkManager/system-connections/walden2
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3983] keyfile: new connection /etc/NetworkManager/system-connections/NETGEAR4
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3991] keyfile: new connection /etc/NetworkManager/system-connections/Tls5
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.3999] keyfile: new connection /etc/NetworkManager/system-connections/JONES
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4062] manager: rfkill: WiFi disabled by radio killswitch; disabled by state file
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4063] manager: rfkill: WWAN enabled by radio killswitch; enabled by state file
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4064] manager: Networking is enabled by state file
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4066] dhcp-init: Using DHCP client 'internal'
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4129] Loaded device plugin: NMAtmManager (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-adsl.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4210] Loaded device plugin: NMBluezManager (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-bluetooth.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4232] Loaded device plugin: NMOvsFactory (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-ovs.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4404] Loaded device plugin: NMTeamFactory (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-team.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4420] Loaded device plugin: NMWifiFactory (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-wifi.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4427] Loaded device plugin: NMWwanFactory (/usr/lib/NetworkManager/1.14.5dev+17+gba83251bb-1/libnm-device-plugin-wwan.so)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4435] device (lo): carrier: link connected
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4439] manager: (lo): new Generic device (/org/freedesktop/NetworkManager/Devices/1)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4449] manager: (eth0): new Ethernet device (/org/freedesktop/NetworkManager/Devices/2)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4461] device (eth0): state change: unmanaged -> unavailable (reason 'managed', sys-iface-state: 'external')
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4481] manager: (eth1): new Ethernet device (/org/freedesktop/NetworkManager/Devices/3)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4491] device (eth1): state change: unmanaged -> unavailable (reason 'managed', sys-iface-state: 'external')
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4504] device (wlan0): driver supports Access Point (AP) mode
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4508] manager: (wlan0): new 802.11 WiFi device (/org/freedesktop/NetworkManager/Devices/4)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4513] device (wlan0): state change: unmanaged -> unavailable (reason 'managed', sys-iface-state: 'external')
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4516] device (wlan0): set-hw-addr: set MAC address to 28:ab:29:45:F9:2B (scanning)
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4586] bluez: use BlueZ version 5
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.4656] bluez5: NAP: added interface 77:ab:29:45:F9:2F
Dec 22 17:58:39 laptop1 NetworkManager[463]: <info> [1545519519.5063] supplicant: wpa_supplicant running
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0759] device (eth1): carrier: link connected
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0792] device (eth1): state change: unavailable -> disconnected (reason 'carrier-changed', sys-iface-state: 'managed')
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0798] policy: auto-activating connection 'USB Ethernet Adapter 1' (fc9310)
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0804] device (eth1): Activation: starting connection 'USB Ethernet Adapter 1' (fc9310)
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0806] device (eth1): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0810] manager: NetworkManager state is now CONNECTING
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0895] device (eth1): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0899] device (eth1): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:42 laptop1 NetworkManager[463]: <info> [1545519522.0902] dhcp4 (eth1): activation: beginning transaction (timeout in 45 seconds)
Dec 22 17:58:45 laptop1 NetworkManager[463]: <info> [1545519525.7908] device (eth1): carrier: link connected
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1025] dhcp4 (eth1): address 192.168.5.2
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1026] dhcp4 (eth1): plen 24
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1026] dhcp4 (eth1): expires in 108000 seconds
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1027] dhcp4 (eth1): nameserver '192.168.5.1'
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1027] dhcp4 (eth1): domain name 'wrkgrp'
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1027] dhcp4 (eth1): hostname 'usb_laptop1'
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1028] dhcp4 (eth1): gateway 192.168.5.1
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1040] dhcp4 (eth1): state changed unknown -> bound
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1075] device (eth1): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1108] device (eth1): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1117] device (eth1): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1134] manager: NetworkManager state is now CONNECTED_LOCAL
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1153] manager: NetworkManager state is now CONNECTED_SITE
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1155] policy: set 'USB Ethernet Adapter 1' (eth1) as default for IPv4 routing and DNS
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1161] device (eth1): Activation: successful, device activated.
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.1167] manager: startup complete
Dec 22 17:58:47 laptop1 NetworkManager[463]: <info> [1545519527.3936] manager: NetworkManager state is now CONNECTED_GLOBAL
(关于我对soft
mount 选项的有争议的使用,使用它的决定是在该选项出现多年问题之后做出的hard
。使用硬选项时,任何连接丢失都会冻结系统并强制硬重启。我正在运行 BTRFS我不想冒这个风险,soft
但是,随着soft
我调整重传、时间和重试选项,我不确定我是否对它们进行了优化。体验比使用该选项要好得多hard
。)
答案1
这对我来说适用于 systemd。automount
,延迟挂载requires
请求,直到网络联机。
我不确定nofail
.我使用它没有问题,但似乎不太正确。应该没有必要requires
……也许有人有想法。
10.0.0.110:/export/3T /mnt/3T nfs nofail,x-systemd.automount,x-systemd.requires=network-online.target,x-systemd.device-timeout=10 0 0
答案2
在 CentOS 7 上,网络文件系统挂载依赖于network-online.target
.一旦接口启动并设置了 IP,即可到达
。这假定第一个接口上的第一个 IP 足以解析主机名。然而,情况并非必须如此。network-online.target
我们可以编写systemd services
测试网络是否应该被标记为“向上” 并使network-online.target
依赖的在该服务上。
对于模板,请查看: https://gitlab.com/ggeurts/extend-network-online.target
答案3
我遇到了类似的问题,特别是 NFS 挂载对 network-online.target 的依赖。
这是特别有问题的,因为我的 Ubuntu 桌面 network-online.target 由 NetworkManager 控制,这将表明甚至在通过 DHCP 安装任何 DNS 或默认路由之前就已经达到了 network-online.target。这里的问题是,如果 IP 层没有到远程目标的默认路由,则从通过 fstab 创建的生成器单元或手动制作的 .mount 单元发出的任何 mount.nfs 命令都将失败,并显示“网络无法访问”。
我想要一个能够无延迟地可靠启动的系统,无论 NFS 是否已成功安装,都能将我带到桌面。此外,我希望当网络可用或 NFS 服务器突然出现在网络上时,NFS 能够自动建立挂载。
实现这个目标比我预想的要困难,但我会分享我的经验,因为其他人可能会发现它有用。
我已从 fstab 中删除了除“/”之外的所有安装定义。我有一种感觉,fstab 正在下降,最终我们将看到所有挂载操作都通过 systemd 简单地执行。尽管生成器确实使 fstab 的使用变得“简单”。因此,我将所有 NFS 挂载移动到我自己创建的挂载单元中。我还发现 Nautilus 或 Nemo,选择你的毒药,不会在左侧“设备”窗格中显示 NFS 挂载,除非它们安装在用户主目录中或安装在 /media 中。当我最终让系统正常运行时,我希望使用 GUI 工具来管理文件。为此,我使用了将单个 NFS 挂载到服务器上,然后使用一组挂载单元的组合,这些挂载单元 --bind 将服务器中的每个导出挂载到我在台式机上想要的位置。当在底层 NFS 挂载点上创建绑定挂载时,systemd 实际上会在幕后创建 NFS 挂载。因此,我只有一个由系统启动的 NFS 挂载,它将 NFS 服务器的根目录附加到 /mnt。
为了克服启动时的依赖性,并在网络或服务器不可用时阻止任何 NFS 挂载超时,我从 mnt-NFS-mx.service 单元启动 NFS 挂载操作。
[Unit]
Description=Attempting to mount server 'mx' using NFS
After=network.target
[Service]
Type=oneshot
# Add a high metric default to 'lo' interface into route table.
# This prevents the mount command from failing with "Network unreachable" is case no external phy link is available.
ExecStart=-/bin/ip route add default dev lo metric 4294967295
ExecStart=/bin/mount 10.0.0.10:/mnt/ /mnt/NFS/mx -t nfs -o nolock,nfsvers=4,fg,retry=10000,hard,timeo=20,retrans=1
# Remove the default once the mount command is successful
ExecStop=-/bin/ip route del default dev lo metric 4294967295
ExecStopPost=-/bin/ip route del default dev lo metric 4294967295
[Install]
WantedBy=network.target
这是在启动时由 network.target 拉入的,并且必须在 network.target 之后启动,因为它添加了默认路由,以防止在找不到默认路由时 mount 命令失败。使用适当高的指标,在获取时不会干扰真正的默认值(仅供参考,该指标为最大值)。然后,它调用 NFS 安装,并且在安装命令运行时该服务将保持“激活”状态。如果安装了 NFS 服务器,该服务将停止并删除多余的默认路由。如果没有,它将永远挂起(大约 1 周),等待服务器。但这并不妨碍系统实现任何进一步的目标。请注意,这是仅限 NFSv4 的环境。如果需要早期版本,您必须确保所有 RPC 基础设施都已启动。我屏蔽了所有这些 RPC 服务,但是如果找不到此遗留环境的某些部分,NFS 挂载仍然会失败,因为它仍然希望与 RPC 的文件锁定交互,即使 NFSv4 不使用它。因此,mount 命令中需要“nolock”。看来这只是 NFS 挂载实现中的一个遗留问题。此外,我还将“timeo”和“retrans”选项调整为更积极的级别,有助于确保在“可用”默认路由可用时快速找到服务器。使用指定的设置,客户端等待 2 秒以获取响应,如果没有响应,则不会重新传输相同的请求。这可确保客户端不会进入指数退避,否则会导致每次尝试查找服务器之间的重传间隔增大。然而,由于重试计时器设置为 10000 分钟,它会立即使用相同的最大超时 2 秒再次尝试新的尝试......如此反复,直到获得连接。在我的系统上,我什至在到达 network-online.target 之前就看到所有 NFS 挂载就位并准备就绪。
假设在某个阶段 NFS 挂载。我有“mx”服务器根@ /mnt/NFS/mx/。由于安装部分中的“WantedBy=mnt-NFS-mx.service”,我的 mnt-NFS-mx.service 单元的激活会触发一系列绑定安装。示例如下:media-NAS_Public.mount
[Unit]
# To make remote NFS shares appear under the devices pane of Nemo
Description=--bind NFS NAS /Public onto /media/NAS_Public
DefaultDependencies=no
After=mnt-NFS-mx.service
[Mount]
What=/mnt/NFS/mx/shared_sun_NAS/Public
Where=/media/NAS_Public
Type=none
Options=bind,_netdev
TimeoutSec=0
[Install]
WantedBy=mnt-NFS-mx.service
在此安装单元中值得注意的是“DefaultDependency=no”与选项“_netdev”的结合使用。 _netdev 通常用于确保 systemd 理解该挂载单元实际上与网络相关,而不是本地文件系统的一部分。但是,这会在remote-fs-pre.target、network.target和network-online.target上添加After=的一系列默认依赖项,并且重要的是在remote-fs.target上添加Before=。这个 Before= 很麻烦,因为如果这些挂载单元没有成功或超时,这会阻止系统到达remote-fs.target!因此,使用“DefaultDependency=no”,可以消除这些依赖关系,并防止在启动时未直接启动该单元时启动挂起。我保留了 _netdev 选项作为辅助回忆录,该绑定挂载实际上将创建一个直接到挂载点目标的 NFS 挂载。
我在 /media 中创建了一堆这些 .mount 单元,也直接在我的个人文档目录等的主目录中创建了这些 .mount 单元。最后,我确保提前创建桌面计算机上的任何安装点,并且不可变在这些目录上设置标志 (chattr +i /media/NAS_Public) 以确保如果安装过程失败,则不会向这些位置写入任何内容。
虽然在我的例子中并不重要,但应该清楚的是,对这些绑定安装使用“DefaultDependency=no”将阻止必须具有 NFS 连接的应用程序的任何依赖关系逻辑意识到失败。话虽如此,可以使用 After=mnt-NFS-mx.service 作为替代品。我没试过这个。
归根结底,这为我提供了一个稳定且快速的启动系统,无论我的网络或服务器的状态如何,还有一个好处是,如果服务器故障得到纠正,安装将突然出现。
希望其他人发现这很有用。
答案4
我发现有效的方法是创建一个专用的 systemd 服务来完成挂载磁盘的工作,并忽略 systemd 的挂载功能。
这种方法的基础是:
NFS 可能由于一千个不同的原因中的任何一个而尚不可用。我们可能会从电源故障中恢复,并且 NFS 服务器比客户端慢。由于客户不知道的问题,网络可能会暂时关闭。 AWS 可能首先实例化了 NFS 客户端。
作为管理员,我们不想手动执行可以描述为“稍后重试”的任务。计算机知道如何稍后重试。
我们将 NFS 故障视为首要概念,方法是使用
Restart
不断重试挂载的选项,直到挂载最终成功。任何需要等待 NFS 可用的操作都可以要求此服务作为依赖项。
[root@adele ~]# cat /etc/systemd/system/srv-music.service
[Unit]
Description=Media devices
After=network.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/bin/mount -t nfs4 -o vers=4,minorversion=1,owner,ro darknight.localdomain6:/Media /srv/music
ExecStop=/usr/bin/umount /srv/music
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target