• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How To Auto Mount a FileSystem Using Systemd

by admin

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.

Filed Under: CentOS/RHEL 7, Linux, OEL 7

Some more articles you might also be interested in …

  1. How to change the Default Log Directory(/var/log) in Rsyslog for CentOS/RHEL 6,7
  2. bsdtar command – Read and write tape archive files
  3. “shutdown” Command Examples in Linux
  4. “passwd: Module is unknown” – error while changing the password in CentOS/RHEL 6
  5. dget Command Examples in Linux
  6. lastb Command Examples in Linux
  7. grub-script-check: command not found
  8. How to Clone Linux disk partition over network using dd
  9. How to create and mount filesystems in Linux
  10. CentOS / RHEL 7 : Beginners guide to systemd service units

You May Also Like

Primary Sidebar

Recent Posts

  • qsub Command Examples in Linux
  • qsub: command not found
  • qrcp Command Examples in Linux
  • qmrestore Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright