# Use systemd to mount filesystem ###### tag: `systemd` `linux` Notes to use systemd mount unit. #### mount local filesytem create systemd file "/etc/systemd/system/mnt-data.mount". The unit filename must reflect to the mount point path. For example, path "/mnt/data" has unit filename "mnt-data.mount". ``` [Unit] Description = mount /dev/sda to /mnt/data [Mount] What = /dev/sda Where = /mnt/data Type = ext4 Options = defaults [Install] WantedBy = local-fs.target ``` ``` root@NUC10:~# systemctl start mnt-data.mount root@NUC10:~# systemctl status mnt-data.mount ● mnt-data.mount - mount /dev/sda to /mnt/data Loaded: loaded (/etc/systemd/system/mnt-data.mount; disabled; vendor preset: enabled) Active: active (mounted) since Tue 2023-11-14 01:53:37 AEDT; 1s ago Where: /mnt/data What: /dev/sda Tasks: 0 (limit: 76507) Memory: 1004.0K CPU: 62ms CGroup: /system.slice/mnt-data.mount Nov 14 01:53:37 NUC10 systemd[1]: Mounting mount /dev/sda to /mnt/data... Nov 14 01:53:37 NUC10 systemd[1]: Mounted mount /dev/sda to /mnt/data. root@NUC10:~# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 6.3G 2.4M 6.3G 1% /run /dev/nvme0n1p1 916G 539G 331G 62% / tmpfs 32G 0 32G 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 32G 0 32G 0% /run/qemu tmpfs 6.3G 104K 6.3G 1% /run/user/1000 /dev/sda 1.8T 844G 897G 49% /mnt/data ``` https://www.freedesktop.org/software/systemd/man/latest/systemd.special.html#local-fs.target > Note Notify system that new unit files by command `systemctl daemon-reload` #### mount network filesystem (NFS) /etc/systemd/system/mnt-nfs.mount ``` [Unit] Description = Mount NFS volume [Mount] What = m900:/mnt/data Where = /mnt/nfs Type = nfs Options = defaults [Install] WantedBy = multi-user.target ``` #### with automount (for mnt-nfs.mount) /etc/systemd/system/mnt-nfs.automount ``` [Unit] Description = Auto mount NFS volume [Automount] Where = /mnt/nfs [Install] WantedBy = multi-user.target ``` ``` root@NUC10:/mnt# systemctl start mnt-nfs.automount root@NUC10:/mnt# systemctl status mnt-nfs.automount ● mnt-nfs.automount - Auto mount NFS volume Loaded: loaded (/etc/systemd/system/mnt-nfs.automount; disabled; vendor preset: enabled) Active: active (waiting) since Tue 2023-11-14 12:01:07 AEDT; 5s ago Triggers: ● mnt-nfs.mount Where: /mnt/nfs Nov 14 12:01:07 NUC10 systemd[1]: Set up automount Auto mount NFS volume. ``` ``` root@NUC10:/mnt# df Filesystem 1K-blocks Used Available Use% Mounted on tmpfs 6536248 2400 6533848 1% /run /dev/nvme0n1p1 960302096 564538812 346908860 62% / tmpfs 32681236 0 32681236 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 32681236 0 32681236 0% /run/qemu tmpfs 6536244 104 6536140 1% /run/user/1000 root@NUC10:/mnt# ls -l /mnt/nfs total 8 drwxrwxrwx 4 root root 4096 Sep 25 01:30 bind9 drwxr-xr-x 5 root root 4096 Sep 22 01:49 gitlab root@NUC10:/mnt# df Filesystem 1K-blocks Used Available Use% Mounted on tmpfs 6536248 2400 6533848 1% /run /dev/nvme0n1p1 960302096 564538812 346908860 62% / tmpfs 32681236 0 32681236 0% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 32681236 0 32681236 0% /run/qemu tmpfs 6536244 104 6536140 1% /run/user/1000 m900:/mnt/data 960301056 36037632 875408384 4% /mnt/nfs ``` > automount is not to mount automatically at boot process, it mounts filesystem when mount point is accessed (on daemon). > you need to have mount unit file created first. > For each automount unit file a matching mount unit file (see systemd.mount(5) for details) must exist which is activated when the automount path is accessed. #### Reference https://blog.csdn.net/seaship/article/details/116237747 https://forum.manjaro.org/t/root-tip-how-to-systemd-mount-unit-samples/1191 https://unix.stackexchange.com/questions/283442/systemd-mount-fails-where-setting-doesnt-match-unit-name https://unix.stackexchange.com/questions/570958/mount-vs-automount-systemd-units-which-one-to-use-for-what