Difficulty: ★★☆☆☆
Example mount units for systemd
The examples here are a supplement to the guide on systemd mount units
Remember to remove the comments placed in the units - they are not writing for copy-paste but as examples. (comments is text beginning with a #
)
Only use an automount unit if the device is not readily available at boot like removable devices and network locations.
Important rule
- For Internal devices enable and start the mount unit .
- For a removable device - never enable the mount unit only the automount unit.
- The mount unit will create the mountpoint if it does not exist.
- The automount unit will fail if the mount unit is active
- The automount unit will start the mount unit when the mountpoint is accessed whether this is terminal or application access.
Unit file names
Local system unit files are stored in /etc/systemd/system
.
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.
Automount units starts the mount unit on demand e.g. when the mount path is accessed. The automount unit will fail if the mount unit is active, so always - in the context of automount - ensure the mount unit is disabled.
You can create the files by using the output like this
touch $(systemd-escape -p --suffix=mount "/data/home-backup")
touch $(systemd-escape -p --suffix=automount "/data/home-backup")
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
If the partition is on an internal disk - there is no need for automount - simply enable the mount unit.
Disk PARTITION
[Type] and [Options] are optional for disk devices.
To reduce unnecessary writings to SSD devices the noatime option is recommended.
mount unit
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=Mount build partition
[Mount]
What=/dev/disk/by-uuid/$UUID
Where=$YOUR_MOUNT_PATH
Type=ext4
Options=rw,noatime
[Install]
WantedBy=multi-user.target
USB device partition
[Type] and [Options] are optional for disk devices.
To reduce unnecessary writings to SSD devices the noatime option is recommended.
mount unit
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=Mount USB backup device partition
[Mount]
What=/dev/disk/by-uuid/$UUID
Where=$YOUR_MOUNT_PATH
Type=ext4
Options=rw,noatime
[Install]
WantedBy=multi-user.target
USB partition automount unit
Name the file according to $YOUR_MOUNT_PATH.automount
[Unit]
Description=Automount USB backup partition
ConditionPathExists=$YOUR_MOUNT_PATH
[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target
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
SMB credentials
Create a file /etc/smb.cred
or in user’s home with content
user=$SMBUSER
password=$SMBPASS
workgroup=$WORKGROUP
Make the file readonly to user
sudo chmod 600 /etc/smb.cred
SMB version
If needed you add a version to the options string e.g. vers=NT1
mount unit
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=NAS SMB video share
[Mount]
What=//$YOUR_SERVER/$YOUR_SHARE
Where=$YOUR_MOUNT_PATH
Type=cifs
Options=_netdev,iocharset=utf8,rw,file_mode=0666,dir_mode=0777,credentials=/etc/smb.cred,vers=$SMBVERSION
TimeoutSec=30
[Install]
WantedBy=multi-user.target
automount unit
Name the file according to $YOUR_MOUNT_PATH.automount
[Unit]
Description=Automount video share using SMB
ConditionPathExists=$YOUR_MOUNT_PATH
[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target
NFS network share
mount unit
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=Mount NAS Video share using NFS
[Mount]
What=$YOUR_SERVER:/$YOUR_SHARE
Where=$YOUR_MOUNT_PATH
Type=nfs
Options=_netdev,auto
[Install]
WantedBy=multi-user.target
automount unit
Name the file according to $YOUR_MOUNT_PATH.automount
[Unit]
Description=Automount video share usuing NFS
ConditionPathExists=$YOUR_MOUNT_PATH
[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=10
[Install]
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.
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=Mount FTP server (ftp.server.tld) using curlftpfs
Wants=network-online.service
[Mount]
What=curlftpfs#ftp.server.tld
Where=$YOUR_MOUNT_PATH
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
Name the file according to $YOUR_MOUNT_PATH.automount
[Unit]
Description=Automount ftp server ftp.server.tld
ConditionPathExists=$YOUR_MOUNT_PATH
[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=300
[Install]
WantedBy=multi-user.target
WEBDAV
See the Arch wiki on webdav. Install davfs2
from the AUR (Arch User Repository)
Edit the file /etc/davfs2/secrets
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
Name the file according to $YOUR_MOUNT_PATH.mount
[Unit]
Description=Mount WebDAV Service on server https://host:port/path
Wants=network-online.service
[Mount]
What=http(s)://address:<port>/path
Where=$YOUR_MOUNT_PATH
Options=rw,_netdev
Type=davfs
TimeoutSec=15
[Install]
WantedBy=remote-fs.target
WantedBy=multi-user.target
automount unit
Name the file according to $YOUR_MOUNT_PATH.automount
[Unit]
Description=Mount WebDAV Service
ConditionPathExists=$YOUR_MOUNT_PATH
[Automount]
Where=$YOUR_MOUNT_PATH
TimeoutIdleSec=300
[Install]
WantedBy=multi-user.target