1 #! /usr/bin/ksh -p
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 # ident "@(#)configure.ksh      1.7     09/08/11 SMI"
  28 #
  29 
  30 #
  31 # Create the configuration file based on passed in variables
  32 # or those set in the tetexec.cfg file.
  33 #
  34 
  35 tet_startup="startup"
  36 tet_cleanup=""
  37 
  38 iclist="ic1 ic2"
  39 ic1="configure"
  40 ic2="unconfigure"
  41 
  42 #
  43 # Check to see if the configure file variable is set,
  44 # and if not then set to the default value
  45 #
  46 [[ -z $SHR_TMPDIR ]] && SHR_TMPDIR=/var/tmp/share
  47 [[ -z $configfile ]] && configfile=$SHR_TMPDIR/test_config
  48 
  49 #
  50 # In order to complete the creation of the config file
  51 #       - create directory if needed
  52 #       - confirm ability to create file
  53 #       - parse the TESTDIR variable and set ZFSPOOL variable accordingly
  54 #       - write config file with variable settings
  55 #       - create new zfs pool if TESTDIR is UFS based
  56 #       - setup test file systems if user choose to build them only once
  57 #
  58 function configure {
  59         # share cannot be done by ordinary user
  60         is_root
  61         # share is not supported in non-global zone
  62         ck_zone
  63 
  64         # create needed test directories
  65         if [[ ! -d $TESTDIR ]]; then
  66             mkdir -p $TESTDIR
  67             if (( $? != 0 )); then
  68                 cti_result FAIL "failed to create $TESTDIR directory"
  69                 return
  70             fi
  71         fi
  72 
  73         if [[ -d $SHR_TMPDIR ]]; then
  74             chmod 0777 $SHR_TMPDIR
  75             if (( $? != 0 )); then
  76                 cti_result FAIL "failed to set permission of $SHR_TMPDIR to 777"
  77                 return
  78             fi
  79         else
  80             mkdir -pm 0777 $SHR_TMPDIR
  81             if (( $? != 0 )); then
  82                 cti_result FAIL "failed to create $SHR_TMPDIR directory"
  83                 return
  84             fi
  85         fi
  86 
  87         # check if the test suite had been configured already
  88         if [[ -f $configfile ]]; then
  89                 tet_infoline "This test suite had already been configured."
  90                 tet_infoline "to unconfigure the test suite use:"
  91                 tet_infoline "   run_test -L $SHR_TMPDIR -F $configfile " \
  92                                         "share unconfigure"
  93                 tet_infoline "or supply an alternate config file by using:"
  94                 tet_infoline "   run_test -L $SHR_TMPDIR -v " \
  95                                         "configfile=<filename> share configure"
  96                 tet_result FAIL
  97                 return
  98         else
  99                 touch ${configfile}
 100                 if (( $? != 0 )); then
 101                         cti_result FAIL "Could not create the configuration \
 102                                 file: $configfile"
 103                         return
 104                 fi
 105                 rm -f ${configfile}
 106         fi
 107 
 108         # set the config file template variable to the test_config template
 109         # file, and check if it exists
 110         configfile_tmpl=${TET_SUITE_ROOT}/sharefs-tests/config/test_config.tmpl
 111         if [[ ! -f $configfile_tmpl ]]; then
 112                 cti_result FAIL "There is no template config file to \
 113                         create config from."
 114                 return
 115         fi
 116 
 117         # check if the TESTDIR has enough space to create test file systems
 118         df -h $TESTDIR > $SHR_TMPDIR/df.out.$$
 119         typeset avail_space=$(tail -1 $SHR_TMPDIR/df.out.$$ | awk '{print $4}')
 120         typeset -i avail_num=${avail_space%?}
 121         if [[ $avail_space == *"K" || $avail_space == *"M" ]] || (( \
 122                 $avail_num < 2 )); then
 123                 cti_reportfile $SHR_TMPDIR/df.out.$$ "df -h $TESTDIR"
 124                 typeset errmsg="ERROR: the test directory<$TESTDIR> does not"
 125                 errmsg="$errmsg have enough space available for testing, please"
 126                 errmsg="$errmsg provide an alternative test directory (reset"
 127                 errmsg="$errmsg TESTDIR var) which has 2G free space at least"
 128                 cti_result FAIL "$errmsg"
 129                 rm -f $SHR_TMPDIR/df.out.$$
 130                 return
 131         else
 132                 rm -f $SHR_TMPDIR/df.out.$$
 133         fi
 134 
 135         # get fs type and set ZFSPOOL variable
 136         typeset strfs=$(get_fstype $TESTDIR)
 137         if (( $? != 0 )); then
 138                 cti_result FAIL "get_fstype<$TESTDIR> failed -"
 139                 tet_infoline "expect OKAY, but got <$strfs>"
 140                 return
 141         fi
 142         typeset fs_type=$(echo $strfs | awk '{print $2}')
 143         if [[ $fs_type == ufs ]]; then
 144                 ZFSPOOL=""      
 145 
 146                 # create new zpool from file
 147                 zpool list | grep share_pool > /dev/null 2>&1
 148                 if (( $? != 0 )); then
 149                         typeset F_ZPOOL=$TESTDIR/newzpool.file.$$
 150                         mkfile 1536m $F_ZPOOL
 151                         if (( $? != 0 )); then
 152                                 cti_result FAIL \
 153                                         "failed to create 1536m file<$F_ZPOOL>"
 154                                 return
 155                         fi
 156                         create_zpool -f share_pool $F_ZPOOL
 157                         if (( $? != 0 )); then
 158                                 cti_result FAIL \
 159                                         "failed to create zpool<share_pool>"
 160                                 return
 161                         fi
 162                 else
 163                         cti_result FAIL "share_pool pool already exists"
 164                         return
 165                 fi
 166         elif [[ $fs_type == zfs ]]; then
 167                 #
 168                 # Do not assume that all the existing pool can be used for testing.
 169                 # Should always create a test pool for testing.
 170                 #
 171                 # The old code always uses root pool for testing purpose
 172                 # which is dangerous and always trashes the root pool after test.
 173                 #
 174                 # To fix the problem, always create a separate test pool
 175                 #
 176                 ZFSPOOL=test_pool
 177                 TESTPOOL_VDEV=$TESTDIR/testpool.file.$$
 178                 mkfile 2g $TESTPOOL_VDEV
 179                 create_zpool -f $ZFSPOOL $TESTPOOL_VDEV
 180                 if (( $? != 0 )); then
 181                         cti_result FAIL \
 182                                 "failed to create zpool<test_pool>"
 183                         return
 184                 fi
 185         else
 186                 cti_result FAIL "TESTDIR<$TESTDIR> is based $fs_type, \
 187                         but this test suite only supports UFS and ZFS!"
 188                 return
 189         fi
 190 
 191         exec 3<$configfile_tmpl
 192         while :
 193         do
 194                 read -u3 line
 195                 if [[ $? = 1 ]]
 196                 then
 197                         break
 198                 fi
 199                 if [[ "$line" = *([     ]) || "$line" = *([     ])#* ]]
 200                 then
 201                         echo $line
 202                         continue
 203                 fi
 204 
 205                 variable_name=`echo $line | awk -F= '{print $1}'`
 206                 eval variable_value=\${$variable_name}
 207                 if [[ -z $variable_value ]]
 208                 then
 209                         echo "$line"
 210                 else
 211                         echo $variable_name=$variable_value
 212                 fi
 213         done > $configfile
 214 
 215         # record CTI_LOGDIR variable used by run_test command at configure phase
 216         # to configuration file for saving results of later phases to the same
 217         # directory if user does not specify the log directory again in other
 218         # run_test commands
 219         typeset cti_logdir=$(dirname $CTI_LOGDIR)
 220         [[ $cti_logdir != "/var/tmp" ]] && \
 221                 echo "CTI_LOGDIR=$cti_logdir" >> $configfile
 222 
 223         chmod 0444 $configfile
 224 
 225         if [[ -n $setup_once && $setup_once != "TRUE" ]]; then
 226                 cti_result PASS "share test suite is ready to run!"
 227                 return
 228         fi
 229 
 230         build_fs
 231         if (( $? != 0 )); then
 232                 cti_result FAIL "failed to build test file systems"
 233                 return
 234         fi
 235 
 236         cti_result PASS "share test suite is ready to run!"
 237 }
 238 
 239 #
 240 # NAME
 241 #       unconfig_test_pool
 242 #
 243 # DESCRIPTION
 244 #       This function is used to cleanup and destroy the pools 
 245 #       created for testing.  The function takes <poolname>
 246 #       as the only argument.
 247 #
 248 function unconfig_test_pool {
 249         POOL=$1
 250 
 251         umount /$POOL
 252         if (( $? != 0 )); then
 253                 tet_infoline "WARNING: unable to umount $POOL"
 254         fi
 255         zpool destroy -f $POOL
 256         [[ $? != 0 ]] && tet_infoline \
 257         "WARNING: unable to remove $POOL"
 258 }
 259 
 260 #
 261 # NAME
 262 #       unconfig_test_suite
 263 #
 264 # DESCRIPTION
 265 #       The test purpose the test suite calls to un-initialize and
 266 #       configure the test suite.
 267 #
 268 function unconfigure {
 269         typeset -i ret=0
 270 
 271         # clean up test fs
 272         [[ $setup_once == "TRUE" ]] && clean_fs
 273 
 274         # delete zfs test pools if needed
 275         zpool list share_pool > /dev/null 2>&1
 276         if (( $? == 0 )); then
 277                 unconfig_test_pool share_pool   
 278         fi
 279 
 280         zpool list test_pool > /dev/null 2>&1
 281         if (( $? == 0 )); then
 282                 unconfig_test_pool test_pool    
 283         fi
 284 
 285         # remove temporary test directory if it is empty,
 286         # but it will not be removed if it contains test result(s)
 287         rmdir $SHR_TMPDIR > /dev/null 2>&1
 288 
 289         #
 290         # Remove the test dir and configuration file provided, and verify
 291         # the results of the dir/file removal.
 292         #
 293         rm -fr $TESTDIR
 294         ret=$((ret + $?))
 295         [[ -d $TESTDIR ]] && tet_infoline \
 296                         "WARNING: unable to remove $TESTDIR directory"
 297         rm -f $configfile
 298         ret=$((ret + $?))
 299         [[ -f $configfile ]] && tet_infoline \
 300                         "WARNING: unable to remove $configfile file"
 301 
 302         if (( $ret == 0 )); then
 303                 cti_result PASS "unconfigure share test suite successfully"
 304         else
 305                 cti_result FAIL "something was wrong during unconfigure, \
 306                         please have a check and do cleanup manually if needed"
 307         fi
 308 }
 309 
 310 function startup {
 311         tet_infoline "Create config file $configfile"
 312 }
 313 
 314 . ${TET_SUITE_ROOT}/sharefs-tests/lib/share_common
 315 . ${TET_ROOT}/common/lib/ctiutils.ksh
 316 . ${TET_ROOT}/lib/ksh/tcm.ksh