]> git.proxmox.com Git - ovs.git/blob - utilities/ovs-save
ovs-save: Fix address label processing.
[ovs.git] / utilities / ovs-save
1 #! /bin/sh
2
3 # Copyright (c) 2011 Nicira Networks, Inc.
4 #
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:
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 if test "X$1" = X--help; then
18 cat <<EOF
19 $0: saves the kernel configuration of network interfaces
20 usage: $0 NETDEV...
21
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.
25
26 This script is meant as a helper for the Open vSwitch init
27 script "force-reload-kmod" command.
28 EOF
29 exit 0
30 fi
31
32 PATH=/sbin:/bin:/usr/sbin:/usr/bin
33
34 missing_program () {
35 save_IFS=$IFS
36 IFS=:
37 for dir in $PATH; do
38 IFS=$save_IFS
39 if test -x $dir/$1; then
40 return 1
41 fi
42 done
43 IFS=$save_IFS
44 return 0
45 }
46 if missing_program ip; then
47 echo "$0: ip not found in $PATH" >&2
48 exit 1
49 fi
50
51 if test "$#" = 0; then
52 echo "# $0: no parameters given (use \"$0 --help\" for help)"
53 fi
54
55 devs=$*
56 for dev in $devs; do
57 state=`ip link show $dev` || continue
58
59 echo "# $dev"
60 # Link state (Ethernet addresses, up/down, ...)
61 linkcmd=
62 case $state in
63 *"state UP"* | *[,\<]"UP"[,\>]* )
64 linkcmd="$linkcmd up"
65 ;;
66 *"state DOWN"*)
67 linkcmd="$linkcmd down"
68 ;;
69 esac
70 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
71 linkcmd="$linkcmd dynamic"
72 fi
73 if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
74 linkcmd="$linkcmd txqueuelen $qlen"
75 fi
76 if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
77 linkcmd="$linkcmd address $hwaddr"
78 fi
79 if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
80 linkcmd="$linkcmd broadcast $brd"
81 fi
82 if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
83 linkcmd="$linkcmd mtu $mtu"
84 fi
85 if test -n "$linkcmd"; then
86 echo ip link set $dev down # Required to change hwaddr.
87 echo ip link set $dev $linkcmd
88 fi
89
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
93 set -- $addr
94
95 # Check and trim family.
96 family=$1
97 shift
98 case $family in
99 inet | inet6) ;;
100 *) continue ;;
101 esac
102
103 # Trim device off the end--"ip" insists on having "dev" precede it.
104 addrcmd=
105 while test $# != 0; do
106 case $1 in
107 dynamic)
108 # Omit kernel-maintained route.
109 continue 2
110 ;;
111 scope)
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.
115 continue 2
116 fi
117 ;;
118 "$dev"|"$dev:"*)
119 # Address label string
120 addrcmd="$addrcmd label $1"
121 shift
122 continue
123 ;;
124 esac
125 addrcmd="$addrcmd $1"
126 shift
127 done
128 if test "$1" != "$dev"; then
129 addrcmd="$addrcmd $1"
130 fi
131
132 echo ip -f $family addr add $addrcmd dev $dev
133 done
134
135 # Routes.
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.
139 case $route in
140 *" proto kernel "*) continue ;;
141 esac
142
143 echo "ip route add $route dev $dev"
144 done
145
146 echo
147 done
148
149 if missing_program iptables-save; then
150 echo "# iptables-save not found in $PATH, not saving iptables state"
151 else
152 echo "# global"
153 echo "iptables-restore <<'EOF'"
154 iptables-save
155 echo "EOF"
156 fi
157
158 exit 0