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