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