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