2 #make running virsh commands on a cluster easier
9 Usage: vircmd [OPTION] ... <COMMAND>
11 -x enable script debugging
12 -n don't include the TSM node (if any)
13 -w, --wait [timeout] for start/shutdown wait for desired state change
20 power off cluster nodes (may cause data loss)
23 shutdown cluster nodes
28 CLUSTERNAME can be a glob-style pattern that specifies cluster nodes
33 ############################
34 # parse command line options
35 temp=$(getopt -n "$prog" -o "xnw::" -l help -l wait:: -- "$@")
48 -n) no_tsm=1; shift ;;
49 -w|--wait) wait=true ; timeout="$2" ; shift 2 ;;
51 -h|--help|*) usage ;; # Shouldn't happen, so this is reasonable.
56 echo "Usage: vircmd COMMAND CLUSTERNAME"
66 start) desired_state="running" ;;
67 shutdown) desired_state="shut off" ;;
68 *) echo "waiting not supported with \"$cmd\"" ; echo ; usage ;;
72 export VIRSH_DEFAULT_CONNECT_URI=qemu:///system
76 for i in $domains ; do
79 # If we're not skipping the TSM node or this isn't the TSM node...
80 if [ "$no_tsm" = 0 -o "${i/tsm/}" = "$i" ] ; then
87 domains=$(virsh list --all | awk '{print $2}' | tail -n +3)
90 # If the cluster name doesn't have a wildcard then we need to be inventive.
91 if [ "${cluster/[\[\]\?\*]/}" = "$cluster" ] ; then
92 get_nodes "${cluster}[a-z]*[0-9]"
98 echo "No nodes in cluster $2"
105 # We want to retry the command when we see an internal error.
106 for x in $(seq 1 5) ; do
107 out=$(virsh $cmd "$i" 2>&1)
108 ret=$? # Hard to avoid this since we always want to echo $out :-(
110 if [ $ret -ne 0 ] ; then
113 echo "Retrying \"virsh $cmd $i\" due to internal error"
120 [ $ret = 0 ] || rc=$ret
123 # Now comes the waiting... but we don't wait if there was an error.
124 if [ $rc -ne 0 ] || ! $wait ; then
130 if [ -n "$timeout" ] && [ $count -ge "$timeout" ] ; then
131 echo "Timed out after ${timeout}s waiting for nodes to enter state \"${desired_state}\":"
134 printf "$fmt" "Domain" "State"
135 printf "$fmt" "------" "-----"
137 state=$(virsh dominfo "$i" | sed -nr -e 's@^State:[[:space:]]+@@p')
138 printf "$fmt" "$i" "$state"
143 pat="^State:[[:space:]]+${desired_state}\$"
146 # Often "vircmd dominfo" returns 1 and prints rubbish like this:
147 # error: operation failed: could not query memory balloon allocation
148 # so we take pains to avoid this cluttering the output...
149 if virsh_out=$(virsh dominfo "$i" 2>&1) ; then
150 if ! echo "$virsh_out" | grep -E -q "$pat" ; then
158 count=$(($count + 1))