1 | #!/bin/ksh |
---|
2 | # IBM_PROLOG_BEGIN_TAG |
---|
3 | # This is an automatically generated prolog. |
---|
4 | # |
---|
5 | # |
---|
6 | # |
---|
7 | # Licensed Materials - Property of IBM |
---|
8 | # |
---|
9 | # (C) COPYRIGHT International Business Machines Corp. 2000,2006 |
---|
10 | # All Rights Reserved |
---|
11 | # |
---|
12 | # US Government Users Restricted Rights - Use, duplication or |
---|
13 | # disclosure restricted by GSA ADP Schedule Contract with IBM Corp. |
---|
14 | # |
---|
15 | # IBM_PROLOG_END_TAG |
---|
16 | # @(#)14 1.44.1.1 src/avs/fs/mmfs/ts/admin/mmglobfuncs.Linux.sh, mmfs, avs_rgpfs24, rgpfs24s007a 9/20/06 11:22:24 |
---|
17 | ############################################################# |
---|
18 | # |
---|
19 | # This file contains declarations and functions that are |
---|
20 | # unique to the Linux operating system environment. |
---|
21 | # |
---|
22 | ############################################################# |
---|
23 | |
---|
24 | sourceFile="mmglobfuncs.Linux.sh" |
---|
25 | [[ -n $DEBUGmmglobfuncsLinux ]] && set -x |
---|
26 | |
---|
27 | #----------------------------------------------------------- |
---|
28 | # Constants and global variables for internal mm command use. |
---|
29 | #----------------------------------------------------------- |
---|
30 | fsDeviceType='b' |
---|
31 | defaultMajorNumber=239 |
---|
32 | minMinorNumber=100 |
---|
33 | maxMinorNumber=255 |
---|
34 | currentMajorNumber=$defaultMajorNumber |
---|
35 | neededMajorNumber=$defaultMajorNumber |
---|
36 | |
---|
37 | # Find out if this is 2.4 or earlier version. |
---|
38 | [[ $(/bin/uname -r) = 2.[0-4].* ]] && version24=yes || version24="" |
---|
39 | |
---|
40 | |
---|
41 | #----------------------------------------------------------- |
---|
42 | # Return codes and constants for internal mm command use. |
---|
43 | #----------------------------------------------------------- |
---|
44 | MM_Remotefs2=66 # remote file system (EREMOTE) |
---|
45 | MM_UnknownCluster2=101 # unknown cluster (ENETUNREACH) |
---|
46 | MM_ConnAborted=103 # connection aborted (ECONNABORTED) |
---|
47 | MM_ConnectionReset2=104 # connection reset (ECONNRESET) |
---|
48 | MM_TimedOut2=110 # connection timed out (ETIMEDOUT) |
---|
49 | MM_HostDown2=112 # host is down (EHOSTDOWN) |
---|
50 | MM_DaemonDown2=127 # daemon not running (second value) |
---|
51 | |
---|
52 | |
---|
53 | #----------------------------------------------------------- |
---|
54 | # Full path names for Linux system commands |
---|
55 | #----------------------------------------------------------- |
---|
56 | automount=/usr/sbin/automount |
---|
57 | awk=/bin/awk |
---|
58 | cat=/bin/cat |
---|
59 | chmod=/bin/chmod |
---|
60 | chown=/bin/chown |
---|
61 | cp=/bin/cp |
---|
62 | cut=cut # /bin/cut or /usr/bin/cut depending on the RH release |
---|
63 | date=/bin/date |
---|
64 | dd=/bin/dd |
---|
65 | df=/bin/df |
---|
66 | diff=/usr/bin/diff |
---|
67 | egrep=/bin/egrep |
---|
68 | find=/usr/bin/find |
---|
69 | fgrep=/bin/fgrep |
---|
70 | grep=/bin/grep |
---|
71 | head=/usr/bin/head |
---|
72 | # host=/usr/bin/host |
---|
73 | host="host_aixformat" # internal function to convert the host output |
---|
74 | hostname=/bin/hostname |
---|
75 | id=/usr/bin/id |
---|
76 | ifconfig=/sbin/ifconfig |
---|
77 | insmod=/sbin/insmod |
---|
78 | join=/usr/bin/join |
---|
79 | kill=/bin/kill |
---|
80 | locale=/usr/bin/locale |
---|
81 | ln=/bin/ln |
---|
82 | ls=/bin/ls |
---|
83 | lsmod=/sbin/lsmod |
---|
84 | lssrc=/usr/bin/lssrc |
---|
85 | mkdir=/bin/mkdir |
---|
86 | mknod=/bin/mknod |
---|
87 | mount=/bin/mount |
---|
88 | mv=/bin/mv |
---|
89 | nm=/usr/bin/nm |
---|
90 | openssl=/usr/bin/openssl |
---|
91 | perl=/usr/bin/perl |
---|
92 | ping=/bin/ping |
---|
93 | ps=/bin/ps |
---|
94 | rcp=/usr/bin/rcp |
---|
95 | rm=/bin/rm |
---|
96 | rmdir=/bin/rmdir |
---|
97 | rmmod=/sbin/rmmod |
---|
98 | rpm=/bin/rpm |
---|
99 | rsh=/usr/bin/rsh |
---|
100 | sed=/bin/sed |
---|
101 | sleep=/bin/sleep |
---|
102 | sort=/bin/sort |
---|
103 | srcmstr=/sbin/srcmstr |
---|
104 | startsrc=/usr/bin/startsrc |
---|
105 | stopsrc=/usr/bin/stopsrc |
---|
106 | sum=/usr/bin/sum |
---|
107 | sync=/bin/sync |
---|
108 | tail=/usr/bin/tail |
---|
109 | tee=/usr/bin/tee |
---|
110 | touch=/bin/touch |
---|
111 | tr=/usr/bin/tr |
---|
112 | umount=/bin/umount |
---|
113 | uname=/bin/uname |
---|
114 | unmount=/bin/umount # synonym for umount |
---|
115 | wc=/usr/bin/wc |
---|
116 | |
---|
117 | |
---|
118 | #----------------------------------------------------------- |
---|
119 | # Well-known file names |
---|
120 | #----------------------------------------------------------- |
---|
121 | etcFilesystems="/etc/fstab" # Filesystem stanzas used by the mount command |
---|
122 | etcHosts="/etc/hosts" # Local host name resolution file |
---|
123 | etcHostConf="/etc/host.conf" # Resolver configuration file |
---|
124 | auto_master="/etc/auto_master" # master input for the automount daemon |
---|
125 | autoMaster="/etc/auto.master" # master input for the automount daemon |
---|
126 | |
---|
127 | |
---|
128 | #------------------------------- |
---|
129 | # Commands with common options |
---|
130 | #------------------------------- |
---|
131 | |
---|
132 | # Command to extract a specific field from a line. |
---|
133 | # Example: print -- $line | $GETVALUE $NODE_NAME_Field |
---|
134 | GETVALUE="$cut -d: -f " |
---|
135 | |
---|
136 | # Command to sort the mmsdrfs file prior to storing it. |
---|
137 | # Example: LC_ALL=C $SORT_MMSDRFS $sdrfs -o $sdrfs |
---|
138 | # The sorting is based on the following keys: |
---|
139 | # - the primary key is the nodeset id |
---|
140 | # - the secondary key is the stripe group id |
---|
141 | # - the next key is the line type |
---|
142 | # - the last key is the line number |
---|
143 | # The sorting is based on the ASCII collating sequence, |
---|
144 | # except for the fourth field, which is sorted in |
---|
145 | # arithmetic order. |
---|
146 | SORT_MMSDRFS="$sort -t: -k 1,1 -k 3,3 -k 2,2 -k 4,4n " |
---|
147 | |
---|
148 | |
---|
149 | #------------------------------- |
---|
150 | # Frequently used functions |
---|
151 | #------------------------------- |
---|
152 | |
---|
153 | |
---|
154 | #################################################################### |
---|
155 | # |
---|
156 | # Function: In the Linux environment, function host_aixformat |
---|
157 | # looks up a name using both /etc/hosts and the Linux |
---|
158 | # host command. This function determines the order |
---|
159 | # in which the name is resolved. |
---|
160 | # |
---|
161 | # Input: None. |
---|
162 | # |
---|
163 | # Output: "hosts" - check first the /etc/hosts file. |
---|
164 | # "bind" - start with the host command. |
---|
165 | # |
---|
166 | # Returns: Always zero. |
---|
167 | # |
---|
168 | #################################################################### |
---|
169 | function setHostResolveOrder |
---|
170 | { |
---|
171 | typeset sourceFile="mmglobfuncs.Linux.sh" |
---|
172 | [[ -n $DEBUG || -n $DEBUGsetHostResolveOrder ]] && set -x |
---|
173 | $mmTRACE_ENTER "$*" |
---|
174 | typeset order |
---|
175 | |
---|
176 | # Find out the host lookup order. Our rules are: |
---|
177 | # If 'hosts' is specified first on the order line in /etc/host.conf, |
---|
178 | # search for the name in /etc/hosts before issuing the host command. |
---|
179 | # Otherwise, start with the host command. |
---|
180 | if [[ -f $etcHostConf ]] |
---|
181 | then |
---|
182 | order=$($awk ' \ |
---|
183 | BEGIN { order = "bind" } \ |
---|
184 | $1 == "order" && $2 ~ /^hosts/ { order = "hosts" } \ |
---|
185 | END { print order } \ |
---|
186 | ' $etcHostConf 2>/dev/null) |
---|
187 | else |
---|
188 | order=bind |
---|
189 | fi |
---|
190 | |
---|
191 | print $order |
---|
192 | return 0 |
---|
193 | |
---|
194 | } #-------------- end of function setHostResolveOrder ------------------ |
---|
195 | |
---|
196 | |
---|
197 | #################################################################### |
---|
198 | # |
---|
199 | # Function: Issues the Linux host command and converts the output |
---|
200 | # to make it look as if it came from an AIX host command. |
---|
201 | # If the host command does not resolve the name, try |
---|
202 | # looking up the name in the local /etc/hosts file. |
---|
203 | # |
---|
204 | # Input: $1 - hostname or IP address |
---|
205 | # |
---|
206 | # Output: A line similar to the following: |
---|
207 | # esj.austin.ibm.com is 9.53.145.122 |
---|
208 | # |
---|
209 | # Returns: 0 - hostname resolved |
---|
210 | # non-zero - name not resolved |
---|
211 | # |
---|
212 | #################################################################### |
---|
213 | function host_aixformat # <hostname|ipa> |
---|
214 | { |
---|
215 | typeset sourceFile="mmglobfuncs.Linux.sh" |
---|
216 | [[ -n $DEBUG || -n $DEBUGhost_aixformat ]] && set -x |
---|
217 | $mmTRACE_ENTER "$*" |
---|
218 | typeset inValue=$1 |
---|
219 | |
---|
220 | typeset nameResolved=no |
---|
221 | typeset etcHostsChecked=no |
---|
222 | typeset hostOutput=${tmpDir}hostOutput.${mmcmd}.$$ |
---|
223 | typeset hostErrMsg=${tmpDir}hostErrMsg.${mmcmd}.$$ |
---|
224 | typeset rc awkRc hostResult |
---|
225 | |
---|
226 | [[ -z $inValue ]] && return 1 |
---|
227 | |
---|
228 | # If still not known, determine the lookup order. |
---|
229 | [[ -z $resolveOrder ]] && \ |
---|
230 | resolveOrder=$(setHostResolveOrder) |
---|
231 | |
---|
232 | # If it is OK to do so, see if the host name appears in /etc/hosts. |
---|
233 | if [[ $resolveOrder = hosts && -f $etcHosts ]] |
---|
234 | then |
---|
235 | $awk ' \ |
---|
236 | BEGIN { entriesFound = 0 } \ |
---|
237 | # Skip comment, empty and short lines. \ |
---|
238 | $1 ~ /^#/ || NF <= 1 { next } \ |
---|
239 | # Compare the inValue to each of the fields. \ |
---|
240 | { for ( i = 1; i <= NF; i++ ) { \ |
---|
241 | if ( $i == "'$inValue'" ) { \ |
---|
242 | { print $2 " is " $1 } \ |
---|
243 | { entriesFound += 1 } \ |
---|
244 | { next } \ |
---|
245 | } \ |
---|
246 | # Ignore in-line comments. \ |
---|
247 | if ( $i ~ /^#/ ) { \ |
---|
248 | { break } \ |
---|
249 | } \ |
---|
250 | } \ |
---|
251 | { next } \ |
---|
252 | } \ |
---|
253 | END { \ |
---|
254 | if ( entriesFound == 1 ) { exit 0 } \ |
---|
255 | if ( entriesFound == 0 ) { exit 1 } \ |
---|
256 | { exit 2 } \ |
---|
257 | } \ |
---|
258 | ' $etcHosts |
---|
259 | awkRc=$? |
---|
260 | |
---|
261 | if [[ $awkRc -eq 0 ]] |
---|
262 | then |
---|
263 | # Name successfully resolved using /etc/hosts. |
---|
264 | nameResolved=yes |
---|
265 | rc=0 |
---|
266 | elif [[ $awkRc -eq 2 ]] |
---|
267 | then |
---|
268 | # There is more than one matching entry in /etc/hosts. |
---|
269 | # Give up - multiple names are not allowed. |
---|
270 | printErrorMsg 498 $mmcmd "$inValue ($etcHosts)" |
---|
271 | return 1 |
---|
272 | else |
---|
273 | # Name not found in /etc/hosts but keep going. |
---|
274 | etcHostsChecked=yes |
---|
275 | rc=1 |
---|
276 | fi # end of if [[ $awkRc -eq 0 ]] |
---|
277 | fi # end of if [[ $resolveOrder = hosts ]] |
---|
278 | |
---|
279 | |
---|
280 | # If the name is not resolved yet (because it is not in /etc/hosts |
---|
281 | # or because we have to start with the host command), try to resolve |
---|
282 | # the name using the Linux host command. |
---|
283 | if [[ $nameResolved = no ]] |
---|
284 | then |
---|
285 | # The output of the host command, and as a result the way we parse it, |
---|
286 | # depends on whether the input parameter is a hostname or an IP address. |
---|
287 | if [[ $inValue = +([0-9]).+([0-9]).+([0-9]).+([0-9])?(.) ]] |
---|
288 | then |
---|
289 | # We start with an IP address. The host command output looks like this: |
---|
290 | # 122.145.53.9.IN-ADDR.ARPA domain name pointer esj.austin.ibm.com |
---|
291 | hostResult=$(LC_ALL=C /usr/bin/host -t a $inValue 2>$hostErrMsg) |
---|
292 | rc=$? |
---|
293 | if [[ $rc -eq 0 ]] |
---|
294 | then |
---|
295 | set -f ; set -- $hostResult ; set +f |
---|
296 | if [[ -z $5 || -n $6 || $3 = not && $4 = found* ]] |
---|
297 | then |
---|
298 | # Something is wrong. Collect error information to be later |
---|
299 | # displayed to the user if the name cannot be resolved. |
---|
300 | # Note: Linux sends the 'not found' message to stdout and returns 0. |
---|
301 | # This forces us to do the funny test for $3 and $4 above. |
---|
302 | |
---|
303 | # Unexpected output from the host command. |
---|
304 | printErrorMsg 499 $mmcmd $inValue 2> $hostErrMsg |
---|
305 | /usr/bin/host -t a $inValue >>$hostErrMsg 2>&1 |
---|
306 | rc=1 |
---|
307 | else |
---|
308 | # Things seem to have worked. |
---|
309 | print -- "${5%.} is $inValue" |
---|
310 | nameResolved=yes |
---|
311 | fi |
---|
312 | fi |
---|
313 | |
---|
314 | else |
---|
315 | # We start with a hostname. The host command output looks like this: |
---|
316 | # esj.austin.ibm.com has address 9.53.145.122 |
---|
317 | # It is possible to have nickname line and more than one address line. |
---|
318 | LC_ALL=C /usr/bin/host -t a $inValue >$hostOutput 2>$hostErrMsg |
---|
319 | rc=$? |
---|
320 | if [[ $rc -eq 0 ]] |
---|
321 | then |
---|
322 | # Strip any nickname lines and eliminate duplicates |
---|
323 | hostResult=$($cat $hostOutput | $grep ' has address ' | $sort -u) |
---|
324 | set -f ; set -- $hostResult ; set +f |
---|
325 | if [[ -z $4 || -n $5 || $3 = not && $4 = found* ]] |
---|
326 | then |
---|
327 | # Something is wrong. Collect error information to be later |
---|
328 | # displayed to the user if the name cannot be resolved. |
---|
329 | # Note: Linux sends the 'not found' message to stdout and returns 0. |
---|
330 | # This forces us to do the funny test for $3 and $4 above. |
---|
331 | |
---|
332 | # Unexpected output from the host command. |
---|
333 | printErrorMsg 499 $mmcmd $inValue 2> $hostErrMsg |
---|
334 | /usr/bin/host -t a $inValue >>$hostErrMsg 2>&1 |
---|
335 | |
---|
336 | # Cannot handle multiple interfaces for host $inValue |
---|
337 | [[ -n $5 ]] && printErrorMsg 498 $mmcmd "$inValue" |
---|
338 | rc=1 |
---|
339 | else |
---|
340 | # Things seem to have worked. |
---|
341 | print -- "${1%.} is $4" |
---|
342 | nameResolved=yes |
---|
343 | fi |
---|
344 | fi |
---|
345 | fi # end of if [[ $inValue = +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]] |
---|
346 | fi # end of if [[ $nameResolved = no ]] |
---|
347 | |
---|
348 | |
---|
349 | # If the name is still not resolved, and we haven't tried it yet, |
---|
350 | # try resolving the name by looking up /etc/hosts. |
---|
351 | if [[ $nameResolved = no ]] |
---|
352 | then |
---|
353 | if [[ $etcHostsChecked = no ]] |
---|
354 | then |
---|
355 | $awk ' \ |
---|
356 | BEGIN { entriesFound = 0 } \ |
---|
357 | # Skip comment, empty and short lines. \ |
---|
358 | $1 ~ /^#/ || NF <= 1 { next } \ |
---|
359 | # Compare the inValue to each of the fields. \ |
---|
360 | { for ( i = 1; i <= NF; i++ ) { \ |
---|
361 | if ( $i == "'$inValue'" ) { \ |
---|
362 | { print $2 " is " $1 } \ |
---|
363 | { entriesFound += 1 } \ |
---|
364 | { next } \ |
---|
365 | } \ |
---|
366 | # Ignore in-line comments. \ |
---|
367 | if ( $i ~ /^#/ ) { \ |
---|
368 | { break } \ |
---|
369 | } \ |
---|
370 | } \ |
---|
371 | { next } \ |
---|
372 | } \ |
---|
373 | END { \ |
---|
374 | if ( entriesFound == 1 ) { exit 0 } \ |
---|
375 | if ( entriesFound == 0 ) { exit 1 } \ |
---|
376 | { exit 2 } \ |
---|
377 | } \ |
---|
378 | ' $etcHosts |
---|
379 | awkRc=$? |
---|
380 | fi # end of if [[ $etcHostsChecked = no ]] |
---|
381 | |
---|
382 | if [[ $awkRc -eq 0 ]] |
---|
383 | then |
---|
384 | # Name successfully resolved using /etc/hosts. |
---|
385 | rc=0 |
---|
386 | elif [[ $awkRc -eq 2 ]] |
---|
387 | then |
---|
388 | # There is more than one matching entry in /etc/hosts. |
---|
389 | printErrorMsg 498 $mmcmd "$inValue ($etcHosts)" |
---|
390 | elif [[ -s $hostErrMsg ]] |
---|
391 | then |
---|
392 | # Name not found in /etc/hosts. Show the error information |
---|
393 | # from the Linux host command above. |
---|
394 | $cat $hostErrMsg 1>&2 |
---|
395 | else |
---|
396 | # Name not found in /etc/hosts and there is no other error information. |
---|
397 | printErrorMsg 500 $mmcmd $inValue |
---|
398 | fi # end of if [[ $awkRc -eq 0 ]] |
---|
399 | fi # end of if [[ $nameResolved = no ]] |
---|
400 | |
---|
401 | $rm -f $hostOutput $hostErrMsg |
---|
402 | return $rc |
---|
403 | |
---|
404 | } #-------------- end of function host_aixformat ------------------ |
---|
405 | |
---|
406 | |
---|
407 | ################################################################## |
---|
408 | # |
---|
409 | # Function: Remap ts return codes to OS-independent values. |
---|
410 | # |
---|
411 | # Input: $1 - the return code to be mapped. |
---|
412 | # |
---|
413 | # Output: the remapped input return code |
---|
414 | # |
---|
415 | # Returns: always zero |
---|
416 | # |
---|
417 | # Example: |
---|
418 | # tstatus -m fsname |
---|
419 | # rc=$(remapRC $?) |
---|
420 | # |
---|
421 | ################################################################## |
---|
422 | function remapRC # <returnCode> |
---|
423 | { |
---|
424 | typeset sourceFile="mmglobfuncs.Linux.sh" |
---|
425 | [[ -n $DEBUG || -n $DEBUGremapRC ]] && set -x |
---|
426 | $mmTRACE_ENTER "$*" |
---|
427 | typeset rcIn=$1 |
---|
428 | typeset rcOut |
---|
429 | |
---|
430 | case $rcIn in |
---|
431 | |
---|
432 | $MM_DaemonDown2 ) rcOut=$MM_DaemonDown ;; |
---|
433 | $MM_HostDown2 ) rcOut=$MM_HostDown ;; |
---|
434 | $MM_TimedOut2 ) rcOut=$MM_TimedOut ;; |
---|
435 | $MM_Remotefs2 ) rcOut=$MM_Remotefs ;; |
---|
436 | $MM_UnknownCluster2 ) rcOut=$MM_UnknownCluster ;; |
---|
437 | $MM_ConnectionReset2 ) rcOut=$MM_ConnectionReset ;; |
---|
438 | $MM_ConnAborted ) rcOut=$MM_DaemonDown ;; |
---|
439 | |
---|
440 | * ) rcOut=$rcIn ;; |
---|
441 | |
---|
442 | esac # end of case $rcIn in |
---|
443 | |
---|
444 | # Return the new value. |
---|
445 | print -- "$rcOut" |
---|
446 | return 0 |
---|
447 | |
---|
448 | } #------- end of function remapRC -------------------- |
---|
449 | |
---|