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 # Address label string
120 addrcmd="$addrcmd label $1"
125 addrcmd="$addrcmd $1"
128 if test "$1" != "$dev"; then
129 addrcmd="$addrcmd $1"
132 echo ip -f $family addr add $addrcmd dev $dev
136 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
137 ip route show dev $dev | while read route; do
138 # "proto kernel" routes are installed by the kernel automatically.
140 *" proto kernel "*) continue ;;
143 echo "ip route add $route dev $dev"
149 if missing_program iptables-save; then
150 echo "# iptables-save not found in $PATH, not saving iptables state"
153 echo "iptables-restore <<'EOF'"