1 #
   2 # CDDL HEADER START
   3 #
   4 # The contents of this file are subject to the terms of the
   5 # Common Development and Distribution License (the "License").
   6 # You may not use this file except in compliance with the License.
   7 #
   8 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9 # or http://www.opensolaris.org/os/licensing.
  10 # See the License for the specific language governing permissions
  11 # and limitations under the License.
  12 #
  13 # When distributing Covered Code, include this CDDL HEADER in each
  14 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15 # If applicable, add the following below this CDDL HEADER, with the
  16 # fields enclosed by brackets "[]" replaced with your own identifying
  17 # information: Portions Copyright [yyyy] [name of copyright owner]
  18 #
  19 # CDDL HEADER END
  20 #
  21 
  22 #
  23 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24 # Use is subject to license terms.
  25 #
  26 
  27 #
  28 # NAME
  29 #       host_is_alive()
  30 # DESCRIPTION
  31 #       function to test whether the host is alive by ping command
  32 #
  33 # ARGUMENT
  34 #       $1 HOSTNAME
  35 #
  36 # RETURN
  37 #       0 - can be pinged.
  38 #       1 - can not be pinged.
  39 #
  40 function host_is_alive
  41 {
  42         cti_report "wait for $HOSTNAME up to ping"
  43         typeset maxRound=30
  44         typeset HOSTNAME=$1
  45 
  46         indexRound=0    
  47         run_ksh_cmd "/usr/sbin/ping $HOSTNAME 1"
  48         while [ `get_cmd_retval` -ne 0 ]
  49         do
  50                 sleep 10
  51                 run_ksh_cmd "/usr/sbin/ping $HOSTNAME 1"
  52                 (( indexRound+=1 ))
  53                 if [ $indexRound -gt $maxRound ];then
  54                         cti_report "ping alive maximum $maxRound round, "\
  55                                 "sys startup failed."
  56                         return 1
  57                 fi
  58         done
  59         cti_report "$HOSTNAME is alive"
  60         return 0
  61 }
  62 #
  63 # NAME
  64 #       host_is_ready()
  65 # DESCRIPTION
  66 #       function to test if the host is ready for rsh or rlogin
  67 #
  68 # ARGUMENT
  69 #       $1 HOSTNAME
  70 #
  71 # RETURN
  72 #       0 - can be rlogin or rsh.
  73 #       1 - can not be rlogin or rsh.
  74 #
  75 function host_is_ready
  76 {
  77         cti_report "wait for $HOSTNAME ready to rsh"
  78         typeset maxRound=30
  79         typeset HOSTNAME=$1
  80 
  81         indexRound=0
  82         run_rsh_cmd $HOSTNAME "ls"
  83         while [ `get_cmd_retval` -ne 0 ]
  84         do
  85                 sleep 10
  86                 run_rsh_cmd $HOSTNAME "ls"
  87                 (( indexRound+=1 ))
  88                 if [ $indexRound -gt $maxRound ];then
  89                         cti_report "attempt rsh maximum $maxRound round, "\
  90                                 "rsh service failed."
  91                         return 1
  92                 fi
  93         done
  94         cti_report "$HOSTNAME is ready"
  95         return 0
  96 }
  97 
  98 #
  99 # NAME
 100 #       host_is_down
 101 # DESCRIPTION
 102 #       function to test if the host is down
 103 #
 104 # ARGUMENT
 105 #       $1 HOSTNAME
 106 # RETURN
 107 #       Sets the result code
 108 #       0 - if the system shutdown was successful
 109 #       1 - if the system shutdown was not successful
 110 #
 111 # this function is to reboot host with options: -r, -d, etc.
 112 function host_is_down
 113 {
 114         cti_report "wait for $HOSTNAME down"
 115         typeset maxRound=30
 116         typeset HOSTNAME=$1
 117   
 118         indexRound=0
 119         run_ksh_cmd "/usr/sbin/ping $HOSTNAME 1"
 120         while [ `get_cmd_retval` -eq 0 ]
 121         do
 122                 sleep 10
 123                 run_ksh_cmd "/usr/sbin/ping $HOSTNAME 1"
 124                 (( indexRound+=1 ))
 125                 if [ $indexRound -gt $maxRound ];then
 126                         cti_report "ping down maximum $maxRound round, "\
 127                                 "sys shutdown failed."
 128                         return 1
 129                 fi
 130         done
 131   
 132         cti_report "$HOSTNAME is down"
 133         return 0
 134 }
 135 
 136 #
 137 # NAME
 138 #       host_reconfigure
 139 # DESCRIPTION
 140 #       function to reconfigure device file system
 141 #
 142 # ARGUMENT
 143 #       $1 HOSTNAME
 144 # RETURN
 145 #       Sets the result code
 146 #       0 - if the system is reconfigured successfully
 147 #       1 - if the system was not reconfigured successfully
 148 #
 149 # this function is to reboot host with options: -r, -d, etc.
 150 function host_reconfigure
 151 {
 152         cti_report "reconfigure $HOSTNAME device filesystem"
 153         typeset HOSTNAME=$1
 154  
 155         typeset cmd="$DEVFSADM -C"
 156         run_rsh_cmd $HOSTNAME "$cmd"
 157         if [ `get_cmd_retval` -ne 0 ];then
 158                 cti_report "WARNING - $HOSTNAME : $cmd failed."
 159                 report_err "$cmd"
 160                 return 1
 161         fi
 162 
 163         cti_report "$HOSTNAME is reconfigured"
 164         return 0
 165 }
 166 
 167 
 168 #
 169 # NAME
 170 #       host_reboot
 171 # DESCRIPTION
 172 #       remote command report FAIL not accomplished in specified 
 173 #       timeout duration, or return the result.
 174 #
 175 # ARGUMENT
 176 #       $1 HOSTNAME
 177 # RETURN
 178 #       Sets the result code
 179 #       0 - if the system reboot was successful
 180 #       1 - if the system reboot was not successful
 181 #
 182 # this function is to reboot host with options: -r, -d, etc.
 183 function host_reboot
 184 {
 185         typeset HOSTNAME=$1
 186         
 187         run_rsh_cmd $HOSTNAME "sync"
 188         # the only way to get the rapid exit of RSH, that's a tip.
 189         case $2 in
 190                 '-r')
 191                         cti_report "reboot -- \"kmdb -rv\" on host $HOSTNAME"
 192                         # workaround for CR 6764660 on ZFS filesystem
 193                         #rsh -l root $HOSTNAME "reboot -- \"kmdb -rv\"" \
 194                         run_rsh_cmd $HOSTNAME "reboot" \
 195                                 >/dev/null 2>&1 &
 196                         ;;
 197                 '-d')
 198                         cti_report "reboot -d on host $HOSTNAME"
 199                         run_rsh_cmd $HOSTNAME "reboot -d" \
 200                                 >/dev/null 2>&1 &
 201                         ;;
 202                 '-w')
 203                         cti_report "waiting host $HOSTNAME DOWN to UP"
 204                         ;;
 205                 *)      
 206                         cti_report "reboot on host $HOSTNAME"
 207                         run_rsh_cmd $HOSTNAME "reboot" >/dev/null 2>&1 &
 208                         ;;
 209         esac
 210 
 211         typeset ret_code=0
 212         
 213         host_is_down $HOSTNAME
 214         (( ret_code+=$? ))
 215 
 216         host_is_alive $HOSTNAME 
 217         (( ret_code+=$? ))
 218 
 219         host_is_ready $HOSTNAME 
 220         (( ret_code+=$? ))
 221 
 222         host_reconfigure $HOSTNAME
 223         (( ret_code+=$? ))
 224 
 225         if [ $ret_code -eq 0 ];then
 226                 cti_report "$HOSTNAME is rebooted successfully"
 227                 return 0
 228         else
 229                 cti_report "$HOSTNAME is rebooted with warnings"
 230                 return 1
 231         fi
 232 }
 233