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