~benji/ubuntu/precise/lxc/bug-951150

« back to all changes in this revision

Viewing changes to debian/lxc.init

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2012-02-09 10:22:20 UTC
  • Revision ID: package-import@ubuntu.com-20120209102220-2hzw60f31if747hr
Tags: 0.7.5-3ubuntu22
* debian/lxc.init:
  - at setup_lxc_bridge, return early if ${LXC_BRIDGE) already exists.
    (LP: #929514)
  - switch 'ip link show' and 'brctl show' checks for /sys/class/net lookups.
  - try to prevent destroying host network setup if /etc/default/lxc is
    bad.  Set defaults for lxc network variables if unset.
  - don't pass along variables as arguments if not needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
LXC_AUTO="${LXC_AUTO:-true}"
32
32
 
33
33
USE_LXC_BRIDGE="${USE_LXC_BRIDGE:-false}"
 
34
 
 
35
# Make sure admin didn't blank these out while leavng USE_LXC_BRIDGE true
 
36
# in /etc/default/lxc
 
37
LXC_BRIDGE="${LXC_BRIDGE:-lxcbr0}"
 
38
LXC_ADDR="${LXC_ADDR:-10.0.3.1}"
 
39
LXC_NETMASK="${LXC_NETMASK:-255.255.255.0}"
 
40
LXC_NETWORK="${LXC_NETWORK:-10.0.3.0/24}"
 
41
LXC_DHCP_RANGE="${LXC_DHCP_RANGE:-10.0.3.2,10.0.3.254}"
 
42
LXC_DHCP_MAX="${LXC_DHCP_MAX:-253}"
34
43
varrun="/var/run/lxc"
35
44
 
36
45
undo_network() {
52
61
 
53
62
setup_lxc_bridge ()
54
63
{
55
 
        br="$1"
56
 
        addr="$2"
57
 
        netmask="$3"
58
 
        net="$4"
59
 
        dhcp_range="$5"
60
 
        dhcp_max="$6"
61
 
 
62
 
        if [ -f ${varrun}/network_up ]; then
 
64
        if [ -f ${varrun}/network_up -o -d /sys/class/net/${LXC_BRIDGE} ]; then
63
65
                return
64
66
        fi
65
67
        trap undo_network EXIT
66
68
        set -e
67
69
        echo 1 > /proc/sys/net/ipv4/ip_forward
68
70
        mkdir -p ${varrun}
69
 
        brctl addbr ${br}
70
 
        ifconfig ${br} ${addr} netmask ${netmask} up
71
 
        iptables -A POSTROUTING -s ${net} -t nat -j MASQUERADE
72
 
        dnsmasq --strict-order --bind-interfaces --pid-file=${varrun}/dnsmasq.pid --conf-file= --listen-address ${addr} --dhcp-range ${dhcp_range} --dhcp-lease-max=${dhcp_max} --dhcp-no-override --except-interface=lo --interface=${br}
 
71
        brctl addbr ${LXC_BRIDGE}
 
72
        ifconfig ${LXC_BRIDGE} ${LXC_ADDR} netmask ${LXC_NETMASK} up
 
73
        iptables -A POSTROUTING -s ${LXC_NETWORK} -t nat -j MASQUERADE
 
74
        dnsmasq --strict-order --bind-interfaces --pid-file=${varrun}/dnsmasq.pid --conf-file= --listen-address ${LXC_ADDR} --dhcp-range ${LXC_DHCP_RANGE} --dhcp-lease-max=${LXC_DHCP_MAX} --dhcp-no-override --except-interface=lo --interface=${LXC_BRIDGE}
73
75
        touch ${varrun}/network_up
74
76
        trap - EXIT
75
77
        set +e
83
85
                start)
84
86
                        case "${USE_LXC_BRIDGE}" in
85
87
                                true)
86
 
                                        setup_lxc_bridge $LXC_BRIDGE $LXC_ADDR $LXC_NETMASK $LXC_NETWORK $LXC_DHCP_RANGE $LXC_DHCP_MAX
 
88
                                        setup_lxc_bridge
87
89
                                        ;;
88
90
                                *)
89
91
                                        log_progress_msg "(Lxc network disabled)"
96
98
 
97
99
bridge_has_ifs ()
98
100
{
99
 
        n=`brctl show ${LXC_BRIDGE} | tail -n +2 | head -1 | wc | awk '{ print $2 }'`
100
 
        if [ $n -lt 4 ]; then
101
 
                # no attached interfaces
102
 
                return 1
103
 
        fi
104
 
        # fourth column has an entry which is an interface
105
 
        return 0
 
101
        ls /sys/class/net/lxcbr0/brif/* > /dev/null 2>&1
 
102
        if [ $? -eq 0 ]; then
 
103
                return 0
 
104
        fi
 
105
        return 1
106
106
}
107
107
 
108
108
teardown_lxc_bridge ()
119
119
                                return
120
120
                        fi
121
121
                        log_action_msg "Tearing down lxc network on ${LXC_BRIDGE}"
122
 
                        ip link show ${LXC_BRIDGE} > /dev/null 2>&1
123
 
                        if [ $? -eq 0 ]; then
 
122
                        if [ -d /sys/class/net/${LXC_BRIDGE} ]; then
124
123
                                ifconfig ${LXC_BRIDGE} down
125
124
                                iptables -t nat -D POSTROUTING -s ${LXC_NETWORK} -j MASQUERADE
126
125
                                pid=`cat ${varrun}/dnsmasq.pid`