Overview
By the end of this article you should be able to answer the following questions:
Announcement
You can find all my latest posts on medium.Let’s say we have the following scenario:
- we have the partition: /dev/sdb1
- we have the mountpoint: /tmp/sdb1custom
- the /dev/sdb1 partition has the xfs mountpoint installed on it.
- The /dev/sdb1 needs to be automounted by the multi-user.target
$ touch /etc/systemd/system/tmp-sdb1custom.mount
# replace the “/” to create the mount unit’s file name.
$ cat /etc/systemd/system/tmp-sdb1custom.mount
[Unit]
Description=My new file system
[Mount]
What=/dev/sdb1
Where=/tmp/sdb1custom
Type=xfs
Options=grpquota,ro
[Install]
WantedBy=multi-user.target
$ systemctl start tmp-sdb1custom.mount
$ systemctl enable tmp-sdb1custom.mount
# stop + disable our new tmp-sdb1custom.mount:
$ systemctl stop tmp-sdb1custom.mount
$ systemctl disable tmp-sdb1custom.mount
# create the tmp-sdb1custom.mount file
$ cp /etc/systemd/system/tmp-sdb1custom.mount /etc/systemd/system/tmp-sdb1custom.automount
# modify this file so it looks like this, i.e. has the ‘[automount]’ stanza
$ cat /etc/systemd/system/tmp-sdb1custom.automount
[Unit]
Description=My new automounted file system
[Automount]
[Install]
WantedBy=multi-user.target
# start and enable the new automount unit
$ systemctl start tmp-sdb1custom.automount
$ systemctl enable tmp-sdb1custom.automount
Earlier when we covered autofs, we found how a filesystem is not mounted until you actually cd into it’s mount point.
You can achieve the same thing with systemd as well. To get started, you first need to create a mountpoint and a *.mount file, like we did in the previous lesson, so that it appears that we are going to auto-mount the file system during machine boot. But this time we don’t start the mount “unit” and we ensure that it is disabled.
Let’s use the filesystem, sdb1, from the last lesson as an example. So we have our filesystem:
$ ls -l /dev/sdb1 brw-rw----. 1 root disk 8, 17 Jun 9 06:12 /dev/sdb1
…and we have our mountpoint:
$ ls -l /tmp | grep sdb1 drwxr-xr-x. 2 root root 6 Jun 8 21:01 sdb1custom
…and we have our *.mount file:
$ cat /etc/systemd/system/tmp-sdb1custom.mount [Unit] Description=My new file system [Mount] What=/dev/sdb1 Where=/tmp/sdb1custom Type=xfs Options=grpquota,ro [Install] WantedBy=multi-user.target
Next in order to take the automount route, we actually need to stop+disable this unit:
$ systemctl stop tmp-sdb1custom.mount $ systemctl disable tmp-sdb1custom.mount rm '/etc/systemd/system/multi-user.target.wants/tmp-sdb1custom.mount'
So far so good. Now we come to the steps we need to take to set up the actual automounting feature. To do this we create a new *.automount unit file in the /etc/systemd/system directory. This file must have the same name as the corresponding *.mount file, but with a “.automount” suffix. Therefore it’s easier to create this file by making it a copy of the .mount file:
$ cp /etc/systemd/system/tmp-sdb1custom.mount /etc/systemd/system/tmp-sdb1custom.automount $ ls -l /etc/systemd/system/tmp-sdb1custom.automount -rw-r--r--. 1 root root 151 Jun 9 06:30 /etc/systemd/system/tmp-sdb1custom.automount
Now you need to edit this file so that it looks like this:
$ cat /etc/systemd/system/tmp-sdb1custom.automount [Unit]Description=My new file systemDescription=My new automounted file system[Mount] What=/dev/sdb1 Where=/tmp/sdb1custom Type=xfs Options=grpquota,ro[Automount] Where=/tmp/sdb1custom [Install] WantedBy=multi-user.target
Notice, that this unit’s main config section is now called “[Automount]”. Also notice that I included a “Where” setting in this section. This is actually optional, and if specified, it must match what is in the corresponding .mount file. It’s doesn’t work like some kind of over-ride mechanism, as you might have initially suspected, you will get an error message if you tried.
Now let’s enable and start this unit:
$ systemctl status tmp-sdb1custom.automount tmp-sdb1custom.automount - My new automounted file system Loaded: loaded (/etc/systemd/system/tmp-sdb1custom.automount; disabled) Active: inactive (dead) Where: /tmp/sdb1custom $ systemctl start tmp-sdb1custom.automount $ systemctl enable tmp-sdb1custom.automount ln -s '/etc/systemd/system/tmp-sdb1custom.automount' '/etc/systemd/system/multi-user.target.wants/tmp-sdb1custom.automount' $ systemctl status tmp-sdb1custom.automount tmp-sdb1custom.automount - My new automounted file system Loaded: loaded (/etc/systemd/system/tmp-sdb1custom.automount; enabled) Active: active (waiting) since Tue 2015-06-09 06:48:00 BST; 11s ago Where: /tmp/sdb1custom Jun 09 06:48:00 localhost.localdomain systemd[1]: Starting My new automounted file system. Jun 09 06:48:00 localhost.localdomain systemd[1]: Set up automount My new automounted file system.
Once it has been started, you will see that the mount command shows that it is now a auto-mounting (aka autofs) mountpoint:
$ mount | grep sdb1 systemd-1 on /tmp/sdb1custom type autofs (rw,relatime,fd=45,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
However lsblk shows that it hasn’t been mounted yet:
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 19.5G 0 part ├─centos-swap 253:0 0 2G 0 lvm [SWAP] └─centos-root 253:1 0 17.5G 0 lvm / sdb 8:16 0 2G 0 disk └─sdb1 8:17 0 1G 0 part sr0 11:0 1 1024M 0 rom
Let’s now cd into the mountpoint and try again:
$ cd /tmp/sdb1custom/
Then try lsblk again:
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 500M 0 part /boot └─sda2 8:2 0 19.5G 0 part ├─centos-swap 253:0 0 2G 0 lvm [SWAP] └─centos-root 253:1 0 17.5G 0 lvm / sdb 8:16 0 2G 0 disk └─sdb1 8:17 0 1G 0 part /tmp/sdb1custom sr0 11:0 1 1024M 0 rom
Success!
Also the mount command also shows that it has been mounted:
$ mount | grep sdb1 systemd-1 on /tmp/sdb1custom type autofs (rw,relatime,fd=45,pgrp=1,timeout=300,minproto=5,maxproto=5,direct) /dev/sdb1 on /tmp/sdb1custom type xfs (ro,relatime,seclabel,attr2,inode64,grpquota)