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