Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / zram / zram_lib.sh
blob9e73a4fb9b0aa9b2a2e81368badfbe278876695d
1 #!/bin/sh
2 # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
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.
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.
14 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
15 # Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
17 MODULE=0
18 dev_makeswap=-1
19 dev_mounted=-1
21 # Kselftest framework requirement - SKIP code is 4.
22 ksft_skip=4
24 trap INT
26 check_prereqs()
28 local msg="skip all tests:"
29 local uid=$(id -u)
31 if [ $uid -ne 0 ]; then
32 echo $msg must be run as root >&2
33 exit $ksft_skip
37 zram_cleanup()
39 echo "zram cleanup"
40 local i=
41 for i in $(seq 0 $dev_makeswap); do
42 swapoff /dev/zram$i
43 done
45 for i in $(seq 0 $dev_mounted); do
46 umount /dev/zram$i
47 done
49 for i in $(seq 0 $(($dev_num - 1))); do
50 echo 1 > /sys/block/zram${i}/reset
51 rm -rf zram$i
52 done
56 zram_unload()
58 if [ $MODULE -ne 0 ] ; then
59 echo "zram rmmod zram"
60 rmmod zram > /dev/null 2>&1
64 zram_load()
66 # check zram module exists
67 MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
68 if [ -f $MODULE_PATH ]; then
69 MODULE=1
70 echo "create '$dev_num' zram device(s)"
71 modprobe zram num_devices=$dev_num
72 if [ $? -ne 0 ]; then
73 echo "failed to insert zram module"
74 exit 1
77 dev_num_created=$(ls /dev/zram* | wc -w)
79 if [ "$dev_num_created" -ne "$dev_num" ]; then
80 echo "unexpected num of devices: $dev_num_created"
81 ERR_CODE=-1
82 else
83 echo "zram load module successful"
85 elif [ -b /dev/zram0 ]; then
86 echo "/dev/zram0 device file found: OK"
87 else
88 echo "ERROR: No zram.ko module or no /dev/zram0 device found"
89 echo "$TCID : CONFIG_ZRAM is not set"
90 exit 1
94 zram_max_streams()
96 echo "set max_comp_streams to zram device(s)"
98 local i=0
99 for max_s in $zram_max_streams; do
100 local sys_path="/sys/block/zram${i}/max_comp_streams"
101 echo $max_s > $sys_path || \
102 echo "FAIL failed to set '$max_s' to $sys_path"
103 sleep 1
104 local max_streams=$(cat $sys_path)
106 [ "$max_s" -ne "$max_streams" ] && \
107 echo "FAIL can't set max_streams '$max_s', get $max_stream"
109 i=$(($i + 1))
110 echo "$sys_path = '$max_streams' ($i/$dev_num)"
111 done
113 echo "zram max streams: OK"
116 zram_compress_alg()
118 echo "test that we can set compression algorithm"
120 local algs=$(cat /sys/block/zram0/comp_algorithm)
121 echo "supported algs: $algs"
122 local i=0
123 for alg in $zram_algs; do
124 local sys_path="/sys/block/zram${i}/comp_algorithm"
125 echo "$alg" > $sys_path || \
126 echo "FAIL can't set '$alg' to $sys_path"
127 i=$(($i + 1))
128 echo "$sys_path = '$alg' ($i/$dev_num)"
129 done
131 echo "zram set compression algorithm: OK"
134 zram_set_disksizes()
136 echo "set disk size to zram device(s)"
137 local i=0
138 for ds in $zram_sizes; do
139 local sys_path="/sys/block/zram${i}/disksize"
140 echo "$ds" > $sys_path || \
141 echo "FAIL can't set '$ds' to $sys_path"
143 i=$(($i + 1))
144 echo "$sys_path = '$ds' ($i/$dev_num)"
145 done
147 echo "zram set disksizes: OK"
150 zram_set_memlimit()
152 echo "set memory limit to zram device(s)"
154 local i=0
155 for ds in $zram_mem_limits; do
156 local sys_path="/sys/block/zram${i}/mem_limit"
157 echo "$ds" > $sys_path || \
158 echo "FAIL can't set '$ds' to $sys_path"
160 i=$(($i + 1))
161 echo "$sys_path = '$ds' ($i/$dev_num)"
162 done
164 echo "zram set memory limit: OK"
167 zram_makeswap()
169 echo "make swap with zram device(s)"
170 local i=0
171 for i in $(seq 0 $(($dev_num - 1))); do
172 mkswap /dev/zram$i > err.log 2>&1
173 if [ $? -ne 0 ]; then
174 cat err.log
175 echo "FAIL mkswap /dev/zram$1 failed"
178 swapon /dev/zram$i > err.log 2>&1
179 if [ $? -ne 0 ]; then
180 cat err.log
181 echo "FAIL swapon /dev/zram$1 failed"
184 echo "done with /dev/zram$i"
185 dev_makeswap=$i
186 done
188 echo "zram making zram mkswap and swapon: OK"
191 zram_swapoff()
193 local i=
194 for i in $(seq 0 $dev_makeswap); do
195 swapoff /dev/zram$i > err.log 2>&1
196 if [ $? -ne 0 ]; then
197 cat err.log
198 echo "FAIL swapoff /dev/zram$i failed"
200 done
201 dev_makeswap=-1
203 echo "zram swapoff: OK"
206 zram_makefs()
208 local i=0
209 for fs in $zram_filesystems; do
210 # if requested fs not supported default it to ext2
211 which mkfs.$fs > /dev/null 2>&1 || fs=ext2
213 echo "make $fs filesystem on /dev/zram$i"
214 mkfs.$fs /dev/zram$i > err.log 2>&1
215 if [ $? -ne 0 ]; then
216 cat err.log
217 echo "FAIL failed to make $fs on /dev/zram$i"
219 i=$(($i + 1))
220 echo "zram mkfs.$fs: OK"
221 done
224 zram_mount()
226 local i=0
227 for i in $(seq 0 $(($dev_num - 1))); do
228 echo "mount /dev/zram$i"
229 mkdir zram$i
230 mount /dev/zram$i zram$i > /dev/null || \
231 echo "FAIL mount /dev/zram$i failed"
232 dev_mounted=$i
233 done
235 echo "zram mount of zram device(s): OK"