1#!/bin/sh 2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. 3# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz> 4# Author: Alexey Kodanev <alexey.kodanev@oracle.com> 5 6dev_makeswap=-1 7dev_mounted=-1 8 9TST_NEEDS_TMPDIR=1 10TST_SETUP="zram_load" 11TST_CLEANUP="zram_cleanup" 12. tst_test.sh 13 14zram_cleanup() 15{ 16 local i 17 18 for i in $(seq 0 $dev_makeswap); do 19 swapoff /dev/zram$i 20 done 21 22 for i in $(seq 0 $dev_mounted); do 23 umount /dev/zram$i 24 done 25 26 for i in $(seq 0 $(($dev_num - 1))); do 27 echo 1 > /sys/block/zram${i}/reset 28 done 29 30 rmmod zram > /dev/null 2>&1 31} 32 33zram_load() 34{ 35 tst_res TINFO "create '$dev_num' zram device(s)" 36 modprobe zram num_devices=$dev_num || \ 37 tst_brk TBROK "failed to insert zram module" 38 39 dev_num_created=$(ls /dev/zram* | wc -w) 40 41 if [ "$dev_num_created" -ne "$dev_num" ]; then 42 tst_brk TFAIL "unexpected num of devices: $dev_num_created" 43 else 44 tst_res TPASS "test succeeded" 45 fi 46} 47 48zram_max_streams() 49{ 50 if tst_kvcmp -lt "3.15" -o -ge "4.7"; then 51 tst_res TCONF "The device attribute max_comp_streams was"\ 52 "introduced in kernel 3.15 and deprecated in 4.7" 53 return 54 fi 55 56 tst_res TINFO "set max_comp_streams to zram device(s)" 57 58 local i=0 59 60 for max_s in $zram_max_streams; do 61 local sys_path="/sys/block/zram${i}/max_comp_streams" 62 echo $max_s > $sys_path || \ 63 tst_brk TFAIL "failed to set '$max_s' to $sys_path" 64 local max_streams=$(cat $sys_path) 65 66 [ "$max_s" -ne "$max_streams" ] && \ 67 tst_brk TFAIL "can't set max_streams '$max_s', get $max_stream" 68 69 i=$(($i + 1)) 70 tst_res TINFO "$sys_path = '$max_streams' ($i/$dev_num)" 71 done 72 73 tst_res TPASS "test succeeded" 74} 75 76zram_compress_alg() 77{ 78 if tst_kvcmp -lt "3.15"; then 79 tst_res TCONF "device attribute comp_algorithm is"\ 80 "introduced since kernel v3.15, the running kernel"\ 81 "does not support it" 82 return 83 fi 84 85 local i=0 86 87 tst_res TINFO "test that we can set compression algorithm" 88 local algs="$(sed 's/[][]//g' /sys/block/zram0/comp_algorithm)" 89 tst_res TINFO "supported algs: $algs" 90 91 local dev_max=$(($dev_num - 1)) 92 93 for i in $(seq 0 $dev_max); do 94 for alg in $algs; do 95 local sys_path="/sys/block/zram${i}/comp_algorithm" 96 echo "$alg" > $sys_path || \ 97 tst_brk TFAIL "can't set '$alg' to $sys_path" 98 tst_res TINFO "$sys_path = '$alg' ($i/$dev_max)" 99 done 100 done 101 102 tst_res TPASS "test succeeded" 103} 104 105zram_set_disksizes() 106{ 107 local i=0 108 local ds 109 110 tst_res TINFO "set disk size to zram device(s)" 111 for ds in $zram_sizes; do 112 local sys_path="/sys/block/zram${i}/disksize" 113 echo "$ds" > $sys_path || \ 114 tst_brk TFAIL "can't set '$ds' to $sys_path" 115 116 i=$(($i + 1)) 117 tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)" 118 done 119 120 tst_res TPASS "test succeeded" 121} 122 123zram_set_memlimit() 124{ 125 if tst_kvcmp -lt "3.18"; then 126 tst_res TCONF "device attribute mem_limit is"\ 127 "introduced since kernel v3.18, the running kernel"\ 128 "does not support it" 129 return 130 fi 131 132 local i=0 133 local ds 134 135 tst_res TINFO "set memory limit to zram device(s)" 136 137 for ds in $zram_mem_limits; do 138 local sys_path="/sys/block/zram${i}/mem_limit" 139 echo "$ds" > $sys_path || \ 140 tst_brk TFAIL "can't set '$ds' to $sys_path" 141 142 i=$(($i + 1)) 143 tst_res TINFO "$sys_path = '$ds' ($i/$dev_num)" 144 done 145 146 tst_res TPASS "test succeeded" 147} 148 149zram_makeswap() 150{ 151 tst_res TINFO "make swap with zram device(s)" 152 tst_require_cmds mkswap swapon swapoff 153 local i=0 154 155 for i in $(seq 0 $(($dev_num - 1))); do 156 ROD mkswap /dev/zram$i 157 ROD swapon /dev/zram$i 158 tst_res TINFO "done with /dev/zram$i" 159 dev_makeswap=$i 160 done 161 162 tst_res TPASS "making zram swap succeeded" 163} 164 165zram_swapoff() 166{ 167 tst_require_cmds swapoff 168 local i 169 170 for i in $(seq 0 $dev_makeswap); do 171 ROD swapoff /dev/zram$i 172 done 173 dev_makeswap=-1 174 175 tst_res TPASS "swapoff completed" 176} 177 178zram_makefs() 179{ 180 tst_require_cmds mkfs 181 local i=0 182 183 for fs in $zram_filesystems; do 184 # if requested fs not supported default it to ext2 185 tst_supported_fs $fs 2> /dev/null || fs=ext2 186 187 tst_res TINFO "make $fs filesystem on /dev/zram$i" 188 mkfs.$fs /dev/zram$i > err.log 2>&1 189 if [ $? -ne 0 ]; then 190 cat err.log 191 tst_brk TFAIL "failed to make $fs on /dev/zram$i" 192 fi 193 194 i=$(($i + 1)) 195 done 196 197 tst_res TPASS "zram_makefs succeeded" 198} 199 200zram_mount() 201{ 202 local i=0 203 204 for i in $(seq 0 $(($dev_num - 1))); do 205 tst_res TINFO "mount /dev/zram$i" 206 mkdir zram$i 207 ROD mount /dev/zram$i zram$i 208 dev_mounted=$i 209 done 210 211 tst_res TPASS "mount of zram device(s) succeeded" 212} 213 214modinfo zram > /dev/null 2>&1 || 215 tst_brk TCONF "zram not configured in kernel" 216