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
14# the GNU General Public License for more details.
15#
16# Test mkfs command with some basic options.
17#
18
19TCID=mkfs01
20TST_TOTAL=5
21. test.sh
22
23setup()
24{
25	tst_require_root
26
27	tst_check_cmds blkid df
28
29	if [ -n "$FS_TYPE" ]; then
30		tst_check_cmds mkfs.${FS_TYPE}
31	fi
32
33	tst_tmpdir
34
35	tst_acquire_device
36
37	TST_CLEANUP="cleanup"
38
39	ROD_SILENT mkdir -p mntpoint
40}
41
42cleanup()
43{
44	tst_release_device
45
46	tst_rmdir
47}
48
49mkfs_mount()
50{
51	mount ${TST_DEVICE} mntpoint
52	local ret=$?
53	if [ $ret -eq 32 ]; then
54		tst_brkm TCONF "Cannot mount ${FS_TYPE}, missing driver?"
55	fi
56
57	if [ $ret -ne 0 ]; then
58		tst_brkm TBROK "Failed to mount device: mount exit = $ret"
59	fi
60}
61
62usage()
63{
64	cat << EOF
65	usage: $0 [-f <ext2|ext3|ext4|vfat|...>]
66
67	OPTIONS
68		-f	Specify the type of filesystem to be built. If not
69			specified, the default filesystem type (currently ext2)
70			is used.
71		-h	Display help text and exit.
72
73EOF
74	tst_brkm TWARN "Display help text or unknown options"
75}
76
77mkfs_verify_type()
78{
79	if [ -z "$1" ]; then
80		blkid $2 -t TYPE="ext2" >/dev/null
81	else
82		if [ "$1" = "msdos" ]; then
83			blkid $2 -t TYPE="vfat" >/dev/null
84		else
85			blkid $2 -t TYPE="$1" >/dev/null
86		fi
87	fi
88}
89
90mkfs_verify_size()
91{
92	mkfs_mount
93	local blocknum=`df -P -B 1k mntpoint | tail -n1 | awk '{print $2}'`
94	tst_umount "$TST_DEVICE"
95
96	if [ $blocknum -gt "$2" ]; then
97		return 1
98	fi
99
100	# Size argument in mkfs.ntfs denotes number-of-sectors which is 512bytes,
101	# 1k-block size should be devided by this argument for ntfs verification.
102	if [ "$1" = "ntfs" ]; then
103		local rate=1024/512
104		if [ $blocknum -lt "$(($2/rate*9/10))" ]; then
105			return 1
106		fi
107	else
108		if [ $blocknum -lt "$(($2*9/10))" ]; then
109			return 1
110		fi
111	fi
112
113	return 0
114}
115
116mkfs_test()
117{
118	local mkfs_op=$1
119	local fs_type=$2
120	local fs_op=$3
121	local device=$4
122	local size=$5
123
124	if [ -n "$fs_type" ]; then
125		mkfs_op="-t $fs_type"
126	fi
127
128	if [ "$fs_type" = "xfs" ] || [ "$fs_type" = "btrfs" ]; then
129		fs_op="$fs_op -f"
130	fi
131
132	local mkfs_cmd="mkfs $mkfs_op $fs_op $device $size"
133
134	echo ${fs_op} | grep -q "\-c"
135	if [ $? -eq 0 ] && [ "$fs_type" = "ntfs" ]; then
136		tst_resm TCONF "'${mkfs_cmd}' not supported."
137		return
138	fi
139
140	if [ -n "$size" ]; then
141		if [ "$fs_type" = "xfs" ] || [ "$fs_type" = "btrfs" ]; then
142			tst_resm TCONF "'${mkfs_cmd}' not supported."
143			return
144		fi
145	fi
146
147	${mkfs_cmd} >temp 2>&1
148	if [ $? -ne 0 ]; then
149		grep -q -E "unknown option | invalid option" temp
150		if [ $? -eq 0 ]; then
151			tst_resm TCONF "'${mkfs_cmd}' not supported."
152			return
153		else
154			tst_resm TFAIL "'${mkfs_cmd}' failed."
155			cat temp
156			return
157		fi
158	fi
159
160	if [ -n "$device" ]; then
161		mkfs_verify_type "$fs_type" "$device"
162		if [ $? -ne 0 ]; then
163			tst_resm TFAIL "'${mkfs_cmd}' failed, not expected."
164			return
165		fi
166	fi
167
168	if [ -n "$size" ]; then
169		mkfs_verify_size "$fs_type" "$size"
170		if [ $? -ne 0 ]; then
171			tst_resm TFAIL "'${mkfs_cmd}' failed, not expected."
172			return
173		fi
174	fi
175
176	tst_resm TPASS "'${mkfs_cmd}' passed."
177}
178
179test1()
180{
181	mkfs_test "" "$FS_TYPE" "" "$TST_DEVICE"
182}
183
184test2()
185{
186	mkfs_test "" "$FS_TYPE" "" "$TST_DEVICE" "16000"
187}
188
189test3()
190{
191	mkfs_test "" "$FS_TYPE" "-c" "$TST_DEVICE"
192}
193
194test4()
195{
196	mkfs_test "-V"
197}
198
199test5()
200{
201	mkfs_test "-h"
202}
203
204FS_TYPE=""
205
206while getopts f:h OPTION; do
207	case $OPTION in
208	f)
209		FS_TYPE=$OPTARG;;
210	h)
211		usage;;
212	?)
213		usage;;
214	esac
215done
216
217setup
218for i in $(seq 1 ${TST_TOTAL})
219do
220	test$i
221done
222
223tst_exit
224