Skip to content

Commit

Permalink
Use systemd for start and stop of zram-config (#78)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ecdye authored Feb 7, 2022
1 parent c85cc84 commit 65403f6
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 48 deletions.
16 changes: 5 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/image.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions uninstall.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 #####"
22 changes: 12 additions & 10 deletions zram-config
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
12 changes: 0 additions & 12 deletions zram-config-shutdown.service

This file was deleted.

9 changes: 6 additions & 3 deletions zram-config.service
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions ztab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 65403f6

Please sign in to comment.