1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License as
6# published by the Free Software Foundation; either version 2 of
7# the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it would be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
12# GNU General Public License for more details.
13#
14# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
15# Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
16
17MODULE=0
18dev_makeswap=-1
19dev_mounted=-1
20
21trap INT
22
23check_prereqs()
24{
25	local msg="skip all tests:"
26	local uid=$(id -u)
27
28	if [ $uid -ne 0 ]; then
29		echo $msg must be run as root >&2
30		exit 0
31	fi
32}
33
34zram_cleanup()
35{
36	echo "zram cleanup"
37	local i=
38	for i in $(seq 0 $dev_makeswap); do
39		swapoff /dev/zram$i
40	done
41
42	for i in $(seq 0 $dev_mounted); do
43		umount /dev/zram$i
44	done
45
46	for i in $(seq 0 $(($dev_num - 1))); do
47		echo 1 > /sys/block/zram${i}/reset
48		rm -rf zram$i
49	done
50
51}
52
53zram_unload()
54{
55	if [ $MODULE -ne 0 ] ; then
56		echo "zram rmmod zram"
57		rmmod zram > /dev/null 2>&1
58	fi
59}
60
61zram_load()
62{
63	# check zram module exists
64	MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
65	if [ -f $MODULE_PATH ]; then
66		MODULE=1
67		echo "create '$dev_num' zram device(s)"
68		modprobe zram num_devices=$dev_num
69		if [ $? -ne 0 ]; then
70			echo "failed to insert zram module"
71			exit 1
72		fi
73
74		dev_num_created=$(ls /dev/zram* | wc -w)
75
76		if [ "$dev_num_created" -ne "$dev_num" ]; then
77			echo "unexpected num of devices: $dev_num_created"
78			ERR_CODE=-1
79		else
80			echo "zram load module successful"
81		fi
82	elif [ -b /dev/zram0 ]; then
83		echo "/dev/zram0 device file found: OK"
84	else
85		echo "ERROR: No zram.ko module or no /dev/zram0 device found"
86		echo "$TCID : CONFIG_ZRAM is not set"
87		exit 1
88	fi
89}
90
91zram_max_streams()
92{
93	echo "set max_comp_streams to zram device(s)"
94
95	local i=0
96	for max_s in $zram_max_streams; do
97		local sys_path="/sys/block/zram${i}/max_comp_streams"
98		echo $max_s > $sys_path || \
99			echo "FAIL failed to set '$max_s' to $sys_path"
100		sleep 1
101		local max_streams=$(cat $sys_path)
102
103		[ "$max_s" -ne "$max_streams" ] && \
104			echo "FAIL can't set max_streams '$max_s', get $max_stream"
105
106		i=$(($i + 1))
107		echo "$sys_path = '$max_streams' ($i/$dev_num)"
108	done
109
110	echo "zram max streams: OK"
111}
112
113zram_compress_alg()
114{
115	echo "test that we can set compression algorithm"
116
117	local algs=$(cat /sys/block/zram0/comp_algorithm)
118	echo "supported algs: $algs"
119	local i=0
120	for alg in $zram_algs; do
121		local sys_path="/sys/block/zram${i}/comp_algorithm"
122		echo "$alg" >	$sys_path || \
123			echo "FAIL can't set '$alg' to $sys_path"
124		i=$(($i + 1))
125		echo "$sys_path = '$alg' ($i/$dev_num)"
126	done
127
128	echo "zram set compression algorithm: OK"
129}
130
131zram_set_disksizes()
132{
133	echo "set disk size to zram device(s)"
134	local i=0
135	for ds in $zram_sizes; do
136		local sys_path="/sys/block/zram${i}/disksize"
137		echo "$ds" >	$sys_path || \
138			echo "FAIL can't set '$ds' to $sys_path"
139
140		i=$(($i + 1))
141		echo "$sys_path = '$ds' ($i/$dev_num)"
142	done
143
144	echo "zram set disksizes: OK"
145}
146
147zram_set_memlimit()
148{
149	echo "set memory limit to zram device(s)"
150
151	local i=0
152	for ds in $zram_mem_limits; do
153		local sys_path="/sys/block/zram${i}/mem_limit"
154		echo "$ds" >	$sys_path || \
155			echo "FAIL can't set '$ds' to $sys_path"
156
157		i=$(($i + 1))
158		echo "$sys_path = '$ds' ($i/$dev_num)"
159	done
160
161	echo "zram set memory limit: OK"
162}
163
164zram_makeswap()
165{
166	echo "make swap with zram device(s)"
167	local i=0
168	for i in $(seq 0 $(($dev_num - 1))); do
169		mkswap /dev/zram$i > err.log 2>&1
170		if [ $? -ne 0 ]; then
171			cat err.log
172			echo "FAIL mkswap /dev/zram$1 failed"
173		fi
174
175		swapon /dev/zram$i > err.log 2>&1
176		if [ $? -ne 0 ]; then
177			cat err.log
178			echo "FAIL swapon /dev/zram$1 failed"
179		fi
180
181		echo "done with /dev/zram$i"
182		dev_makeswap=$i
183	done
184
185	echo "zram making zram mkswap and swapon: OK"
186}
187
188zram_swapoff()
189{
190	local i=
191	for i in $(seq 0 $dev_makeswap); do
192		swapoff /dev/zram$i > err.log 2>&1
193		if [ $? -ne 0 ]; then
194			cat err.log
195			echo "FAIL swapoff /dev/zram$i failed"
196		fi
197	done
198	dev_makeswap=-1
199
200	echo "zram swapoff: OK"
201}
202
203zram_makefs()
204{
205	local i=0
206	for fs in $zram_filesystems; do
207		# if requested fs not supported default it to ext2
208		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
209
210		echo "make $fs filesystem on /dev/zram$i"
211		mkfs.$fs /dev/zram$i > err.log 2>&1
212		if [ $? -ne 0 ]; then
213			cat err.log
214			echo "FAIL failed to make $fs on /dev/zram$i"
215		fi
216		i=$(($i + 1))
217		echo "zram mkfs.$fs: OK"
218	done
219}
220
221zram_mount()
222{
223	local i=0
224	for i in $(seq 0 $(($dev_num - 1))); do
225		echo "mount /dev/zram$i"
226		mkdir zram$i
227		mount /dev/zram$i zram$i > /dev/null || \
228			echo "FAIL mount /dev/zram$i failed"
229		dev_mounted=$i
230	done
231
232	echo "zram mount of zram device(s): OK"
233}
234