vmcontext.sh

Samuele Tognini, 05/18/2011 09:04 AM

Download (3.64 KB)

 
1
#!/bin/bash
2
# Opennebula network contextualization initscript for debian
3
# Copy in /etc/init.d and install:
4
# update-rc.d vmcontext.sh start 01 S
5

    
6
### BEGIN INIT INFO
7
# Provides:       vmcontext
8
# Required-Start: mountkernfs $local_fs
9
# Required-Stop:
10
# Default-Stop:
11
# Default-Start:  S
12
# X-Start-Before: ifupdown networking
13
# Short-Description: Start the Opennebula context.
14
### END INIT INFO
15

    
16
# -------------------------------------------------------------------------- #
17
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
18
#                                                                            #
19
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
20
# not use this file except in compliance with the License. You may obtain    #
21
# a copy of the License at                                                   #
22
#                                                                            #
23
# http://www.apache.org/licenses/LICENSE-2.0                                 #
24
#                                                                            #
25
# Unless required by applicable law or agreed to in writing, software        #
26
# distributed under the License is distributed on an "AS IS" BASIS,          #
27
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
28
# See the License for the specific language governing permissions and        #
29
# limitations under the License.                                             #
30
#--------------------------------------------------------------------------- #
31

    
32
. /lib/lsb/init-functions
33

    
34
# Gets IP address from a given MAC
35
mac2ip() {
36
    mac=$1
37
 
38
    let ip_a=0x`echo $mac | cut -d: -f 3`
39
    let ip_b=0x`echo $mac | cut -d: -f 4`
40
    let ip_c=0x`echo $mac | cut -d: -f 5`
41
    let ip_d=0x`echo $mac | cut -d: -f 6`
42
 
43
    ip="$ip_a.$ip_b.$ip_c.$ip_d"
44
 
45
    echo $ip
46
}
47
 
48
# Gets the network part of an IP
49
get_network() {
50
    IP=$1
51
 
52
    echo $IP | cut -d'.' -f1,2,3
53
}
54
 
55
get_interfaces() {
56
    IFCMD="/sbin/ifconfig -a"
57
 
58
    $IFCMD | grep ^eth | sed 's/ *Link encap:Ethernet.*HWaddr /-/g'
59
}
60
 
61
get_dev() {
62
    echo $1 | cut -d'-' -f 1
63
}
64
 
65
get_mac() {
66
    echo $1 | cut -d'-' -f 2
67
}
68
 
69
gen_hosts() {
70
    NETWORK=$1
71
    echo "127.0.0.1 localhost"
72
    for n in `seq -w 01 99`; do
73
        n2=`echo $n | sed 's/^0*//'`
74
        echo ${NETWORK}.$n2 cluster${n}
75
    done
76
}
77
 
78
gen_exports() {
79
    NETWORK=$1
80
    echo "/images ${NETWORK}.0/255.255.255.0(rw,async,no_subtree_check)"
81
}
82
 
83
gen_hostname() {
84
    MAC=$1
85
    NUM=`mac2ip $MAC | cut -d'.' -f4`
86
    NUM2=`echo 000000$NUM | sed 's/.*\(..\)/\1/'`
87
    echo cluster$NUM2
88
}
89
 
90
gen_interface() {
91
    DEV_MAC=$1
92
    DEV=`get_dev $DEV_MAC`
93
    MAC=`get_mac $DEV_MAC`
94
    IP=`mac2ip $MAC`
95
    NETWORK=`get_network $IP`
96
 
97
    cat <<EOT
98
auto $DEV
99
iface $DEV inet static
100
  address $IP
101
  network $NETWORK.0
102
  netmask 255.255.255.0
103
EOT
104
 
105
    if [ $DEV == "eth0" ]; then
106
      echo "  gateway $NETWORK.1"
107
    fi
108
 
109
echo ""
110
}
111
 
112
case "$1" in
113
  start)
114
	log_action_msg "Starting the Opennebula contextualization network"
115
	IFACES=`get_interfaces`
116
 
117
	for i in $IFACES; do
118
	    MASTER_DEV_MAC=$i
119
	    DEV=`get_dev $i`
120
	    MAC=`get_mac $i`
121
	    IP=`mac2ip $MAC`
122
	    NETWORK=`get_network $IP`
123
	done
124
 
125
	# gen_hosts $NETWORK > /etc/hosts
126
 
127
	# gen_exports $NETWORK  > /etc/exports
128
 
129
	# gen_hostname $MAC  > /etc/hostname
130
 
131
	(
132
cat <<EOT
133
auto lo
134
iface lo inet loopback
135
 
136
EOT
137
 
138
	for i in $IFACES; do
139
	    gen_interface $i
140
	done
141
	) > /etc/network/interfaces
142
 
143
	# /bin/hostname `cat /etc/hostname`
144
	;;
145
  restart|reload|force-reload)
146
	echo "Error: argument '$1' not supported" >&2
147
        exit 3
148
        ;;
149
  stop)
150
        ;;
151
  *)
152
        echo "Usage: $0 start|stop" >&2
153
        exit 3
154
        ;;
155
esac