I find myself using systemd's built-in functions more and more. This time, automounts and mounts which I prefer to autofs due to systemd-automount's simplicity.
Talking about simplicity, all it takes for a automount is a unit-file (.automount), reloading the systemd daemon and enable the automount unit.
Here's a quick reference on how to set it up using a nfs-share but keep in mind that automounts work with pretty much any other filesystem aswell.
Ensure a nfs-share exists on your nfs server (any nfs-server will do):
[root@nfs ~]# cat << EOF >> /etc/exports
/var/nfs/data server.archyslife.lan(rw,secure,sync,no_root_squash,nohide)
EOF
On the server that's going to be mounting the share, create the systemd-mount unit first:
[root@server ~]# cat << EOF > /etc/systemd/system/var-backup-data.mount
[Unit]
Description=Mount for /var/backup/data
[Mount]
What=nfs.archyslife.lan:/var/nfs/data
Where=/var/backup/data
Type=nfs
Options=rw,vers=4.2,sec=sys,soft,relatime,namlen=255,timeo=30,retry=3,retrans=3,proto=tcp,_netdev
EOF
Test if the mount is working as expected:
[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl start var-backup-data.mount
[root@server ~]# systemctl status var-backup-data.mount
If the mount unit itself worked as expected, stop the mount and continue to with the automount unit:
[root@server ~]# systemctl stop var-backup-data.mount
[root@server ~]# cat << EOF > /etc/systemd/system/var-backup-data.automount
[Unit]
Description=Automount for /var/backup/data
After=network.target
[Automount]
Where=/var/backup/data
DirectoryMode=0755
TimeoutIdleSec=30
[Install]
WantedBy=multi-user.target
EOF
Reload systemd and enable the automount unit:
[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl enable --now var-backup-data.automount
Now let's get to testing:
[root@server ~]# dd if=/dev/zero of=/var/backup/data/test bs=512K count=1000 status=progress
Check if the directory has been mounted on access:
[root@server ~]# findmnt /var/backup/data
[root@server ~]# systemctl status var-backup-data.automount var-backup-data.mount
Success, the directory '/var/backup/data' was automatically mounted when it was accessed and the data has been written to it.
Feel free to comment and / or suggest a topic.
Comments
Post a Comment