config: fix selinux context handling
[xfstests-dev.git] / common / config
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2003,2006 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # setup and check for config parameters, and in particular
20 #
21 # EMAIL -           email of the script runner.
22 # TEST_DIR -        scratch test directory that is in an already
23 #                   mounted XFS file system, needs to be be world
24 #                   writeable
25 # TEST_DEV -        device for file system containing TEST_DIR
26 #
27 # and optionally:
28 # SCRATCH_DEV -     device you can make a scratch file system on
29 # SCRATCH_MNT -     mount point for scratch file system
30 # SCRATCH_LOGDEV -  scratch log device for external log testing
31 # SCRATCH_RTDEV -   scratch rt dev
32 # TEST_LOGDEV -     test log device for external log testing
33 # TEST_RTDEV -      test rt dev
34 # TAPE_DEV -        the tape device for the xfsdump tests
35 # RMT_TAPE_DEV -    the remote tape device for the xfsdump tests
36 # RMT_IRIXTAPE_DEV- the IRIX remote tape device for the xfsdump tests
37 # RMT_TAPE_USER -   remote user for tape device
38 #
39 # - These can be added to $HOST_CONFIG_DIR (witch default to ./config)
40 #   below or a separate local configuration file can be used (using
41 #   the HOST_OPTIONS variable).
42 # - This script is shared by the stress test system and the auto-qa
43 #   system
44 # - TEST_DEV & TEST_DIR must be assigned.
45 # - this script shouldn't make any assertions about filesystem
46 #   validity or mountedness.
47 #
48
49 # all tests should use a common language setting to prevent golden
50 # output mismatches.
51 export LANG=C
52 export LC_ALL=C
53
54 # Warning: don't put freeware before /usr/bsd on IRIX coz you'll
55 #  get the wrong hostname and set your system name to -s  :)
56 [ -d /usr/bsd ] && PATH=$PATH:/usr/bsd
57 [ -d /usr/freeware/bin ] && PATH=$PATH:/usr/freeware/bin
58 PATH=".:$PATH"
59
60 export HOST=`hostname -s`
61 export HOSTOS=`uname -s`
62 [ "$HOSTOS" = "IRIX64" ] && export HOSTOS="IRIX"
63
64 export MODULAR=0               # using XFS as a module or not
65 export BOOT="/boot"            # install target for kernels
66 export EXTRA=${EXTRA:=xfs-qa}
67
68 # general parameters (mainly for auto-qa)
69 export SOAK_PROC=3             # -p option to fsstress
70 export SOAK_STRESS=10000       # -n option to fsstress
71 export SOAK_PASSES=-1          # count of repetitions of fsstress (while soaking)
72 export EMAIL=root@localhost    # where auto-qa will send its status messages
73 export HOST_OPTIONS=${HOST_OPTIONS:=local.config}
74 export CHECK_OPTIONS=${CHECK_OPTIONS:="-g auto"}
75 export BENCH_PASSES=${BENCH_PASSES:=5}
76 export XFS_MKFS_OPTIONS=${XFS_MKFS_OPTIONS:=-bsize=4096}
77 export TIME_FACTOR=${TIME_FACTOR:=1}
78 export LOAD_FACTOR=${LOAD_FACTOR:=1}
79 export DEBUGFS_MNT=${DEBUGFS_MNT:="/sys/kernel/debug"}
80
81 export PWD=`pwd`
82 #export DEBUG=${DEBUG:=...} # arbitrary CFLAGS really.
83 export MALLOCLIB=${MALLOCLIB:=/usr/lib/libefence.a}
84 export LOCAL_CONFIGURE_OPTIONS=${LOCAL_CONFIGURE_OPTIONS:=--enable-readline=yes}
85
86 export RECREATE_TEST_DEV=false
87
88 # $1 = prog to look for, $2* = default pathnames if not found in $PATH
89 set_prog_path()
90 {
91     p=`which $1 2> /dev/null`
92     if [ -n "$p" -a -x "$p" ]; then
93         echo $p
94         return 0
95     fi
96     p=$1
97
98     shift
99     for f; do
100         if [ -x $f ]; then
101             echo $f
102             return 0
103         fi
104     done
105
106     echo ""
107     return 1
108 }
109
110 # Handle mkfs.btrfs which does (or does not) require -f to overwrite
111 set_btrfs_mkfs_prog_path_with_opts()
112 {
113         p=`set_prog_path mkfs.btrfs`
114         if [ "$p" != "" ] && grep -q 'force overwrite' $p; then
115                 echo "$p -f"
116         else
117                 echo $p
118         fi
119 }
120
121 _fatal()
122 {
123     echo "$*"
124     status=1
125     exit 1
126 }
127
128 export MKFS_PROG="`set_prog_path mkfs`"
129 [ "$MKFS_PROG" = "" ] && _fatal "mkfs not found"
130
131 export MOUNT_PROG="`set_prog_path mount`"
132 [ "$MOUNT_PROG" = "" ] && _fatal "mount not found"
133
134 export UMOUNT_PROG="`set_prog_path umount`"
135 [ "$UMOUNT_PROG" = "" ] && _fatal "umount not found"
136
137 export FSSTRESS_PROG="./ltp/fsstress"
138 [ "$FSSTRESS_PROG" = "" ] && _fatal "fsstress not found"
139
140 export PERL_PROG="`set_prog_path perl`"
141 [ "$PERL_PROG" = "" ] && _fatal "perl not found"
142
143 export AWK_PROG="`set_prog_path awk`"
144 [ "$AWK_PROG" = "" ] && _fatal "awk not found"
145
146 export SED_PROG="`set_prog_path sed`"
147 [ "$SED_PROG" = "" ] && _fatal "sed not found"
148
149 export BC_PROG="`set_prog_path bc`"
150 [ "$BC_PROG" = "" ] && _fatal "bc not found"
151
152 export PS_ALL_FLAGS="-ef"
153
154 export DF_PROG="`set_prog_path df`"
155 [ "$DF_PROG" = "" ] && _fatal "df not found"
156 [ "$HOSTOS" = "Linux" ] && export DF_PROG="$DF_PROG -T -P"
157
158 export XFS_LOGPRINT_PROG="`set_prog_path xfs_logprint`"
159 export XFS_REPAIR_PROG="`set_prog_path xfs_repair`"
160 export XFS_DB_PROG="`set_prog_path xfs_db`"
161 export XFS_GROWFS_PROG=`set_prog_path xfs_growfs`
162 export XFS_IO_PROG="`set_prog_path xfs_io`"
163 export XFS_PARALLEL_REPAIR_PROG="`set_prog_path xfs_prepair`"
164 export XFS_PARALLEL_REPAIR64_PROG="`set_prog_path xfs_prepair64`"
165 export __XFSDUMP_PROG="`set_prog_path xfsdump`"
166 export XFSDUMP_PROG="$__XFSDUMP_PROG -e"
167 export XFSRESTORE_PROG="`set_prog_path xfsrestore`"
168 export XFSINVUTIL_PROG="`set_prog_path xfsinvutil`"
169 export GETFATTR_PROG="`set_prog_path getfattr`"
170 export SETFATTR_PROG="`set_prog_path setfattr`"
171 export ATTR_PROG="`set_prog_path attr`"
172 export QUOTA_PROG="`set_prog_path quota`"
173 export XFS_QUOTA_PROG="`set_prog_path xfs_quota`"
174 export KILLALL_PROG="`set_prog_path killall`"
175 export INDENT_PROG="`set_prog_path indent`"
176 export XFS_COPY_PROG="`set_prog_path xfs_copy`"
177 export FSTRIM_PROG="`set_prog_path fstrim`"
178 export DUMPE2FS_PROG="`set_prog_path dumpe2fs`"
179 export FIO_PROG="`set_prog_path fio`"
180 export FILEFRAG_PROG="`set_prog_path filefrag`"
181 export E4DEFRAG_PROG="`set_prog_path e4defrag`"
182 export LOGGER_PROG="`set_prog_path logger`"
183 export DBENCH_PROG="`set_prog_path dbench`"
184 export DMSETUP_PROG="`set_prog_path dmsetup`"
185 export WIPEFS_PROG="`set_prog_path wipefs`"
186
187 # Generate a comparable xfsprogs version number in the form of
188 # major * 10000 + minor * 100 + release
189 #
190 # $ xfs_db -V
191 # xfs_db version 2.9.7
192 #
193 # so, 2.9.7 = 20907
194 _version=`$XFS_DB_PROG -V | $AWK_PROG '
195         /version/ {
196                 if (split($3,ver,".") == 3)
197                         print (ver[1] * 10000) + (ver[2] * 100) + ver[3];
198         }'`
199 [ -z "$_version" ] && _fatal "xfsprogs version cannot be found"
200 export XFSPROGS_VERSION="$_version"
201
202 case "$HOSTOS" in
203     IRIX*)
204         export MKFS_XFS_PROG="`set_prog_path mkfs_xfs`"
205         export MKFS_UDF_PROG="`set_prog_path mkfs_udf`"
206         export XFS_FSR_PROG="`set_prog_path /usr/etc/fsr_xfs`"
207         export MKFS_NFS_PROG="false"
208         ;;
209     Linux)
210         export MKFS_XFS_PROG="`set_prog_path mkfs.xfs`"
211         export MKFS_UDF_PROG="`set_prog_path mkudffs`"
212         export MKFS_BTRFS_PROG="`set_btrfs_mkfs_prog_path_with_opts`"
213         export BTRFS_UTIL_PROG="`set_prog_path btrfs`"
214         export BTRFS_SHOW_SUPER_PROG="`set_prog_path btrfs-show-super`"
215         export XFS_FSR_PROG="`set_prog_path xfs_fsr`"
216         export MKFS_NFS_PROG="false"
217         ;;
218 esac
219
220 # SELinux adds extra xattrs which can mess up our expected output.
221 # So, mount with a context, and they won't be created
222 # # nfs_t is a "liberal" context so we can use it.
223 if [ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
224         SELINUX_MOUNT_OPTIONS="-o context=system_u:object_r:nfs_t:s0"
225         export SELINUX_MOUNT_OPTIONS
226 fi
227
228 _mount_opts()
229 {
230         case $FSTYP in
231         xfs)
232                 export MOUNT_OPTIONS=$XFS_MOUNT_OPTIONS
233                 ;;
234         udf)
235                 export MOUNT_OPTIONS=$UDF_MOUNT_OPTIONS
236                 ;;
237         nfs)
238                 export MOUNT_OPTIONS=$NFS_MOUNT_OPTIONS
239                 ;;
240         ext2|ext3|ext4|ext4dev)
241                 # acls & xattrs aren't turned on by default on ext$FOO
242                 export MOUNT_OPTIONS="-o acl,user_xattr $EXT_MOUNT_OPTIONS"
243                 ;;
244         reiserfs)
245                 # acls & xattrs aren't turned on by default on reiserfs
246                 export MOUNT_OPTIONS="-o acl,user_xattr $REISERFS_MOUNT_OPTIONS"
247                 ;;
248         gfs2)
249                 # acls aren't turned on by default on gfs2
250                 export MOUNT_OPTIONS="-o acl $GFS2_MOUNT_OPTIONS"
251                 ;;
252         tmpfs)
253                 # We need to specify the size at mount, use 512 MB by default
254                 export MOUNT_OPTIONS="-o size=512M $TMPFS_MOUNT_OPTIONS"
255                 ;;
256         *)
257                 ;;
258         esac
259 }
260
261 _mkfs_opts()
262 {
263         case $FSTYP in
264         xfs)
265                 export MKFS_OPTIONS=$XFS_MKFS_OPTIONS
266                 ;;
267         udf)
268                 [ ! -z "$udf_fsize" ] && \
269                         UDF_MKFS_OPTIONS="$UDF_MKFS_OPTIONS -s $udf_fsize"
270                 export MKFS_OPTIONS=$UDF_MKFS_OPTIONS
271                 ;;
272         nfs)
273                 export MKFS_OPTIONS=$NFS_MKFS_OPTIONS
274                 ;;
275         reiserfs)
276                 export MKFS_OPTIONS="$REISERFS_MKFS_OPTIONS -q"
277                 ;;
278         gfs2)
279                 export MKFS_OPTIONS="$GFS2_MKFS_OPTIONS -O -p lock_nolock"
280                 ;;
281         jfs)
282                 export MKFS_OPTIONS="$JFS_MKFS_OPTIONS -q"
283                 ;;
284         *)
285                 ;;
286         esac
287 }
288
289 _fsck_opts()
290 {
291         case $FSTYP in
292         ext2|ext3|ext4|ext4dev)
293                 export FSCK_OPTIONS="-nf"
294                 ;;
295         reiserfs)
296                 export FSCK_OPTIONS="--yes"
297                 ;;
298         *)
299                 export FSCK_OPTIONS="-n"
300                 ;;
301         esac
302 }
303
304 known_hosts()
305 {
306         [ "$HOST_CONFIG_DIR" ] || HOST_CONFIG_DIR=`pwd`/configs
307
308         [ -f /etc/xfsqa.config ]             && export HOST_OPTIONS=/etc/xfsqa.config
309         [ -f $HOST_CONFIG_DIR/$HOST ]        && export HOST_OPTIONS=$HOST_CONFIG_DIR/$HOST
310         [ -f $HOST_CONFIG_DIR/$HOST.config ] && export HOST_OPTIONS=$HOST_CONFIG_DIR/$HOST.config
311 }
312
313 # Returns a list of sections in config file
314 # Each section starts with the section name in the format
315 # [section_name1]. Only alphanumeric characters and '_' is allowed
316 # in the section name otherwise the section will not be resognised.
317 # Section name must be contained between square brackets.
318 get_config_sections() {
319         sed -n -e "s/^\[\([[:alnum:]_]*\)\]/\1/p" < $1
320 }
321
322 if [ ! -f "$HOST_OPTIONS" ]; then
323         known_hosts
324 fi
325
326 export HOST_OPTIONS_SECTIONS="-no-sections-"
327 export OPTIONS_HAVE_SECTIONS=false
328 if [ -f "$HOST_OPTIONS" ]; then
329         export HOST_OPTIONS_SECTIONS=`get_config_sections $HOST_OPTIONS`
330         if [ -z "$HOST_OPTIONS_SECTIONS" ]; then
331                 . $HOST_OPTIONS
332                 export HOST_OPTIONS_SECTIONS="-no-sections-"
333         else
334                 export OPTIONS_HAVE_SECTIONS=true
335         fi
336 fi
337
338 # Parse config section options. This function will parse all the configuration
339 # within a single section which name is passed as an argument. For section
340 # name format see comments in get_config_sections().
341 # Empty lines and everything after '#' will be ignored.
342 # Configuration options should be defined in the format
343 #
344 # CONFIG_OPTION=value
345 #
346 # This 'CONFIG_OPTION' variable and will be exported as an environment variable.
347 parse_config_section() {
348         SECTION=$1
349         if ! $OPTIONS_HAVE_SECTIONS; then
350                 return 0
351         fi
352         eval `sed -e 's/[[:space:]]*\=[[:space:]]*/=/g' \
353                 -e 's/#.*$//' \
354                 -e 's/[[:space:]]*$//' \
355                 -e 's/^[[:space:]]*//' \
356                 -e "s/^\([^=]*\)=\"\?'\?\([^\"']*\)\"\?'\?$/export \1=\"\2\"/" \
357                 < $HOST_OPTIONS \
358                 | sed -n -e "/^\[$SECTION\]/,/^\s*\[/{/^[^#].*\=.*/p;}"`
359 }
360
361 get_next_config() {
362         if [ ! -z "$CONFIG_INCLUDED" ] && ! $OPTIONS_HAVE_SECTIONS; then
363                 return 0
364         fi
365
366         local OLD_FSTYP=$FSTYP
367         local OLD_MOUNT_OPTIONS=$MOUNT_OPTIONS
368         local OLD_MKFS_OPTIONS=$MKFS_OPTIONS
369         local OLD_FSCK_OPTIONS=$FSCK_OPTIONS
370
371         unset MOUNT_OPTIONS
372         unset MKFS_OPTIONS
373         unset FSCK_OPTIONS
374         # We might have deduced SCRATCH_DEV from the SCRATCH_DEV_POOL in the previous
375         # run, so we have to unset it now.
376         if [ "$SCRATCH_DEV_NOT_SET" == "true" ]; then
377                 unset SCRATCH_DEV
378         fi
379
380         parse_config_section $1
381
382         if [ ! -z "$OLD_FSTYP" ] && [ $OLD_FSTYP != $FSTYP ]; then
383                 [ -z "$MOUNT_OPTIONS" ] && _mount_opts
384                 [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
385                 [ -z "$FSCK_OPTIONS" ] && _fsck_opts
386         else
387                 [ -z "$MOUNT_OPTIONS" ] && export MOUNT_OPTIONS=$OLD_MOUNT_OPTIONS
388                 [ -z "$MKFS_OPTIONS" ] && export MKFS_OPTIONS=$OLD_MKFS_OPTIONS
389                 [ -z "$FSCK_OPTIONS" ] && export FSCK_OPTIONS=$OLD_FSCK_OPTIONS
390         fi
391
392         # set default RESULT_BASE
393         if [ -z "$RESULT_BASE" ]; then
394                 export RESULT_BASE="$here/results/"
395         fi
396
397         #  Mandatory Config values.
398         MC=""
399         [ -z "$EMAIL" ]          && MC="$MC EMAIL"
400         [ -z "$TEST_DIR" ]       && MC="$MC TEST_DIR"
401         [ -z "$TEST_DEV" ]       && MC="$MC TEST_DEV"
402
403         if [ -n "$MC" ]; then
404                 echo "Warning: need to define parameters for host $HOST"
405                 echo "       or set variables:"
406                 echo "       $MC"
407                 exit 1
408         fi
409
410         echo $TEST_DEV | grep -q ":" > /dev/null 2>&1
411         if [ ! -b "$TEST_DEV" -a "$?" != "0" ]; then
412                 echo "common/config: Error: \$TEST_DEV ($TEST_DEV) is not a block device or a NFS filesystem"
413                 exit 1
414         fi
415
416         if [ ! -d "$TEST_DIR" ]; then
417                 echo "common/config: Error: \$TEST_DIR ($TEST_DIR) is not a directory"
418                 exit 1
419         fi
420
421         # a btrfs tester will set only SCRATCH_DEV_POOL, we will put first of its dev
422         # to SCRATCH_DEV and rest to SCRATCH_DEV_POOL to maintain the backward compatibility
423         if [ ! -z "$SCRATCH_DEV_POOL" ]; then
424                 if [ ! -z "$SCRATCH_DEV" ]; then
425                         echo "common/config: Error: \$SCRATCH_DEV ($SCRATCH_DEV) should be unset when \$SCRATCH_DEV_POOL ($SCRATCH_DEV_POOL) is set"
426                         exit 1
427                 fi
428                 SCRATCH_DEV=`echo $SCRATCH_DEV_POOL | awk '{print $1}'`
429                 export SCRATCH_DEV
430                 export SCRATCH_DEV_NOT_SET=true
431         fi
432
433         echo $SCRATCH_DEV | grep -q ":" > /dev/null 2>&1
434         if [ ! -z "$SCRATCH_DEV" -a ! -b "$SCRATCH_DEV" -a "$?" != "0" ]; then
435                 echo "common/config: Error: \$SCRATCH_DEV ($SCRATCH_DEV) is not a block device or a NFS filesystem"
436                 exit 1
437         fi
438
439         if [ ! -z "$SCRATCH_MNT" -a ! -d "$SCRATCH_MNT" ]; then
440                 echo "common/config: Error: \$SCRATCH_MNT ($SCRATCH_MNT) is not a directory"
441                 exit 1
442         fi
443 }
444
445 if [ -z "$CONFIG_INCLUDED" ]; then
446         get_next_config `echo $HOST_OPTIONS_SECTIONS | cut -f1 -d" "`
447         export CONFIG_INCLUDED=true
448
449         # Autodetect fs type based on what's on $TEST_DEV unless it's been set
450         # externally
451         if [ -z "$FSTYP" ] && \
452            [ "$HOSTOS" == "Linux" -o "$OSTYPE" == "linux-gnu" ] && \
453            [ ! -z "$TEST_DEV" ]; then
454                 FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
455         fi
456         FSTYP=${FSTYP:=xfs}
457         export FSTYP
458         [ -z "$MOUNT_OPTIONS" ] && _mount_opts
459         [ -z "$MKFS_OPTIONS" ] && _mkfs_opts
460         [ -z "$FSCK_OPTIONS" ] && _fsck_opts
461 fi
462
463 # make sure this script returns success
464 /bin/true