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