Use zswap instead of swap in zram (#2420)

* Use zswap instead of swap in zram

This requires a swap file which will get generated automatically on
startup.

* Fix file size and free disk space comparison

* Set zswap factor to 33%

* Set vm.swappiness to 1

Decrease swapping to a minimum. This is also recommended for database
work loads by the MariaDB documentation. In practice it causes the least
amount of writes to disk when under memory pressure, while still making
swap available when needed.
This commit is contained in:
Stefan Agner 2023-03-22 11:08:05 +01:00 committed by GitHub
parent 5c6330f70f
commit 75dcb932f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 25 deletions

View File

@ -8,6 +8,9 @@ CONFIG_IKCONFIG_PROC=y
CONFIG_MODULE_COMPRESS_NONE=y
CONFIG_ZRAM=y
CONFIG_ZSWAP=y
CONFIG_ZSWAP_DEFAULT_ON=y
CONFIG_ZSWAP_COMPRESSOR_DEFAULT_LZ4=y
CONFIG_ZSMALLOC=y
CONFIG_LRU_GEN=y

View File

@ -0,0 +1,2 @@
# Decrease swapping to a minimum
vm.swappiness=1

View File

@ -1,10 +0,0 @@
[Unit]
Description=HassOS ZRAM swap
Requires=zram-swap.service
After=zram-swap.service
[Swap]
What=/dev/zram0
[Install]
WantedBy=swap.target

View File

@ -0,0 +1,13 @@
[Unit]
Description=HAOS swap
DefaultDependencies=no
Requires=mnt-data.mount
After=mnt-data.mount systemd-growfs@mnt-data.service
Before=mnt-data-swapfile.swap
[Service]
Type=oneshot
ExecStart=/usr/libexec/haos-swapfile
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,8 @@
[Unit]
Description=HAOS swap file
[Swap]
What=/mnt/data/swapfile
[Install]
WantedBy=swap.target

View File

@ -1,15 +0,0 @@
[Unit]
Description=HassOS ZRAM swap
DefaultDependencies=no
Before=dev-zram0.swap
RefuseManualStart=true
RefuseManualStop=true
[Service]
Type=oneshot
ExecStart=/usr/libexec/hassos-zram -t swap
RemainAfterExit=true
StandardOutput=null
[Install]
WantedBy=hassos-zram.target

View File

@ -0,0 +1,21 @@
#!/bin/sh
set -e
swapfile="/mnt/data/swapfile"
swapsize="$(awk '/MemTotal/{ print $2 * 0.33 }' /proc/meminfo)"
if [ ! -s "${swapfile}" ] || [ "$(stat "${swapfile}" -c '%s')" -lt $((swapsize * 1024)) ]; then
# Check free space (in 4k blocks)
if [ "$(stat -f /mnt/data -c '%f')" -lt $((swapsize / 4)) ]; then
echo "[WARNING] Not enough space to allocate swapfile"
exit 1
fi
dd if=/dev/zero of="${swapfile}" bs=1k count="${swapsize}"
fi
if ! swaplabel "${swapfile}" > /dev/null 2>&1; then
/usr/lib/systemd/systemd-makefs swap "${swapfile}"
fi