Update ODL version for fdio_l2 and fdio_l3 scenarios
[apex.git] / lib / configure-deps-functions.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 ##download dependencies if missing and configure host
12 #params: none
13 function configure_deps {
14   if ! verify_internet; then
15     echo "${red}Will not download dependencies${reset}"
16     internet=false
17   fi
18
19   # verify ip forwarding
20   if sysctl net.ipv4.ip_forward | grep 0; then
21     sudo sysctl -w net.ipv4.ip_forward=1
22     sudo sh -c "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"
23   fi
24
25   # ensure no dhcp server is running on jumphost
26   if ! sudo systemctl status dhcpd | grep dead; then
27     echo "${red}WARN: DHCP Server detected on jumphost, disabling...${reset}"
28     sudo systemctl stop dhcpd
29     sudo systemctl disable dhcpd
30   fi
31
32   # ensure networks are configured
33   systemctl status libvirtd || systemctl start libvirtd
34   systemctl status openvswitch || systemctl start openvswitch
35
36   # For baremetal we only need to create/attach Undercloud to admin and external
37   if [ "$virtual" == "FALSE" ]; then
38     virsh_enabled_networks="admin external"
39   else
40     virsh_enabled_networks=$enabled_network_list
41   fi
42
43   # ensure default network is configured correctly
44   libvirt_dir="/usr/share/libvirt/networks"
45   virsh net-list --all | grep default || virsh net-define ${libvirt_dir}/default.xml
46   virsh net-list --all | grep -E "default\s+active" > /dev/null || virsh net-start default
47   virsh net-list --all | grep -E "default\s+active\s+yes" > /dev/null || virsh net-autostart --network default
48
49   if [[ -z "$virtual" || "$virtual" == "FALSE" ]]; then
50     for network in ${enabled_network_list}; do
51       echo "${blue}INFO: Creating Virsh Network: $network & OVS Bridge: ${NET_MAP[$network]}${reset}"
52       ovs-vsctl list-br | grep "^${NET_MAP[$network]}$" > /dev/null || ovs-vsctl add-br ${NET_MAP[$network]}
53       virsh net-list --all | grep " $network " > /dev/null || (cat > ${libvirt_dir}/apex-virsh-net.xml && virsh net-define ${libvirt_dir}/apex-virsh-net.xml) << EOF
54 <network ipv6='yes'>
55   <name>$network</name>
56   <forward mode='bridge'/>
57   <bridge name='${NET_MAP[$network]}'/>
58   <virtualport type='openvswitch'/>
59 </network>
60 EOF
61       if ! (virsh net-list --all | grep " $network " > /dev/null); then
62           echo "${red}ERROR: unable to create network: ${network}${reset}"
63           exit 1;
64       fi
65       rm -f ${libvirt_dir}/apex-virsh-net.xml &> /dev/null;
66       virsh net-list | grep -E "$network\s+active" > /dev/null || virsh net-start $network
67       virsh net-list | grep -E "$network\s+active\s+yes" > /dev/null || virsh net-autostart --network $network
68     done
69
70     echo -e "${blue}INFO: Bridges set: ${reset}"
71     ovs-vsctl list-br
72
73     # bridge interfaces to correct OVS instances for baremetal deployment
74     for network in ${enabled_network_list}; do
75       if [[ "$network" != "admin" && "$network" != "external" ]]; then
76         continue
77       fi
78       this_interface=$(eval echo \${${network}_installer_vm_members})
79       # check if this a bridged interface for this network
80       if [[ ! -z "$this_interface" || "$this_interface" != "none" ]]; then
81         if ! attach_interface_to_ovs ${NET_MAP[$network]} ${this_interface} ${network}; then
82           echo -e "${red}ERROR: Unable to bridge interface ${this_interface} to bridge ${NET_MAP[$network]} for enabled network: ${network}${reset}"
83           exit 1
84         else
85           echo -e "${blue}INFO: Interface ${this_interface} bridged to bridge ${NET_MAP[$network]} for enabled network: ${network}${reset}"
86         fi
87       else
88         echo "${red}ERROR: Unable to determine interface to bridge to for enabled network: ${network}${reset}"
89         exit 1
90       fi
91     done
92   else
93     for network in ${OPNFV_NETWORK_TYPES}; do
94       if ! ovs-vsctl --may-exist add-br ${NET_MAP[$network]}; then
95        echo -e "${red}ERROR: Failed to create ovs bridge ${NET_MAP[$network]}{$reset}"
96        exit 1
97       fi
98       echo "${blue}INFO: Creating Virsh Network: $network${reset}"
99       virsh net-list --all | grep " $network " > /dev/null || (cat > ${libvirt_dir}/apex-virsh-net.xml && virsh net-define ${libvirt_dir}/apex-virsh-net.xml) << EOF
100 <network ipv6='yes'>
101 <name>$network</name>
102 <forward mode='bridge'/>
103 <bridge name='${NET_MAP[$network]}'/>
104 <virtualport type='openvswitch'/>
105 </network>
106 EOF
107       if ! (virsh net-list --all | grep $network > /dev/null); then
108           echo "${red}ERROR: unable to create network: ${network}${reset}"
109           exit 1;
110       fi
111       rm -f ${libvirt_dir}/apex-virsh-net.xml &> /dev/null;
112       virsh net-list | grep -E "$network\s+active" > /dev/null || virsh net-start $network
113       virsh net-list | grep -E "$network\s+active\s+yes" > /dev/null || virsh net-autostart --network $network
114     done
115
116     echo -e "${blue}INFO: Bridges set: ${reset}"
117     ovs-vsctl list-br
118   fi
119
120   echo -e "${blue}INFO: virsh networks set: ${reset}"
121   virsh net-list
122
123   # ensure storage pool exists and is started
124   virsh pool-list --all | grep default > /dev/null || virsh pool-define-as --name default dir --target /var/lib/libvirt/images
125   virsh pool-list | grep -Eo "default\s+active" > /dev/null || (virsh pool-autostart default; virsh pool-start default)
126
127   if ! egrep '^flags.*(vmx|svm)' /proc/cpuinfo > /dev/null; then
128     echo "${red}virtualization extensions not found, kvm kernel module insertion may fail.\n  \
129 Are you sure you have enabled vmx in your bios or hypervisor?${reset}"
130   fi
131
132   if ! lsmod | grep kvm > /dev/null; then modprobe kvm; fi
133   if ! lsmod | grep kvm_intel > /dev/null; then modprobe kvm_intel; fi
134
135   if ! lsmod | grep kvm > /dev/null; then
136     echo "${red}kvm kernel modules not loaded!${reset}"
137     return 1
138   fi
139
140   # try to enabled nested kvm
141   if [ "$virtual" == "TRUE" ]; then
142     nested_kvm=`cat /sys/module/kvm_intel/parameters/nested`
143     if [ "$nested_kvm" != "Y" ]; then
144       # try to enable nested kvm
145       echo 'options kvm-intel nested=1' > /etc/modprobe.d/kvm_intel.conf
146       if rmmod kvm_intel; then
147         modprobe kvm_intel
148       fi
149       nested_kvm=`cat /sys/module/kvm_intel/parameters/nested`
150     fi
151     if [ "$nested_kvm" != "Y" ]; then
152       echo "${red}Cannot enable nested kvm, falling back to qemu for deployment${reset}"
153       DEPLOY_OPTIONS+=" --libvirt-type qemu"
154     else
155       echo "${blue}Nested kvm enabled, deploying with kvm acceleration${reset}"
156     fi
157   fi
158
159   ##sshkeygen for root
160   if [ ! -e ~/.ssh/id_rsa.pub ]; then
161     ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
162   fi
163
164   echo "${blue}All dependencies installed and running${reset}"
165 }