xref: /linux/tools/testing/selftests/zram/zram_lib.sh (revision 8c994eff8fcfe8ecb1f1dbebed25b4d7bb75be12)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
4#
5# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
6# Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
7
8dev_makeswap=-1
9dev_mounted=-1
10dev_start=0
11dev_end=-1
12module_load=-1
13sys_control=-1
14# Kselftest framework requirement - SKIP code is 4.
15ksft_skip=4
16kernel_version=`uname -r | cut -d'.' -f1,2`
17kernel_major=${kernel_version%.*}
18kernel_minor=${kernel_version#*.}
19
20trap INT
21
22check_prereqs()
23{
24	local msg="skip all tests:"
25	local uid=$(id -u)
26
27	if [ $uid -ne 0 ]; then
28		echo $msg must be run as root >&2
29		exit $ksft_skip
30	fi
31}
32
33kernel_gte()
34{
35	major=${1%.*}
36	minor=${1#*.}
37
38	if [ $kernel_major -gt $major ]; then
39		return 0
40	elif [[ $kernel_major -eq $major && $kernel_minor -ge $minor ]]; then
41		return 0
42	fi
43
44	return 1
45}
46
47zram_cleanup()
48{
49	echo "zram cleanup"
50	local i=
51	for i in $(seq $dev_start $dev_makeswap); do
52		swapoff /dev/zram$i
53	done
54
55	for i in $(seq $dev_start $dev_mounted); do
56		umount /dev/zram$i
57	done
58
59	for i in $(seq $dev_start $dev_end); do
60		echo 1 > /sys/block/zram${i}/reset
61		rm -rf zram$i
62	done
63
64	if [ $sys_control -eq 1 ]; then
65		for i in $(seq $dev_start $dev_end); do
66			echo $i > /sys/class/zram-control/hot_remove
67		done
68	fi
69
70	if [ $module_load -eq 1 ]; then
71		rmmod zram > /dev/null 2>&1
72	fi
73}
74
75zram_load()
76{
77	echo "create '$dev_num' zram device(s)"
78
79	# zram module loaded, new kernel
80	if [ -d "/sys/class/zram-control" ]; then
81		echo "zram modules already loaded, kernel supports" \
82			"zram-control interface"
83		dev_start=$(ls /dev/zram* | wc -w)
84		dev_end=$(($dev_start + $dev_num - 1))
85		sys_control=1
86
87		for i in $(seq $dev_start $dev_end); do
88			cat /sys/class/zram-control/hot_add > /dev/null
89		done
90
91		echo "all zram devices (/dev/zram$dev_start~$dev_end" \
92			"successfully created"
93		return 0
94	fi
95
96	# detect old kernel or built-in
97	modprobe zram num_devices=$dev_num
98	if [ ! -d "/sys/class/zram-control" ]; then
99		if grep -q '^zram' /proc/modules; then
100			rmmod zram > /dev/null 2>&1
101			if [ $? -ne 0 ]; then
102				echo "zram module is being used on old kernel" \
103					"without zram-control interface"
104				exit $ksft_skip
105			fi
106		else
107			echo "test needs CONFIG_ZRAM=m on old kernel without" \
108				"zram-control interface"
109			exit $ksft_skip
110		fi
111		modprobe zram num_devices=$dev_num
112	fi
113
114	module_load=1
115	dev_end=$(($dev_num - 1))
116	echo "all zram devices (/dev/zram0~$dev_end) successfully created"
117}
118
119zram_max_streams()
120{
121	echo "set max_comp_streams to zram device(s)"
122
123	kernel_gte 4.7
124	if [ $? -eq 0 ]; then
125		echo "The device attribute max_comp_streams was"\
126		               "deprecated in 4.7"
127		return 0
128	fi
129
130	local i=$dev_start
131	for max_s in $zram_max_streams; do
132		local sys_path="/sys/block/zram${i}/max_comp_streams"
133		echo $max_s > $sys_path || \
134			echo "FAIL failed to set '$max_s' to $sys_path"
135		sleep 1
136		local max_streams=$(cat $sys_path)
137
138		[ "$max_s" -ne "$max_streams" ] && \
139			echo "FAIL can't set max_streams '$max_s', get $max_stream"
140
141		i=$(($i + 1))
142		echo "$sys_path = '$max_streams'"
143	done
144
145	echo "zram max streams: OK"
146}
147
148zram_compress_alg()
149{
150	echo "test that we can set compression algorithm"
151
152	local i=$dev_start
153	local algs=$(cat /sys/block/zram${i}/comp_algorithm)
154	echo "supported algs: $algs"
155
156	for alg in $zram_algs; do
157		local sys_path="/sys/block/zram${i}/comp_algorithm"
158		echo "$alg" >	$sys_path || \
159			echo "FAIL can't set '$alg' to $sys_path"
160		i=$(($i + 1))
161		echo "$sys_path = '$alg'"
162	done
163
164	echo "zram set compression algorithm: OK"
165}
166
167zram_set_disksizes()
168{
169	echo "set disk size to zram device(s)"
170	local i=$dev_start
171	for ds in $zram_sizes; do
172		local sys_path="/sys/block/zram${i}/disksize"
173		echo "$ds" >	$sys_path || \
174			echo "FAIL can't set '$ds' to $sys_path"
175
176		i=$(($i + 1))
177		echo "$sys_path = '$ds'"
178	done
179
180	echo "zram set disksizes: OK"
181}
182
183zram_set_memlimit()
184{
185	echo "set memory limit to zram device(s)"
186
187	local i=$dev_start
188	for ds in $zram_mem_limits; do
189		local sys_path="/sys/block/zram${i}/mem_limit"
190		echo "$ds" >	$sys_path || \
191			echo "FAIL can't set '$ds' to $sys_path"
192
193		i=$(($i + 1))
194		echo "$sys_path = '$ds'"
195	done
196
197	echo "zram set memory limit: OK"
198}
199
200zram_makeswap()
201{
202	echo "make swap with zram device(s)"
203	local i=$dev_start
204	for i in $(seq $dev_start $dev_end); do
205		mkswap /dev/zram$i > err.log 2>&1
206		if [ $? -ne 0 ]; then
207			cat err.log
208			echo "FAIL mkswap /dev/zram$1 failed"
209		fi
210
211		swapon /dev/zram$i > err.log 2>&1
212		if [ $? -ne 0 ]; then
213			cat err.log
214			echo "FAIL swapon /dev/zram$1 failed"
215		fi
216
217		echo "done with /dev/zram$i"
218		dev_makeswap=$i
219	done
220
221	echo "zram making zram mkswap and swapon: OK"
222}
223
224zram_swapoff()
225{
226	local i=
227	for i in $(seq $dev_start $dev_end); do
228		swapoff /dev/zram$i > err.log 2>&1
229		if [ $? -ne 0 ]; then
230			cat err.log
231			echo "FAIL swapoff /dev/zram$i failed"
232		fi
233	done
234	dev_makeswap=-1
235
236	echo "zram swapoff: OK"
237}
238
239zram_makefs()
240{
241	local i=$dev_start
242	for fs in $zram_filesystems; do
243		# if requested fs not supported default it to ext2
244		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
245
246		echo "make $fs filesystem on /dev/zram$i"
247		mkfs.$fs /dev/zram$i > err.log 2>&1
248		if [ $? -ne 0 ]; then
249			cat err.log
250			echo "FAIL failed to make $fs on /dev/zram$i"
251		fi
252		i=$(($i + 1))
253		echo "zram mkfs.$fs: OK"
254	done
255}
256
257zram_mount()
258{
259	local i=0
260	for i in $(seq $dev_start $dev_end); do
261		echo "mount /dev/zram$i"
262		mkdir zram$i
263		mount /dev/zram$i zram$i > /dev/null || \
264			echo "FAIL mount /dev/zram$i failed"
265		dev_mounted=$i
266	done
267
268	echo "zram mount of zram device(s): OK"
269}
270