From 5ff4ea6bc4d2bff3407e5ec509ff779ccf66782c Mon Sep 17 00:00:00 2001
From: Emil Muratov <gpm@hotplug.ru>
Date: Thu, 2 Aug 2018 00:15:44 +0300
Subject: [PATCH] zram-swap: compression algorithm configuration option

Compression algorithms for zram are provided by kernel crypto API, could
be any of [lzo|zl4|deflate|<some_more>] depending on kernel modules.
Compress algo for zram-swap could be defined via 'zram_comp_algo' option
in 'system' section of '/etc/config/system' file, or via cli (for e.x.
with 'uci set system.@System[0].zram_comp_algo=lz4 && uci commit
system'). check available algo's via 'cat /sys/block/zram0
/comp_algorithm'

Signed-off-by: Emil Muratov <gpm@hotplug.ru>
(backported from b9e89adfb7881806d01e3bd259852e352f3b3ce8)
---
 package/system/zram-swap/files/zram.init | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/package/system/zram-swap/files/zram.init b/package/system/zram-swap/files/zram.init
index 63a20479d1..419221b1a3 100644
--- a/package/system/zram-swap/files/zram.init
+++ b/package/system/zram-swap/files/zram.init
@@ -69,6 +69,23 @@ zram_reset()
 	echo "1" >"$proc_entry"
 }
 
+zram_comp_algo()
+{
+	local dev="$1"
+	local zram_comp_algo="$( uci -q get system.@system[0].zram_comp_algo )"
+
+	if [ -z "$zram_comp_algo" ] || [ ! -e /sys/block/$( basename $dev )/comp_algorithm ]; then
+		return 0
+	fi
+
+	if [ `grep -c "$zram_comp_algo" /sys/block/$( basename $dev )/comp_algorithm` -ne 0 ]; then
+		logger -s -t zram_comp_algo -p daemon.debug "Set compression algorithm '$zram_comp_algo' for zram '$dev'"
+		echo $zram_comp_algo > "/sys/block/$( basename $dev )/comp_algorithm"
+	else
+		logger -s -t zram_comp_algo -p daemon.debug "Compression algorithm '$zram_comp_algo' is not supported for '$dev'"
+	fi
+}
+
 list_cpu_idx()
 {
 	# Offset by 1 if /dev/zram0 is in use by /tmp
@@ -108,6 +125,7 @@ start()
 
 		zram_reset "$zram_dev" "enforcing defaults"
 		echo $(( zram_size * 1024 * 1024 )) >"/sys/block/$( basename "$zram_dev" )/disksize"
+		zram_comp_algo "$zram_dev"
 		mkswap "$zram_dev"
 		swapon "$zram_dev"
 	} done
-- 
2.30.2