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