Home | History | Annotate | Download | only in fs_readonly
      1 #!/bin/sh
      2 #
      3 #   Copyright (c) International Business Machines  Corp., 2008
      4 #
      5 #   This program is free software;  you can redistribute it and/or modify
      6 #   it under the terms of the GNU General Public License as published by
      7 #   the Free Software Foundation; either version 2 of the License, or
      8 #   (at your option) any later version.
      9 #
     10 #   This program is distributed in the hope that it will be useful,
     11 #   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13 #   the GNU General Public License for more details.
     14 #
     15 #   You should have received a copy of the GNU General Public License
     16 #   along with this program;  if not, write to the Free Software
     17 #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18 #
     19 #*******************************************************************************
     20 # Readme_ROBind has more details on the tests running for ROBIND.
     21 # TEST:
     22 #   NAME:       test_robind.sh
     23 #   FUNCTIONALITY:  File system tests for normal mount, bind mount and RO mount
     24 #
     25 #   DESCRIPTION:    Performs filesystems tests for RO mount.
     26 #     For filesystem, like ext2, ext3, reiserfs, jfs & xfs,
     27 #     This test needs a big block device(>=500MB is ok), and you can specify
     28 #     it by -z option when running runltp.
     29 #        a)  mounts on dir1,
     30 #        b)  mount --bind dir2
     31 #        c)  mount -o remount,ro
     32 #       It verifies the tests on a) and b) works correctly.
     33 #       For the c) option it checks that the tests are not able to write
     34 #       into dir.
     35 #===============================================================================
     36 #
     37 # CHANGE HISTORY:
     38 # DATE           AUTHOR                  REASON
     39 # 09/06/2008     Veerendra Chandrappa    For Container, testing of RO-Bind mount
     40 #                Dave Hansen
     41 # This script is based on the Dave Hansen script for testing the robind.
     42 #*******************************************************************************
     43 
     44 export TCID="test_robind"
     45 export TST_TOTAL=3
     46 
     47 DIRS="dir1 dir2-bound dir3-ro"
     48 dir1_mount_flag=0
     49 dir2_bound_mount_flag=0
     50 dir3_ro_mount_flag=0
     51 
     52 . test.sh
     53 
     54 usage()
     55 {
     56 	cat << EOF
     57 	usage: $0 -c command [ext3,ext2,jfs,xfs,reiserfs,ramfs]
     58 
     59 	This script verifies ReadOnly-filesystem, by mounting block device and
     60 	executing the filesystem tests.
     61 
     62 	OPTIONS
     63 		-h    display this message and exit
     64 		-c    command to be executed
     65 
     66 EOF
     67 	exit 1
     68 }
     69 
     70 umount_mntpoint()
     71 {
     72 	if [ $dir3_ro_mount_flag -eq 1 ];then
     73 		umount dir3-ro
     74 		if [ $? -ne 0 ];then
     75 			tst_resm TWARN "umount dir3-ro failed"
     76 		else
     77 			dir3_ro_mount_flag=0
     78 		fi
     79 	fi
     80 
     81 	if [ $dir2_bound_mount_flag -eq 1 ];then
     82 		umount dir2-bound
     83 		if [ $? -ne 0 ];then
     84 			tst_resm TWARN "umount dir2-bound failed"
     85 		else
     86 			dir2_bound_mount_flag=0
     87 		fi
     88 	fi
     89 
     90 	if [ $dir1_mount_flag -eq 1 ];then
     91 		umount dir1
     92 		if [ $? -ne 0 ];then
     93 			tst_resm TWARN "umount dir1"
     94 		else
     95 			dir1_mount_flag=0
     96 		fi
     97 	fi
     98 }
     99 
    100 cleanup()
    101 {
    102 	umount_mntpoint
    103 	tst_rmdir
    104 }
    105 
    106 # parameters: file_systems (if any )
    107 setup()
    108 {
    109 	tst_require_root
    110 
    111 	if [ -z "$LTP_BIG_DEV" ];then
    112 		tst_brkm TCONF "tests need a big block device(>=500MB)"
    113 	else
    114 		device=$LTP_BIG_DEV
    115 	fi
    116 
    117 	tst_tmpdir
    118 	TST_CLEANUP=cleanup
    119 
    120 	for dir in $DIRS
    121 	do
    122 		rm -rf $dir
    123 		mkdir -p $dir
    124 	done
    125 
    126 	# populating the default FS as $LTP_BIG_DEV_FS_TYPE
    127 	# (or ext3 if it's not set), if FS is not given
    128 	if [ -z "$*" ]; then
    129 		FSTYPES=${LTP_BIG_DEV_FS_TYPE:-ext3}
    130 	else
    131 		FSTYPES="$*"
    132 	fi
    133 }
    134 
    135 # the core function where it runs the tests
    136 # $1 - directory where to run tests
    137 # $2 - file system type
    138 # $3 - read-only flag [true|false]
    139 testdir()
    140 {
    141 	local dir=$1
    142 	local fs_type=$2
    143 	local RO=$3
    144 	local tst_result=0
    145 	local curdir=$(pwd)
    146 
    147 	cd $dir
    148 	tst_resm TINFO "command: $command"
    149 
    150 	# we need to export TMPDIR, in case test calls tst_rmdir()
    151 	export TMPDIR=$curdir/$dir
    152 
    153 	eval $command > $curdir/test.log 2>&1
    154 	tst_result=$?
    155 
    156 	# if tst_result isn't 0 and read-only flag is false, the test failed
    157 	# or if tst_result is 0 and read-only flag is true, the test failed.
    158 	if [ "$RO" = "false" -a $tst_result -ne 0 -o "$RO" = "true" -a \
    159 	     $tst_result -eq 0 ];then
    160 		tst_resm TINFO "error info:"
    161 		cat $curdir/test.log
    162 		tst_resm TFAIL "RO-FileSystem Tests FAILED for \
    163 				$dir $fs_type read-only flag: $RO"
    164 	else
    165 		tst_resm TPASS "RO-FileSystem Tests PASSED for \
    166 				$dir $fs_type read-only flag: $RO"
    167 	fi
    168 
    169 	# remove all the temp files created.
    170 	cd ..
    171 	rm -f $curdir/test.log
    172 	rm -rf $curdir/$dir/*
    173 }
    174 
    175 #=============================================================================
    176 # MAIN
    177 #     See the description, purpose, and design of this test under TEST
    178 #     in this test's prolog.
    179 #=============================================================================
    180 
    181 while getopts c:h: OPTION; do
    182 	case $OPTION in
    183 	c)
    184 		command=$OPTARG;;
    185 	h)
    186 		usage;;
    187 	?)
    188 		usage;;
    189 	esac
    190 done
    191 shift $((OPTIND-1))
    192 
    193 setup $*
    194 
    195 # Executes the tests for differnt FS's
    196 for fstype in $FSTYPES; do
    197 	if [ "$fstype" = "reiserfs" ]; then
    198 		opts="-f --journal-size 513 -q"
    199 	elif echo "$fstype" | grep -q "ext"; then
    200 		opts="-F"
    201 	elif [ "$fstype" = "xfs" ]; then
    202 		opts="-f"
    203 	elif [ "$fstype" = "btrfs" ]; then
    204 		opts="-f"
    205 	fi
    206 
    207 	if [ "$fstype" != "ramfs" ]; then
    208 		tst_mkfs $fstype $device $opts
    209 	fi
    210 
    211 	mount -t $fstype $device  dir1
    212 	if [ $? -ne 0 ];then
    213 		tst_brkm TBROK "mount $device to dir1 failed"
    214 	else
    215 		dir1_mount_flag=1
    216 	fi
    217 
    218 	mount --bind dir1 dir2-bound
    219 	if [ $? -ne 0 ];then
    220 		tst_brkm TBROK "mount --bind dir1 dir2-bound failed"
    221 	else
    222 		dir2_bound_mount_flag=1
    223 	fi
    224 
    225 	mount --bind dir1 dir3-ro
    226 	if [ $? -ne 0 ];then
    227 		tst_brkm TBROK "mount --bind dir1 dir3-ro failed"
    228 	else
    229 		dir3_ro_mount_flag=1
    230 	fi
    231 
    232 	mount -o remount,ro,bind dir1 dir3-ro
    233 	if [ $? -ne 0 ];then
    234 		tst_brkm TBROK "mount -o remount,ro,bind dir1 dir3-ro failed"
    235 	fi
    236 
    237 	testdir dir1 $fstype false
    238 	testdir dir2-bound $fstype false
    239 	testdir dir3-ro $fstype true
    240 	umount_mntpoint
    241 done
    242 
    243 tst_exit
    244