From 65403f6116709343caef30d8232d862238f87971 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Sun, 6 Feb 2022 20:10:25 -0700 Subject: [PATCH] Use systemd for start and stop of zram-config (#78) This implements properly working support for systemd start and stop functions. Proper integration with systemd means that there is no longer a need to use hacky workarounds when the user wants to stop zram-config manually. Signed-off-by: Ethan Dye --- README.md | 16 +++++----------- install.bash | 5 ++--- tests/image.bash | 4 ++-- uninstall.bash | 4 ++-- update.bash | 3 +-- zram-config | 22 ++++++++++++---------- zram-config-shutdown.service | 12 ------------ zram-config.service | 9 ++++++--- ztab | 6 +++--- 9 files changed, 33 insertions(+), 48 deletions(-) delete mode 100644 zram-config-shutdown.service diff --git a/README.md b/README.md index 40f7f4f..8497224 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ See [raspberrypi/linux@cef3970381](https://github.com/raspberrypi/linux/commit/c ### Table of Contents 1. [Install](#install) + - [Manually start or stop](#manually-start-or-stop) 2. [Update](#update) 3. [Uninstall](#uninstall) 4. [Configure](#customize) @@ -53,17 +54,10 @@ git clone https://github.com/ecdye/zram-config sudo ./zram-config/install.bash ``` -Note: The recommended way to stop the `zram-config.service` is to run -``` shell -sudo zram-config "stop" -``` -**NOT** -``` shell -sudo systemctl stop zram-config.service -``` -because of issues with the way systemd works with zram logging. +#### Manually start or stop -The service will stop normally on reboot, there is no need to manually stop it. +Use `sudo systemctl {start|stop} zram-config.service` to start or stop zram-config. +This will ensure that any changes are properly synced to the persistent storage before system poweroff. ### Update @@ -108,7 +102,7 @@ Usually in `/opt` or `/var`, name optional. Usually in `/opt` or `/var`, name optional. If you need multiple zram swaps or zram directories, just create another entry in `/etc/ztab`. -To do this simply add the new entries to the `/etc/ztab`, if you need to edit an active zram device you must stop zram with `sudo zram-config "stop"` and then edit any entries you need to. +To do this simply add the new entries to the `/etc/ztab`, if you need to edit an active zram device you must stop zram with `sudo systemctl stop zram-config.service` and then edit any entries you need to. Once finished, start zram using `sudo systemctl start zram-config.service` which will only add the new entries if zram is already running. #### Example configuration diff --git a/install.bash b/install.bash index b50dc33..c4f6ce8 100755 --- a/install.bash +++ b/install.bash @@ -31,7 +31,6 @@ make --always-make --directory="${BASEDIR}/overlayfs-tools" echo "Installing zram-config files" install -m 755 "${BASEDIR}/zram-config" /usr/local/sbin/ install -m 644 "${BASEDIR}/zram-config.service" /etc/systemd/system/zram-config.service -install -m 644 "${BASEDIR}/zram-config-shutdown.service" /etc/systemd/system/zram-config-shutdown.service install -m 644 "${BASEDIR}/ztab" /etc/ztab mkdir -p /usr/local/share/zram-config/log ln -s /usr/local/share/zram-config/log /var/log/zram-config @@ -43,8 +42,8 @@ echo "ReadWritePaths=/usr/local/share/zram-config/log" >> /lib/systemd/system/lo echo "Starting zram-config.service" systemctl daemon-reload -systemctl enable --now zram-config.service zram-config-shutdown.service -until [[ $(systemctl show -p SubState --value zram-config) == "dead" ]]; do +systemctl enable --now zram-config.service +until [[ $(systemctl show -p SubState --value zram-config) == "exited" ]]; do sleep 5 done diff --git a/tests/image.bash b/tests/image.bash index 19da7fb..bca3b97 100755 --- a/tests/image.bash +++ b/tests/image.bash @@ -23,9 +23,9 @@ if [[ $1 == "setup" ]]; then gpg -q --keyserver keyserver.ubuntu.com --recv-key 0x8738CD6B956F460C gpg -q --trust-model always --verify "${2}.sig" "$2" unzip -q "$2" -d . - qemu-img resize -f raw "$3" 4G - echo ", +" | sfdisk -N 2 "$3" fi + qemu-img resize -f raw "$3" 4G + echo ", +" | sfdisk -N 2 "$3" mountImageFile "mount" "$3" rsync -avr --exclude="*.zip" --exclude="*.img" --exclude="*.sig" --exclude="tests/fs" --exclude="tests/dtb" --exclude="tests/kernel" ./ tests/fs/opt/zram systemd-nspawn --directory="tests/fs" /opt/zram/tests/install-packages.bash diff --git a/uninstall.bash b/uninstall.bash index 77398b4..10b22e6 100755 --- a/uninstall.bash +++ b/uninstall.bash @@ -13,8 +13,8 @@ fi zram-config "stop" tar -cf "${BASEDIR}/logs.tar" --directory=/usr/local/share/zram-config log -systemctl disable zram-config.service zram-config-shutdown.service -rm -f /etc/systemd/system/zram-config.service /etc/systemd/system/zram-config-shutdown.service +systemctl disable zram-config.service +rm -f /etc/systemd/system/zram-config.service sed -i '\|^ReadWritePaths=/usr/local/share/zram-config/log$|d' /lib/systemd/system/logrotate.service systemctl daemon-reload rm -f /usr/local/sbin/zram-config diff --git a/update.bash b/update.bash index a552f15..4b4b41b 100755 --- a/update.bash +++ b/update.bash @@ -36,7 +36,6 @@ zram-config "stop" echo "Updating zram-config files" install -m 755 "${BASEDIR}/zram-config" /usr/local/sbin/ install -m 644 "${BASEDIR}/zram-config.service" /etc/systemd/system/zram-config.service -install -m 644 "${BASEDIR}/zram-config-shutdown.service" /etc/systemd/system/zram-config-shutdown.service install -m 755 "${BASEDIR}/uninstall.bash" /usr/local/share/zram-config/uninstall.bash if ! [[ -f /etc/ztab ]]; then install -m 644 "${BASEDIR}/ztab" /etc/ztab @@ -60,7 +59,7 @@ fi echo "Starting zram-config.service" systemctl daemon-reload -systemctl enable --now zram-config.service zram-config-shutdown.service +systemctl enable --now zram-config.service echo "##### zram-config has been updated #####" echo "##### edit /etc/ztab to configure options #####" diff --git a/zram-config b/zram-config index 5e903ea..8a4af74 100755 --- a/zram-config +++ b/zram-config @@ -141,10 +141,12 @@ removeZdir() { fi rm -rfv "${ZDIR}${BIND_DIR}" >> "$ZLOG" 2>&1 || return 1 - until zramctl --reset "/dev${ZRAM_DEV}" >> "$ZLOG" 2>&1 || [[ count -ge 5 ]]; do - count=$(( count + 1 )) - sleep 5 - done + if [[ -z $SERVICE ]]; then # We don't care about device reset when shutting down system + until zramctl --reset "/dev${ZRAM_DEV}" >> "$ZLOG" 2>&1 || [[ count -ge 5 ]]; do + count=$(( count + 1 )) + sleep 5 + done + fi echo "removeZdir: Device /dev$ZRAM_DEV removed." >> "$ZLOG" } @@ -163,20 +165,20 @@ serviceConfiguration() { echo "Stopping services that interfere with zram device configuration." >> "$ZLOG" if [[ $(systemctl is-active rsyslog.service) == "active" ]]; then export rsyslogActive="true" - systemctl stop syslog.socket >> "$ZLOG" 2>&1 || return 1 + systemctl --no-block stop syslog.socket >> "$ZLOG" 2>&1 || return 1 fi if [[ $(systemctl is-active systemd-journald.service) == "active" ]]; then export journaldActive="true" journalctl --flush >> "$ZLOG" 2>&1 || return 1 - systemctl stop systemd-journald.socket systemd-journald-audit.socket systemd-journald-dev-log.socket >> "$ZLOG" 2>&1 || return 1 + systemctl --no-block stop systemd-journald.socket systemd-journald-audit.socket systemd-journald-dev-log.socket >> "$ZLOG" 2>&1 || return 1 fi elif [[ $1 == "start" ]]; then echo "Restarting services that interfere with zram device configuration." >> "$ZLOG" if [[ -n $journaldActive ]]; then - systemctl restart systemd-journald.socket systemd-journald-audit.socket systemd-journald-dev-log.socket >> "$ZLOG" 2>&1 || return 1 + systemctl --no-block restart systemd-journald.socket systemd-journald-audit.socket systemd-journald-dev-log.socket >> "$ZLOG" 2>&1 || return 1 fi if [[ -n $rsyslogActive ]]; then - systemctl restart syslog.socket >> "$ZLOG" 2>&1 || return 1 + systemctl --no-block restart syslog.socket >> "$ZLOG" 2>&1 || return 1 fi fi } @@ -185,8 +187,8 @@ TMPDIR="/tmp" ZDIR="/opt/zram" ZSHARE="/usr/local/share/zram-config" ZLOG="${ZSHARE}/log/zram-config.log" -if [[ $2 == "shutdown" ]]; then - SERVICE="1" +if [[ -f /run/systemd/shutdown/scheduled ]]; then + SERVICE="1" fi case "$1" in diff --git a/zram-config-shutdown.service b/zram-config-shutdown.service deleted file mode 100644 index df46d04..0000000 --- a/zram-config-shutdown.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=zram-config-shutdown -Documentation=https://github.com/ecdye/zram-config/blob/main/README.md - -[Service] -Type=oneshot -TimeoutSec=300 -RemainAfterExit=yes -ExecStop=/usr/local/sbin/zram-config "stop" "shutdown" - -[Install] -WantedBy=basic.target diff --git a/zram-config.service b/zram-config.service index c1aa996..69fcfb1 100644 --- a/zram-config.service +++ b/zram-config.service @@ -1,13 +1,16 @@ [Unit] Description=zram-config Documentation=https://github.com/ecdye/zram-config/blob/main/README.md +Requires=local-fs.target After=local-fs.target -Before=zram-config-shutdown.service [Service] Type=exec -TimeoutSec=300 +TimeoutSec=120 +RemainAfterExit=yes ExecStart=/usr/local/sbin/zram-config "start" +ExecReload=/usr/local/sbin/zram-config "start" +ExecStop=/usr/local/sbin/zram-config "stop" [Install] -WantedBy=basic.target +WantedBy=multi-user.target diff --git a/ztab b/ztab index a047784..3ab94f5 100644 --- a/ztab +++ b/ztab @@ -36,9 +36,9 @@ # If you need multiple zram swaps or zram directories, just create another entry # in this file. To do this simply add the new entries to this file, if you need # to edit an active zram device you must stop zram with -# 'sudo zram-config "stop"' and then edit any entries you need to. Once -# finished, start zram using 'sudo systemctl start zram-config.service' which -# will only add the new entries if zram is already running. +# 'sudo systemctl stop zram-config.service' and then edit any entries you need +# to. Once finished, start zram using 'sudo systemctl start zram-config.service' +# which will only add the new entries if zram is already running. # swap alg mem_limit disk_size swap_priority page-cluster swappiness swap lzo-rle 250M 750M 75 0 150