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 "@(#)srv_setup.ksh 1.16 09/08/11 SMI" 28 # 29 30 # Setup the SERVER for testing remote locking. 31 32 NAME=$(basename $0) 33 34 Usage="Usage: $NAME -s | -c | -r | -f | -u \n 35 -s: to setup this host with mountd/nfsd\n 36 -c: to cleanup the server\n 37 -r: to reshare SHRDIR with specified options\n 38 -f: to do some FMRI operations on server\n 39 -u: find the shared file and unshare it\n 40 " 41 if (( $# < 1 )); then 42 echo $Usage 43 exit 99 44 fi 45 46 # variables gotten from client system: 47 STF_TMPDIR=STF_TMPDIR_from_client 48 SHAREMNT_DEBUG=${SHAREMNT_DEBUG:-"SHAREMNT_DEBUG_from_client"} 49 50 . $STF_TMPDIR/srv_config.vars 51 52 SHROPTS=$STF_TMPDIR/ShrOpts.sharemnt 53 TESTGRPSTAT=$STF_TMPDIR/FtgStat.sharemnt 54 55 # Include common STC utility functions for SMF 56 if [[ -s $STC_GENUTILS/include/libsmf.shlib ]]; then 57 . $STC_GENUTILS/include/libsmf.shlib 58 . $STC_GENUTILS/include/nfs-smf.kshlib 59 else 60 . $STF_TMPDIR/libsmf.shlib 61 . $STF_TMPDIR/nfs-smf.kshlib 62 fi 63 . $STF_TMPDIR/sharemnt.kshlib 64 65 # Turn on debug info, if requested 66 export STC_GENUTILS_DEBUG=$SHAREMNT_DEBUG 67 [[ :$SHAREMNT_DEBUG: == *:$NAME:* \ 68 || :$SHAREMNT_DEBUG: == *:all:* ]] && set -x 69 70 # cleanup function on all exit 71 function cleanup { 72 [[ :$SHAREMNT_DEBUG: == *:$NAME:* \ 73 || :$SHAREMNT_DEBUG: == *:all:* ]] && set -x 74 75 rm -fr $STF_TMPDIR/*.$$ 76 exit $1 77 } 78 79 getopts scr:f:u: opt 80 case $opt in 81 s) 82 # get fs type 83 strfs=$(get_fstype $TESTDIR) 84 if (( $? != 0 )); then 85 echo "$NAME: get_fstype<$TESTDIR> failed" 86 echo $strfs 87 cleanup 1 88 fi 89 fs_type=$(echo $strfs | awk '{print $2}') 90 if [[ $fs_type == ufs ]]; then 91 ZFSPOOL="" 92 elif [[ $fs_type == zfs ]]; then 93 ZFSPOOL=$(echo $strfs | awk '{print $3}') 94 ZFSPOOL_STAT=$(echo $strfs | awk '{print $4}') 95 if [[ $ZFSPOOL_STAT != "ONLINE" ]]; then 96 echo "zpool<$ZFSPOOL> is not online" 97 cleanup 1 98 fi 99 100 echo "export ZFSPOOL=$ZFSPOOL" >> $STF_TMPDIR/srv_config.vars 101 else 102 cleanup 2 103 fi 104 # print for client's need 105 echo "SRV_FS=$fs_type $ZFSPOOL" 106 107 # create test user 108 useradd -u $TUID01 -c $TUSER_UTAG -d /tmp $TUSER01 \ 109 > $STF_TMPDIR/useradd.out.$$ 2>&1 110 if (( $? != 0 )); then 111 echo "could not create $TUSER01" 112 cat $STF_TMPDIR/useradd.out.$$ 113 cleanup 1 114 fi 115 116 # setup testing group 117 if [[ -z $TESTGRP ]]; then 118 echo "Testing group <TESTGRP> must be specified" 119 cleanup 1 120 fi 121 122 sharemgr list | grep -w "$TESTGRP" > /dev/null 2>&1 123 if (( $? != 0 )); then 124 sharemgr create -P nfs $TESTGRP > $STF_TMPDIR/sh-create.$$ 2>&1 125 if (( $? != 0 )); then 126 echo "could not create $TESTGRP" 127 cat $STF_TMPDIR/sh-create.$$ 128 cleanup 1 129 fi 130 else 131 GrpStat=$(sharemgr list -v | grep -w "$TESTGRP" | \ 132 awk '{print $2}') 133 if [[ $GrpStat == "disabled" ]]; then 134 sharemgr enable $TESTGRP 135 if (( $? != 0 )); then 136 echo "could not enable $TESTGRP" 137 cleanup 1 138 fi 139 fi 140 echo "$TESTGRP $GrpStat" > $TESTGRPSTAT 141 fi 142 143 144 # get NFSMAPID_DOMAIN and client will use it 145 srv_nfsmapid_domain=$(cat /var/run/nfs4_domain) 146 echo "SRV_NFSMAPID_DOMAIN=$srv_nfsmapid_domain" 147 148 # set up ZFS 149 if [[ -n $ZFSPOOL ]]; then 150 ZFSBASE=$(zfs list -o mountpoint,name \ 151 | egrep "^$TESTDIR " | nawk '{print $2}') 152 if (( $? == 0 )) && [[ -n $ZFSBASE ]]; then 153 zfs destroy -r -f $ZFSBASE > $STF_TMPDIR/cleanFS.out.$$ 2>&1 154 if (( $? != 0 )); then 155 echo "WARNING, unable to destroy [$ZFSBASE];" 156 cat $STF_TMPDIR/cleanFS.out.$$ 157 echo "\t Please clean it up manually." 158 cleanup 2 159 fi 160 fi 161 162 create_zfs_fs $ZFSPOOL $TESTDIR > $STF_TMPDIR/zfs.out.$$ 2>&1 163 if (( $? != 0 )); then 164 echo "$NAME: failed to create_zfs_fs $TESTDIR - " 165 cat $STF_TMPDIR/zfs.out.$$ 166 cleanup 2 167 fi 168 169 ZFSBASE=$(zfs list -o mountpoint,name \ 170 | egrep "^$TESTDIR " | nawk '{print $2}') 171 echo "export ZFSBASE=$ZFSBASE" >> $STF_TMPDIR/srv_config.vars 172 173 create_zfs_fs $ZFSBASE $SHRDIR > $STF_TMPDIR/zfs.out.$$ 2>&1 174 if (( $? != 0 )); then 175 echo "$NAME: failed to create_zfs_fs $SHRDIR -" 176 cat $STF_TMPDIR/zfs.out.$$ 177 cleanup 2 178 fi 179 print_debug $STF_TMPDIR/zfs.out.$$ 180 else 181 rm -rf $TESTDIR 182 mkdir -pm 0777 $SHRDIR 183 if (( $? != 0 )); then 184 echo "$NAME: could not create $SHRDIR" 185 cleanup 1 186 fi 187 fi 188 189 nfs_smf_setup "rw" $SHRDIR $SMF_TIMEOUT > $STF_TMPDIR/setup.$$ 2>&1 190 if (( $? != 0 )); then 191 echo "$NAME: nfs_smf_setup failed for $SHRDIR." 192 cat $STF_TMPDIR/setup.$$ 193 cleanup 1 194 fi 195 print_debug $STF_TMPDIR/setup.$$ 196 197 /usr/sbin/svcadm refresh $SRV_FMRI 198 sleep 5 199 200 # Check the state of the SMF FMRI's to verify this. 201 for fmri in $LCK_FMRI $STAT_FMRI ; do 202 smf_fmri_transition_state "do" $fmri "online" $SMF_TIMEOUT 203 if (( $? != 0 )); then 204 echo "$NAME: unable to set $fmri to state online" 205 cleanup 1 206 fi 207 done 208 209 # Create few test files for tests/shmnt_file 210 cd $SHRDIR 211 rm -f rwfile rootfile nopermfile 212 cp $0 rwfile; chmod 666 rwfile 213 cp $0 rootfile; chmod 644 rootfile 214 head -22 $0 > nopermfile; chmod 400 nopermfile 215 216 echo "Done - setup NFSD/MOUNTD, and SHRDIR." 217 ;; 218 r) 219 SHRDIR=$OPTARG 220 # Unshare SHRDIR and reshare it with option provided by client 221 if [[ ! -f $SHROPTS ]]; then 222 echo "$NAME: Can't find <$SHROPTS> file" 223 exit 2 224 fi 225 ShrOpts=$(cat $SHROPTS) 226 227 $MISCSHARE $TESTGRP unshare $SHRDIR > $STF_TMPDIR/unshare.$$ 228 if (( $? != 0 )); then 229 echo "Failed - unshare $SHRDIR" 230 cat $STF_TMPDIR/unshare.$$ 2>&1 231 cleanup 2 232 fi 233 print_debug $STF_TMPDIR/unshare.$$ 234 235 $MISCSHARE $TESTGRP share $SHRDIR $ShrOpts > $STF_TMPDIR/share.$$ 2>&1 236 if (( $? != 0 )); then 237 echo "$NAME: failed to share $SHRDIR with <$ShrOpts> options" 238 cat $STF_TMPDIR/share.$$ 239 cleanup 2 240 fi 241 print_debug $STF_TMPDIR/share.$$ 242 243 # sharemgr/share prints share_options in random order 244 NSopts=$(echo $ShrOpts | sed 's/,/ /g') 245 for opt in $NSopts; do 246 if echo $opt | grep ":" | grep "sec=" > /dev/null; then 247 opt_name=${opt%%=*} 248 opt=$(echo $opt | sed "s/:/,.*$opt_name=/g") 249 fi 250 condition="share | grep \" $SHRDIR \" | egrep \"$opt\" \ 251 > $STF_TMPDIR/share.$$ 2>&1" 252 wait_now 10 "$condition" 253 if (( $? != 0 )); then 254 echo "$NAME: share -o <$ShrOpts> $SHRDIR was unsuccessful" 255 echo "\tExpected to see <$opt> from share:" 256 share 257 cleanup 2 258 fi 259 done 260 261 echo "Done - reshare SHRDIR with <$ShrOpts> OK" 262 ;; 263 c) 264 EXIT_CODE=0 265 # cleanup SHRDIR 266 nfs_smf_clean $SHRDIR $SMF_TIMEOUT >> $STF_TMPDIR/cleanup.$$ 2>&1 267 if (( $? != 0 )); then 268 echo "Failed - cleanup server program." 269 echo "\t nfs_smf_clean $SHRDIR" 270 cat $STF_TMPDIR/cleanup.$$ 271 (( EXIT_CODE += 1 )) 272 fi 273 print_debug $STF_TMPDIR/cleanup.$$ 274 sleep 5 275 276 # destory zfs of TESTDIR 277 if [[ -n $ZFSPOOL ]]; then 278 ZFSBASE=$(zfs list -o mountpoint,name \ 279 | egrep "^$TESTDIR " | nawk '{print $2}') 280 if (( $? == 0 )) && [[ -n $ZFSBASE ]]; then 281 zfs destroy -r -f $ZFSBASE > $STF_TMPDIR/cleanFS.out.$$ 2>&1 282 if (( $? != 0 )); then 283 echo "WARNING, unable to cleanup [$ZFSBASE];" 284 cat $STF_TMPDIR/cleanFS.out.$$ 285 echo "\t Please clean it up manually." 286 (( EXIT_CODE += 1 )) 287 fi 288 fi 289 fi 290 rm -rf $SHRDIR $TESTDIR # if ufs, remove it directly 291 292 # remove BASEDIR if needed 293 ls -d $BASEDIR/clnt_* > $STF_TMPDIR/cleanup.$$ 2>&1 294 if (( $? == 0 )); then 295 echo "Warning: $BASEDIR is not removed for the existing dirs:" 296 cat $STF_TMPDIR/cleanup.$$ 297 else 298 rm -rf $BASEDIR > $STF_TMPDIR/cleanup.$$ 2>&1 299 if (( $? != 0 )); then 300 echo "Failed - cleanup server program." 301 echo "can not remove the directories $BASEDIR" 302 cat $STF_TMPDIR/cleanup.$$ 303 (( EXIT_CODE += 1 )) 304 fi 305 fi 306 307 # remove/restore testing group 308 if [[ -f $TESTGRPSTAT ]]; then 309 GrpStat=$(grep $TESTGRP $TESTGRPSTAT | awk '{print $2}') 310 if [[ $GrpStat == "disabled" ]]; then 311 sharemgr disable $TESTGRP 312 if (( $? != 0 )); then 313 echo "Waring: disable $TESTGRP failed." 314 sharemgr list -v | grep $TESTGRP 315 (( EXIT_CODE += 1 )) 316 fi 317 fi 318 rm -f $TESTGRPSTAT 319 else 320 sharemgr delete -f $TESTGRP 321 if (( $? != 0 )); then 322 echo "Warning: $TESTGRP is not removed" 323 sharemgr show -pv $TESTGRP 324 (( EXIT_CODE += 1 )) 325 fi 326 fi 327 328 # delete test user 329 del_users $TUSER_UTAG > $STF_TMPDIR/userdel.out.$$ 2>&1 330 if (( $? != 0 )); then 331 echo "WARNING, failed to delete test users whose tag: $TUSER_UTAG" 332 cat $STF_TMPDIR/userdel.out.$$ 333 (( EXIT_CODE += 1 )) 334 fi 335 336 (( EXIT_CODE == 0 )) && echo "Done - cleanup server program." 337 ;; 338 f) 339 typeset do=$OPTARG 340 shift $((OPTIND - 1)) 341 typeset fmri=$1 342 typeset expstat="online" 343 [[ $do == disable ]] && expstat="disabled" 344 svcadm $do $fmri 345 if (( $? != 0 )); then 346 echo "$NAME: unable to $do $fmri" 347 cleanup 1 348 fi 349 wait_now 10 "svcs $fmri | grep -w $expstat > /dev/null 2>&1" 350 if (( $? != 0 )); then 351 echo "$NAME: failed to $do $fmri" 352 cleanup 1 353 fi 354 echo "Done - $do $fmri OK" 355 ;; 356 u) 357 SHRDIR=$OPTARG 358 # find the shared file from sharetab and unshare it 359 REAL_SHRDIR=$(grep "$SHRDIR" /etc/dfs/sharetab | awk '{print $1}') 360 361 if [[ -n $REAL_SHRDIR ]]; then 362 $MISCSHARE $TESTGRP unshare $REAL_SHRDIR >$STF_TMPDIR/unshare.$$ 363 if (( $? != 0 )); then 364 echo "Failed - unshare $REAL_SHRDIR" 365 cat $STF_TMPDIR/unshare.$$ 2>&1 366 cleanup 2 367 fi 368 print_debug $STF_TMPDIR/unshare.$$ 369 fi 370 371 echo "[-u] option: unshare <$REAL_SHRDIR> OK" 372 ;; 373 374 \?) 375 echo $Usage 376 exit 2 377 ;; 378 379 esac 380 381 cleanup $EXIT_CODE 382