1#!/bin/sh 2# 3# Copyright (c) 2015 Fujitsu Ltd. 4# Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com> 5# 6# This program is free software; you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation; either version 2 of the License, or 9# (at your option) any later version. 10# 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15# 16# Test mkswap command with some basic options. 17# 18 19TST_CNT=10 20TST_SETUP=setup 21TST_TESTFUNC=do_test 22TST_NEEDS_ROOT=1 23TST_NEEDS_TMPDIR=1 24TST_NEEDS_DEVICE=1 25TST_NEEDS_CMDS="uuidgen blkid blockdev mkswap" 26. tst_test.sh 27 28setup() 29{ 30 UUID=`uuidgen` 31 32 PAGE_SIZE=`getconf PAGE_SIZE` 33 34 # Here get the size of the device and align it down to be the 35 # multiple of $PAGE_SIZE and use that as the size for testing. 36 real_size=`blockdev --getsize64 $TST_DEVICE` 37 DEVICE_SIZE=$((($real_size/$PAGE_SIZE * $PAGE_SIZE)/1024)) 38} 39 40wait_for_file() 41{ 42 local path="$1" 43 local retries=10 44 45 if [ -z "$path" ]; then 46 return 47 fi 48 49 while [ $retries -gt 0 ]; do 50 if [ -e "$path" ]; then 51 return 52 fi 53 tst_res TINFO "Waiting for $path to appear" 54 retries=$((retries - 1)) 55 tst_sleep 10ms 56 done 57 58 tst_res TINFO "The file $path haven't appeared" 59} 60 61mkswap_verify() 62{ 63 local mkswap_op="$1" 64 local op_arg="$2" 65 local swapfile="$3" 66 local dev_file="$5" 67 68 local before=`awk '/SwapTotal/ {print $2}' /proc/meminfo` 69 70 local swapsize=${4:-$DEVICE_SIZE} 71 72 if [ "$mkswap_op" = "-p" ]; then 73 local pagesize=$op_arg 74 else 75 local pagesize=$PAGE_SIZE 76 fi 77 78 wait_for_file "$dev_file" 79 80 swapon $swapfile 2>/dev/null 81 82 if [ $? -ne 0 ]; then 83 tst_res TINFO "Can not do swapon on $swapfile." 84 if [ $pagesize -ne $PAGE_SIZE ]; then 85 tst_res TINFO "Page size specified by 'mkswap -p' \ 86is not equal to system's page size." 87 tst_res TINFO "Swapon failed expectedly." 88 return 0 89 fi 90 91 if [ $swapsize -gt $DEVICE_SIZE ]; then 92 tst_res TINFO "Device size specified by 'mkswap' \ 93greater than real size." 94 tst_res TINFO "Swapon failed expectedly." 95 return 0 96 fi 97 98 tst_res TINFO "Swapon failed unexpectedly." 99 return 1 100 fi 101 102 local after=`awk '/SwapTotal/ {print $2}' /proc/meminfo` 103 104 local diff=$((after-before)) 105 local filesize=$((swapsize-pagesize/1024)) 106 107 local ret=0 108 109 # In general, the swap increment by doing swapon should be equal to 110 # the device size minus a page size, however for some kernels, the 111 # increment we get is not exactly equal to that value, but is equal 112 # to the value minus an extra page size, e.g. on RHEL5.11GA. 113 if [ $diff -ne $filesize ] && \ 114 [ $diff -ne $((filesize-pagesize/1024)) ]; then 115 ret=1 116 fi 117 118 swapoff $swapfile 2>/dev/null 119 if [ $? -ne 0 ]; then 120 tst_res TWARN "Can not do swapoff on $swapfile." 121 fi 122 123 return $ret 124} 125 126mkswap_test() 127{ 128 local mkswap_op="$1" 129 local op_arg="$2" 130 local device="$3" 131 local size="$4" 132 local dev_file="$5" 133 134 local mkswap_cmd="mkswap $mkswap_op $op_arg $TST_DEVICE $size" 135 136 ${mkswap_cmd} >temp 2>&1 137 if [ $? -ne 0 ]; then 138 grep -q -E "unknown option|invalid option|Usage" temp 139 if [ $? -eq 0 ]; then 140 tst_res TCONF "'${mkswap_cmd}' not supported." 141 return 142 fi 143 144 tst_res TFAIL "'${mkswap_cmd}' failed." 145 cat temp 146 return 147 fi 148 149 if [ -n "$device" ]; then 150 mkswap_verify "$mkswap_op" "$op_arg" "$device" "$size" "$dev_file" 151 if [ $? -ne 0 ]; then 152 tst_res TFAIL "'${mkswap_cmd}' failed, not expected." 153 return 154 fi 155 fi 156 157 tst_res TPASS "'${mkswap_cmd}' passed." 158} 159 160do_test() 161{ 162 case $1 in 163 1) mkswap_test "" "" "$TST_DEVICE";; 164 2) mkswap_test "" "" "$TST_DEVICE" "$((DEVICE_SIZE-PAGE_SIZE/1024))";; 165 3) mkswap_test "-f" "" "$TST_DEVICE" "$((DEVICE_SIZE+PAGE_SIZE/1024))";; 166 4) mkswap_test "-c" "" "$TST_DEVICE";; 167 5) mkswap_test "-p" "2048" "$TST_DEVICE";; 168 6) mkswap_test "-L" "ltp_testswap" "-L ltp_testswap" "" "/dev/disk/by-label/ltp_testswap";; 169 7) mkswap_test "-v1" "" "$TST_DEVICE";; 170 8) mkswap_test "-U" "$UUID" "-U $UUID" "" "/dev/disk/by-uuid/$UUID";; 171 9) mkswap_test "-V";; 172 10) mkswap_test "-h";; 173 esac 174} 175 176tst_run 177