Statistics
| Branch: | Tag: | Revision:

root / tools / kvm-ifup.in @ 7f965aea

History | View | Annotate | Download (2.4 kB)

1 1817dca9 Michael Hanselmann
#!/bin/bash
2 f299ca21 Michael Hanselmann
#
3 26916aad Apollon Oikonomopoulos
4 1817dca9 Michael Hanselmann
# Copyright (C) 2011, 2012 Google Inc.
5 26916aad Apollon Oikonomopoulos
#
6 26916aad Apollon Oikonomopoulos
# This program is free software; you can redistribute it and/or modify
7 26916aad Apollon Oikonomopoulos
# it under the terms of the GNU General Public License as published by
8 26916aad Apollon Oikonomopoulos
# the Free Software Foundation; either version 2 of the License, or
9 26916aad Apollon Oikonomopoulos
# (at your option) any later version.
10 26916aad Apollon Oikonomopoulos
#
11 26916aad Apollon Oikonomopoulos
# This program is distributed in the hope that it will be useful, but
12 26916aad Apollon Oikonomopoulos
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 26916aad Apollon Oikonomopoulos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 26916aad Apollon Oikonomopoulos
# General Public License for more details.
15 26916aad Apollon Oikonomopoulos
#
16 26916aad Apollon Oikonomopoulos
# You should have received a copy of the GNU General Public License
17 26916aad Apollon Oikonomopoulos
# along with this program; if not, write to the Free Software
18 26916aad Apollon Oikonomopoulos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 26916aad Apollon Oikonomopoulos
# 02110-1301, USA.
20 26916aad Apollon Oikonomopoulos
21 6e3bf290 Michael Hanselmann
@SHELL_ENV_INIT@
22 6e3bf290 Michael Hanselmann
23 26916aad Apollon Oikonomopoulos
if [ -z "$INTERFACE" ]; then
24 1e00889c Iustin Pop
  echo "No network interface specified"
25 1e00889c Iustin Pop
  exit 1
26 26916aad Apollon Oikonomopoulos
fi
27 26916aad Apollon Oikonomopoulos
28 26916aad Apollon Oikonomopoulos
if [ -z "$MODE" ]; then
29 1e00889c Iustin Pop
  echo "MODE not specified"
30 1e00889c Iustin Pop
  exit 1
31 26916aad Apollon Oikonomopoulos
fi
32 26916aad Apollon Oikonomopoulos
33 26916aad Apollon Oikonomopoulos
# Execute the user-supplied network script, if applicable
34 6e3bf290 Michael Hanselmann
if [ -x "$CONF_DIR/kvm-vif-bridge" ]; then
35 6e3bf290 Michael Hanselmann
  exec $CONF_DIR/kvm-vif-bridge
36 26916aad Apollon Oikonomopoulos
fi
37 26916aad Apollon Oikonomopoulos
38 99e92fa0 Michael Hanselmann
if [ "$MODE" = "bridged" ]; then
39 1e00889c Iustin Pop
  # Fix the autogenerated MAC to have the first octet set to "fe"
40 1e00889c Iustin Pop
  # to discourage the bridge from using the TAP dev's MAC
41 1e00889c Iustin Pop
  FIXED_MAC=$(ip link show $INTERFACE | \
42 1e00889c Iustin Pop
    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
43 1e00889c Iustin Pop
  ip link set $INTERFACE address $FIXED_MAC
44 be3f52f3 Simon Deziel
45 1e00889c Iustin Pop
  ip link set $INTERFACE up
46 1e00889c Iustin Pop
  ip link set $INTERFACE mtu $(</sys/class/net/${BRIDGE}/mtu)
47 3bdca55d Andrea Spadaccini
48 1e00889c Iustin Pop
  # Connect the interface to the bridge
49 1e00889c Iustin Pop
  brctl addif $BRIDGE $INTERFACE
50 57fb6fcb Guido Trotter
51 57fb6fcb Guido Trotter
elif [ "$MODE" = "openvswitch" ]; then
52 57fb6fcb Guido Trotter
  ovs-vsctl add-port ${LINK} $INTERFACE
53 57fb6fcb Guido Trotter
54 26916aad Apollon Oikonomopoulos
else
55 1e00889c Iustin Pop
  ip link set $INTERFACE up
56 be3f52f3 Simon Deziel
57 1e00889c Iustin Pop
  if [ -z "$IP" ]; then
58 1e00889c Iustin Pop
    echo "Routed NIC but no IP address specified"
59 1e00889c Iustin Pop
    exit 1
60 1e00889c Iustin Pop
  fi
61 26916aad Apollon Oikonomopoulos
62 1e00889c Iustin Pop
  # Route traffic targeted at the IP to the interface
63 1e00889c Iustin Pop
  if [ -n "$LINK" ]; then
64 1e00889c Iustin Pop
    while ip rule del dev $INTERFACE; do :; done
65 1e00889c Iustin Pop
    ip rule add dev $INTERFACE table $LINK
66 1e00889c Iustin Pop
    ip route replace $IP table $LINK proto static dev $INTERFACE
67 26916aad Apollon Oikonomopoulos
68 1e00889c Iustin Pop
  else
69 1e00889c Iustin Pop
    ip route replace $IP proto static dev $INTERFACE
70 1e00889c Iustin Pop
  fi
71 26916aad Apollon Oikonomopoulos
72 57fb6fcb Guido Trotter
  # Allow routing and arp proxying, or ndp proxying (IPv6)
73 1e00889c Iustin Pop
  if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
74 1e00889c Iustin Pop
    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
75 1e00889c Iustin Pop
    echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
76 1e00889c Iustin Pop
  fi
77 26916aad Apollon Oikonomopoulos
78 1e00889c Iustin Pop
  if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
79 1e00889c Iustin Pop
    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
80 1e00889c Iustin Pop
    echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
81 1e00889c Iustin Pop
  fi
82 26916aad Apollon Oikonomopoulos
fi