hyper-v/hyper-v.tools.hv.hv_set_ifconfig.sh

186 lines
4.0 KiB
Bash

#!/bin/bash
#
# In the interest of keeping the KVP daemon code free of distro specific
# information; the kvp daemon code invokes this external script to configure
# the interface.
#
# The only argument to this script is the configuration file that is to
# be used to configure the interface.
#
# Here is the format of the ip configuration file:
#
# HWADDR=macaddr
# IF_NAME=interface name
# DHCP=yes (This is optional; if yes, DHCP is configured)
#
# IPADDR=ipaddr1
# IPADDR_1=ipaddr2
# IPADDR_x=ipaddry (where y = x + 1)
#
# NETMASK=netmask1
# NETMASK_x=netmasky (where y = x + 1)
#
# GATEWAY=ipaddr1
# GATEWAY_x=ipaddry (where y = x + 1)
#
# DNSx=ipaddrx (where first DNS address is tagged as DNS1 etc)
#
# IPV6 addresses will be tagged as IPV6ADDR, IPV6 gateway will be
# tagged as IPV6_DEFAULTGW and IPV6 NETMASK will be tagged as
# IPV6NETMASK.
#
# The host can specify multiple ipv4 and ipv6 addresses to be
# configured for the interface. Furthermore, the configuration
# needs to be persistent. A subsequent GET call on the interface
# is expected to return the configuration that is set via the SET
# call.
#
cfg=$1
if ! test -f "${cfg}"
then
: expect configuration datafile as first argument
exit 1
fi
# send subshell output to syslog
(
f=/etc/sysconfig/network/scripts/functions
if test -f ${f}
then
. ${f}
else
echo "MISSING ${f}"
exit 1
fi
# remove known config variables from environment
unset HWADDR
unset DHCP
unset IF_NAME
unset ${!IPADDR*}
unset ${!NETMASK*}
unset ${!GATEWAY*}
unset ${!IPV6ADDR*}
unset ${!IPV6NETMASK*}
unset ${!IPV6_DEFAULTGW*}
unset ${!DNS*}
. "$1"
#
if test -z "${IF_NAME}"
then
echo "Missing IF_NAME= in ${cfg}"
exit 1
fi
#
t_ifcfg=`mktemp`
t_ifroute=`mktemp`
_exit() {
rm -f "${t_ifcfg}" "${t_ifroute}"
}
trap _exit EXIT
#
if test -z "${t_ifcfg}" || test -z "${t_ifroute}"
then
exit 1
fi
#
# Create ifcfg-* file
(
echo "STARTMODE=auto"
#
if test -n "${HWADDR}"
then
: # ignore HWADDR, it just repeats the existing MAC value
fi
#
if test "${DHCP}" = "yes"
then
echo "BOOTPROTO=dhcp"
fi
# single index for all ipv4 and ipv6 adresses in final ifcfg file
i=0
idx=""
# loop through all ipv4 adresses
for var in ${!IPADDR*}
do
index=${var#IPADDR}
pfx=
# find corresponding NETMASK variable
eval nm=\$NETMASK${index}
# if specified, calculate prefix
if test -n "${nm}"
then
pfx=`mask2pfxlen "${nm}" 2>/dev/null`
fi
# if not specified, force prefix
if test -z "${pfx}"
then
pfx="32"
fi
# construct actual value
eval val=\$IPADDR${index}
# write config variable
echo "IPADDR${idx}='${val}/${pfx}'"
idx="_$((++i))"
done
# loop through all ipv6 adresses
for var in ${!IPV6ADDR*}
do
index=${var#IPV6ADDR}
# find corresponding IPV6NETMASK variable
eval pfx=\$IPV6NETMASK${index}
# if not specified, force prefix
if test -z "${pfx}"
then
pfx=128
fi
# construct actual value
eval val=\$IPV6ADDR${index}
# write config variable
echo "IPADDR${idx}='${val}/${pfx}'"
idx="_$((++i))"
done
) >> "${t_ifcfg}"
# Create ifroute-* file
(
if test -n "${GATEWAY}"
then
echo "default $GATEWAY - $IF_NAME"
fi
if test -n "${IPV6_DEFAULTGW}"
then
echo "default $IPV6_DEFAULTGW - $IF_NAME"
fi
) >> "${t_ifroute}"
# Only a single default gateway is supported
unset GATEWAY IPV6_DEFAULTGW
if test -n "${!GATEWAY*}${!IPV6_DEFAULTGW*}"
then
echo "WARNING: multiple gateways not supported: ${!GATEWAY*} ${!IPV6_DEFAULTGW*}"
fi
# collect DNS info
_DNS_=
for var in ${!DNS*}
do
eval val=\$${var}
if test -n "${_DNS_}"
then
_DNS_="${_DNS_} ${val}"
else
_DNS_=${val}
fi
done
#
echo "$0: working on network interface ifcfg-${IF_NAME}"
cp -fb ${t_ifcfg} "/etc/sysconfig/network/ifcfg-${IF_NAME}"
cp -fb ${t_ifroute} "/etc/sysconfig/network/ifroute-${IF_NAME}"
if test -w /etc/sysconfig/network/config
then
sed -i "s@^NETCONFIG_DNS_STATIC_SERVERS=.*@NETCONFIG_DNS_STATIC_SERVERS='$_DNS_'@" /etc/sysconfig/network/config
netconfig update -m dns
fi
ifdown "${IF_NAME}"
ifup "${IF_NAME}"
) 2>&1 | logger -t "${0##*/}[$PPID / $$]"