在启动时使用 systemd 挂载 samba 共享

在启动时使用 systemd 挂载 samba 共享

我有一些想要在启动时挂载的 Samba 共享。任何事情都不需要立即使用它们,但我希望它们在我第一次访问它们时可用。我尝试使用 .mount 文件和 /etc/fstab 中的条目以及特殊的 systemd 选项。问题似乎是 DNS 解析器无法解析文件服务器主机,即使我强制 .mount 单元依赖于网络启动和解析器运行。我知道我可以让它依赖于稍后的目标或添加一些延迟,但我有兴趣了解 systemd 并以正确的方式执行此操作。

[Unit]
Description=Hardware file share
Requires=network-online.target
After=network-online.target systemd-resolved.service
Wants=network-online.target systemd-resolved.service

[Mount]
What=//files.example.com/hardware
Where=/media/hardware
Type=cifs
Options=rw,uid=jpkotta,gid=users,credentials=/etc/samba/private/files.cred,iocharset=utf8,file_mode=0644,dir_mode=0755
TimeoutSec=30

[Install]
WantedBy=multi-user.target

这是(已编辑的)日志:

Jun 27 12:01:28 shannon systemd[1]: Starting Network Service...
Jun 27 12:01:28 shannon systemd[1]: Started Periodic Command Scheduler.
Jun 27 12:01:28 shannon systemd[1]: Started Entropy Harvesting Daemon.
Jun 27 12:01:28 shannon systemd[1]: Started CUPS Scheduler.
Jun 27 12:01:28 shannon systemd[1]: Starting Login Service...
Jun 27 12:01:28 shannon systemd[1]: Starting Save/Restore Sound Card State...
Jun 27 12:01:28 shannon systemd[1]: Starting Avahi mDNS/DNS-SD Stack...
Jun 27 12:01:28 shannon systemd[1]: Starting Virtual console mouse server...
Jun 27 12:01:28 shannon systemd-logind[417]: New seat seat0.
Jun 27 12:01:28 shannon systemd[1]: Started Login Service.
Jun 27 12:01:28 shannon systemd-logind[417]: Watching system buttons on /dev/input/event4 (Power Button)
Jun 27 12:01:28 shannon systemd-logind[417]: Watching system buttons on /dev/input/event3 (Power Button)
Jun 27 12:01:28 shannon crond[414]: (CRON) INFO (Syslog will be used instead of sendmail.)
Jun 27 12:01:28 shannon crond[414]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 59% if used.)
Jun 27 12:01:28 shannon avahi-daemon[419]: Found user 'avahi' (UID 84) and group 'avahi' (GID 84).
Jun 27 12:01:28 shannon avahi-daemon[419]: Successfully dropped root privileges.
Jun 27 12:01:28 shannon avahi-daemon[419]: avahi-daemon 0.6.32 starting up.
Jun 27 12:01:28 shannon avahi-daemon[419]: WARNING: No NSS support for mDNS detected, consider installing nss-mdns!
Jun 27 12:01:28 shannon systemd[1]: Started Avahi mDNS/DNS-SD Stack.
Jun 27 12:01:28 shannon avahi-daemon[419]: Successfully called chroot().
Jun 27 12:01:28 shannon avahi-daemon[419]: Successfully dropped remaining capabilities.
Jun 27 12:01:28 shannon avahi-daemon[419]: No service file found in /etc/avahi/services.
Jun 27 12:01:28 shannon avahi-daemon[419]: Network interface enumeration completed.
Jun 27 12:01:28 shannon systemd[1]: Started Make remote CUPS printers available locally.
Jun 27 12:01:28 shannon avahi-daemon[419]: Server startup complete. Host name is shannon.local. Local service cookie is 740728414.
Jun 27 12:01:28 shannon /usr/bin/gpm[429]: *** info [daemon/startup.c(136)]:
Jun 27 12:01:28 shannon systemd[1]: Started Virtual console mouse server.
Jun 27 12:01:28 shannon /usr/bin/gpm[429]: Started gpm successfully. Entered daemon mode.
Jun 27 12:01:28 shannon systemd[1]: Started Save/Restore Sound Card State.
Jun 27 12:01:28 shannon crond[414]: (CRON) INFO (running with inotify support)
Jun 27 12:01:28 shannon systemd-networkd[413]: Enumeration completed
Jun 27 12:01:28 shannon systemd[1]: Started Network Service.
Jun 27 12:01:28 shannon systemd-networkd[413]: enp4s0: Renamed to eth0
Jun 27 12:01:28 shannon systemd-networkd[413]: eth0: Renamed to enp4s0
Jun 27 12:01:28 shannon systemd[1]: Reached target Network.
Jun 27 12:01:28 shannon systemd-networkd[413]: enp4s0: IPv6 enabled for interface: Success
Jun 27 12:01:28 shannon kernel: IPv6: ADDRCONF(NETDEV_UP): enp4s0: link is not ready
Jun 27 12:01:28 shannon haveged[415]: haveged: ver: 1.9.1; arch: x86; vend: GenuineIntel; build: (gcc 4.9.2 ITV); collect: 128K
Jun 27 12:01:28 shannon haveged[415]: haveged: cpu: (L4 VC); data: 32K (L2 L4 V); inst: 32K (L2 L4 V); idx: 21/40; sz: 31910/59039
Jun 27 12:01:28 shannon haveged[415]: haveged: tot tests(BA8): A:1/1 B:1/1 continuous tests(B):  last entropy estimate 7.99984
Jun 27 12:01:28 shannon haveged[415]: haveged: fills: 0, generated: 0
Jun 27 12:01:28 shannon systemd[1]: Started OpenSSH Daemon.
Jun 27 12:01:28 shannon systemd[1]: Started A file transfer program to keep remote files in sync.
Jun 27 12:01:28 shannon systemd[1]: Starting Samba NetBIOS name server...
Jun 27 12:01:28 shannon /usr/bin/gpm[429]: *** info [mice.c(1990)]:
Jun 27 12:01:28 shannon /usr/bin/gpm[429]: imps2: Auto-detected intellimouse PS/2
Jun 27 12:01:28 shannon systemd[1]: Starting Network Time Service...
Jun 27 12:01:28 shannon systemd[1]: Reached target Network is Online.
Jun 27 12:01:29 shannon rsyncd[440]: rsyncd version 3.1.2 starting, listening on port 873
Jun 27 12:01:29 shannon systemd[1]: Starting Permit User Sessions...
Jun 27 12:01:29 shannon systemd[1]: Started Pacserve.
Jun 27 12:01:29 shannon systemd[1]: Starting Network Name Resolution...
Jun 27 12:01:29 shannon systemd[1]: Started Permit User Sessions.
Jun 27 12:01:29 shannon systemd[1]: Started Getty on tty1.
Jun 27 12:01:29 shannon systemd[1]: Reached target Login Prompts.
Jun 27 12:01:29 shannon systemd[1]: Started SLiM Simple Login Manager.
Jun 27 12:01:30 shannon systemd-resolved[461]: Positive Trust Anchors:
Jun 27 12:01:30 shannon systemd-resolved[461]: . IN DS 19036 8 2 49aac11d7b6f6446702e54a1607371607a1a41855200fd2ce1cdde32f24e8fb5
Jun 27 12:01:30 shannon systemd-resolved[461]: Negative trust anchors: 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa 168.192.in-addr.arpa corp home internal intranet lan local private test
Jun 27 12:01:30 shannon systemd-resolved[461]: Using system hostname 'shannon'.
Jun 27 12:01:30 shannon systemd-resolved[461]: Switching to system DNS server 10.0.0.22.
Jun 27 12:01:30 shannon systemd[1]: Started Network Name Resolution.
Jun 27 12:01:30 shannon systemd[1]: Mounting Hardware file share...
Jun 27 12:01:30 shannon mount[467]: mount error: could not resolve address for files.packetdigital.com: Unknown error
Jun 27 12:01:30 shannon systemd[1]: media-hardware.mount: Mount process exited, code=exited status=1
Jun 27 12:01:30 shannon systemd[1]: Failed to mount Hardware file share.
Jun 27 12:01:30 shannon systemd[1]: media-hardware.mount: Unit entered failed state.
Jun 27 12:01:30 shannon sshd[431]: Server listening on 0.0.0.0 port 22.
Jun 27 12:01:30 shannon sshd[431]: Server listening on :: port 22.
Jun 27 12:01:30 shannon ntpd[443]: ntpd [email protected] Sat Jun  4 14:58:28 UTC 2016 (1): Starting
Jun 27 12:01:30 shannon ntpd[443]: Command line: /usr/bin/ntpd -g -u ntp:ntp
Jun 27 12:01:30 shannon systemd[1]: Started Network Time Service.
Jun 27 12:01:30 shannon ntpd[472]: proto: precision = 0.087 usec (-23)
Jun 27 12:01:30 shannon ntpd[472]: Listen and drop on 0 v6wildcard [::]:123
Jun 27 12:01:30 shannon ntpd[472]: Listen and drop on 1 v4wildcard 0.0.0.0:123
Jun 27 12:01:30 shannon ntpd[472]: Listen normally on 2 lo 127.0.0.1:123
Jun 27 12:01:30 shannon ntpd[472]: Listen normally on 3 lo [::1]:123
Jun 27 12:01:30 shannon ntpd[472]: Listening on routing socket on fd #20 for interface updates
Jun 27 12:01:30 shannon dbus[412]: [system] Activating via systemd: service name='org.freedesktop.ColorManager' unit='colord.service'
Jun 27 12:01:30 shannon systemd[1]: Starting Manage, Install and Generate Color Profiles...
Jun 27 12:01:31 shannon dbus[412]: [system] Successfully activated service 'org.freedesktop.ColorManager'
Jun 27 12:01:31 shannon systemd[1]: Started Manage, Install and Generate Color Profiles.
Jun 27 12:01:31 shannon kernel: tg3 0000:04:00.0 enp4s0: Link is up at 1000 Mbps, full duplex
Jun 27 12:01:31 shannon kernel: tg3 0000:04:00.0 enp4s0: Flow control is on for TX and on for RX
Jun 27 12:01:31 shannon kernel: IPv6: ADDRCONF(NETDEV_CHANGE): enp4s0: link becomes ready
Jun 27 12:01:31 shannon systemd-networkd[413]: enp4s0: Gained carrier
Jun 27 12:01:31 shannon colord[474]: (colord:474): Cd-WARNING **: failed to get session [pid 416]: No such device or address
Jun 27 12:01:31 shannon colord[474]: /usr/lib/colord/colord-sane: error while loading shared libraries: libsane.so.1: cannot open shared object file: No such file or directory
Jun 27 12:01:32 shannon slim[466]: /usr/bin/fvwm - FVWM
Jun 27 12:01:32 shannon avahi-daemon[419]: Joining mDNS multicast group on interface enp4s0.IPv6 with address fe80::21d:9ff:fe26:b135.
Jun 27 12:01:32 shannon systemd-networkd[413]: enp4s0: Gained IPv6LL
Jun 27 12:01:32 shannon avahi-daemon[419]: New relevant interface enp4s0.IPv6 for mDNS.
Jun 27 12:01:32 shannon avahi-daemon[419]: Registering new address record for fe80::21d:9ff:fe26:b135 on enp4s0.*.
Jun 27 12:01:33 shannon systemd-networkd[413]: enp4s0: DHCPv4 address 10.0.1.87/16 via 10.0.0.22
Jun 27 12:01:33 shannon dbus[412]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service'
Jun 27 12:01:33 shannon avahi-daemon[419]: Joining mDNS multicast group on interface enp4s0.IPv4 with address 10.0.1.87.
Jun 27 12:01:33 shannon avahi-daemon[419]: New relevant interface enp4s0.IPv4 for mDNS.
Jun 27 12:01:33 shannon avahi-daemon[419]: Registering new address record for 10.0.1.87 on enp4s0.IPv4.
Jun 27 12:01:33 shannon systemd[1]: Starting Hostname Service...
Jun 27 12:01:33 shannon dbus[412]: [system] Successfully activated service 'org.freedesktop.hostname1'
Jun 27 12:01:33 shannon systemd[1]: Started Hostname Service.
Jun 27 12:01:34 shannon ntpd[472]: Listen normally on 4 enp4s0 10.0.1.87:123
Jun 27 12:01:34 shannon ntpd[472]: Listen normally on 5 enp4s0 [fe80::21d:9ff:fe26:b135%2]:123
Jun 27 12:01:34 shannon ntpd[472]: new interface(s) found: waking up resolver

答案1

我在这上面浪费了半天时间,但简而言之,该.mount文件只是一个挂载定义,但要像服务(即自动启动)一样使用它,您还需要一个.automount文件。这确实是多余的,但根据我的经验,没有其他方法可以做到这一点。例子:

/etc/systemd/system/finance.mount:

[Mount]
What=//smb.ad.foobar.com/finance
Where=/finance
Options=credentials=/etc/fstab.password,noperm,vers=2.1,_netdev
Type=cifs

/etc/systemd/system/finance.automount:

[Unit]
Requires=remote-fs-pre.target
After=remote-fs-pre.target

[Install]
WantedBy=remote-fs.target

您可以使用以下命令使其自动启动:

systemctl enable finance.automount

重要的提示:Where指令必须匹配单位的文件名!

进一步阅读:

如何使用systemd挂载CIFS目录?
Systemd 挂载失败。其中= 设置与单位名称不匹配
挂载系统
systemd.automount

答案2

我最终使用了自动挂载,因此共享直到第一次访问时才会挂载。我选择在空闲超时后卸载它们,因为安装所需的时间通常不易察觉。中的相关选项/etc/fstabx-systemd.automount.这是一个示例条目:

//files.example.com/software /media/software cifs x-systemd.automount,x-systemd.idle-timeout=1min,rw,uid=jpkotta,gid=users,credentials=/etc/samba/private/files.cred,iocharset=utf8,file_mode=0644,dir_mode=0755 0 0

注意:我喜欢使用fstab,但显然这也可以使用 systemd 单元文件来完成。事实上,使用时fstab仍然会在运行时生成短暂的 systemd 单元。您可以使用例如systemctl cat media-software.mount和 来查看它们systemctl cat media-software.automount

答案3

对我来说,解决方案是启用systemd-networkd-wait-online.service.

你可以这样检查它的状态

 systemctl status systemd-networkd-wait-online.service

并像这样启用它

 systemctl enable systemd-networkd-wait-online.service

相关内容