systemd: Setup Overlay-Filesystem on Debian 12
An Overlay-Filesystem is useful to reduce read/write access to the root-filesystem. Minimizing root filesystem access extends the lifespan of the main SD card or flash memory device in SBC (Single Board Computer) environments.
This article describes how to set it up with systemd and its mount or service units.
Mount the another device
Create a mount point and systemd mount unit.
sudo mkdir -p /mnt/externalsudo systemctl edit --full mnt-external.mount
[Unit] Description=Extra Device Storage Documentation=man:fstab(5) man:systemd-fstab-generator(8) # dependencies... DefaultDependencies=no Conflicts=umount.target After= Before=local-fs.target umount.target [Mount] What=/dev/sda1 Where=/mnt/external Type=ext4 Options= [Install] WantedBy=local-fs.target
Mount Overlay-Filesystem
Create the overlay filesystem directory on the secondary storage and make a overlay-filesystem mount unit.
sudo mkdir -p /mnt/external/var-log{,.tmp}sudo systemctl edit --full --force var-log.mount
[Unit] Description=Overlay Mount for Log Directory Documentation=man:fstab(5) man:systemd-fstab-generator(8) # dependencies... DefaultDependencies=no Conflicts=umount.target Requires=mnt-external.mount After=mnt-external.mount Before=local-fs.target umount.target [Mount] What=overlay Where=/var/log Type=overlay Options=index=off,metacopy=off,workdir=/mnt/external/var-log.tmp,lowerdir=/var/log,upperdir=/mnt/external/var-log [Install] # mount automatically after booting WantedBy=local-fs.target
Restart several services after var-log
Create a systemd service unit that depends on the previous mount unit.
sudo systemctl edit --full after-var-log.service
[Unit] Description=Run Custom Boot Script after var-log mount DefaultDependencies=no Conflicts=shutdown.target Requires=var-log.mount After=var-log.mount Before=shutdown.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/bin/systemctl restart systemd-journald.service [Install] WantedBy=multi-user.target
Enable mount and service units
Finally, enable created mount and service units. These will be activated in this step and will take effect from next boot.
$ sudo systemctl daemon-reload $ sudo systemctl enable mnt-external.mount $ sudo systemctl enable var-log.mount $ sudo systemctl enable after-var-log.service
コメント
Comments powered by Disqus