[packages/zram-init] sysconfig, init script and config-driven systemd unit; rel 2

arekm arekm at pld-linux.org
Mon Sep 14 17:10:57 CEST 2026


commit 1661a7a72b7f781e12e33eeaf22e6a1efa6c44b3
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Mon Sep 14 16:30:08 2026 +0200

    sysconfig, init script and config-driven systemd unit; rel 2
    
    Also fix MODIR_PLAIN: the locale dir baked into the script pointed at
    /usr/local, so the de/fr translations never loaded.

 zram-init.init      | 260 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 zram-init.service   |  16 ++++
 zram-init.spec      |  41 ++++++++-
 zram-init.sysconfig |  87 ++++++++++++++++++
 4 files changed, 400 insertions(+), 4 deletions(-)
---
diff --git a/zram-init.spec b/zram-init.spec
index 1f464f3..8b56663 100644
--- a/zram-init.spec
+++ b/zram-init.spec
@@ -1,14 +1,26 @@
 Summary:	A wrapper script for the zram linux kernel module
 Name:		zram-init
 Version:	13.3
-Release:	1
+Release:	2
 License:	GPL v2
 Group:		Applications
 Source0:	https://github.com/vaeth/zram-init/archive/v%{version}/%{name}-%{version}.tar.gz
 # Source0-md5:	957f391e485bc3327fafccc0b0e28fdb
+Source1:	%{name}.init
+Source2:	%{name}.sysconfig
+Source3:	%{name}.service
 URL:		https://github.com/vaeth/zram-init/
 BuildRequires:	gettext-tools
 BuildRequires:	rpmbuild(macros) >= 1.644
+Requires(post,preun):	/sbin/chkconfig
+Requires(post,preun,postun):	systemd-units
+Requires:	module-init-tools
+Requires:	rc-scripts
+Requires:	systemd-units
+Requires:	util-linux
+Suggests:	btrfs-progs
+Suggests:	e2fsprogs
+Suggests:	xfsprogs
 BuildArch:	noarch
 BuildRoot:	%{tmpdir}/%{name}-%{version}-root-%(id -u -n)
 
@@ -28,12 +40,18 @@ ZSH completion for zram-init command line.
 %prep
 %setup -q
 
-%{__sed} -i -e '1s,/usr/bin/env sh,%{__sh},' sbin/zram-init.in
-
-%{__make}
+%build
+# the Makefile rewrites the shebang to whatever "command -v sh" finds
+# MODIR_PLAIN is the locale path baked into the script; it defaults to
+# $PREFIX/share/locale with PREFIX=/usr/local, so gettext would look in
+# a directory the package never installs into
+SHEBANG='#!%{__sh}' \
+%{__make} \
+	MODIR_PLAIN=%{_localedir}
 
 %install
 rm -rf $RPM_BUILD_ROOT
+install -d $RPM_BUILD_ROOT/etc/{rc.d/init.d,sysconfig}
 
 %{__make} install \
 	BINDIR=$RPM_BUILD_ROOT/sbin \
@@ -49,14 +67,26 @@ rm -rf $RPM_BUILD_ROOT
 	SYSTEMDDIR=$RPM_BUILD_ROOT%{systemdunitdir} \
 	OPENRC=FALSE
 
+install -p %{SOURCE1} $RPM_BUILD_ROOT/etc/rc.d/init.d/%{name}
+cp -p %{SOURCE2} $RPM_BUILD_ROOT/etc/sysconfig/%{name}
+cp -p %{SOURCE3} $RPM_BUILD_ROOT%{systemdunitdir}/%{name}.service
+
 %find_lang %{name}
 
 %clean
 rm -rf $RPM_BUILD_ROOT
 
 %post
+/sbin/chkconfig --add %{name}
 %systemd_reload
 
+%preun
+if [ "$1" = "0" ]; then
+	%service -q %{name} stop
+	/sbin/chkconfig --del %{name}
+fi
+%systemd_preun %{name}.service
+
 %postun
 %systemd_reload
 
@@ -64,7 +94,10 @@ rm -rf $RPM_BUILD_ROOT
 %defattr(644,root,root,755)
 %doc AUTHORS ChangeLog README.md
 %attr(755,root,root) /sbin/zram-init
+%attr(754,root,root) /etc/rc.d/init.d/%{name}
 %config(noreplace) %verify(not md5 mtime size) /etc/modprobe.d/zram.conf
+%config(noreplace) %verify(not md5 mtime size) /etc/sysconfig/%{name}
+%{systemdunitdir}/%{name}.service
 %{systemdunitdir}/zram_btrfs.service
 %{systemdunitdir}/zram_swap.service
 %{systemdunitdir}/zram_tmp.service
diff --git a/zram-init.init b/zram-init.init
new file mode 100755
index 0000000..be2ec6a
--- /dev/null
+++ b/zram-init.init
@@ -0,0 +1,260 @@
+#!/bin/sh
+#
+# zram-init	Set up zRAM block devices as swap and/or filesystems
+#
+# chkconfig:	- 01 99
+#
+# description:	Compressed block devices in RAM, used as swap or carrying \
+#		a filesystem, as configured in /etc/sysconfig/zram-init.
+#
+# config:	/etc/sysconfig/zram-init
+#
+
+# Source function library
+. /etc/rc.d/init.d/functions
+
+# Get service config - may override defaults
+[ -f /etc/sysconfig/zram-init ] && . /etc/sysconfig/zram-init
+
+: ${num_devices:=0}
+
+case "$num_devices" in
+*[!0-9]*)
+	nls "Invalid num_devices in %s: %s" "/etc/sysconfig/zram-init" "$num_devices" >&2
+	exit 6
+	;;
+esac
+
+if [ "$num_devices" -eq 0 ]; then
+	case "$1" in
+	start|stop|restart|try-restart|force-reload)
+		exit 0
+		;;
+	esac
+fi
+
+# Load the settings of device $1 into unnumbered variables. Returns non-zero
+# for a device the admin left unconfigured, which the callers skip.
+zram_getconf() {
+	eval "fstype=\${type$1-}	size=\${size$1-}
+		mlim=\${mlim$1-}	back=\${back$1-}
+		icmp=\${icmp$1-}	idle=\${idle$1-}
+		wlim=\${wlim$1-}	algo=\${algo$1-}
+		para=\${para$1-}	flag=\${flag$1-}
+		blck=\${blck$1-}	irat=\${irat$1-}
+		inod=\${inod$1-}	owgr=\${owgr$1-}
+		mode=\${mode$1-}	labl=\${labl$1-}
+		uuid=\${uuid$1-}	opts=\${opts$1-}
+		notr=\${notr$1-}	args=\${args$1-}"
+
+	case "$fstype" in
+	swap|-|/*)
+		;;
+	*)
+		return 1
+		;;
+	esac
+
+	case "$size" in
+	''|0|*[!0-9]*)
+		return 1
+		;;
+	esac
+}
+
+zram_start_dev() {
+	local dev="$1" rc
+
+	set -- -d "$dev" -D "$num_devices" \
+		${mlim:+-S "$mlim"} ${back:+-b "$back"} \
+		${icmp:+-I} ${idle:+-w} ${wlim:+-W "$wlim"} \
+		${algo:+-a "$algo"} ${para:+-A "$para"} \
+		${labl:+-L "$labl"} ${uuid:+-U "$uuid"} ${notr:+-T} \
+		${blck:+-B "$blck"} ${irat:+-i "$irat"} ${inod:+-N "$inod"}
+	# args* may carry several quoted arguments, so it has to be re-parsed
+	[ -z "${args:++}" ] || eval 'set -- "$@" '"$args"
+
+	case "$fstype" in
+	swap)
+		show "Setting up zram%s as swap (%s MB)" "$dev" "$size"
+		busy
+		/sbin/zram-init ${flag:+-p "$flag"} "$@" -- "$size"
+		rc=$?
+		;;
+	-)
+		show "Setting up zram%s as %s (%s MB)" "$dev" "${flag:-ext4}" "$size"
+		busy
+		/sbin/zram-init ${flag:+-t "$flag"} "$@" -- "$size" -
+		rc=$?
+		;;
+	*)
+		show "Mounting zram%s on %s (%s MB, %s)" "$dev" "$fstype" "$size" "${flag:-ext4}"
+		busy
+		/sbin/zram-init ${flag:+-t "$flag"} \
+			${opts:+-o "$opts"} ${owgr:+-c "$owgr"} ${mode:+-m "$mode"} \
+			"$@" -- "$size" "$fstype"
+		rc=$?
+		;;
+	esac
+
+	[ $rc -eq 0 ] && ok || fail
+	return $rc
+}
+
+zram_stop_dev() {
+	local dev="$1" rc
+
+	case "$fstype" in
+	swap)
+		show "Removing swap on zram%s" "$dev"
+		busy
+		/sbin/zram-init -d "$dev" -- 0
+		rc=$?
+		;;
+	-)
+		show "Removing zram%s" "$dev"
+		busy
+		/sbin/zram-init -d "$dev" -- 0 -
+		rc=$?
+		;;
+	*)
+		show "Unmounting zram%s from %s" "$dev" "$fstype"
+		busy
+		/sbin/zram-init -d "$dev" -- 0 "$fstype"
+		rc=$?
+		;;
+	esac
+
+	[ $rc -eq 0 ] && ok || fail
+	return $rc
+}
+
+start() {
+	local i started
+
+	if [ -f /var/lock/subsys/zram-init ]; then
+		msg_already_running "zram-init"
+		return
+	fi
+
+	if is_yes "$load_on_start"; then
+		show "Loading zram kernel module"
+		busy
+		if /sbin/modprobe zram num_devices="$num_devices"; then
+			ok
+		else
+			fail
+			RETVAL=1
+			return
+		fi
+	fi
+
+	i=0
+	started=0
+	while [ $i -lt "$num_devices" ]; do
+		if zram_getconf $i; then
+			if zram_start_dev $i; then
+				started=1
+			else
+				RETVAL=1
+			fi
+		fi
+		i=$((i + 1))
+	done
+
+	# a device that came up has to be reachable by stop even when another
+	# one failed, or it stays allocated until reboot
+	[ $started -eq 0 ] || touch /var/lock/subsys/zram-init
+}
+
+stop() {
+	local i
+
+	if [ ! -f /var/lock/subsys/zram-init ]; then
+		msg_not_running "zram-init"
+		return
+	fi
+
+	# tear down in reverse order: a later device may be mounted below an
+	# earlier one
+	i="$num_devices"
+	while [ $i -gt 0 ]; do
+		i=$((i - 1))
+		if zram_getconf $i; then
+			zram_stop_dev $i || RETVAL=1
+		fi
+	done
+
+	if is_yes "$unload_on_stop"; then
+		show "Unloading zram kernel module"
+		busy
+		/sbin/modprobe -r zram && ok || fail
+	fi
+
+	rm -f /var/lock/subsys/zram-init
+}
+
+condrestart() {
+	if [ ! -f /var/lock/subsys/zram-init ]; then
+		msg_not_running "zram-init"
+		RETVAL=$1
+		return
+	fi
+
+	stop
+	start
+}
+
+status() {
+	local i
+
+	if [ ! -f /var/lock/subsys/zram-init ]; then
+		msg_not_running "zram-init"
+		return 3
+	fi
+
+	i=0
+	while [ $i -lt "$num_devices" ]; do
+		if zram_getconf $i; then
+			if [ -b "/dev/zram$i" ]; then
+				nls "zram%s: %s, %s MB" "$i" "$fstype" "$size"
+			else
+				nls "zram%s: %s, %s MB (device missing)" "$i" "$fstype" "$size"
+			fi
+		fi
+		i=$((i + 1))
+	done
+
+	[ ! -x /sbin/zramctl ] || /sbin/zramctl
+	return 0
+}
+
+RETVAL=0
+# See how we were called.
+case "$1" in
+  start)
+	start
+	;;
+  stop)
+	stop
+	;;
+  restart)
+	stop
+	start
+	;;
+  try-restart)
+	condrestart 0
+	;;
+  force-reload)
+	condrestart 7
+	;;
+  status)
+	status
+	RETVAL=$?
+	;;
+  *)
+	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
+	exit 3
+esac
+
+exit $RETVAL
diff --git a/zram-init.service b/zram-init.service
new file mode 100644
index 0000000..a20cca6
--- /dev/null
+++ b/zram-init.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=zRAM swap and filesystems
+Documentation=man:zram-init(8)
+DefaultDependencies=no
+Conflicts=umount.target
+Before=swap.target local-fs.target umount.target
+ConditionPathExists=/etc/sysconfig/zram-init
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=/etc/rc.d/init.d/zram-init start
+ExecStop=/etc/rc.d/init.d/zram-init stop
+
+[Install]
+WantedBy=local-fs-pre.target
diff --git a/zram-init.sysconfig b/zram-init.sysconfig
new file mode 100644
index 0000000..48946bd
--- /dev/null
+++ b/zram-init.sysconfig
@@ -0,0 +1,87 @@
+# /etc/sysconfig/zram-init - zRAM devices set up by /etc/rc.d/init.d/zram-init
+# and by zram-init.service.
+#
+# The service is not enabled by default. Enable it with:
+#	chkconfig zram-init on		(rc-scripts)
+#	systemctl enable zram-init	(systemd)
+#
+# This package also ships the upstream example units zram_swap.service,
+# zram_tmp.service, zram_var_tmp.service and zram_btrfs.service, which carry
+# their sizes hardcoded in ExecStart and ignore this file. Do not enable them
+# together with zram-init.service: both would claim the same /dev/zram* and
+# the second one to start would fail.
+#
+# See zram-init(8) for the meaning of the underlying options.
+
+# Load the zram kernel module on start. zram-init loads it on its own when a
+# device node is missing, so this only matters if you want the module present
+# before the first device is set up.
+load_on_start=yes
+
+# Unload the zram kernel module on stop.
+unload_on_stop=yes
+
+# How many devices to configure. Only the variables numbered
+# 0 ... num_devices-1 below are used. This count is also passed to modprobe,
+# so it overrides num_devices= from /etc/modprobe.d/zram.conf.
+num_devices=1
+
+# Per-device settings. Replace N with the device number (0, 1, 2, ...):
+#
+# typeN  "swap"  use /dev/zramN as swap
+#        "/dir"  create a filesystem on /dev/zramN and mount it on /dir
+#        "-"     create the filesystem but do not mount it
+#        A device with any other type, or with sizeN empty or 0, is skipped.
+# sizeN  maximal size in MB (uncompressed); this is the only required setting
+# flagN  for type swap: the swap priority (16383 if empty, "-" for the
+#        kernel default of -1); otherwise the filesystem type
+#        (ext2, ext4, btrfs or xfs; ext4 if empty). Anything other than swap
+#        needs the matching mkfs: e2fsprogs, btrfs-progs or xfsprogs.
+# algoN  compression algorithm: zstd, lz4 or lzo.
+#        Ratio: zstd > lzo > lz4. Speed: lz4 > zstd > lzo.
+# paraN  algorithm parameters, e.g. "level=3" (needs util-linux >= 2.41)
+# mlimN  hard limit in MB on the uncompressed memory the device may use
+# lablN  filesystem/swap label
+# uuidN  force this UUID instead of a generated one
+# notrN  nonempty disables discard (TRIM); wastes memory, only useful on
+#        linux-3.14 and earlier
+# backN  backing device for pages zram cannot keep in RAM
+# icmpN  nonempty writes incompressible pages to backN
+# idleN  nonempty writes idle pages to backN
+# wlimN  writeback_limit for idle pages, in units of 4kB (implies idleN)
+# argsN  extra arguments passed to zram-init; eval'd, so quote accordingly
+#
+# Ignored for type swap:
+# optsN  mount options
+# modeN  chmod the mount point to this mode after mounting
+# owgrN  chown the mount point to OWNER or OWNER:GROUP after mounting
+# blckN  block size for ext2/ext4 (1024, 2048 or 4096)
+# iratN  bytes/inode ratio for ext2/ext4
+# inodN  inode count for ext2/ext4
+
+# swap on /dev/zram0, sized at half of the physical memory
+type0=swap
+size0=$(awk '/^MemTotal:/ {print int($2 / 2048)}' /proc/meminfo)
+algo0=zstd
+labl0=zram_swap
+flag0=		# the default priority of 16383 is what you want for zram swap
+
+# /tmp on /dev/zram1. Under systemd, mask tmp.mount before enabling this.
+# Raise num_devices above to 2 to activate it.
+#type1=/tmp
+#size1=2048
+#flag1=ext4
+#algo1=zstd
+#para1=level=3
+#opts1=strictatime,nosuid,nodev,noexec
+#mode1=1777
+#labl1=tmp_dir
+
+# /var/tmp on /dev/zram2. Raise num_devices above to 3 to activate it.
+#type2=/var/tmp
+#size2=1024
+#flag2=ext2
+#algo2=zstd
+#opts2=relatime
+#mode2=1777
+#labl2=var_tmp_dir
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/zram-init.git/commitdiff/1661a7a72b7f781e12e33eeaf22e6a1efa6c44b3



More information about the pld-cvs-commit mailing list