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>
23 # Kselftest framework requirement - SKIP code is 4.
25 kernel_version=`uname -r | cut -d'.' -f1,2`
26 kernel_major=${kernel_version%.*}
27 kernel_minor=${kernel_version#*.}
33 local msg="skip all tests:"
36 if [ $uid -ne 0 ]; then
37 echo $msg must be run as root >&2
47 if [ $kernel_major -gt $major ]; then
49 elif [[ $kernel_major -eq $major && $kernel_minor -ge $minor ]]; then
60 for i in $(seq $dev_start $dev_makeswap); do
64 for i in $(seq $dev_start $dev_mounted); do
68 for i in $(seq $dev_start $dev_end); do
69 echo 1 > /sys/block/zram${i}/reset
73 if [ $sys_control -eq 1 ]; then
74 for i in $(seq $dev_start $dev_end); do
75 echo $i > /sys/class/zram-control/hot_remove
79 if [ $module_load -eq 1 ]; then
80 rmmod zram > /dev/null 2>&1
86 echo "create '$dev_num' zram device(s)"
88 # zram module loaded, new kernel
89 if [ -d "/sys/class/zram-control" ]; then
90 echo "zram modules already loaded, kernel supports" \
91 "zram-control interface"
92 dev_start=$(ls /dev/zram* | wc -w)
93 dev_end=$(($dev_start + $dev_num - 1))
96 for i in $(seq $dev_start $dev_end); do
97 cat /sys/class/zram-control/hot_add > /dev/null
100 echo "all zram devices (/dev/zram$dev_start~$dev_end" \
101 "successfully created"
105 # detect old kernel or built-in
106 modprobe zram num_devices=$dev_num
107 if [ ! -d "/sys/class/zram-control" ]; then
108 if grep -q '^zram' /proc/modules; then
109 rmmod zram > /dev/null 2>&1
110 if [ $? -ne 0 ]; then
111 echo "zram module is being used on old kernel" \
112 "without zram-control interface"
116 echo "test needs CONFIG_ZRAM=m on old kernel without" \
117 "zram-control interface"
120 modprobe zram num_devices=$dev_num
124 dev_end=$(($dev_num - 1))
125 echo "all zram devices (/dev/zram0~$dev_end) successfully created"
130 echo "set max_comp_streams to zram device(s)"
133 if [ $? -eq 0 ]; then
134 echo "The device attribute max_comp_streams was"\
140 for max_s in $zram_max_streams; do
141 local sys_path="/sys/block/zram${i}/max_comp_streams"
142 echo $max_s > $sys_path || \
143 echo "FAIL failed to set '$max_s' to $sys_path"
145 local max_streams=$(cat $sys_path)
147 [ "$max_s" -ne "$max_streams" ] && \
148 echo "FAIL can't set max_streams '$max_s', get $max_stream"
151 echo "$sys_path = '$max_streams'"
154 echo "zram max streams: OK"
159 echo "test that we can set compression algorithm"
162 local algs=$(cat /sys/block/zram${i}/comp_algorithm)
163 echo "supported algs: $algs"
165 for alg in $zram_algs; do
166 local sys_path="/sys/block/zram${i}/comp_algorithm"
167 echo "$alg" > $sys_path || \
168 echo "FAIL can't set '$alg' to $sys_path"
170 echo "$sys_path = '$alg'"
173 echo "zram set compression algorithm: OK"
178 echo "set disk size to zram device(s)"
180 for ds in $zram_sizes; do
181 local sys_path="/sys/block/zram${i}/disksize"
182 echo "$ds" > $sys_path || \
183 echo "FAIL can't set '$ds' to $sys_path"
186 echo "$sys_path = '$ds'"
189 echo "zram set disksizes: OK"
194 echo "set memory limit to zram device(s)"
197 for ds in $zram_mem_limits; do
198 local sys_path="/sys/block/zram${i}/mem_limit"
199 echo "$ds" > $sys_path || \
200 echo "FAIL can't set '$ds' to $sys_path"
203 echo "$sys_path = '$ds'"
206 echo "zram set memory limit: OK"
211 echo "make swap with zram device(s)"
213 for i in $(seq $dev_start $dev_end); do
214 mkswap /dev/zram$i > err.log 2>&1
215 if [ $? -ne 0 ]; then
217 echo "FAIL mkswap /dev/zram$1 failed"
220 swapon /dev/zram$i > err.log 2>&1
221 if [ $? -ne 0 ]; then
223 echo "FAIL swapon /dev/zram$1 failed"
226 echo "done with /dev/zram$i"
230 echo "zram making zram mkswap and swapon: OK"
236 for i in $(seq $dev_start $dev_end); do
237 swapoff /dev/zram$i > err.log 2>&1
238 if [ $? -ne 0 ]; then
240 echo "FAIL swapoff /dev/zram$i failed"
245 echo "zram swapoff: OK"
251 for fs in $zram_filesystems; do
252 # if requested fs not supported default it to ext2
253 which mkfs.$fs > /dev/null 2>&1 || fs=ext2
255 echo "make $fs filesystem on /dev/zram$i"
256 mkfs.$fs /dev/zram$i > err.log 2>&1
257 if [ $? -ne 0 ]; then
259 echo "FAIL failed to make $fs on /dev/zram$i"
262 echo "zram mkfs.$fs: OK"
269 for i in $(seq $dev_start $dev_end); do
270 echo "mount /dev/zram$i"
272 mount /dev/zram$i zram$i > /dev/null || \
273 echo "FAIL mount /dev/zram$i failed"
277 echo "zram mount of zram device(s): OK"