3 # Copyright (c) 2011 Nicira Networks, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 if test "X$1" = X--help; then
19 $0: saves the kernel configuration of network interfaces
22 Outputs a shell script on stdout that will restore the current
23 kernel configuration of the specified network interfaces, as
24 well as the system iptables configuration.
26 This script is meant as a helper for the Open vSwitch init
27 script "force-reload-kmod" command.
32 PATH=/sbin:/bin:/usr/sbin:/usr/bin
39 if test -x $dir/$1; then
46 if missing_program ip; then
47 echo "$0: ip not found in $PATH" >&2
51 if test "$#" = 0; then
52 echo "# $0: no parameters given (use \"$0 --help\" for help)"
57 state=`ip link show $dev` || continue
60 # Link state (Ethernet addresses, up/down, ...)
63 *"state UP"* | *[,\<]"UP"[,\>]* )
67 linkcmd="$linkcmd down"
70 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
71 linkcmd="$linkcmd dynamic"
73 if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
74 linkcmd="$linkcmd txqueuelen $qlen"
76 if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
77 linkcmd="$linkcmd address $hwaddr"
79 if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
80 linkcmd="$linkcmd broadcast $brd"
82 if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
83 linkcmd="$linkcmd mtu $mtu"
85 if test -n "$linkcmd"; then
86 echo ip link set $dev down # Required to change hwaddr.
87 echo ip link set $dev $linkcmd
90 # IP addresses (including IPv6).
91 echo "ip addr flush $dev 2>/dev/null" # Suppresses "Nothing to flush".
92 ip addr show dev $dev | while read addr; do
95 # Check and trim family.
103 # Trim device off the end--"ip" insists on having "dev" precede it.
105 while test $# != 0; do
108 # Omit kernel-maintained route.
112 if test "$2" = link; then
113 # Omit route derived from IP address, e.g.
114 # 172.16.0.0/16 derived from 172.16.12.34.
119 # Omit because "ip" wants "dev" keyword in front.
124 addrcmd="$addrcmd $1"
127 if test "$1" != "$dev"; then
128 addrcmd="$addrcmd $1"
131 echo ip -f $family addr add $addrcmd dev $dev
135 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
136 ip route show dev $dev | while read route; do
137 # "proto kernel" routes are installed by the kernel automatically.
139 *" proto kernel "*) continue ;;
142 echo "ip route add $route dev $dev"
148 if missing_program iptables-save; then
149 echo "# iptables-save not found in $PATH, not saving iptables state"
152 echo "iptables-restore <<'EOF'"