• 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*8/10))" ]; then
73			return 1
74		fi
75	else
76		if [ $blocknum -lt "$(($2*8/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	if [ "$fs_type" = "ext3" ] || [ "$fs_type" = "ext4" ]; then
100		fs_op="$fs_op -b 1024"
101	fi
102
103	local mkfs_cmd="mkfs $mkfs_op $fs_op $device $size"
104
105	echo ${fs_op} | grep -q "\-c"
106	if [ $? -eq 0 ] && [ "$fs_type" = "ntfs" ]; then
107		tst_res TCONF "'$mkfs_cmd' not supported."
108		return
109	fi
110
111	if [ -n "$size" ]; then
112		if [ "$fs_type" = "xfs" ] || [ "$fs_type" = "btrfs" ]; then
113			tst_res TCONF "'$mkfs_cmd' not supported."
114			return
115		fi
116	fi
117
118	${mkfs_cmd} >temp 2>&1
119	if [ $? -ne 0 ]; then
120		grep -q -E "unknown option | invalid option" temp
121		if [ $? -eq 0 ]; then
122			tst_res TCONF "'$mkfs_cmd' not supported."
123			return
124		else
125			tst_res TFAIL "'$mkfs_cmd' failed."
126			cat temp
127			return
128		fi
129	fi
130
131	if [ -n "$device" ]; then
132		mkfs_verify_type "$fs_type" "$device"
133		if [ $? -ne 0 ]; then
134			tst_res TFAIL "'$mkfs_cmd' failed, unexpected type."
135			cat temp
136			return
137		fi
138	fi
139
140	if [ -n "$size" ]; then
141		mkfs_verify_size "$fs_type" "$size"
142		if [ $? -ne 0 ]; then
143			tst_res TFAIL "'$mkfs_cmd' failed, unexpected size."
144			cat temp
145			return
146		fi
147	fi
148
149	tst_res TPASS "'$mkfs_cmd' passed."
150}
151
152test1()
153{
154	mkfs_test "" "$TST_FS_TYPE" "" "$TST_DEVICE"
155}
156
157test2()
158{
159	mkfs_test "" "$TST_FS_TYPE" "" "$TST_DEVICE" "16000"
160}
161
162test3()
163{
164	mkfs_test "" "$TST_FS_TYPE" "-c" "$TST_DEVICE"
165}
166
167test4()
168{
169	mkfs_test "-V"
170}
171
172test5()
173{
174	mkfs_test "-h"
175}
176
177tst_run
178