[oe-commits] [meta-openembedded] 01/03: zram: properly implement systemd service

git at git.openembedded.org git at git.openembedded.org
Mon Oct 7 01:44:38 UTC 2019


This is an automated email from the git hooks/post-receive script.

khem pushed a commit to branch master-next
in repository meta-openembedded.

commit 501df47c331e851925f1965b547e4d888a89555d
Author: Stefan Agner <stefan.agner at toradex.com>
AuthorDate: Sun Oct 6 23:00:32 2019 +0200

    zram: properly implement systemd service
    
    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 <stefan.agner at toradex.com>
    Signed-off-by: Khem Raj <raj.khem at gmail.com>
---
 meta-oe/recipes-extended/zram/zram/dev-zram0.swap  | 10 +++++
 .../recipes-extended/zram/zram/zram-swap-deinit    | 19 ++++++++
 meta-oe/recipes-extended/zram/zram/zram-swap-init  | 26 +++++++++++
 .../recipes-extended/zram/zram/zram-swap.service   | 10 +++++
 meta-oe/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          | 50 ++++++++++++++++++++++
 8 files changed, 120 insertions(+), 45 deletions(-)

diff --git a/meta-oe/recipes-extended/zram/zram/dev-zram0.swap b/meta-oe/recipes-extended/zram/zram/dev-zram0.swap
new file mode 100644
index 0000000..05eae7e
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram/dev-zram0.swap
@@ -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
diff --git a/meta-oe/recipes-extended/zram/zram/zram-swap-deinit b/meta-oe/recipes-extended/zram/zram/zram-swap-deinit
new file mode 100755
index 0000000..46248c4
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram/zram-swap-deinit
@@ -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
+
diff --git a/meta-oe/recipes-extended/zram/zram/zram-swap-init b/meta-oe/recipes-extended/zram/zram/zram-swap-init
new file mode 100755
index 0000000..0643dbc
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram/zram-swap-init
@@ -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
diff --git a/meta-oe/recipes-extended/zram/zram/zram-swap.service b/meta-oe/recipes-extended/zram/zram/zram-swap.service
new file mode 100644
index 0000000..a4dc951
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram/zram-swap.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Create compressed swap in memory using zram
+DefaultDependencies=no
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+TimeoutStartSec=30sec
+ExecStart=@LIBEXECDIR@/zram-swap-init /dev/zram0
+ExecStop=@LIBEXECDIR@/zram-swap-deinit /dev/zram0
diff --git a/meta-oe/recipes-extended/zram/zram/zram.service b/meta-oe/recipes-extended/zram/zram/zram.service
deleted file mode 100644
index 4a19367..0000000
--- a/meta-oe/recipes-extended/zram/zram/zram.service
+++ /dev/null
@@ -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
diff --git a/meta-oe/recipes-extended/zram/zram/zramstop b/meta-oe/recipes-extended/zram/zram/zramstop
new file mode 100644
index 0000000..0777797
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram/zramstop
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+for i in $(grep '^/dev/zram' /proc/swaps | awk '{ print $1 }'); do
+	swapoff "$i" && zramctl --reset "$i"
+done
diff --git a/meta-oe/recipes-extended/zram/zram_0.1.bb b/meta-oe/recipes-extended/zram/zram_0.1.bb
deleted file mode 100644
index dfd75e7..0000000
--- a/meta-oe/recipes-extended/zram/zram_0.1.bb
+++ /dev/null
@@ -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"
diff --git a/meta-oe/recipes-extended/zram/zram_0.2.bb b/meta-oe/recipes-extended/zram/zram_0.2.bb
new file mode 100644
index 0000000..98c47af
--- /dev/null
+++ b/meta-oe/recipes-extended/zram/zram_0.2.bb
@@ -0,0 +1,50 @@
+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/zram-swap.service
+        sed -i -e "s, at LIBEXECDIR@,${libexecdir},g" ${D}${systemd_unitdir}/system/zram-swap.service
+        install -m 0644 ${WORKDIR}/dev-zram0.swap ${D}${systemd_unitdir}/system/dev-zram0.swap
+    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"

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the Openembedded-commits mailing list