mkswap01.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. # Copyright (c) 2015 Fujitsu Ltd.
  4. # Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
  5. #
  6. # Test mkswap command with some basic options.
  7. TST_CNT=10
  8. TST_SETUP=setup
  9. TST_TESTFUNC=do_test
  10. TST_NEEDS_ROOT=1
  11. TST_NEEDS_DEVICE=1
  12. TST_NEEDS_CMDS="uuidgen blkid blockdev mkswap"
  13. . tst_test.sh
  14. setup()
  15. {
  16. UUID=`uuidgen`
  17. PAGE_SIZE=`tst_getconf PAGESIZE`
  18. # Here get the size of the device and align it down to be the
  19. # multiple of $PAGE_SIZE and use that as the size for testing.
  20. real_size=`blockdev --getsize64 $TST_DEVICE`
  21. DEVICE_SIZE=$((($real_size/$PAGE_SIZE * $PAGE_SIZE)/1024))
  22. }
  23. check_for_file()
  24. {
  25. local path="$1"
  26. if [ -z "$path" -o -e "$path" ]; then
  27. return
  28. fi
  29. return 1
  30. }
  31. mkswap_verify()
  32. {
  33. local mkswap_op="$1"
  34. local op_arg="$2"
  35. local swapfile="$3"
  36. local dev_file="$5"
  37. local before=`awk '/SwapTotal/ {print $2}' /proc/meminfo`
  38. local swapsize=${4:-$DEVICE_SIZE}
  39. if [ "$mkswap_op" = "-p" ]; then
  40. local pagesize=$op_arg
  41. else
  42. local pagesize=$PAGE_SIZE
  43. fi
  44. if tst_kvcmp -lt "2.6.35" && [ -n "$dev_file" ]; then
  45. tst_res TINFO "Waiting for $dev_file to appear"
  46. tst_sleep 100ms
  47. else
  48. TST_RETRY_FUNC "check_for_file $dev_file" 0
  49. fi
  50. swapon $swapfile 2>/dev/null
  51. if [ $? -ne 0 ]; then
  52. tst_res TINFO "Can not do swapon on $swapfile."
  53. if [ $pagesize -ne $PAGE_SIZE ]; then
  54. tst_res TINFO "Page size specified by 'mkswap -p' \
  55. is not equal to system's page size."
  56. tst_res TINFO "Swapon failed expectedly."
  57. return 0
  58. fi
  59. if [ $swapsize -gt $DEVICE_SIZE ]; then
  60. tst_res TINFO "Device size specified by 'mkswap' \
  61. greater than real size."
  62. tst_res TINFO "Swapon failed expectedly."
  63. return 0
  64. fi
  65. tst_res TINFO "Swapon failed unexpectedly."
  66. return 1
  67. fi
  68. local after=`awk '/SwapTotal/ {print $2}' /proc/meminfo`
  69. local diff=$((after-before))
  70. local filesize=$((swapsize-pagesize/1024))
  71. local ret=0
  72. # In general, the swap increment by doing swapon should be equal to
  73. # the device size minus a page size, however for some kernels, the
  74. # increment we get is not exactly equal to that value, but is equal
  75. # to the value minus an extra page size, e.g. on RHEL5.11GA.
  76. if [ $diff -ne $filesize ] && \
  77. [ $diff -ne $((filesize-pagesize/1024)) ]; then
  78. ret=1
  79. fi
  80. swapoff $swapfile 2>/dev/null
  81. if [ $? -ne 0 ]; then
  82. tst_res TWARN "Can not do swapoff on $swapfile."
  83. fi
  84. return $ret
  85. }
  86. mkswap_test()
  87. {
  88. local mkswap_op="$1"
  89. local op_arg="$2"
  90. local device="$3"
  91. local size="$4"
  92. local dev_file="$5"
  93. local mkswap_cmd="mkswap $mkswap_op $op_arg $TST_DEVICE $size"
  94. ${mkswap_cmd} >temp 2>&1
  95. if [ $? -ne 0 ]; then
  96. grep -q -E "unknown option|invalid option|Usage" temp
  97. if [ $? -eq 0 ]; then
  98. tst_res TCONF "'${mkswap_cmd}' not supported."
  99. return
  100. fi
  101. tst_res TFAIL "'${mkswap_cmd}' failed."
  102. cat temp
  103. return
  104. fi
  105. udevadm trigger --name-match=$TST_DEVICE
  106. if [ -n "$device" ]; then
  107. mkswap_verify "$mkswap_op" "$op_arg" "$device" "$size" "$dev_file"
  108. if [ $? -ne 0 ]; then
  109. tst_res TFAIL "'${mkswap_cmd}' failed, not expected."
  110. return
  111. fi
  112. fi
  113. tst_res TPASS "'${mkswap_cmd}' passed."
  114. }
  115. do_test()
  116. {
  117. case $1 in
  118. 1) mkswap_test "" "" "$TST_DEVICE";;
  119. 2) mkswap_test "" "" "$TST_DEVICE" "$((DEVICE_SIZE-PAGE_SIZE/1024))";;
  120. 3) mkswap_test "-f" "" "$TST_DEVICE" "$((DEVICE_SIZE+PAGE_SIZE/1024))";;
  121. 4) mkswap_test "-c" "" "$TST_DEVICE";;
  122. 5) mkswap_test "-p" "2048" "$TST_DEVICE";;
  123. 6) mkswap_test "-L" "ltp_testswap" "-L ltp_testswap" "" "/dev/disk/by-label/ltp_testswap";;
  124. 7) mkswap_test "-v1" "" "$TST_DEVICE";;
  125. 8) mkswap_test "-U" "$UUID" "-U $UUID" "" "/dev/disk/by-uuid/$UUID";;
  126. 9) mkswap_test "-V";;
  127. 10) mkswap_test "-h";;
  128. esac
  129. }
  130. tst_run