sample mount units

Example mount units for systemd


Unit file names


Mount unit

  • Mount units must be named the mount point with the extension of .mount
    e.g. mountpoint /data/backup - unit file data-backup.mount

Automount unit

  • Automount units use the name of the mount unit but an extension of .automount
    e.g. data-backup.automount

Content of a mount unit


Depending on the Type, the What is different and so is the Options

  • What do you want to mount (disk, server, share)?

Where do you want it to mount (a path on your system)?

Which Type is your mount (filesystem, cifs, nfs)?

What Options should the mount use(rw, auto, netdev)?

Example

If you want to mount a Samba share named video provided by a local NAS named server

  • What is the name of server and the share e.g. //server/video
  • Where is the path where you can browse the data provided by the share e.g. /data/smb/video
  • Type will be cifs because that is what this type of connection is called
  • Options will instruct the system it is a net device _netdev, it should be possible to read and write rw, and connect using a specific workgroup, username and password.

Sample MOUNT unit for SMB share


It is also possible to store the username and password in a safe location readable only by root.

Replace the $VARIABLES with the values for your system

The package smbclient is enough.

/etc/systemd/system/data-smb-video.mount

[Unit]
Description=NAS SMB video share
After=network.target

[Mount]
What=//server/video
Where=/data/smb/video
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0777,dir_mode=0777,user=$SMBUSER,password=$SMBPASS,workgroup=$WORKGROUP
TimeoutSec=30

[Install]
WantedBy=multi-user.target

NOTE: According to the archlinux wiki the uid and gid can cause I/O errors.

Warning: Using uid and/or gid as mount options may cause I/O errors, it is recommended to set/check correct File permissions and attributes instead. - https://wiki.archlinux.org/index.php/Samba#Manual_mounting

More information on Samba can be found on the archlinux wiki

Sample MOUNT unit for DISK device


[Type] and [Options] are optional for disk devices. To reduce unnecessary writings to SSD devices the noatime option is recommended.

/etc/systemd/system/data-build.mount

[Unit]
Description=Mount build partition

[Mount]
What=/dev/disk/by-uuid/899b1bc4-219c-4b92-8539-0aa118d9780a
Where=/data/build
#Type=
#Options=

[Install]
WantedBy=multi-user.target

Sample MOUNT unit for NFS share


/etc/systemd/system/data-nfs-video.mount

[Unit]
Description=Video
After=network.target

[Mount]
What=diskstation:/volume1/video
Where=/data/nfs/video
Type=nfs
Options=_netdev,auto

[Install]
WantedBy=multi-user.target

Sample MOUNT unit for FTP server


Using curlftpfs

It is also possible to store the username and password in a safe location readable only by root.

You can put the user and password in a .netrc file in the home directory of the user that executes CurlFtpFS. It can have 600 permission. It's still clear text but at least is not accessible by all.
The format is:

machine ftp.host.com  
login myuser  
password mypass 

curlftpfs on archlinux wiki

Replace the $VARIABLES with the actual values for your use case.

/etc/systemd/system/data-ftp-server.mount

[Unit]
Description=My FTP server (ftp.server.tld)
After=network.target

[Mount]
What=curlftpfs#ftp.server.tld
Where=/data/ftp/server
Type=fuse
Options=rw,nosuid,uid=$UID,gid=$GID,allow_other,user=$FTPUSER:$FTPPASS

[Install]
WantedBy=multi-user.target

Sample NFS AUTOMOUNT unit


/etc/systemd/system/data-nfs-video.automount

[Unit]
Description=Automount video share
ConditionPathExists=/data/nfs/video

[Automount]
Where=/data/nfs/video
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

WEBDAV


See https://wiki.archlinux.org/index.php/Davfs2

Install davfs2 from the repo

Sample WEBDAV MOUNT unit

/etc/systemd/system/data-webdav-service.mount

[Unit]
Description=Mount WebDAV Service
After=network-online.target
Wants=network-online.target

[Mount]
What=http(s)://address:<port>/path
Where=/data/webdav/service
Options=rw,_netdev
Type=davfs
TimeoutSec=15

[Install]
WantedBy=multi-user.target

Edit the file /etc/davfs2/secrects and append your webdav service and credentials

http(s)://address:<port>/path    davusername    "davpassword"

Or create a user file at ~/.davfs2/secrets for user specific mounts

Sample AUTOMOUNT units


Same rule applies as for mount units. It must be the path to the mountpoint with the .automount extension e.g. data-backup.automount

Network device

This is sample for webdav.

/etc/systemd/data-webdav-service.automount

[Unit]
Description=Mount WebDAV Service
After=network-online.target
Wants=network-online.target

[Automount]
Where=/data/webdav
TimeoutIdleSec=300

[Install]
WantedBy=remote-fs.target

Disk device

cat /etc/systemd/system/data-backup.automount

[Unit]
Description=Automount backup partition
ConditionPathExists=/data/backup

[Automount]
Where=/data/backup
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target