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 #
29 # Setup the SERVER for testing nfslogd.
30 #
31
32 NAME=$(basename $0)
33
34 Usage="Usage: $NAME -s | -c | -C \n
35 -s: to setup this host for nfslogd test\n
36 -c: to cleanup the server for nfslogd test\n
37 -C: to check if nfslogd is running, if not, try to start 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 # Include common STC utility functions
51 if [[ -s $STC_GENUTILS/include/nfs-util.kshlib ]]; then
52 . $STC_GENUTILS/include/nfs-util.kshlib
53 else
54 . $STF_TMPDIR/nfs-util.kshlib
55 fi
56
57 # cleanup function on all exit
58 function cleanup {
59 [[ :$SHAREMNT_DEBUG: == *:$NAME:* \
60 || :$SHAREMNT_DEBUG: == *:all:* ]] && set -x
61
62 rm -fr $STF_TMPDIR/*.$$
63 exit $1
64 }
65
66 # Turn on debug info, if requested
67 export STC_GENUTILS_DEBUG=$SHAREMNT_DEBUG
68 [[ :$SHAREMNT_DEBUG: == *:$NAME:* \
69 || :$SHAREMNT_DEBUG: == *:all:* ]] && set -x
70
71 Test_Log_Dir="/var/nfs/smtest"
72 Lock_Dir="/var/tmp/sharemnt_lock"
73 Zonename=$(zonename)
74 Timeout=600
75
76 getopts scC opt
77 case $opt in
78 s)
79 # check if multi-client are talking the server
80 # Only first client does real setup, other clients
81 # just wait for the end.
82 if [[ -f $Lock_Dir/.stf_configure && \
83 -f $Lock_Dir/.stf_unconfigure ]]; then
84 # increase the referent count
85 ref_unconfig=$(cat $Lock_Dir/.stf_unconfigure)
86 ref_unconfig=$((ref_unconfig + 1))
87 echo "$ref_unconfig" > $Lock_Dir/.stf_unconfigure
88 sync
89
90 # wait first client to finish setup
91 condition="(( \$(cat $Lock_Dir/.stf_configure) == 0 ))"
92 wait_now $Timeout "$condition" 5
93 if (( $? == 0 )); then
94 echo "Done - Other client has finished the setup."
95 exit 0
96 fi
97
98 echo "$NAME: TIMEOUT - Other clients can not finish setup \c"
99 echo " after 10 minutes"
100 exit 1
101 else
102 # create lock files for three phases.
103 mkdir -p $Lock_Dir
104 # "1" for config starting
105 echo "1" > $Lock_Dir/.stf_configure
106 # No test is running
107 echo "0" > $Lock_Dir/.stf_execute
108 # the current number of client
109 echo "1" > $Lock_Dir/.stf_unconfigure
110 sync
111 fi
112
113 if [[ ! -d $NFSLOGDDIR ]]; then
114 mkdir -p -m 0777 $NFSLOGDDIR
115 (( $? != 0 )) && echo "could not create $NFSLOGDDIR" && exit 1
116 fi
117
118 # set up ZFS
119 if [[ -n $ZFSPOOL ]]; then
120 create_zfs_fs $ZFSBASE $NFSLOGDDIR > $STF_TMPDIR/$NAME.zfs.$$ 2>&1
121 if (( $? != 0 )); then
122 echo "$NAME: failed to create_zfs_fs $NFSLOGDDIR"
123 cat $STF_TMPDIR/$NAME.zfs.$$
124 cleanup 99
125 fi
126 fi
127
128 # Save original nfslog.conf and nfslogd config files
129 mv /etc/nfs/nfslog.conf /etc/nfs/nfslog.conf.orig
130 mv /etc/default/nfslogd /etc/default/nfslogd.orig
131 cp $STF_TMPDIR/nfslog.conf /etc/nfs/nfslog.conf \
132 > $STF_TMPDIR/$NAME.cp.$$ 2>&1
133 if (( $? != 0 )); then
134 echo "$NAME: ERROR - failed to cp [/etc/nfs/nfslog.conf]"
135 cat $STF_TMPDIR/$NAME.cp.$$
136 cleanup 99
137 fi
138 cp $STF_TMPDIR/nfslogd /etc/default/nfslogd \
139 > $STF_TMPDIR/$NAME.cp.$$ 2>&1
140 if (( $? != 0 )); then
141 echo "$NAME: ERROR - failed to cp [/etc/default/nfslogd]"
142 cat $STF_TMPDIR/$NAME.cp.$$
143 cleanup 99
144 fi
145
146 pgrep -z $Zonename -x -u 0 nfslogd > /dev/null 2>&1
147 if (( $? == 0 )); then
148 # stop the daemon first
149 pkill -HUP -z $Zonename -x -u 0 nfslogd
150 condition="! pgrep -z $Zonename -x -u 0 nfslogd > /dev/null"
151 wait_now 10 "$condition"
152 (( $? != 0 )) && pkill -TERM -z $Zonename -x -u 0 nfslogd
153 fi
154 pgrep -z $Zonename -x -u 0 nfslogd > $STF_TMPDIR/$NAME.pgrep.$$ 2>&1
155 if (( $? == 0 )); then
156 echo "$NAME: ERROR - failed to kill nfslogd"
157 cat $STF_TMPDIR/$NAME.pgrep.$$
158 cleanup 99
159 fi
160
161 # remove the log file if exists
162 rm -rf $Test_Log_Dir/
163
164 # create the log directory
165 mkdir $Test_Log_Dir
166 cd $Test_Log_Dir; mkdir results defaults absolute;
167
168 # start nfslogd
169 touch /etc/nfs/nfslogtab
170 /usr/lib/nfs/nfslogd > $STF_TMPDIR/$NAME.nfslogd.$$ 2>&1
171 if (( $? != 0 )); then
172 echo "$NAME: ERROR - failed to start nfslogd"
173 cat $STF_TMPDIR/$NAME.nfslogd.$$
174 cleanup 99
175 fi
176 # wait a while and check nfslogd is running
177 condition="pgrep -z $Zonename -x -u 0 nfslogd > /dev/null 2>&1"
178 wait_now 20 "$condition"
179 if (( $? != 0 )); then
180 echo "$NAME: ERROR - nfslogd is still not running after 20 seconds"
181 cleanup 99
182 fi
183
184 echo "0" > $Lock_Dir/.stf_configure # "0" for setup end
185 sync
186 echo "Done - setup nfslogd OKAY."
187 cleanup 0
188 ;;
189 c)
190 if [[ ! -f $Lock_Dir/.stf_unconfigure ]]; then
191 echo "ERROR - failed to find lock file<$Lock_Dir/.stf_unconfigure>"
192 exit 1
193 fi
194
195 ref_unconfig=$(cat $Lock_Dir/.stf_unconfigure)
196 if (( $ref_unconfig != 1 )); then
197 ref_unconfig=$((ref_unconfig - 1))
198 echo "$ref_unconfig" > $Lock_Dir/.stf_unconfigure
199 sync
200 echo "Done - other clients will do cleanup, ref=$ref_unconfig"
201 exit 0
202 fi
203
204 # the last client will do real cleanup and remove all lock files
205 rm -rf $Lock_Dir
206
207 # stop the daemon and unshare
208 pkill -HUP -z $Zonename -x -u 0 nfslogd
209 $MISCSHARE $TESTGRP unshare $NFSLOGDDIR
210 sleep 5
211
212 if [[ -n $ZFSPOOL ]]; then
213 Zfs=$(zfs list | grep "$NFSLOGDDIR" | nawk '{print $1}')
214 zfs destroy -f $Zfs > $STF_TMPDIR/$NAME.cleanFS.$$ 2>&1
215 if (( $? != 0 )); then
216 echo "WARNING, unable to cleanup [$Zfs];"
217 cat $STF_TMPDIR/$NAME.cleanFS.$$
218 echo "\t Please clean it up manually."
219 cleanup 2
220 fi
221 fi
222
223 # Restore original nfslog.conf and nfslogd config files:
224 [[ -f /etc/nfs/nfslog.conf.orig ]] && \
225 mv /etc/nfs/nfslog.conf.orig /etc/nfs/nfslog.conf
226 [[ -f /etc/default/nfslogd.orig ]] && \
227 mv /etc/default/nfslogd.orig /etc/default/nfslogd
228
229 rm -rf $Test_Log_Dir $STF_TMPDIR/sharemnt.nfslogd.* \
230 $STF_TMPDIR/test_nfslogd \
231 $STF_TMPDIR/nfslog.conf $STF_TMPDIR/nfslogd
232
233 echo "Done - restore nfslogd configure file OKAY"
234 ;;
235 C)
236 # check if nfslogd is running on server
237 pgrep -z $Zonename -x -u 0 nfslogd > /dev/null 2>&1
238 if (( $? != 0 )); then
239 # start nfslogd
240 touch /etc/nfs/nfslogtab
241 /usr/lib/nfs/nfslogd > $STF_TMPDIR/$NAME.nfslogd.$$ 2>&1
242 if (( $? != 0 )); then
243 echo "$NAME: ERROR - failed to start nfslogd"
244 cat $STF_TMPDIR/$NAME.nfslogd.$$
245 cleanup 99
246 fi
247 # wait a while and check nfslogd is running
248 condition="pgrep -z $Zonename -x -u 0 nfslogd > /dev/null 2>&1"
249 wait_now 20 "$condition"
250 if (( $? != 0 )); then
251 echo "$NAME: ERROR - nfslogd is still not running \c"
252 echo " after 20 seconds"
253 cleanup 99
254 fi
255 fi
256 ;;
257 \?)
258 echo $Usage
259 exit 2
260 ;;
261 esac
262
263 cleanup 0