• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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=`tst_getconf PAGESIZE`
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
40check_for_file()
41{
42	local path="$1"
43
44	if [ -z "$path" -o -e "$path" ]; then
45		return
46	fi
47	return 1
48}
49
50mkswap_verify()
51{
52	local mkswap_op="$1"
53	local op_arg="$2"
54	local swapfile="$3"
55	local dev_file="$5"
56
57	local before=`awk '/SwapTotal/ {print $2}' /proc/meminfo`
58
59	local swapsize=${4:-$DEVICE_SIZE}
60
61	if [ "$mkswap_op" = "-p" ]; then
62		local pagesize=$op_arg
63	else
64		local pagesize=$PAGE_SIZE
65	fi
66
67	if tst_kvcmp -lt "2.6.35" && [ -n "$dev_file" ]; then
68		tst_res TINFO "Waiting for $dev_file to appear"
69		tst_sleep 100ms
70	else
71		TST_RETRY_FUNC "check_for_file $dev_file" 0
72	fi
73
74	swapon $swapfile 2>/dev/null
75
76	if [ $? -ne 0 ]; then
77		tst_res TINFO "Can not do swapon on $swapfile."
78		if [ $pagesize -ne $PAGE_SIZE ]; then
79			tst_res TINFO "Page size specified by 'mkswap -p' \
80is not equal to system's page size."
81			tst_res TINFO "Swapon failed expectedly."
82			return 0
83		fi
84
85		if [ $swapsize -gt $DEVICE_SIZE ]; then
86			tst_res TINFO "Device size specified by 'mkswap' \
87greater than real size."
88			tst_res TINFO "Swapon failed expectedly."
89			return 0
90		fi
91
92		tst_res TINFO "Swapon failed unexpectedly."
93		return 1
94	fi
95
96	local after=`awk '/SwapTotal/ {print $2}' /proc/meminfo`
97
98	local diff=$((after-before))
99	local filesize=$((swapsize-pagesize/1024))
100
101	local ret=0
102
103	# In general, the swap increment by doing swapon should be equal to
104	# the device size minus a page size, however for some kernels, the
105	# increment we get is not exactly equal to that value, but is equal
106	# to the value minus an extra page size, e.g. on RHEL5.11GA.
107	if [ $diff -ne $filesize ] && \
108		[ $diff -ne $((filesize-pagesize/1024)) ]; then
109		ret=1
110	fi
111
112	swapoff $swapfile 2>/dev/null
113	if [ $? -ne 0 ]; then
114		tst_res TWARN "Can not do swapoff on $swapfile."
115	fi
116
117	return $ret
118}
119
120mkswap_test()
121{
122	local mkswap_op="$1"
123	local op_arg="$2"
124	local device="$3"
125	local size="$4"
126	local dev_file="$5"
127
128	local mkswap_cmd="mkswap $mkswap_op $op_arg $TST_DEVICE $size"
129
130	${mkswap_cmd} >temp 2>&1
131	if [ $? -ne 0 ]; then
132		grep -q -E "unknown option|invalid option|Usage" temp
133		if [ $? -eq 0 ]; then
134			tst_res TCONF "'${mkswap_cmd}' not supported."
135			return
136		fi
137
138		tst_res TFAIL "'${mkswap_cmd}' failed."
139		cat temp
140		return
141	fi
142
143	if [ -n "$device" ]; then
144		mkswap_verify "$mkswap_op" "$op_arg" "$device" "$size" "$dev_file"
145		if [ $? -ne 0 ]; then
146			tst_res TFAIL "'${mkswap_cmd}' failed, not expected."
147			return
148		fi
149	fi
150
151	tst_res TPASS "'${mkswap_cmd}' passed."
152}
153
154do_test()
155{
156	case $1 in
157	1) mkswap_test "" "" "$TST_DEVICE";;
158	2) mkswap_test "" "" "$TST_DEVICE" "$((DEVICE_SIZE-PAGE_SIZE/1024))";;
159	3) mkswap_test "-f" "" "$TST_DEVICE" "$((DEVICE_SIZE+PAGE_SIZE/1024))";;
160	4) mkswap_test "-c" "" "$TST_DEVICE";;
161	5) mkswap_test "-p" "2048" "$TST_DEVICE";;
162	6) mkswap_test "-L" "ltp_testswap" "-L ltp_testswap" "" "/dev/disk/by-label/ltp_testswap";;
163	7) mkswap_test "-v1" "" "$TST_DEVICE";;
164	8) mkswap_test "-U" "$UUID" "-U $UUID" "" "/dev/disk/by-uuid/$UUID";;
165	9) mkswap_test "-V";;
166	10) mkswap_test "-h";;
167	esac
168}
169
170tst_run
171