diff --git a/heartbeat/findif.sh b/heartbeat/findif.sh index 495a71c89..5201be271 100644 --- a/heartbeat/findif.sh +++ b/heartbeat/findif.sh @@ -1,163 +1,190 @@ #!/bin/sh ipcheck_ipv4() { local ip=$1 echo "$ip" | grep -qs '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$' if [ $? -ne 0 ] ; then return 1 fi echo "$ip" | awk -F. '{if(NF!=4)exit(1);for(i=1;i<=4;i++)if(!($i>=0&&$i<=255))exit(1)}' } ipcheck_ipv6() { local ipaddr=$1 echo "$ipaddr" | grep -qs "[^0-9:a-fA-F]" if [ $? = 1 ] ; then return 0 else return 1 fi } ifcheck_ipv4() { local ifcheck=$1 local ifstr local counter=0 local procfile="/proc/net/dev" while read LINE do if [ $counter -ge 2 ] ; then ifstr=`echo $LINE | cut -d ':' -f 1` if [ "$ifstr" = "$ifcheck" ] ; then return 0 fi fi counter=`expr $counter + 1` done < $procfile return 1 } ifcheck_ipv6() { local ifcheck="$1" local ifstr local procfile="/proc/net/if_inet6" while read LINE do ifstr=`echo $LINE | awk -F ' ' '{print $6}'` if [ "$ifstr" = "$ifcheck" ] ; then return 0 fi done < $procfile return 1 } prefixcheck() { local prefix=$1 local prefix_length=${#prefix} local prefix_check=$2 if [ $prefix_length -gt 3 -o $prefix_length -eq 0 ] ; then return 1 fi echo "$prefix" | grep -qs "[^0-9]" if [ $? = 0 ] ; then return 1 fi if [ $prefix -lt 1 -o $prefix -gt $prefix_check ] ; then return 1 fi return 0 } getnetworkinfo() { ip -o -f inet route list match $OCF_RESKEY_ip table local scope host | (while read LINE; do IP=`echo $LINE | awk '{print $2}'` case $IP in */*) set -- $LINE break ;; esac done echo $LINE) } findif() { local match="$OCF_RESKEY_ip" local family="inet" local scope local NIC="$OCF_RESKEY_nic" local NETMASK="$OCF_RESKEY_cidr_netmask" local BRDCAST="$OCF_RESKEY_broadcast" echo $match | grep -qs ":" if [ $? = 0 ] ; then ipcheck_ipv6 $match - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "IP address [$match] not valid." + return $OCF_ERR_CONFIGURED + fi if [ -n "$NIC" ] ; then ifcheck_ipv6 $NIC - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "Unknown interface [$NIC] No such device." + return $OCF_ERR_CONFIGURED + fi else echo $match | grep -qis '^fe80::' if [ $? = 0 ] ; then - return 1 + ocf_log err "'nic' parameter is mandatory for a link local address [$match]." + return $OCF_ERR_CONFIGURED fi fi if [ -n "$NETMASK" ] ; then prefixcheck $NETMASK 128 - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "Invalid netmask specification [$NETMASK]." + return $OCF_ERR_CONFIGURED + fi match=$match/$NETMASK fi family="inet6" else ipcheck_ipv4 $match - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "IP address [$match] not valid." + return $OCF_ERR_CONFIGURED + fi if [ -n "$NIC" ] ; then ifcheck_ipv4 $NIC - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "Unknown interface [$NIC] No such device." + return $OCF_ERR_CONFIGURED + fi fi if [ -n "$NETMASK" ] ; then prefixcheck $NETMASK 32 - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "Invalid netmask specification [$NETMASK]." + return $OCF_ERR_CONFIGURED + fi match=$match/$NETMASK fi if [ -n "$BRDCAST" ] ; then ipcheck_ipv4 $BRDCAST - [ $? = 1 ] && return 6 + if [ $? = 1 ] ; then + ocf_log err "Invalid broadcast address [$BRDCAST]." + return $OCF_ERR_CONFIGURED + fi fi scope="scope link" fi if [ -n "$NIC" ] ; then # NIC supports more than two. set -- `ip -o -f $family route list match $match $scope | grep "dev $NIC"` else set -- `ip -o -f $family route list match $match $scope` fi if [ $# = 0 ] ; then case $OCF_RESKEY_ip in 127.*) set -- `getnetworkinfo` shift;; esac fi if [ -z "$NIC" -o -z "$NETMASK" ] ; then - [ $# = 0 ] && return 1 + if [ $# = 0 ] ; then + ocf_log err "Unable to find nic or netmask." + return $OCF_ERR_GENERIC + fi case $1 in */*) : OK ;; *) - return 1 ;; + ocf_log err "Unable to find cidr_netmask." + return $OCF_ERR_GENERIC ;; esac fi [ -z "$NIC" ] && NIC=$3 [ -z "$NETMASK" ] && NETMASK=${1#*/} if [ $family = "inet" ] ; then if [ -z "$BRDCAST" ] ; then if [ -n "$7" ] ; then set -- `ip -o -f $family addr show | grep $7` [ "$5" = brd ] && BRDCAST=$6 fi fi else if [ -z "$OCF_RESKEY_nic" -a "$NETMASK" != "${1#*/}" ] ; then - return 1 + ocf_log err "Unable to find nic, or netmask mismatch." + return $OCF_ERR_GENERIC fi fi echo "$NIC netmask $NETMASK broadcast $BRDCAST" - return 0 + return $OCF_SUCCESS } diff --git a/tools/ocft/IPaddr2v6 b/tools/ocft/IPaddr2v6 index 0685dea0c..d24d89072 100644 --- a/tools/ocft/IPaddr2v6 +++ b/tools/ocft/IPaddr2v6 @@ -1,250 +1,250 @@ # IPaddr2v6 # Note: This test case uses two NICs(eth0, eth1) and # a IPv6 address prefix (2001:db8::/32, RFC3849). # Adjust them according to your environment at VARIABLE section if needed. CONFIG Agent IPaddr2 AgentRoot /usr/lib/ocf/resource.d/heartbeat HangTimeout 20 VARIABLE OCFT_target_ip=2001:db8:1234::2 OCFT_target_nic=eth0 OCFT_target_prefix=64 OCFT_target_netaddr=2001:db8:1234::1/$OCFT_target_prefix OCFT_target_linklocal=fe80::2 OCFT_wrong_ip=2001:db8:5678::2 OCFT_force_nic=eth1 OCFT_force_prefix=80 OCFT_force_prefix2=48 SETUP-AGENT ip addr add $OCFT_target_netaddr dev $OCFT_target_nic CLEANUP-AGENT ip addr del $OCFT_target_netaddr dev $OCFT_target_nic CASE-BLOCK required_args Env OCF_RESKEY_ip=$OCFT_target_ip Env OCFT_check_ip=$OCFT_target_ip Env OCFT_check_prefix=$OCFT_target_prefix Env OCFT_check_nic=$OCFT_target_nic CASE-BLOCK check_ip_assigned Bash ip -6 -o addr show $OCFT_check_nic | grep -w $OCFT_check_ip/$OCFT_check_prefix >/dev/null # checking if the IPv6 address was assigned correctly CASE-BLOCK check_ip_removed Bash ! ip -6 -o addr show $OCFT_check_nic | grep -w $OCFT_check_ip/$OCFT_check_prefix >/dev/null # checking if the IPv6 address was removed correctly CASE-BLOCK base_ip_assigned Bash ip addr add $OCFT_target_netaddr dev $OCFT_target_nic CASE-BLOCK base_ip_removed Bash ip addr del $OCFT_target_netaddr dev $OCFT_target_nic CASE-BLOCK default_status AgentRun stop CASE-BLOCK prepare Include required_args Include default_status # CASE No.0 # CASE "normal start" Include prepare AgentRun start OCF_SUCCESS Include check_ip_assigned # CASE No.1 # CASE "normal stop" Include prepare AgentRun start OCF_SUCCESS AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.2 # CASE "double start" Include prepare AgentRun start AgentRun start OCF_SUCCESS # CASE No.3 # CASE "double stop" Include prepare AgentRun stop OCF_SUCCESS # CASE No.4 # CASE "monitor with running" Include prepare AgentRun start AgentRun monitor OCF_SUCCESS # CASE No.5 # CASE "monitor with not running" Include prepare AgentRun monitor OCF_NOT_RUNNING # CASE No.6 # CASE "error params with wrong ip" Include prepare Env OCF_RESKEY_ip=$OCFT_wrong_ip AgentRun start OCF_ERR_GENERIC # CASE No.7 # -CASE "start for a link-local IPv6 address when no nic" +CASE "error params with no nic for a link-local IPv6 address" Include prepare Env OCF_RESKEY_ip=$OCFT_target_linklocal Env OCFT_check_ip=$OCFT_target_linklocal # nic is mandatory for a link-local address - AgentRun start OCF_ERR_GENERIC + AgentRun start OCF_ERR_CONFIGURED # CASE No.8 # CASE "params with nic, no cidr_netmask" Include prepare Env OCF_RESKEY_nic=$OCFT_target_nic AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun monitor OCF_SUCCESS AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.9 # CASE "normal usage for a link-local IPv6 address, params with nic" Include prepare Env OCF_RESKEY_ip=$OCFT_target_linklocal Env OCFT_check_ip=$OCFT_target_linklocal # nic is mandatory for a link-local address Env OCF_RESKEY_nic=$OCFT_target_nic Env OCFT_check_nic=$OCFT_target_nic AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.10 # CASE "error params with wrong ip and nic (not exist base_ip)" Include prepare Include base_ip_removed Env OCF_RESKEY_nic=$OCFT_target_nic Env OCFT_check_nic=$OCFT_target_nic AgentRun start OCF_ERR_GENERIC Include check_ip_removed Include base_ip_assigned # CASE No.11 # CASE "params with force nic" Include prepare Env OCF_RESKEY_nic=$OCFT_force_nic Env OCFT_check_nic=$OCFT_force_nic AgentRun start OCF_ERR_GENERIC Include check_ip_removed Unenv OCF_RESKEY_nic # CASE No.12 # CASE "params with force cidr_netmask" Include prepare Env OCF_RESKEY_cidr_netmask=$OCFT_target_prefix Env OCFT_check_prefix=$OCFT_target_prefix AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.13 # CASE "params with force cidr_netmask (base netmask < assigned netmask)" Include prepare Env OCF_RESKEY_cidr_netmask=$OCFT_force_prefix Env OCFT_check_prefix=$OCFT_force_prefix AgentRun start OCF_ERR_GENERIC Include check_ip_removed # CASE No.14 # CASE "params with force cidr_netmask (base netmask > assigned netmask)" Include prepare Env OCF_RESKEY_cidr_netmask=$OCFT_force_prefix2 Env OCFT_check_prefix=$OCFT_force_prefix2 AgentRun start OCF_ERR_GENERIC Include check_ip_removed # CASE No.15 # CASE "params with cidr_netmask" Include prepare Include base_ip_removed Env OCF_RESKEY_cidr_netmask=$OCFT_target_prefix Env OCFT_check_prefix=$OCFT_target_prefix AgentRun start OCF_ERR_GENERIC Include base_ip_assigned # CASE No.16 # CASE "params with nic, cidr_netmask" Include prepare Env OCF_RESKEY_nic=$OCFT_target_nic Env OCF_RESKEY_cidr_netmask=$OCFT_target_prefix Env OCFT_check_nic=$OCFT_target_nic Env OCFT_check_prefix=$OCFT_target_prefix AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.17 # CASE "force to use the specified nic and cidr_netmask (base netmask < assigned netmask)" Include prepare Env OCF_RESKEY_nic=$OCFT_force_nic Env OCF_RESKEY_cidr_netmask=$OCFT_force_prefix Env OCFT_check_nic=$OCFT_force_nic Env OCFT_check_prefix=$OCFT_force_prefix AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.18 # This use case is now valid. It was not allowed until v3.9.2. # CASE "force to use the specified nic and cidr_netmask (base netmask > assigned netmask)" Include prepare Env OCF_RESKEY_nic=$OCFT_force_nic Env OCF_RESKEY_cidr_netmask=$OCFT_force_prefix2 Env OCFT_check_nic=$OCFT_force_nic Env OCFT_check_prefix=$OCFT_force_prefix2 AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed # CASE No.19 # CASE "force to use the specified nic and cidr_netmask (base netmask > assigned netmask)" Include prepare Include base_ip_removed Env OCF_RESKEY_nic=$OCFT_force_nic Env OCF_RESKEY_cidr_netmask=$OCFT_force_prefix2 Env OCFT_check_nic=$OCFT_force_nic Env OCFT_check_prefix=$OCFT_force_prefix2 AgentRun start OCF_SUCCESS Include check_ip_assigned AgentRun stop OCF_SUCCESS Include check_ip_removed Include base_ip_assigned