目录
- 1、介绍
- 2、ifup流程
- 【1】与NetworkManager兼容
- 【2】ifup-eth设置ip
- 【3】ifup-routes设置路由
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" ]; thenif [ "foo$2" = "fooboot" ] && [ "${TYPE}" = "Wireless" ]; thenexit 0fi[ -n "${DEVICE}" ] && is_nm_handling ${DEVICE} && exit 0echo ${DEVICE} ${UUID}nmcli con up uuid "$UUID" #通过nmcli去激活连接exit $?
fi
否则就会走到对应接口类型的脚本中去,比如以太网就会执行ifup-eth,内容如下:
OTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${DEVICETYPE}"if [ ! -x ${OTHERSCRIPT} ]; thenOTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-${TYPE}"
fiif [ ! -x ${OTHERSCRIPT} ]; thenOTHERSCRIPT="/etc/sysconfig/network-scripts/ifup-eth" #以太网接口对应的脚本
fiexec ${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}; thennet_log $"Error adding address ${ipaddr[$idx]} for ${DEVICE}."elseecho "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 existsif [ -n "${GATEWAY}" ] && [ "$(ipcalc --network ${GATEWAY} ${netmask[0]} 2>/dev/null)" = "NETWORK=${NETWORK}" ]; thenip 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}" ]; thenip 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 () {. $1routenum=0while [ "x$(eval echo '$'ADDRESS$routenum)x" != "xx" ]; doeval $(ipcalc -p $(eval echo '$'ADDRESS$routenum) $(eval echo '$'NETMASK$routenum))line="$(eval echo '$'ADDRESS$routenum)/$PREFIX"if [ "x$(eval echo '$'GATEWAY$routenum)x" != "xx" ]; thenline="$line via $(eval echo '$'GATEWAY$routenum)"filine="$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" ]; thenproto="-6"fi{ cat "$file" ; echo ; } | while read line; doif [[ ! "$line" =~ $MATCH ]]; thenecho $proto $type $line /sbin/ip $proto $type add $line #设置路由fidone
}FILES="/etc/sysconfig/network-scripts/route-$1 /etc/sysconfig/network-scripts/route6-$1"
if [ -n "$2" -a "$2" != "$1" ]; thenFILES="$FILES /etc/sysconfig/network-scripts/route-$2 /etc/sysconfig/network-scripts/route6-$2"
fifor file in $FILES; doif [ -f "$file" ]; thenif grep -Eq '^[[:space:]]*ADDRESS[0-9]+=' $file ; then# new formathandle_file $file ${1%:*}else# older formathandle_ip_file $filefifi
done