#!/bin/bash #============================================================================ # Default Xen network start/stop script. # Xend calls a network script when it starts. # The script name to use is defined in /etc/xen/xend-config.sxp # in the network-script field. # # This script creates a bridge (default ${netdev}), adds a device # (defaults to the device on the default gateway route) to it, copies # the IP addresses from the device to the bridge and adjusts the routes # accordingly. # # If all goes well, this should ensure that networking stays up. # However, some configurations are upset by this, especially # NFS roots. If the bridged setup does not meet your needs, # configure a different script, for example using routing instead. # # Usage: # # network-bridge (start|stop|status) {VAR=VAL}* # # Vars: # # bridge The bridge to use (default ${netdev}). # netdev The interface to add to the bridge (default gateway device). # antispoof Whether to use iptables to prevent spoofing (default no). # # Internal Vars: # pdev="p${netdev}" # tdev=tmpbridge # # start: # Creates the bridge as tdev # Copies the IP and MAC addresses from pdev to bridge # Renames netdev to be pdev # Renames tdev to bridge # Enslaves pdev to bridge # # stop: # Removes pdev from the bridge # Transfers addresses, routes from bridge to pdev # Renames bridge to tdev # Renames pdev to netdev # Deletes tdev # # status: # Print addresses, interfaces, routes # #============================================================================ dir=$(dirname "$0") . "$dir/xen-script-common.sh" . "$dir/xen-network-common.sh" findCommand "$@" evalVariables "$@" netdev=${netdev:-$(ip route list | awk '/^default / { print $NF }' | sed 's/.* dev //')} bridge=${bridge:-${netdev}} antispoof=${antispoof:-no} pdev="p${netdev}" tdev=tmpbridge get_ip_info() { addr_pfx=`ip addr show dev $1 | egrep '^ *inet' | sed -e 's/ *inet //' -e 's/ .*//'` gateway=`ip route show dev $1 | fgrep default | sed 's/default via //'` } do_ifup() { if ! ifup $1 ; then if [ ${addr_pfx} ] ; then # use the info from get_ip_info() ip addr flush $1 ip addr add ${addr_pfx} dev $1 ip link set dev $1 up [ ${gateway} ] && ip route add default via ${gateway} fi fi } # Usage: transfer_addrs src dst # Copy all IP addresses (including aliases) from device $src to device $dst. transfer_addrs () { local src=$1 local dst=$2 # Don't bother if $dst already has IP addresses. if ip addr show dev ${dst} | egrep -q '^ *inet ' ; then return fi # Address lines start with 'inet' and have the device in them. # Replace 'inet' with 'ip addr add' and change the device name $src # to 'dev $src'. ip addr show dev ${src} | egrep '^ *inet ' | sed -e " s/inet/ip addr add/ s@\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+/[0-9]\+\)@\1@ s/${src}/dev ${dst}/ " | sh -e # Remove automatic routes on destination device ip route list | sed -ne " /dev ${dst}\( \|$\)/ { s/^/ip route del / p }" | sh -e } # Usage: transfer_routes src dst # Get all IP routes to device $src, delete them, and # add the same routes to device $dst. # The original routes have to be deleted, otherwise adding them # for $dst fails (duplicate routes). transfer_routes () { local src=$1 local dst=$2 # List all routes and grep the ones with $src in. # Stick 'ip route del' on the front to delete. # Change $src to $dst and use 'ip route add' to add. ip route list | sed -ne " /dev ${src}\( \|$\)/ { h s/^/ip route del / P g s/${src}/${dst}/ s/^/ip route add / P d }" | sh -e } ## # link_exists interface # # Returns 0 if the interface named exists (whether up or down), 1 otherwise. # link_exists() { if ip link show "$1" >/dev/null 2>/dev/null then return 0 else return 1 fi } # Set the default forwarding policy for $dev to drop. # Allow forwarding to the bridge. antispoofing () { iptables -P FORWARD DROP iptables -F FORWARD iptables -A FORWARD -m physdev --physdev-in ${pdev} -j ACCEPT } # Usage: show_status dev bridge # Print ifconfig and routes. show_status () { local dev=$1 local bridge=$2 echo '============================================================' ip addr show ${dev} ip addr show ${bridge} echo ' ' brctl show ${bridge} echo ' ' ip route list echo ' ' route -n echo '============================================================' } op_start () { if [ "${bridge}" = "null" ] ; then return fi if link_exists "$pdev"; then # The device is already up. return fi create_bridge ${tdev} preiftransfer ${netdev} transfer_addrs ${netdev} ${tdev} if ! ifdown ${netdev}; then # If ifdown fails, remember the IP details. get_ip_info ${netdev} ip link set ${netdev} down ip addr flush ${netdev} fi ip link set ${netdev} name ${pdev} ip link set ${tdev} name ${bridge} setup_bridge_port ${pdev} add_to_bridge2 ${bridge} ${pdev} do_ifup ${bridge} if [ ${antispoof} = 'yes' ] ; then antispoofing fi } op_stop () { if [ "${bridge}" = "null" ]; then return fi if ! link_exists "$bridge"; then return fi transfer_addrs ${bridge} ${pdev} if ! ifdown ${bridge}; then get_ip_info ${bridge} fi ip link set ${pdev} down ip addr flush ${bridge} brctl delif ${bridge} ${pdev} ip link set ${bridge} down ip link set ${bridge} name ${tdev} ip link set ${pdev} name ${netdev} do_ifup ${netdev} brctl delbr ${tdev} } # adds $dev to $bridge but waits for $dev to be in running state first add_to_bridge2() { local bridge=$1 local dev=$2 local maxtries=10 echo -n "Waiting for ${dev} to negotiate link." ip link set ${dev} up for i in `seq ${maxtries}` ; do if ifconfig ${dev} | grep -q RUNNING ; then break else echo -n '.' sleep 1 fi done if [ ${i} -eq ${maxtries} ] ; then echo '(link isnt in running state)' ; fi add_to_bridge ${bridge} ${dev} } case "$command" in start) op_start ;; stop) op_stop ;; status) show_status ${netdev} ${bridge} ;; *) echo "Unknown command: $command" >&2 echo 'Valid commands are: start, stop, status' >&2 exit 1 esac 5 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427