2 # Common Functions used by OPNFV Apex
3 # author: Tim Rozet (trozet@redhat.com)
6 ##params: interface name
8 ip addr show $1 | grep -Eo '^\s+inet\s+[\.0-9]+' | awk '{print $2}'
11 ##finds subnet of ip and netmask
13 function find_subnet {
14 IFS=. read -r i1 i2 i3 i4 <<< "$1"
15 IFS=. read -r m1 m2 m3 m4 <<< "$2"
16 printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
19 ##verify subnet has at least n IPs
20 ##params: subnet mask, n IPs
21 function verify_subnet_size {
22 IFS=. read -r i1 i2 i3 i4 <<< "$1"
25 ##this function assumes you would never need more than 254
26 ##we check here to make sure
27 if [ "$num_ips_required" -ge 254 ]; then
28 echo -e "\n\n${red}ERROR: allocating more than 254 IPs is unsupported...Exiting${reset}\n\n"
32 ##we just return if 3rd octet is not 255
33 ##because we know the subnet is big enough
34 if [ "$i3" -ne 255 ]; then
36 elif [ $((254-$i4)) -ge "$num_ips_required" ]; then
39 echo -e "\n\n${red}ERROR: Subnet is too small${reset}\n\n"
44 ##finds last usable ip (broadcast minus 1) of a subnet from an IP and netmask
45 ## Warning: This function only works for IPv4 at the moment.
47 function find_last_ip_subnet {
48 IFS=. read -r i1 i2 i3 i4 <<< "$1"
49 IFS=. read -r m1 m2 m3 m4 <<< "$2"
50 IFS=. read -r s1 s2 s3 s4 <<< "$((i1 & m1)).$((i2 & m2)).$((i3 & m3)).$((i4 & m4))"
51 printf "%d.%d.%d.%d\n" "$((255 - $m1 + $s1))" "$((255 - $m2 + $s2))" "$((255 - $m3 + $s3))" "$((255 - $m4 + $s4 - 1))"
54 ##increments subnet by a value
57 function increment_subnet {
58 IFS=. read -r i1 i2 i3 i4 <<< "$1"
59 printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 | $2))"
62 ##finds netmask of interface
64 ##returns long format 255.255.x.x
65 function find_netmask {
66 ifconfig $1 | grep -Eo 'netmask\s+[\.0-9]+' | awk '{print $2}'
69 ##finds short netmask of interface
71 ##returns short format, ex: /21
72 function find_short_netmask {
73 echo "/$(ip addr show $1 | grep -Eo '^\s+inet\s+[\/\.0-9]+' | awk '{print $2}' | cut -d / -f2)"
78 ##assumes a /24 subnet
80 baseaddr="$(echo $1 | cut -d. -f1-3)"
81 lsv="$(echo $1 | cut -d. -f4)"
82 if [ "$lsv" -ge 254 ]; then
89 ##subtracts a value from an IP address
90 ##params: last ip, ip_count
91 ##assumes ip_count is less than the last octect of the address
93 IFS=. read -r i1 i2 i3 i4 <<< "$1"
95 if [ $i4 -lt $ip_count ]; then
96 echo -e "\n\n${red}ERROR: Can't subtract $ip_count from IP address $1 Exiting${reset}\n\n"
99 printf "%d.%d.%d.%d\n" "$i1" "$i2" "$i3" "$((i4 - $ip_count ))"
102 ##check if IP is in use
104 ##ping ip to get arp entry, then check arp
105 function is_ip_used {
106 ping -c 5 $1 > /dev/null 2>&1
107 arp -n | grep "$1 " | grep -iv incomplete > /dev/null 2>&1
110 ##find next usable IP
112 function next_usable_ip {
114 while [ "$new_ip" ]; do
115 if ! is_ip_used $new_ip; then
119 new_ip=$(next_ip $new_ip)
124 ##increment ip by value
125 ##params: ip, amount to increment by
126 ##increment_ip $next_private_ip 10
127 function increment_ip {
128 baseaddr="$(echo $1 | cut -d. -f1-3)"
129 lsv="$(echo $1 | cut -d. -f4)"
132 if [ "$lsv" -ge 254 ]; then
138 ##finds gateway on system
139 ##params: interface to validate gateway on (optional)
141 function find_gateway {
142 local gw gw_interface
143 gw=$(ip route | grep default | awk '{print $3}')
144 gw_interface=$(ip route get $gw | awk '{print $3}')
146 if [ "$gw_interface" == "$1" ]; then
152 ##finds subnet in CIDR notation for interface
153 ##params: interface to find CIDR
155 local cidr network ip netmask short_mask
157 netmask=$(find_netmask $1)
158 if [[ -z "$ip" || -z "$netmask" ]]; then
161 network=$(find_subnet ${ip} ${netamsk})
162 short_mask=$(find_short_netmask $1)
163 if [[ -z "$network" || -z "$short_mask" ]]; then
166 cidr="${subnet}'\'${short_mask}"
170 ##finds block of usable IP addresses for an interface
171 ##simply returns at the moment the correct format
172 ##after first 20 IPs, and leave 20 IPs at end of subnet (for floating ips, etc)
173 ##params: interface to find IP
174 function find_usable_ip_range {
175 local interface_ip subnet_mask first_block_ip last_block_ip
176 interface_ip=$(find_ip $1)
177 subnet_mask=$(find_netmask $1)
178 if [[ -z "$interface_ip" || -z "$subnet_mask" ]]; then
181 interface_ip=$(increment_ip ${interface_ip} 20)
182 first_block_ip=$(next_usable_ip ${interface_ip})
183 if [ -z "$first_block_ip" ]; then
186 last_block_ip=$(find_last_ip_subnet ${interface_ip} ${subnet_mask})
187 if [ -z "$last_block_ip" ]; then
190 last_block_ip=$(subtract_ip ${last_block_ip} 20)
191 echo "${first_block_ip},${last_block_ip}"
196 ##generates usable IP range in correct format based on CIDR
197 ##assumes the first 20 IPs are used (by instack or otherwise)
199 function generate_usable_ip_range {
200 local first_ip first_block_ip last_block_ip
201 first_ip=$(ipcalc -nb $1 | grep HostMin: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
202 first_block_ip=$(increment_ip ${first_ip} 20)
203 last_block_ip=$(ipcalc -nb $1 | grep HostMax: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
204 if [[ -z "$first_block_ip" || -z "$last_block_ip" ]]; then
207 last_block_ip=$(subtract_ip ${last_block_ip} 20)
208 echo "${first_block_ip},${last_block_ip}"
212 ##find the instack IP address
213 ##finds first usable IP on subnet
215 function find_provisioner_ip {
217 interface_ip=$(find_ip $1)
218 if [ -z "$interface_ip" ]; then
221 echo $(increment_ip ${interface_ip} 1)
224 ##generates instack IP address based on CIDR
226 function generate_provisioner_ip {
228 provisioner_ip=$(ipcalc -nb $1 | grep HostMin: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
231 ##finds the dhcp range available via interface
232 ##uses first 8 IPs, after 2nd IP
234 function find_dhcp_range {
235 local dhcp_range_start dhcp_range_end interface_ip
236 interface_ip=$(find_ip $1)
237 if [ -z "$interface_ip" ]; then
240 dhcp_range_start=$(increment_ip ${interface_ip} 2)
241 dhcp_range_end=$(increment_ip ${dhcp_range_start} 8)
242 echo "${dhcp_range_start},${dhcp_range_end}"
245 ##generates the dhcp range available via CIDR
246 ##uses first 8 IPs, after 1st IP
248 function generate_dhcp_range {
249 local dhcp_range_start dhcp_range_end first_ip
250 first_ip=$(ipcalc -nb $1 | grep HostMin: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
251 if [ -z "$first_ip" ]; then
254 dhcp_range_start=$(increment_ip ${first_ip} 1)
255 dhcp_range_end=$(increment_ip ${dhcp_range_start} 8)
256 echo "${dhcp_range_start},${dhcp_range_end}"
259 ##finds the introspection range available via interface
260 ##uses 8 IPs, after the first 10 IPs
262 function find_introspection_range {
263 local inspect_range_start inspect_range_end interface_ip
264 interface_ip=$(find_ip $1)
265 if [ -z "$interface_ip" ]; then
268 inspect_range_start=$(increment_ip ${interface_ip} 10)
269 inspect_range_end=$(increment_ip ${inspect_range_start} 8)
270 echo "${inspect_range_start},${inspect_range_end}"
273 ##generate the introspection range available via CIDR
274 ##uses 8 IPs, after the first 10 IPs
276 function generate_introspection_range {
277 local inspect_range_start inspect_range_end first_ip
278 first_ip=$(ipcalc -nb $1 | grep HostMin: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
279 if [ -z "$first_ip" ]; then
282 inspect_range_start=$(increment_ip ${first_ip} 10)
283 inspect_range_end=$(increment_ip ${inspect_range_start} 8)
284 echo "${inspect_range_start},${inspect_range_end}"
287 ##finds the floating ip range available via interface
288 ##uses last 20 IPs of a subnet
290 function find_floating_ip_range {
291 local float_range_start float_range_end interface_ip subnet_mask
292 interface_ip=$(find_ip $1)
293 subnet_mask=$(find_netmask $1)
294 if [[ -z "$interface_ip" || -z "$subnet_mask" ]]; then
297 float_range_end=$(find_last_ip_subnet ${interface_ip} ${subnet_mask})
298 float_range_start=$(subtract_ip ${float_range_end} 19)
299 echo "${float_range_start},${float_range_end}"
302 ##generate the floating range available via CIDR
303 ##uses last 20 IPs of subnet
305 function generate_floating_ip_range {
306 local float_range_start float_range_end last_ip
307 last_ip=$(ipcalc -nb $1 | grep HostMax: | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")
308 if [ -z "$last_ip" ]; then
311 float_range_start=$(subtract_ip ${last_ip} 19)
312 float_range_end=${last_ip}
313 echo "${float_range_start},${float_range_end}"
316 ##attach interface to OVS and set the network config correctly
317 ##params: bride to attach to, interface to attach, network type (optional)
318 ##public indicates attaching to a public interface
319 function attach_interface_to_ovs {
320 local bridge interface
321 local if_ip if_mask if_gw if_file ovs_file
323 if [[ -z "$1" || -z "$2" ]]; then
330 if ovs-vsctl list-ports ${bridge} | grep ${interface}; then
334 if_file=/etc/sysconfig/network-scripts/ifcfg-${interface}
335 ovs_file=/etc/sysconfig/network-scripts/ifcfg-${bridge}
337 if [ -e "$if_file" ]; then
338 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
339 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
340 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
342 echo "ERROR: ifcfg file missing for ${interface}"
346 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
347 echo "ERROR: IPADDR or NETMASK missing for ${interface}"
349 elif [[ -z "$if_gw" && "$3" == "public_network" ]]; then
350 echo "ERROR: GATEWAY missing for ${interface}, which is public"
354 # move old config file to .orig
355 mv -f ${if_file} ${if_file}.orig
356 echo "DEVICE=${interface}
364 PROMISC=yes" > ${if_file}
366 if [ -z ${if_gw} ]; then
368 echo "DEVICE=${bridge}
376 PEERDNS=no" > ${ovs_file}
379 echo "DEVICE=${bridge}
388 PEERDNS=no" > ${ovs_file}
391 sudo systemctl restart network
394 ##detach interface from OVS and set the network config correctly
395 ##params: bridge to detach from
396 ##assumes only 1 real interface attached to OVS
397 function detach_interface_from_ovs {
399 local port_output ports_no_orig
401 local if_ip if_mask if_gw
403 net_path=/etc/sysconfig/network-scripts/
404 if [[ -z "$1" ]]; then
410 # if no interfaces attached then return
411 if ! ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*"; then
415 # look for .orig ifcfg files to use
416 port_output=$(ovs-vsctl list-ports ${bridge} | grep -Ev "vnet[0-9]*")
417 while read -r line; do
418 if [ -z "$line" ]; then
420 elif [ -e ${net_path}/ifcfg-${line}.orig ]; then
421 mv -f ${net_path}/ifcfg-${line}.orig ${net_path}/ifcfg-${line}
422 elif [ -e ${net_path}/ifcfg-${bridge} ]; then
423 if_ip=$(sed -n 's/^IPADDR=\(.*\)$/\1/p' ${if_file})
424 if_mask=$(sed -n 's/^NETMASK=\(.*\)$/\1/p' ${if_file})
425 if_gw=$(sed -n 's/^GATEWAY=\(.*\)$/\1/p' ${if_file})
427 if [[ -z "$if_ip" || -z "$if_mask" ]]; then
428 echo "ERROR: IPADDR or NETMASK missing for ${bridge} and no .orig file for interface ${line}"
432 if [ -z ${if_gw} ]; then
441 PEERDNS=no" > ${net_path}/ifcfg-${line}
451 PEERDNS=no" > ${net_path}/ifcfg-${line}
455 echo "ERROR: Real interface ${line} attached to bridge, but no interface or ${bridge} ifcfg file exists"
459 done <<< "$port_output"
461 # modify the bridge ifcfg file
462 # to remove IP params
463 sudo sed -i 's/IPADDR=.*//' ${net_path}/ifcfg-${bridge}
464 sudo sed -i 's/NETMASK=.*//' ${net_path}/ifcfg-${bridge}
465 sudo sed -i 's/GATEWAY=.*//' ${net_path}/ifcfg-${bridge}
467 sudo systemctl restart network