zram: properly implement systemd service
PATCH 1/1 zram: properly implement systemd service
From: Stefan Agner <[email protected]>
The systemd service points ot a script which is not installed by zram or any of its dependencies. It seems that the service got migrated without the necessary script.
The sysvinit script seems rather dated and initializes multiple zram instances to support multiprocessor systems. This is no longer necessary with modern implementations as newer kernel version support multiple streams by default.
Create a modern implementation based on Fedoras zram package. Make use of systemd swap unit files instead of enabling swap directly.
This removes the need for util-linux-swaponoff (since swap is now handled by systemd, which presumably depends on swaponoff). However, it adds the dependency to util-linux for zramctl.
--- .../recipes-extended/zram/zram/dev-zram0.swap | 10 ++++ .../zram/zram/zram-swap-deinit | 19 +++++++ .../recipes-extended/zram/zram/zram-swap-init | 26 ++++++++++ .../zram/zram/zram-swap.service | 10 ++++ .../recipes-extended/zram/zram/zram.service | 12 ----- meta-oe/recipes-extended/zram/zram/zramstop | 5 ++ meta-oe/recipes-extended/zram/zram_0.1.bb | 33 ------------- meta-oe/recipes-extended/zram/zram_0.2.bb | 49 +++++++++++++++++++ 8 files changed, 119 insertions(+), 45 deletions(-) create mode 100644 meta-oe/recipes-extended/zram/zram/dev-zram0.swap create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-deinit create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-init create mode 100644 meta-oe/recipes-extended/zram/zram/zram-swap.service delete mode 100644 meta-oe/recipes-extended/zram/zram/zram.service create mode 100644 meta-oe/recipes-extended/zram/zram/zramstop delete mode 100644 meta-oe/recipes-extended/zram/zram_0.1.bb create mode 100644 meta-oe/recipes-extended/zram/zram_0.2.bb
meta-oe/recipes-extended/zram/zram/dev-zram0.swap new file +10 -0
@@ -0,0 +1,10 @@ +[Unit] +Description=Enable compressed swap in memory using zram +Requires=zram-swap.service +After=zram-swap.service + +[Swap] +What=/dev/zram0 + +[Install] +WantedBy=swap.target
meta-oe/recipes-extended/zram/zram/zram-swap-deinit new file +19 -0
@@ -0,0 +1,19 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-deinit <device>" + exit 1 +fi + +sysblockdev=/sys/block/$(basename $device) +if [ ! -d $sysblockdev ]; then + echo "Block device not found in sysfs" + exit 1 +fi + +# zramctl -r is not suitable as it also removes the actual device. Recreating +# it is non-trivial, especially if not /dev/zram0 is used... +echo 1 > ${sysblockdev}/reset +
meta-oe/recipes-extended/zram/zram/zram-swap-init new file +26 -0
@@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-init <device>" + exit 1 +fi + +# Allocate zram to be size of actual system memory +# Note: zram is only allocated when used. When swapped pages compress with a +# a 2:1 ratio zram will require 50% of system memory (while allowing to use +# 150% memory). +ZRAM_SIZE_PERCENT=100 +ZRAM_ALGORITHM=lz4 + +[ -f /etc/default/zram ] && ./etc/default/zram || true + +memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') +memzram=$(($memtotal*${ZRAM_SIZE_PERCENT}/100)) + +# Try loading zram module +modprobe -q zram || true + +zramctl -a ${ZRAM_ALGORITHM} -s ${memzram}KB $device +mkswap -L "zram-swap" $device
meta-oe/recipes-extended/zram/zram/zram-swap.service new file +10 -0
@@ -0,0 +1,10 @@ +[Unit] +Description=Create compressed swap in memory using zram +DefaultDependencies=no + +[Service] +Type=oneshot +RemainAfterExit=yes +TimeoutStartSec=30sec +ExecStart=/usr/libexec/zram-swap-init /dev/zram0 +ExecStop=/usr/libexec/zram-swap-deinit /dev/zram0
meta-oe/recipes-extended/zram/zram/zram.service deleted +0 -12
@@ -1,12 +0,0 @@ -[Unit] -Description=Enable zram compressed in-memory swap. -After=multi-user.target - -[Service] -RemainAfterExit=yes -ExecStart=/usr/bin/zram-load.sh --load -ExecStop=/usr/bin/zram-load.sh --unload -Type=oneshot - -[Install] -WantedBy=multi-user.target
meta-oe/recipes-extended/zram/zram/zramstop new file +5 -0
@@ -0,0 +1,5 @@ +#!/bin/sh + +for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do + swapoff "$i" && zramctl --reset "$i" +done
meta-oe/recipes-extended/zram/zram_0.1.bb deleted +0 -33
@@ -1,33 +0,0 @@ -SUMMARY = "Linux zram compressed in-memory swap" -LICENSE = "MIT" -LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" - -inherit update-rc.d systemd - -RDEPENDS_${PN} = "util-linux-swaponoff kmod" -RRECOMMENDS_${PN} = "kernel-module-zram" - -PR = "r3" - -SRC_URI = " \ - file://init \ - file://zram.service \ -" - -do_install () { - # Sysvinit - install -d ${D}${sysconfdir}/init.d - install -m 0755 ${WORKDIR}/init ${D}${sysconfdir}/init.d/zram - - install -d ${D}${systemd_unitdir}/system - install -m 0644 ${WORKDIR}/zram.service ${D}${systemd_unitdir}/system -} - -FILES_${PN} = "${sysconfdir}" -INITSCRIPT_NAME = "zram" -INITSCRIPT_PARAMS = "start 05 2 3 4 5 . stop 22 0 1 6 ." - -RPROVIDES_${PN} += "${PN}-systemd" -RREPLACES_${PN} += "${PN}-systemd" -RCONFLICTS_${PN} += "${PN}-systemd" -SYSTEMD_SERVICE_${PN} = "zram.service"
meta-oe/recipes-extended/zram/zram_0.2.bb new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.20.1
Hi Stefan
Thanks for the patch, it would be nicer if hardcoded paths were inserted during do_install e.g. using ${libexecdir} instead of /usr/libexec etc.
Quoted 260 lines · Stefan Agner
From: Stefan Agner <[email protected]>
The systemd service points ot a script which is not installed by zram or any of its dependencies. It seems that the service got migrated without the necessary script.
The sysvinit script seems rather dated and initializes multiple zram instances to support multiprocessor systems. This is no longer necessary with modern implementations as newer kernel version support multiple streams by default.
Create a modern implementation based on Fedoras zram package. Make use of systemd swap unit files instead of enabling swap directly.
This removes the need for util-linux-swaponoff (since swap is now handled by systemd, which presumably depends on swaponoff). However, it adds the dependency to util-linux for zramctl.
Signed-off-by Stefan Agner <[email protected]>--- .../recipes-extended/zram/zram/dev-zram0.swap | 10 ++++ .../zram/zram/zram-swap-deinit | 19 +++++++ .../recipes-extended/zram/zram/zram-swap-init | 26 ++++++++++ .../zram/zram/zram-swap.service | 10 ++++ .../recipes-extended/zram/zram/zram.service | 12 ----- meta-oe/recipes-extended/zram/zram/zramstop | 5 ++ meta-oe/recipes-extended/zram/zram_0.1.bb | 33 ------------- meta-oe/recipes-extended/zram/zram_0.2.bb | 49 +++++++++++++++++++ 8 files changed, 119 insertions(+), 45 deletions(-) create mode 100644 meta-oe/recipes-extended/zram/zram/dev-zram0.swap create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-deinit create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-init create mode 100644 meta-oe/recipes-extended/zram/zram/zram-swap.service delete mode 100644 meta-oe/recipes-extended/zram/zram/zram.service create mode 100644 meta-oe/recipes-extended/zram/zram/zramstop delete mode 100644 meta-oe/recipes-extended/zram/zram_0.1.bb create mode 100644 meta-oe/recipes-extended/zram/zram_0.2.bbmeta-oe/recipes-extended/zram/zram/dev-zram0.swap new file +10
-0@@ -0,0 +1,10 @@ +[Unit] +Description=Enable compressed swap in memory using zram +Requires=zram-swap.service +After=zram-swap.service + +[Swap] +What=/dev/zram0 + +[Install] +WantedBy=swap.targetmeta-oe/recipes-extended/zram/zram/zram-swap-deinit new file +19
-0@@ -0,0 +1,19 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-deinit <device>" + exit 1 +fi + +sysblockdev=/sys/block/$(basename $device) +if [ ! -d $sysblockdev ]; then + echo "Block device not found in sysfs" + exit 1 +fi + +# zramctl -r is not suitable as it also removes the actual device. Recreating +# it is non-trivial, especially if not /dev/zram0 is used... +echo 1 > ${sysblockdev}/reset +meta-oe/recipes-extended/zram/zram/zram-swap-init new file +26
-0@@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-init <device>" + exit 1 +fi + +# Allocate zram to be size of actual system memory +# Note: zram is only allocated when used. When swapped pages compress with a +# a 2:1 ratio zram will require 50% of system memory (while allowing to use +# 150% memory). +ZRAM_SIZE_PERCENT=100 +ZRAM_ALGORITHM=lz4 + +[ -f /etc/default/zram ] && ./etc/default/zram || true + +memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') +memzram=$(($memtotal*${ZRAM_SIZE_PERCENT}/100)) + +# Try loading zram module +modprobe -q zram || true + +zramctl -a ${ZRAM_ALGORITHM} -s ${memzram}KB $device +mkswap -L "zram-swap" $devicemeta-oe/recipes-extended/zram/zram/zram-swap.service new file +10
-0@@ -0,0 +1,10 @@ +[Unit] +Description=Create compressed swap in memory using zram +DefaultDependencies=no + +[Service] +Type=oneshot +RemainAfterExit=yes +TimeoutStartSec=30sec +ExecStart=/usr/libexec/zram-swap-init /dev/zram0 +ExecStop=/usr/libexec/zram-swap-deinit /dev/zram0meta-oe/recipes-extended/zram/zram/zram.service deleted +0
-12@@ -1,12 +0,0 @@ -[Unit] -Description=Enable zram compressed in-memory swap. -After=multi-user.target - -[Service] -RemainAfterExit=yes -ExecStart=/usr/bin/zram-load.sh --load -ExecStop=/usr/bin/zram-load.sh --unload -Type=oneshot - -[Install] -WantedBy=multi-user.targetmeta-oe/recipes-extended/zram/zram/zramstop new file +5
-0@@ -0,0 +1,5 @@ +#!/bin/sh + +for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do + swapoff "$i" && zramctl --reset "$i" +donemeta-oe/recipes-extended/zram/zram_0.1.bb deleted +0
-33@@ -1,33 +0,0 @@ -SUMMARY = "Linux zram compressed in-memory swap" -LICENSE = "MIT" -LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" - -inherit update-rc.d systemd - -RDEPENDS_${PN} = "util-linux-swaponoff kmod" -RRECOMMENDS_${PN} = "kernel-module-zram" - -PR = "r3" - -SRC_URI = " \ - file://init \ - file://zram.service \ -" - -do_install () { - # Sysvinit - install -d ${D}${sysconfdir}/init.d - install -m 0755 ${WORKDIR}/init ${D}${sysconfdir}/init.d/zram - - install -d ${D}${systemd_unitdir}/system - install -m 0644 ${WORKDIR}/zram.service ${D}${systemd_unitdir}/system -} - -FILES_${PN} = "${sysconfdir}" -INITSCRIPT_NAME = "zram" -INITSCRIPT_PARAMS = "start 05 2 3 4 5 . stop 22 0 1 6 ." - -RPROVIDES_${PN} += "${PN}-systemd" -RREPLACES_${PN} += "${PN}-systemd" -RCONFLICTS_${PN} += "${PN}-systemd" -SYSTEMD_SERVICE_${PN} = "zram.service"meta-oe/recipes-extended/zram/zram_0.2.bb new file +49
-1@@ -0,0 +1,49 @@ +SUMMARY = "Linux zram compressed in-memory swap" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" + +inherit update-rc.d systemd + +RDEPENDS_${PN} = "kmod \ + ${@bb.utils.contains('DISTRO_FEATURES','systemd','util-linux','util-linux-swaponoff',d)}" +RRECOMMENDS_${PN} = "kernel-module-zram" + +PR = "r3" + +SRC_URI = " \ + file://init \ + file://zram-swap-init \ + file://zram-swap-deinit \ + file://zram-swap.service \ + file://dev-zram0.swap \ +" + +do_install () { + # Install systemd related configuration file + if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then + install -d ${D}${sysconfdir}/init.d + install -m 0755 ${WORKDIR}/init ${D}${sysconfdir}/init.d/zram + fi + + if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then + install -d ${D}${libexecdir} + install -m 0755 ${WORKDIR}/zram-swap-init ${D}${libexecdir} + install -m 0755 ${WORKDIR}/zram-swap-deinit ${D}${libexecdir} + install -d ${D}${systemd_unitdir}/system + install -m 0644 ${WORKDIR}/zram-swap.service ${D}${systemd_unitdir}/system + install -m 0644 ${WORKDIR}/dev-zram0.swap ${D}${systemd_unitdir}/system + fi +} + +FILES_${PN} = " \ + ${sysconfdir} \ + ${systemd_unitdir} \ + ${libexecdir} \ +" +INITSCRIPT_NAME = "zram" +INITSCRIPT_PARAMS = "start 05 2 3 4 5 . stop 22 0 1 6 ." + +RPROVIDES_${PN} += "${PN}-systemd" +RREPLACES_${PN} += "${PN}-systemd" +RCONFLICTS_${PN} += "${PN}-systemd" +SYSTEMD_SERVICE_${PN} = "dev-zram0.swap" --2.20.1
--
_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-devel
Hi Khem,
Quoted 5 lines · Khem Raj
Hi Stefan
Thanks for the patch, it would be nicer if hardcoded paths were inserted during
do_installe.g. using ${libexecdir} instead of/usr/libexecetc.
Sure that makes sense! Will send v2.
--
Stefan
Quoted 262 lines · Stefan Agner
Quoted 260 lines
From: Stefan Agner <[email protected]>
The systemd service points ot a script which is not installed by zram or any of its dependencies. It seems that the service got migrated without the necessary script.
The sysvinit script seems rather dated and initializes multiple zram instances to support multiprocessor systems. This is no longer necessary with modern implementations as newer kernel version support multiple streams by default.
Create a modern implementation based on Fedoras zram package. Make use of systemd swap unit files instead of enabling swap directly.
This removes the need for util-linux-swaponoff (since swap is now handled by systemd, which presumably depends on swaponoff). However, it adds the dependency to util-linux for zramctl.
Signed-off-by Stefan Agner <[email protected]>--- .../recipes-extended/zram/zram/dev-zram0.swap | 10 ++++ .../zram/zram/zram-swap-deinit | 19 +++++++ .../recipes-extended/zram/zram/zram-swap-init | 26 ++++++++++ .../zram/zram/zram-swap.service | 10 ++++ .../recipes-extended/zram/zram/zram.service | 12 ----- meta-oe/recipes-extended/zram/zram/zramstop | 5 ++ meta-oe/recipes-extended/zram/zram_0.1.bb | 33 ------------- meta-oe/recipes-extended/zram/zram_0.2.bb | 49 +++++++++++++++++++ 8 files changed, 119 insertions(+), 45 deletions(-) create mode 100644 meta-oe/recipes-extended/zram/zram/dev-zram0.swap create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-deinit create mode 100755 meta-oe/recipes-extended/zram/zram/zram-swap-init create mode 100644 meta-oe/recipes-extended/zram/zram/zram-swap.service delete mode 100644 meta-oe/recipes-extended/zram/zram/zram.service create mode 100644 meta-oe/recipes-extended/zram/zram/zramstop delete mode 100644 meta-oe/recipes-extended/zram/zram_0.1.bb create mode 100644 meta-oe/recipes-extended/zram/zram_0.2.bbmeta-oe/recipes-extended/zram/zram/dev-zram0.swap new file +10
-0@@ -0,0 +1,10 @@ +[Unit] +Description=Enable compressed swap in memory using zram +Requires=zram-swap.service +After=zram-swap.service + +[Swap] +What=/dev/zram0 + +[Install] +WantedBy=swap.targetmeta-oe/recipes-extended/zram/zram/zram-swap-deinit new file +19
-0@@ -0,0 +1,19 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-deinit <device>" + exit 1 +fi + +sysblockdev=/sys/block/$(basename $device) +if [ ! -d $sysblockdev ]; then + echo "Block device not found in sysfs" + exit 1 +fi + +# zramctl -r is not suitable as it also removes the actual device. Recreating +# it is non-trivial, especially if not /dev/zram0 is used... +echo 1 > ${sysblockdev}/reset +meta-oe/recipes-extended/zram/zram/zram-swap-init new file +26
-0@@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +device=$1 +if [ "$device" = "" ]; then + echo "Usage: zram-swap-init <device>" + exit 1 +fi + +# Allocate zram to be size of actual system memory +# Note: zram is only allocated when used. When swapped pages compress with a +# a 2:1 ratio zram will require 50% of system memory (while allowing to use +# 150% memory). +ZRAM_SIZE_PERCENT=100 +ZRAM_ALGORITHM=lz4 + +[ -f /etc/default/zram ] && ./etc/default/zram || true + +memtotal=$(grep MemTotal /proc/meminfo | awk ' { print $2 } ') +memzram=$(($memtotal*${ZRAM_SIZE_PERCENT}/100)) + +# Try loading zram module +modprobe -q zram || true + +zramctl -a ${ZRAM_ALGORITHM} -s ${memzram}KB $device +mkswap -L "zram-swap" $devicemeta-oe/recipes-extended/zram/zram/zram-swap.service new file +10
-0@@ -0,0 +1,10 @@ +[Unit] +Description=Create compressed swap in memory using zram +DefaultDependencies=no + +[Service] +Type=oneshot +RemainAfterExit=yes +TimeoutStartSec=30sec +ExecStart=/usr/libexec/zram-swap-init /dev/zram0 +ExecStop=/usr/libexec/zram-swap-deinit /dev/zram0meta-oe/recipes-extended/zram/zram/zram.service deleted +0
-12@@ -1,12 +0,0 @@ -[Unit] -Description=Enable zram compressed in-memory swap. -After=multi-user.target - -[Service] -RemainAfterExit=yes -ExecStart=/usr/bin/zram-load.sh --load -ExecStop=/usr/bin/zram-load.sh --unload -Type=oneshot - -[Install] -WantedBy=multi-user.targetmeta-oe/recipes-extended/zram/zram/zramstop new file +5
-0@@ -0,0 +1,5 @@ +#!/bin/sh + +for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do + swapoff "$i" && zramctl --reset "$i" +donemeta-oe/recipes-extended/zram/zram_0.1.bb deleted +0
-33@@ -1,33 +0,0 @@ -SUMMARY = "Linux zram compressed in-memory swap" -LICENSE = "MIT" -LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" - -inherit update-rc.d systemd - -RDEPENDS_${PN} = "util-linux-swaponoff kmod" -RRECOMMENDS_${PN} = "kernel-module-zram" - -PR = "r3" - -SRC_URI = " \ - file://init \ - file://zram.service \ -" - -do_install () { - # Sysvinit - install -d ${D}${sysconfdir}/init.d - install -m 0755 ${WORKDIR}/init ${D}${sysconfdir}/init.d/zram - - install -d ${D}${systemd_unitdir}/system - install -m 0644 ${WORKDIR}/zram.service ${D}${systemd_unitdir}/system -} - -FILES_${PN} = "${sysconfdir}" -INITSCRIPT_NAME = "zram" -INITSCRIPT_PARAMS = "start 05 2 3 4 5 . stop 22 0 1 6 ." - -RPROVIDES_${PN} += "${PN}-systemd" -RREPLACES_${PN} += "${PN}-systemd" -RCONFLICTS_${PN} += "${PN}-systemd" -SYSTEMD_SERVICE_${PN} = "zram.service"meta-oe/recipes-extended/zram/zram_0.2.bb new file +49
-1@@ -0,0 +1,49 @@ +SUMMARY = "Linux zram compressed in-memory swap" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" + +inherit update-rc.d systemd + +RDEPENDS_${PN} = "kmod \ + ${@bb.utils.contains('DISTRO_FEATURES','systemd','util-linux','util-linux-swaponoff',d)}" +RRECOMMENDS_${PN} = "kernel-module-zram" + +PR = "r3" + +SRC_URI = " \ + file://init \ + file://zram-swap-init \ + file://zram-swap-deinit \ + file://zram-swap.service \ + file://dev-zram0.swap \ +" + +do_install () { + # Install systemd related configuration file + if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then + install -d ${D}${sysconfdir}/init.d + install -m 0755 ${WORKDIR}/init ${D}${sysconfdir}/init.d/zram + fi + + if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then + install -d ${D}${libexecdir} + install -m 0755 ${WORKDIR}/zram-swap-init ${D}${libexecdir} + install -m 0755 ${WORKDIR}/zram-swap-deinit ${D}${libexecdir} + install -d ${D}${systemd_unitdir}/system + install -m 0644 ${WORKDIR}/zram-swap.service ${D}${systemd_unitdir}/system + install -m 0644 ${WORKDIR}/dev-zram0.swap ${D}${systemd_unitdir}/system + fi +} + +FILES_${PN} = " \ + ${sysconfdir} \ + ${systemd_unitdir} \ + ${libexecdir} \ +" +INITSCRIPT_NAME = "zram" +INITSCRIPT_PARAMS = "start 05 2 3 4 5 . stop 22 0 1 6 ." + +RPROVIDES_${PN} += "${PN}-systemd" +RREPLACES_${PN} += "${PN}-systemd" +RCONFLICTS_${PN} += "${PN}-systemd" +SYSTEMD_SERVICE_${PN} = "dev-zram0.swap" --2.20.1
--
_______________________________________________
Openembedded-devel mailing list
[email protected]
http://lists.openembedded.org/mailman/listinfo/openembedded-devel