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