2022-09-11 14:22:54 +02:00
|
|
|
---
|
2023-02-23 14:48:51 +01:00
|
|
|
title: "Systemd Automount"
|
2022-11-19 15:25:20 +01:00
|
|
|
visible: true
|
2022-09-11 14:22:54 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
[toc]
|
|
|
|
|
2023-02-23 14:48:51 +01:00
|
|
|
Systemd can be used to mount filesystems not only on boot (simple `.mount` file), but also on request by any process. (`.automount` file)
|
2022-09-11 14:22:54 +02:00
|
|
|
|
|
|
|
## Mount file
|
2023-02-23 14:48:51 +01:00
|
|
|
|
2022-09-11 14:22:54 +02:00
|
|
|
The `.mount` file should be placed in `/etc/systemd/system`
|
|
|
|
**NOTE: The filename must be (mountpoint).mount with slashes `/` being replaced with dashes `-`**
|
2023-02-23 14:48:51 +01:00
|
|
|
Example: `/mnt/target` --> `mnt-target.mount`
|
|
|
|
|
|
|
|
Here's an example `.mount` file for a CIFS share
|
2022-09-11 14:22:54 +02:00
|
|
|
|
2023-02-19 15:10:34 +01:00
|
|
|
```systemd
|
2022-09-11 14:22:54 +02:00
|
|
|
[Unit]
|
|
|
|
Description=cifs mount
|
|
|
|
|
|
|
|
[Mount]
|
|
|
|
What=//(url/ip)/(sharename)
|
|
|
|
Where=/(target mountpoint)
|
|
|
|
Type=cifs
|
|
|
|
Options=defaults,username=(user),password=(password),file_mode=0640,dir_mode=0750,iocharset=utf8,uid=(local uid),gid=(local gid)
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
```
|
|
|
|
|
|
|
|
## Automount file
|
2023-02-23 14:48:51 +01:00
|
|
|
|
2022-09-11 14:22:54 +02:00
|
|
|
The corresponding `.automount` file needs to have the same name as its `.mount` file
|
2023-02-23 14:48:51 +01:00
|
|
|
Example: `mnt-target.mount` and `mnt-target.automount`
|
2022-09-11 14:22:54 +02:00
|
|
|
|
2023-02-23 14:48:51 +01:00
|
|
|
```systemd
|
2022-09-11 14:22:54 +02:00
|
|
|
[Unit]
|
|
|
|
Description=cifs automount
|
|
|
|
|
|
|
|
[Automount]
|
|
|
|
Where=/(target mountpoint)
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
```
|
|
|
|
|
|
|
|
Enable the `.automount` file to mount the filesystem when necessary
|
2023-02-23 14:48:51 +01:00
|
|
|
`# systemctl enable (target-mount).automount`
|
2023-07-15 16:32:48 +02:00
|
|
|
|
|
|
|
## Service - require mount
|
|
|
|
|
|
|
|
Other services that depend on the filesystem being mounted might have issues with the built-in automounting.
|
|
|
|
In these cases, the option `RequiresMountsFor=` can be set under the `[Unit]` configuration to ensure a path is mounted.
|
|
|
|
Paths are space separated
|
|
|
|
|
|
|
|
```systemd
|
|
|
|
[Unit]
|
|
|
|
...
|
|
|
|
RequiresMountsFor=[PATH 1] [PATH 2]
|
|
|
|
```
|
|
|
|
|
|
|
|
> [Systemd RequiresMountsFor documentation](https://www.freedesktop.org/software/systemd/man/systemd.unit.html#RequiresMountsFor=)
|