• 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. tst_test.sh
18
19usage()
20{
21	cat << EOF
22usage: $0 [-f <ext2|ext3|ext4|vfat|...>]
23
24OPTIONS
25-f	Specify the type of filesystem to be built.  If not
26	specified, the default filesystem type (currently ext2)
27	is used.
28EOF
29}
30
31parse_args()
32{
33	TST_FS_TYPE="$2"
34}
35
36setup()
37{
38	if [ -n "$TST_FS_TYPE" ]; then
39		tst_require_cmds mkfs.${TST_FS_TYPE}
40	fi
41
42	ROD_SILENT mkdir -p mntpoint
43}
44
45mkfs_verify_type()
46{
47	if [ -z "$1" ]; then
48		blkid $2 -t TYPE="ext2" >/dev/null
49	else
50		if [ "$1" = "msdos" ]; then
51			blkid $2 -t TYPE="vfat" >/dev/null
52		else
53			blkid $2 -t TYPE="$1" >/dev/null
54		fi
55	fi
56}
57
58mkfs_verify_size()
59{
60	tst_mount
61	local blocknum=`df -P -B 1k mntpoint | tail -n1 | awk '{print $2}'`
62	tst_umount
63
64	if [ $blocknum -gt "$2" ]; then
65		return 1
66	fi
67
68	# Size argument in mkfs.ntfs denotes number-of-sectors which is 512bytes,
69	# 1k-block size should be devided by this argument for ntfs verification.
70	if [ "$1" = "ntfs" ]; then
71		local rate=1024/512
72		if [ $blocknum -lt "$(($2/$rate*9/10))" ]; then
73			return 1
74		fi
75	else
76		if [ $blocknum -lt "$(($2*9/10))" ]; then
77			return 1
78		fi
79	fi
80
81	return 0
82}
83
84mkfs_test()
85{
86	local mkfs_op=$1
87	local fs_type=$2
88	local fs_op=$3
89	local device=$4
90	local size=$5
91
92	if [ -n "$fs_type" ]; then
93		mkfs_op="-t $fs_type"
94	fi
95
96	if [ "$fs_type" = "xfs" ] || [ "$fs_type" = "btrfs" ]; then
97		fs_op="$fs_op -f"
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, not expected."
132			return
133		fi
134	fi
135
136	if [ -n "$size" ]; then
137		mkfs_verify_size "$fs_type" "$size"
138		if [ $? -ne 0 ]; then
139			tst_res TFAIL "'${mkfs_cmd}' failed, not expected."
140			return
141		fi
142	fi
143
144	tst_res TPASS "'${mkfs_cmd}' passed."
145}
146
147test1()
148{
149	mkfs_test "" "$TST_FS_TYPE" "" "$TST_DEVICE"
150}
151
152test2()
153{
154	mkfs_test "" "$TST_FS_TYPE" "" "$TST_DEVICE" "16000"
155}
156
157test3()
158{
159	mkfs_test "" "$TST_FS_TYPE" "-c" "$TST_DEVICE"
160}
161
162test4()
163{
164	mkfs_test "-V"
165}
166
167test5()
168{
169	mkfs_test "-h"
170}
171
172tst_run
173