ironic-python-agent-builder/tinyipa/build_files/static_network.sh

122 lines
2.8 KiB
Bash
Executable File

#!/bin/sh
#
# Try to configure NICs and routes from OpenStack metadata
#
# Exit with success if at least one NIC has been configured
#
. /etc/init.d/tc-functions
NETWORK_DATA=openstack/latest/metadata/network_data.json
if [ ! -b /dev/cdrom ]; then
echo "CDROM device not present - no static network config"
exit 1
fi
mkdir -p /mnt
mount -o ro /dev/cdrom /mnt
if [ "$?" != 0 ]; then
echo "CDROM device cannot be mounted - no static network config"
exit 1
fi
if [ ! -f /mnt/config-2.img ]; then
echo "Config drive image not present - no static network config"
exit 1
fi
mkdir -p /config-2
mount -o ro /mnt/config-2.img /config-2
if [ "$?" != 0 ]; then
echo "Config drive cannot be mounted - no static network config"
exit 1
fi
if [ ! -f /config-2/$NETWORK_DATA ]; then
echo "No network_data.json found on config drive - no static network config"
exit 1
fi
# If succeeded, this script will print out bash variables
eval $(/opt/export_network_data.py --source /config-2/$NETWORK_DATA)
eval_rc=$?
umount -f /config-2 /mnt
if [ "$eval_rc" != 0 ]; then
echo "Processing network data failed - no static network config"
exit 1
fi
rc=1
declare -A mac_to_dev
while [ $ND_NIC_COUNT -gt 0 ]; do
ND_NIC_COUNT=$((ND_NIC_COUNT - 1))
ND_MAC=${ND_L2_ETHERNET_MAC_ADDRESS[$ND_NIC_COUNT]}
ND_L2_ID=${ND_L2_IDS[$ND_NIC_COUNT]}
if [ ! -z "$ND_L2_ID" ]; then
ip link show $ND_L2_ID > /dev/null 2>&1
fi
if [ -z "$ND_L2_ID" -o "$?" != 0 ]; then
# NIC is not known, look up NIC name by MAC
if [ -z "$ND_MAC" ]; then
echo "Neither name nor MAC is configured for a L2 device, continuing"
continue
fi
ND_L2_ID=$(ip -o link | awk "/.*?BROADCAST.*?LOWER_UP.*?${ND_MAC}.*?/{print \$2}")
ND_L2_ID=${ND_L2_ID/:/}
if [ -z "$ND_L2_ID" ]; then
echo "No NIC found by MAC $ND_MAC, continuing"
continue
fi
fi
mac_to_dev[$ND_MAC] = $ND_L2_ID
ifconfig $ND_L2_ID \
${ND_L3_IPV4_ADDRESS[$ND_NIC_COUNT]} \
netmask ${ND_L3_IPV4_NETMASK[$ND_NIC_COUNT]} \
up
if [ "$?" != 0 ]; then
echo "Failed to assign IP address to NIC $ND_L2_ID, continuing"
continue
fi
# consider it a success if at least one NIC is brought up
rc=0
echo "Configured $ND_L2_ID IP ${ND_L3_IPV4_ADDRESS[$ND_NIC_COUNT]} netmask ${ND_L3_IPV4_NETMASK[$ND_NIC_COUNT]}"
done
while [ $ND_L3_ROUTES_COUNT -gt 0 ]; do
ND_L3_ROUTES_COUNT=$((ND_L3_ROUTES_COUNT - 1))
route add -net ${ND_L3_ROUTES_NETWORK[ND_L3_ROUTES_COUNT]} \
netmask ${ND_L3_ROUTES_NETMASK[ND_L3_ROUTES_COUNT]} \
gw ${ND_L3_ROUTES_GATEWAY[ND_L3_ROUTES_COUNT]} \
dev ${mac_to_dev[$ND_L2_ID]}
if [ "$?" != 0 ]; then
echo "Failed to set route for network ${ND_L3_ROUTES_NETWORK[ND_L3_ROUTES_COUNT]}, continuing"
continue
fi
echo "Set route ${ND_L3_ROUTES_NETWORK[ND_L3_ROUTES_COUNT]}"
done
exit $rc