Use default route to find HOST_IP

When running devstack, nova moves the host ip from eth0 onto the
bridge. This causes devstack to fail on the second run unless you
explicitly set HOST_IP in localrc.

This patch searches for an ip on the interface that is used for
the default route. This will be eth0 (or en0) in most cases, but
it will search br100 instead if nova has moved the ip, since it
moves the default route as well.

It also will filter out ips from the potential list that are part
of the fixed range and floating range if the netaddr library is
installed. This allows us to find the proper ip even if we have
accidentally left a floating ip or fixed ip on the bridge.

Change-Id: I13288e53ee2786c5ae0edb3f9ab457be8303f1f6
This commit is contained in:
Vishvananda Ishaya 2012-07-03 20:29:01 +00:00
parent 14ab37eb41
commit c9ad14bd38
2 changed files with 34 additions and 8 deletions

View File

@ -9,6 +9,18 @@ XTRACE=$(set +o | grep xtrace)
set +o xtrace
# Exit 0 if address is in network or 1 if
# address is not in network or netaddr library
# is not installed.
function address_in_net() {
python -c "
import netaddr
import sys
sys.exit(netaddr.IPAddress('$1') not in netaddr.IPNetwork('$2'))
"
}
# apt-get wrapper to set arguments correctly
# apt_get operation package [package ...]
function apt_get() {

View File

@ -283,13 +283,30 @@ LIBVIRT_TYPE=${LIBVIRT_TYPE:-kvm}
# cases.
SCHEDULER=${SCHEDULER:-nova.scheduler.filter_scheduler.FilterScheduler}
HOST_IP_IFACE=${HOST_IP_IFACE:-eth0}
# Use the eth0 IP unless an explicit is set by ``HOST_IP`` environment variable
# Set fixed and floating range here so we can make sure not to use addresses
# from either range when attempting to guess the ip to use for the host
FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
# Find the interface used for the default route
HOST_IP_IFACE=${HOST_IP_IFACE:-$(ip route | sed -n '/^default/{ s/.*dev \(\w\+\)\s\+.*/\1/; p; }')}
# Search for an IP unless an explicit is set by ``HOST_IP`` environment variable
if [ -z "$HOST_IP" -o "$HOST_IP" == "dhcp" ]; then
HOST_IP=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/"); print parts[1]}' | head -n1`
if [ "$HOST_IP" = "" ]; then
HOST_IP=""
HOST_IPS=`LC_ALL=C ip -f inet addr show ${HOST_IP_IFACE} | awk '/inet/ {split($2,parts,"/"); print parts[1]}'`
for IP in $HOST_IPS; do
# Attempt to filter out ip addresses that are part of the fixed and
# floating range. Note that this method only works if the 'netaddr'
# python library is installed. If it is not installed, an error
# will be printed and the first ip from the interface will be used.
if ! (address_in_net $IP $FIXED_RANGE || address_in_net $IP $FLOATING_RANGE); then
HOST_IP=$IP
break;
fi
done
if [ "$HOST_IP" == "" ]; then
echo "Could not determine host ip address."
echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted to eth0"
echo "Either localrc specified dhcp on ${HOST_IP_IFACE} or defaulted"
exit 1
fi
fi
@ -368,11 +385,8 @@ else
fi
PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-$PUBLIC_INTERFACE_DEFAULT}
PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-br100}
FIXED_RANGE=${FIXED_RANGE:-10.0.0.0/24}
FIXED_NETWORK_SIZE=${FIXED_NETWORK_SIZE:-256}
NETWORK_GATEWAY=${NETWORK_GATEWAY:-10.0.0.1}
FLOATING_RANGE=${FLOATING_RANGE:-172.24.4.224/28}
NET_MAN=${NET_MAN:-FlatDHCPManager}
EC2_DMZ_HOST=${EC2_DMZ_HOST:-$SERVICE_HOST}
FLAT_NETWORK_BRIDGE=${FLAT_NETWORK_BRIDGE:-$FLAT_NETWORK_BRIDGE_DEFAULT}