blob: e5133c9ec59a9a3930272c8549c76dafec811ea7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
|
function destroy_nat() {
sudo virsh net-destroy $1 2>&1
sudo virsh net-undefine $1 2>&1
rm -rf $COMPASS_DIR/deploy/work/network/$1.xml
}
function destroy_bridge()
{
bridge=$1
nic=$2
ip="$3"
brige_info=$(ip addr show $bridge 2>/dev/null)
if [[ -z $brige_info ]]; then
return
fi
if [[ -n $ip ]]; then
ip addr delete $ip dev $bridge | true
fi
ips=`echo "$brige_info" | grep 'inet ' | sed "s/inet //g"`
routes=$(ip route show | grep $bridge)
ip link set $bridge down
brctl delbr $bridge
if [[ -n $ips ]]; then
echo "$ips" | while read line; do
echo $line | sed "s/$bridge/dev $nic/g" | xargs ip addr add | true
done
fi
if [[ -n $routes ]]; then
echo "$routes" | while read line; do
echo $line | sed "s/$bridge/$nic/g" | xargs ip route add | true
done
fi
}
function get_broadcast_addr()
{
ip=(${1//[!0-9]/ })
mask=(${2//[!0-9]/ })
for i in {0..3}; do
num=$((${ip[$i]} | $((${mask[$i]} ^ 255))))
if [[ -z $broadcast ]]; then
broadcast="$num"
else
broadcast="$broadcast.$num"
fi
done
echo ${broadcast}
}
function get_mask_len()
{
mask=`echo $1 | awk -F'.' '{print ($1*(2^24)+$2*(2^16)+$3*(2^8)+$4)}'`
mask_len=`echo "obase=2;${mask}"|bc|awk -F'0' '{print length($1)}'`
echo $mask_len
}
function create_bridge()
{
bridge=$1
nic=$2
ips=$(ip addr show $nic | grep 'inet ' | sed "s/inet //g")
routes=$(ip route show | grep $nic)
ip addr flush $nic
brctl addbr $bridge
brctl addif $bridge $nic
ip link set $bridge up
if [[ -n $ips ]]; then
echo "$ips" | while read line; do
echo $line | sed -e "s/$nic/dev $bridge/g" | xargs ip addr add
done
fi
if [[ -n $routes ]]; then
echo "$routes" | while read line; do
echo $line | sed "s/$nic/$bridge/g" | xargs ip route add | true
done
fi
}
function add_install_ip() {
mask_len=`get_mask_len $INSTALL_MASK`
broadcast=`get_broadcast_addr $INSTALL_GW $INSTALL_MASK`
ip addr add $INSTALL_GW/$mask_len brd $broadcast dev $bridge
}
function setup_install_bridge() {
destroy_bridge br_install $INSTALL_NIC $INSTALL_GW
create_bridge br_install $INSTALL_NIC
add_install_ip
}
function setup_external_net() {
if [[ -z `brctl show br_external 2>/dev/null` ]]; then
brctl addbr br_external
fi
if [[ -z $EXTERNAL_NIC ]]; then
return
fi
if [[ $TYPE == baremetal && $EXTERNAL_NIC == $INSTALL_NIC ]]; then
exit 1
fi
destroy_bridge br_external $EXTERNAL_NIC ""
create_bridge br_external $EXTERNAL_NIC
}
function setup_install_nat() {
destroy_nat install
# create install network
sed -e "s/REPLACE_BRIDGE/br_install/g" \
-e "s/REPLACE_NAME/install/g" \
-e "s/REPLACE_GATEWAY/$INSTALL_GW/g" \
-e "s/REPLACE_MASK/$INSTALL_MASK/g" \
-e "s/REPLACE_START/$INSTALL_IP_START/g" \
-e "s/REPLACE_END/$INSTALL_IP_END/g" \
$COMPASS_DIR/deploy/template/network/nat.xml \
> $WORK_DIR/network/install.xml
sudo virsh net-define $WORK_DIR/network/install.xml
sudo virsh net-start install
}
function create_nets() {
destroy_nat mgmt
# create mgmt network
sed -e "s/REPLACE_BRIDGE/br_mgmt/g" \
-e "s/REPLACE_NAME/mgmt/g" \
-e "s/REPLACE_GATEWAY/$MGMT_GW/g" \
-e "s/REPLACE_MASK/$MGMT_MASK/g" \
-e "s/REPLACE_START/$MGMT_IP_START/g" \
-e "s/REPLACE_END/$MGMT_IP_END/g" \
$COMPASS_DIR/deploy/template/network/nat.xml \
> $WORK_DIR/network/mgmt.xml
sudo virsh net-define $WORK_DIR/network/mgmt.xml
sudo virsh net-start mgmt
# create install network
if [[ ! -z $VIRT_NUMBER ]];then
setup_install_nat
else
setup_install_bridge
fi
# create external network
setup_external_net
}
|