vmcontext-rpm.sh

VM Network init script for RPM based distros. - netmatters-gmail-com -, 03/18/2009 03:19 PM

Download (2.87 KB)

 
1
#!/bin/bash
2
# -------------------------------------------------------------------------- #
3
# Copyright 2002-2009, Distributed Systems Architecture Group, Universidad   #
4
# Complutense de Madrid (dsa-research.org)                                   #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18
#
19
# Gets IP address from a given MAC
20
mac2ip() {
21
  mac=$1
22

    
23
  let ip_a=0x`echo $mac | cut -d: -f 3`
24
  let ip_b=0x`echo $mac | cut -d: -f 4`
25
  let ip_c=0x`echo $mac | cut -d: -f 5`
26
  let ip_d=0x`echo $mac | cut -d: -f 6`
27

    
28
  ip="$ip_a.$ip_b.$ip_c.$ip_d"
29

    
30
  echo $ip
31
}
32

    
33
# Gets the network part of an IP
34
get_network() {
35
  IP=$1
36

    
37
  echo $IP | cut -d'.' -f1,2,3
38
}
39

    
40
get_interfaces() {
41
  IFCMD="/sbin/ifconfig -a"
42

    
43
  $IFCMD | grep ^eth | sed 's/ *Link encap:Ethernet.*HWaddr /-/g'
44
}
45

    
46
get_dev() {
47
  echo $1 | cut -d'-' -f 1
48
}
49

    
50
get_mac() {
51
  echo $1 | cut -d'-' -f 2
52
}
53

    
54
gen_hosts() {
55
  NETWORK=$1
56
  echo "127.0.0.1 localhost"
57
  for n in `seq -w 01 99`; do
58
      n2=`echo $n | sed 's/^0*//'`
59
      echo ${NETWORK}.$n2 cluster${n}
60
  done
61
}
62

    
63
gen_exports() {
64
  NETWORK=$1
65
  echo "/var/nebula/machines ${NETWORK}.0/255.255.255.0(rw,async,no_subtree_check)"
66
}
67

    
68
gen_hostname() {
69
  MAC=$1
70
  NUM=`mac2ip $MAC | cut -d'.' -f4`
71
  NUM2=`echo 000000$NUM | sed 's/.*\(..\)/\1/'`
72
  echo cluster$NUM2
73
}
74

    
75
gen_interface() {
76
DEV_MAC=$1
77
DEV=`get_dev $DEV_MAC`
78
MAC=`get_mac $DEV_MAC`
79
IP=`mac2ip $MAC`
80
NETWORK=`get_network $IP`
81

    
82
cat <<EOT
83
DEVICE=$DEV
84
BOOTPROTO=none
85
HWADDR=$MAC
86
ONBOOT=yes
87
TYPE=Ethernet
88
NETMASK=255.255.255.0
89
IPADDR=$IP
90
EOT
91

    
92
  if [ $DEV == "eth0" ]; then
93
    echo "GATEWAY=$NETWORK.1"
94
  fi
95

    
96
echo ""
97
}
98

    
99
IFACES=`get_interfaces`
100

    
101
for i in $IFACES; do
102
    MASTER_DEV_MAC=$i
103
    DEV=`get_dev $i`
104
    MAC=`get_mac $i`
105
    IP=`mac2ip $MAC`
106
    NETWORK=`get_network $IP`
107
done
108

    
109
gen_hosts $NETWORK > /etc/hosts
110
gen_hostname $MAC  > /etc/hostname
111

    
112
for i in $IFACES; do
113
  DEV=`get_dev $i`
114
  (gen_interface $i) > /etc/sysconfig/network-scripts/ifcfg-${DEV}
115
done
116

    
117
/bin/hostname `cat /etc/hostname`
118