Difficulty: ★★☆☆☆
Example mount units for systemd
The examples here are a supplement to the guide on systemd mount units
Unit file names
Mount unit
- Mount units must be named the mount point with the extension of .mount so if you use a mountpoint named /data/backup the unit file must be named data-backup.mount
Automount unit
- Automount units use the name of the mount unit with an extension of .automount so the automount unit for the example data-backup.mount must be named 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 units
SMB network share
The package smbclient
is enough. The credentials can be stored in location readable only by root. Replace the $VARIABLES with the values for your system
NOTE: According to the archlinux wiki the uid and gid can cause I/O errors.
Warning: Using
uid
and/orgid
as mount options may cause I/O errors, it is recommended to set/check correct File permissions and attributes instead. - Samba - ArchWiki
More information on Samba can be found on the archlinux wiki
mount unit
$ cat /etc/systemd/system/data-smb-video.mount
## sample smb share mount unit
[Unit]
Description=NAS SMB video share
After=network-online.target
Wants=network-online.target
[Mount]
What=//server/video
Where=/data/smb/video # unit file name data-smb-video.mount
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0777,dir_mode=0777,user=$SMBUSER,password=$SMBPASS,workgroup=$WORKGROUP
TimeoutSec=30
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
$ cat /etc/systemd/system/data-smb-video.automount
## sample automount unit for smb share
[Unit]
Description=Automount video share using SMB
ConditionPathExists=/data/smb/video
After=network-online.target
Wants=network-online.target
[Automount]
Where=/data/smb/video # unit file name data-smb-video.automount
TimeoutIdleSec=10
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
Disk PARTITION
[Type] and [Options] are optional for disk devices.
To reduce unnecessary writings to SSD devices the noatime option is recommended.
mount unit
$ cat /etc/systemd/system/data-build.mount
## sample partition mount unit
[Unit]
Description=Mount build partition
[Mount]
What=/dev/disk/by-uuid/$UUID
Where=/data/build # unit file name data-build.mount
Type=ext4
Options=rw,noatime
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
cat /etc/systemd/system/data-backup.automount
## sample automount unit for partition
[Unit]
Description=Automount backup partition
ConditionPathExists=/data/backup # unit file name data-backup.automount
[Automount]
Where=/data/backup
TimeoutIdleSec=10
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
NFS network share
mount unit
$ cat /etc/systemd/system/data-nfs-video.mount
## sample nfs mount unit
[Unit]
Description=Mount NAS Video share using NFS
After=network-online.target
Wants=network-online.target
[Mount]
What=diskstation:/volume1/video
Where=/data/nfs/video # unit file name data-nfs-video.mount
Type=nfs
Options=_netdev,auto
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
$ cat /etc/systemd/system/data-nfs-video.automount
## sample automount unit for nfs share
[Unit]
Description=Automount video share usuing NFS
ConditionPathExists=/data/nfs/video
After=network-online.target
Wants=network-online.target
[Automount]
Where=/data/nfs/video # unit file name data-nfs-video.automount
TimeoutIdleSec=10
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
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
See curlftpfs on archlinux wiki. Replace the $VARIABLES with the actual values for your use case.
$ cat /etc/systemd/system/data-ftp-server.mount
## sample ftp mount unit - curlftpfs
[Unit]
Description=Mount FTP server (ftp.server.tld) using curlftpfs
After=network-online.target
Wants=network-online.target
[Mount]
What=curlftpfs#ftp.server.tld
Where=/data/ftp/server # unit file name data-ftp-server.mount
Type=fuse
Options=rw,nosuid,uid=$UID,gid=$GID,allow_other,user=$FTPUSER:$FTPPASS
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
$ cat /etc/systemd/system/data-ftp-server.automount
## sample automount unit for ftp server
[Unit]
Description=Automount ftp server ftp.server.tld
After=network-online.target
Wants=network-online.target
[Automount]
Where=/data/ftp/server # unit file name data-ftp-server.automount
TimeoutIdleSec=300
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
WEBDAV
See the Arch wiki on webdav. Install davfs2
from the repo
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
mount unit
$ cat /etc/systemd/system/data-webdav-service.mount
## sample mount unit for webdav service
[Unit]
## sample webdav mount unit
Description=Mount WebDAV Service on server https://host:port/path
After=network-online.target
Wants=network-online.target
[Mount]
What=http(s)://address:<port>/path
Where=/data/webdav/service # unit file name data-webdav-service.mount
Options=rw,_netdev
Type=davfs
TimeoutSec=15
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
$ cat /etc/systemd/system/data-webdav-service.automount
## sample automount unit for webdav service
[Unit]
Description=Mount WebDAV Service
After=network-online.target
Wants=network-online.target
[Automount]
Where=/data/webdav/service # unit file name data-webdav-service.automount
TimeoutIdleSec=300
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target