summarylogtreecommitdiffstats
path: root/zramctrl
blob: 6f0f5086c5c5ab2db6a97ee2b2b7a290893e4839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
declare -i ZRAM_MAX_PERCENT=50
declare -i RAM_PERCENT=0 SIZE=0

if [ -f /etc/zramswap.conf ]; then
	source /etc/zramswap.conf
fi

start() {
	local Item Multiplier AVAILABLE_ALGO
	local -i Amount RAM_TOTAL_BYTES ZRAM_SIZE ZRAM_SIZE_REQ FOUND_ALGO

	test $RAM_PERCENT -eq 0 && RAM_PERCENT=20
	test $RAM_PERCENT -gt $ZRAM_MAX_PERCENT && RAM_PERCENT=$ZRAM_MAX_PERCENT
	# echo $RAM_PERCENT

	# Grab max memory
	while read -r Item Amount Multiplier; do
		test "$Item" != "MemTotal:" && continue
		# echo "|$Item|$Amount|$Multiplier|"
		case $Multiplier in
			kB)	RAM_TOTAL_BYTES=$((Amount * 1024)) ;;
			mB)	RAM_TOTAL_BYTES=$((Amount * (1024**2) )) ;;
			gB)	RAM_TOTAL_BYTES=$((Amount * (1024**3) )) ;;
			*)	RAM_TOTAL_BYTES=$Amount ;;
		esac
		# echo "$RAM_TOTAL_BYTES $RAM_PERCENT"
	done </proc/meminfo

	# SIZE=8
	# Manual size config?
	if test $SIZE -gt 0; then
		case "${SIZE_TYPE:-GiB}" in
			KiB)	ZRAM_SIZE_REQ=$((SIZE * 1024)) ;;
			MiB)	ZRAM_SIZE_REQ=$((SIZE * (1024**2) )) ;;
			GiB)	ZRAM_SIZE_REQ=$((SIZE * (1024**3) )) ;;
			*)		ZRAM_SIZE_REQ=$SIZE ;;
		esac
		if test $ZRAM_SIZE_REQ -gt $((RAM_TOTAL_BYTES * ZRAM_MAX_PERCENT / 100)); then
			ZRAM_SIZE=$((RAM_TOTAL_BYTES * ZRAM_MAX_PERCENT / 100))
			echo "Using: $ZRAM_SIZE Bytes (Clipped at $ZRAM_MAX_PERCENT% of max.)"
		else
			ZRAM_SIZE=$ZRAM_SIZE_REQ
			echo "Using: $ZRAM_SIZE Bytes ($SIZE ${SIZE_TYPE:-GiB})"
		fi
	else
		ZRAM_SIZE=$((RAM_TOTAL_BYTES * RAM_PERCENT / 100))
		echo "Using: $ZRAM_SIZE Bytes ($RAM_PERCENT% of max.)"
	fi

	# Load the zram module
	modprobe zram
	if test -n "$ZRAM_COMPRESSION_ALGO"; then
		# Iterate over available compression algorithms.
		for AVAILABLE_ALGO in $(</sys/block/zram0/comp_algorithm); do
			# Remove brackets from active one
			AVAILABLE_ALGO=${AVAILABLE_ALGO#[}
			AVAILABLE_ALGO=${AVAILABLE_ALGO%]}
			# echo "$AVAILABLE_ALGO"
			if test "$ZRAM_COMPRESSION_ALGO" = "$AVAILABLE_ALGO"; then
				FOUND_ALGO=1
				break
			fi
		done
		# Only change algorithm when available.
		if test $FOUND_ALGO -eq 1; then
			echo "$ZRAM_COMPRESSION_ALGO" > /sys/block/zram0/comp_algorithm
		fi
	fi

	# Setup the ZRam swap.
	echo $ZRAM_SIZE > /sys/block/zram0/disksize
	mkswap /dev/zram0 -L zram0
	swapon -p 100 --discard /dev/zram0
}

stop() {
  swapoff /dev/zram0
  rmmod zram
}

case $1 in
  start|stop) "$1" ;;
esac