How To Auto Mount a FileSystem Using Systemd?

Below example shows how to create a systemd script to mount a disk under a specific path on reboot automatically.

 

1. Check the filesystem UUID using the command “blkid”.

# blkid /dev/sdb
/dev/sdb: UUID="5813cd72-ff30-44bc-a7a3-27c68fe3e6c7" UUID_SUB="ccd243dc-1481-403f-aa51-1502a95cdf2f" TYPE="btrfs"

 

2. We need to create a new mount unit to mount the filesystem. Mount units must be named after the mount point directories they control. For Example: the mount point /var/lib/docker must be configured in a unit file var-lib-docker.mount. For details about the escaping logic used to convert a file system path to a unit name, see manpage of systemd.unit.

# vi /etc/systemd/system/var-lib-docker.mount
[Unit]
Description=Docker mount

[Mount]
What=/dev/disk/by-uuid/5813cd72-ff30-44bc-a7a3-27c68fe3e6c7
Where=/var/lib/docker
Type=btrfs
Options=defaults

[Install]
WantedBy=multi-user.target

 

The option for “What” can be UUID, LABEL, path to disk

 

3. Enable the systemd unit to be started after boot.

# systemctl enable var-lib-docker.mount

 

4. The below docker service script is modified to make sure that the service should be only started after the mount service is called.

# cat /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket var-lib-docker.mount     ### Added mount unit for docker service to wait
Requires=docker.socket
...

 

5. Reboot the node and check for the status of the mount service unit.

# shutdown -r now

 

# systemctl status var-lib-docker.mount
● var-lib-docker.mount - Docker mount
Loaded: loaded (/etc/systemd/system/var-lib-docker.mount; enabled; vendor preset: disabled)
Active: active (mounted) since Tue 2016-06-07 02:06:07 IST; 54s ago
Where: /var/lib/docker
What: /dev/sdb
Process: 416 ExecMount=/bin/mount /dev/disk/by-uuid/5813cd72-ff30-44bc-a7a3-27c68fe3e6c7 /var/lib/docker -n -t btrfs -o defaults (code=exited, status=0/SUCCESS)

Jun 07 02:06:07 OL7-docker-TD systemd[1]: Mounting Docker mount...
Jun 07 02:06:07 OL7-docker-TD systemd[1]: Mounted Docker mount.

 

6. Check for the mount point in the “mount” command.

# mount | grep docker
/dev/sdb on /var/lib/docker type btrfs (rw,relatime,seclabel,space_cache)

 

The mount unit cannot be added to be part of another service operation. Available systemd Unit Types:

Unit Type File        Extension Description
Service unit         .service A system service.
Target unit           .target A group of systemd units.
Automount unit        .automount A file system automount point.
Device unit           .device A device file recognized by the kernel.
Mount unit            .mount A file system mount point.
Path unit             .path A file or directory in a file system.
Scope unit            .scope An externally created process.
Slice unit            .slice A group of hierarchically organized units that manage system processes.
Snapshot unit         .snapshot A saved state of the systemd manager.
Socket unit           .socket An inter-process communication socket.
Swap unit             .swap A swap device or a swap file.
Timer unit            .timer A systemd timer.

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)