Mount a volume using systemd
Tags: linux, devops, os
Published: 2018-05-29 15:29:09

---
title: Mount a volume using systemd
published: true
description: What is systemd and how to mount a volumes using it.
tags: linux,devops,os
---

## What is systemd ?

`systemd`  is a suite of software that provides fundamental building blocks for a Linux operating system. It is an initialization system and hence the first process to start (with pid 1). It will start and manage other services and manage dependencies between them.

It replaced sysvinit, upstart in many Linux distributions. As of now almost all popular Linux distributions use systemd as the default init system (including my favorite Debian).

All this while to mount a volume after boot automatically, we used to mess around with `fstab`. Recently I came to know we can use systemd to mount volumes and here is how to do it.

## Unit configuration file 
Configuration for any entity (service,socket,device,mount point etc) that is managed by systemd is maintained in unit configuration files. They are usually located in `/etc/systemd/system`.

You can use `systemctl` command to start/stop/restart check status of services in your machine.

```
systemctl status postgresql.service
systemctl start postgresql.service
systemctl stop postgresql.service
systemctl restart postgresql.service
```
This command offers much more functionality please refer the manpage for more details.
```
# systemctl status postgresql.service 

● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sun 2018-05-20 17:37:20 IST; 1 day 20h ago
  Process: 976 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 976 (code=exited, status=0/SUCCESS)
    Tasks: 0 (limit: 4915)
   Memory: 0B
      CPU: 0
   CGroup: /system.slice/postgresql.service
```

## Mount unit Configuration

To mount a filesystem using systemd we create special type of unit files which have an extension of `.mount` and `.automount`

> Mount units must be named after the mount point directories they control. Example: the mount point /home/lennart must be configured in a unit file home-lennart.mount.

```
[Unit]
Description=Additional drive

[Mount]
What=/dev/disk/by-uuid/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Where=/mnt/driveone
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target
```
Then you can enable this unit using :

```
systemctl enable mnt-driveone.mount
```

## Automount

These unit file configurations ends with `.automount`. This can be used to automatically mount the filesystem on demand.

```
Description=Automount Additional Drive

[Automount]
Where=/mnt/driveone

[Install]
WantedBy=multi-user.target
```

> Automount units must be named after the automount directories they control.
 
Every `automount` unit file must have a matching `mount` unit configuration, which is activated when the path (`Where`) in `automount` configuration is accessed.

so the unit configuration required to mount `/mnt/mydata` is :

```
/etc/systemd/system/mnt-mydata.automount
/etc/systemd/system/mnt-mydata.mount
```
>Originally posted in [JigsawCode](https://www.jigsawcode.com/)

## References :
[Systemd mount reference](https://www.freedesktop.org/software/systemd/man/systemd.mount.html)
[Systemd automount reference](https://www.freedesktop.org/software/systemd/man/systemd.automount.html)