zram_lib.sh 4.4 KB

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