本文を読み飛ばす

systemd: Reducing SD Card Wear with OverlayFS and Systemd

SD cards have a limited number of write cycles, making them a common failure point for SBCs like the Raspberry Pi. One of the most effective ways to prolong the life of your SD card is to use an OverlayFS to redirect write-heavy operations to a different storage medium (or RAM).

In this article, I will demonstrate how to set up persistent OverlayFS mounts using systemd and Makefile.

Sample OverlayFS using Makefile

Write down followings to Makefile:

mount-overlay-usr-src: /usr/src/f_overlay
mount-overlay-var-log: /var/log/f_overlay

/usr/src/f_overlay: d:=/mnt/storage1/usr-src
/usr/src/f_overlay: l:=lowerdir=/usr/src
/usr/src/f_overlay: u:=$d/usr-src-upper
/usr/src/f_overlay: w:=$d/usr-src-temp
/usr/src/f_overlay:
    mkdir -p $u $w
    mount -t overlay overlay -o $l,upperdir=$u,workdir=$w $(dir $@)
    touch $@

/var/log/f_overlay: d:=/mnt/storage1/var-log
/var/log/f_overlay: l:=lowerdir=/var/log
/var/log/f_overlay: u:=$d/var-log-upper
/var/log/f_overlay: w:=$d/var-log-temp
/var/log/f_overlay:
    mkdir -p $d $u $w
    mount -t overlay overlay -o $l,upperdir=$u,workdir=$w $(dir $@)
    touch $@

After that, Run scripts:

$ make mount-overlay-usr-src
$ make mount-overlay-var-log

Persistent OverlayFS with Systemd

Make the script to the systemd unit file for persistence overlayfs mount.

Create following file with systemctl edit --full --force mount-overlay:

[Unit]
Description = mount overlay filesystem to reduce SD card access.
After = mnt-storage1.mount
Requires = mnt-storage1.mount

[Service]
Type = oneshot
ExecStart = make -C /path/to/script mount-overlay-var-log
ExecStop = umount -f /var/log

# see Note section
ExecStopPost = systemctl restart systemd-journald
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy = multi-user.target
EOF

$ systemctl daemon-reload
$ systemctl start mount-overlay
$ systemctl enable mount-overlay

Note Be careful to unmount overlayfs, Because another services relies on /var/log would be corrupt and should be restart.

コメント

Comments powered by Disqus
宣伝: