1、介绍
network服务的核心由/etc/sysconfig/network-scripts/下一堆脚本配置来生效,其中启动网卡就是通过ifup脚本来实现的,下面就讲一下ifup如何恢复ip和route的流程。
2、ifup流程
【1】与NetworkManager兼容
ifup有一段内容为如果网卡被NetworkManager接管就通过nmcli去激活连接,内容如下:
if is_true "$_use_nm" && [ -n "$UUID" ] && [ "$REALDEVICE" != "lo" ]; then
if [ "foo$2" = "fooboot" ] && [ "${TYPE}" = "Wireless" ]; then
exit 0
fi
[ -n "${DEVICE}" ] && is_nm_handling ${DEVICE} && exit 0
echo ${DEVICE} ${UUID}
nmcli con up uuid "$UUID" #通过nmcli去激活连接
exit $?
fi
否则就会走到对应接口类型的脚本中去,比如以太网就会执行ifup-eth,内容如下:
OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${DEVICETYPE}"
if [ ! -x ${OTHERSCRIPT} ]; then
OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${TYPE}"
fi
if [ ! -x ${OTHERSCRIPT} ]; then
OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-eth" #以太网接口对应的脚本
fi
exec ${OTHERSCRIPT} ${CONFIG} $2
【2】ifup-eth设置ip
网卡上的ip设置就是通过ifup-eth来设置的,读取ifcfg-xxx配置,通过ip addr add设置,内容如下:
if ! ip addr add ${ipaddr[$idx]}/${prefix[$idx]} \
brd ${broadcast[$idx]:-+} dev ${REALDEVICE} ${SCOPE} label ${DEVICE}; then
net_log $"Error adding address ${ipaddr[$idx]} for ${DEVICE}."
else
echo "ip addr add ${ipaddr[$idx]}/${prefix[$idx]} dev {REALDEVICE}"
fi
如果ifcfg-xxx中有配置GATEWAY就会去设置默认路由,通过ip route reolace去设置,内容如下:
# Set a default route.
if ! is_false "${DEFROUTE}" && [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${REALDEVICE}" ]; then
# set up default gateway. replace if one already exists
if [ -n "${GATEWAY}" ] && [ "$(ipcalc --network ${GATEWAY} ${netmask[0]} 2>/dev/null)" = "NETWORK=${NETWORK}" ]; then
ip route replace default ${METRIC:+metric $METRIC} \
${EXTRA_ROUTE_OPTS} \
via ${GATEWAY} ${WINDOW:+window $WINDOW} ${SRC} \
${GATEWAYDEV:+dev $GATEWAYDEV} ||
net_log $"Error adding default gateway ${GATEWAY} for ${DEVICE}."
elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then
ip route replace default ${METRIC:+metric $METRIC} \
${EXTRA_ROUTE_OPTS} \
${SRC} ${WINDOW:+window $WINDOW} dev ${REALDEVICE} ||
net_log $"Error adding default gateway for ${REALDEVICE}."
fi
fi
【3】ifup-routes设置路由
网卡上的路由恢复是通过ifup-routes来设置的,读去route-xxx的配置,通过ip route add设置,根据不同的格式执行不同的函数,内容如下:
handle_file () {
. $1
routenum=0
while [ "x$(eval echo '$'ADDRESS$routenum)x" != "xx" ]; do
eval $(ipcalc -p $(eval echo '$'ADDRESS$routenum) $(eval echo '$'NETMASK$routenum))
line="$(eval echo '$'ADDRESS$routenum)/$PREFIX"
if [ "x$(eval echo '$'GATEWAY$routenum)x" != "xx" ]; then
line="$line via $(eval echo '$'GATEWAY$routenum)"
fi
line="$line dev $2"
echo $line
/sbin/ip route add $line #设置路由
routenum=$(($routenum+1))
done
}
handle_ip_file() {
local f t type= file=$1 proto="-4"
f=${file##*/}
t=${f%%-*}
type=${t%%6}
if [ "$type" != "$t" ]; then
proto="-6"
fi
{ cat "$file" ; echo ; } | while read line; do
if [[ ! "$line" =~ $MATCH ]]; then
echo $proto $type $line
/sbin/ip $proto $type add $line #设置路由
fi
done
}
FILES="/etc/sysconfig/network-scripts/route-$1 /etc/sysconfig/network-scripts/route6-$1"
if [ -n "$2" -a "$2" != "$1" ]; then
FILES="$FILES /etc/sysconfig/network-scripts/route-$2 /etc/sysconfig/network-scripts/route6-$2"
fi
for file in $FILES; do
if [ -f "$file" ]; then
if grep -Eq '^[[:space:]]*ADDRESS[0-9]+=' $file ; then
# new format
handle_file $file ${1%:*}
else
# older format
handle_ip_file $file
fi
fi
done