#!/bin/sh # ## Script to manage XEN virtual machines as cluster resources # # Created: Ivan A. Vari, Oct 2007, # Version: 1.2 # License: Free # Executables CRM_ADMIN=/usr/sbin/crmadmin CRM_RESOURCE=/usr/sbin/crm_resource CRM_STANDBY=/usr/sbin/crm_standby ECHO_BIN=/bin/echo AWK_BIN=/usr/bin/awk GREP_BIN=/usr/bin/grep # Space separated list of domUs should not be migrated EXCLUDE="" ##---------NO NEED CHANGING ANYTHING BELOW----------## # Variables DOMAINS=`$CRM_RESOURCE -L | $GREP_BIN Xen | $AWK_BIN '{ print $1 }'` # filtering valid domUs HOSTS=`$CRM_ADMIN -N | $AWK_BIN '{ print $3 }'` # filtering valid nodes # This is a function for the displaying the error / help / usage message usage_msg() { $ECHO_BIN $ECHO_BIN "A script to manage XEN virtual machines as cluster resources in HA stack." $ECHO_BIN "Remember: once domU is configured in heartbeat as a cluster resource, it" $ECHO_BIN "shouldn't be managed by traditional utilities such as \"xm\" or \"libvirt\"(GUI)" $ECHO_BIN $ECHO_BIN "Usage: ${0##*/} [resource] [node]" $ECHO_BIN $ECHO_BIN "subcommands:" $ECHO_BIN -e "\033[1m list\033[0m Displays available Xen cluster resources (domU)" $ECHO_BIN -e "\033[1m hosts\033[0m Displays cluster member nodes (dom0)" $ECHO_BIN -e "\033[1m locate\033[0m [resource] Displays the status of the specified Xen cluster resource" $ECHO_BIN -e "\033[1m stop\033[0m [resource] Stops the specified Xen cluster resource" $ECHO_BIN -e "\033[1m start\033[0m [resource] Starts the specified Xen cluster resource" $ECHO_BIN -e "\033[1m standby\033[0m [node] Puts the specified node into standby mode" $ECHO_BIN -e "\033[1m active\033[0m [node] Puts the specified node back into active mode (standby off)" $ECHO_BIN -e "\033[1m migrate\033[0m [resource] [node] Migrates the Xen cluster resource to the specified host" $ECHO_BIN -e "\033[1m restore\033[0m [resource] This function removes the constraints applied by \"migrate\" option." $ECHO_BIN " Migration in HA stack is done by location constraints, scoring very" $ECHO_BIN " high to the preferred and very low to the non preferred node. This" $ECHO_BIN " feature will remove all \"migration\" applied interim constraints and" $ECHO_BIN " put the policy back to its initial state. You should run \"restore\"" $ECHO_BIN " after \"migrate\" every time if you plan to leave the resource at the" $ECHO_BIN " new location." $ECHO_BIN $ECHO_BIN -e "\033[31mWARNING:\033[0m" "\"migrate\" option should be avoided, due to its nature modifying the location constraints." $ECHO_BIN " If you need to free one of your nodes up (for scheduled maintenance, etc.) use \"standby\"" $ECHO_BIN " option instead." $ECHO_BIN exit 65 } # Set up variables according to given subcommand case "$1" in list|hosts|locate|stop|start|migrate|restore) PARAM1=$1 PARAM2=$2 PARAM3=$3 ;; standby|active) PARAM1=$1 PARAM3=$2 ;; *) usage_msg ;; esac # This function is to validate the given parameter as domU name in the cluster validate_domu() { let MATCH=0 for DOMU in ${DOMAINS}; do if [ "$DOMU" = "$PARAM2" ]; then let "MATCH = 1" fi done if [ ! "$MATCH" -eq 1 ]; then $ECHO_BIN "INVALID Xen cluster resource \"${PARAM2}\"" exit 1 fi } # This function is to validate the given parameter as target host name in the cluster validate_host() { let MATCH=0 for DOM0 in ${HOSTS}; do if [ "$DOM0" = "$PARAM3" ]; then let "MATCH = 1" fi done if [ ! "$MATCH" -eq 1 ]; then $ECHO_BIN "INVALID Xen cluster node \"${PARAM3}\"" exit 1 fi } # This function is to evaluate the given parameter (target resource) againts exclusions validate_exclusions() { let MATCH=0 for DOMU in $EXCLUDE; do if [ "$DOMU" = "$PARAM2" ]; then let "MATCH = 1" fi done if [ "$MATCH" -eq 1 ]; then $ECHO_BIN "Xen cluster resource \"${PARAM2}\" must not be migrated" exit 1 fi } ##---------END FUNCTIONS----------## if [ $# -eq "1" ] && [ "$PARAM1" = "list" ]; then $CRM_RESOURCE -L | $GREP_BIN Xen elif [ $# -eq "1" ] && [ "$PARAM1" = "hosts" ]; then $CRM_ADMIN -N elif [ $# -eq "2" ] && [ "$PARAM1" = "locate" ] && [ $PARAM2 ]; then validate_domu $CRM_RESOURCE -W -r $PARAM2 elif [ $# -eq "2" ] && [ "$PARAM1" = "stop" ] && [ $PARAM2 ]; then validate_domu $CRM_RESOURCE -r $PARAM2 -p target_role -v stopped elif [ $# -eq "2" ] && [ "$PARAM1" = "start" ] && [ $PARAM2 ]; then validate_domu $CRM_RESOURCE -r $PARAM2 -p target_role -v started elif [ $# -eq "2" ] && [ "$PARAM1" = "standby" ] && [ $PARAM3 ]; then validate_host $CRM_STANDBY -U $PARAM3 -v on elif [ $# -eq "2" ] && [ "$PARAM1" = "active" ] && [ $PARAM3 ]; then validate_host $CRM_STANDBY -U $PARAM3 -v off # $CRM_STANDBY -D -U $PARAM3 elif [ $# -eq "3" ] && [ "$PARAM1" = "migrate" ] && [ $PARAM2 ] && [ $PARAM3 ]; then validate_domu validate_exclusions validate_host $CRM_RESOURCE -f -M -Q -r $PARAM2 -H $PARAM3 elif [ $# -eq "2" ] && [ "$PARAM1" = "restore" ] && [ $PARAM2 ]; then validate_domu $CRM_RESOURCE -U -r $PARAM2 else usage_msg fi