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