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