Mount samba shares at boot with systemd

sambasystemdsystemd-networkd

I have some Samba shares that I want to mount at boot time. They're not needed for anything immediately, but I'd like them to be available when I first access them. I've tried using a .mount file and an entry in /etc/fstab with a special systemd option. The problem seems to be that the DNS resolver can't resolve the fileserver host, even though I'm forcing the .mount unit to depend on both the network being up and the resolver running. I know I can just make it depend on a later target or add some delay, but I'm interested in understanding systemd and doing this the Right Way.

[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

Here's the (redacted) log:

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 4.2.8p8@1.3265-o 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

Best Answer

I ended up using automounts, so the share isn't mounted until the first time it's accessed. I opted to have them unmounted after a idle timeout because the time it takes to mount is generally unnoticeable. The relevant option in /etc/fstab is x-systemd.automount. Here's an example entry:

//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

Note: I like using fstab, but obviously this can be done with systemd unit files too. In fact, when using fstab there are still ephemeral systemd units generated at run time. You can see them with e.g. systemctl cat media-software.mount and systemctl cat media-software.automount.

Related Question