5h4d0w.net

The Cat's den

Mount CIFS shares with systemd

- Posted in SysAdmin Stuff by

Create the mount point

mkdir /mnt/share

Create the systemd definition file

nano /etc/systemd/system/mnt-share.mount

Note : The filename must contain the mountpoint name where the slashes are replaced with dashes.

[Unit]
Description=CIFS mount service
Requires=network-online.target
After=network-online.service

[Mount]
What=//server/myshare
Where=/mnt/share
Options=guest,uid=root,iocharset=utf8,file_mode=0777,dir_mode=0777,noperm
Type=cifs

[Install]
WantedBy=multi-user.target

Enable the mounting service

Start the service

systemctl start mnt-share.mount

Check for errors

systemctl status mnt-share.mount

If no error occured, you can now enable the service at boot

systemctl enable mnt-share.mount

(Optional) Delay the mounting service

If mount at boot doesn't work, systemd is probably trying to mount shares too fast after initialising the network.

nano /etc/systemd/system/network-online.target.wants/networking.service
[Service]
Type=oneshot
EnvironmentFile=-/etc/default/networking
ExecStart=/sbin/ifup -a --read-environment
ExecStartPost=/bin/sleep 10
ExecStop=/sbin/ifdown -a --read-environment --exclude=lo
RemainAfterExit=true
TimeoutStartSec=5min

ExecStartPost=/bin/sleep 10 makes networking.service wait for 10 seconds after execution before doing anything else.
Ugly, but it works ¯\_(ツ)_/¯

Source : michlstechblog.info