Bumps OVS version to 2.8 for OVN
[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     # verify virtualbmc is installed for a virtual install
94     if ! rpm -q python2-virtualbmc; then
95        echo -e "${red}ERROR: Package python2-virtualbmc is required to do a virtual install.$reset"
96        exit 1
97     fi
98     for network in ${OPNFV_NETWORK_TYPES}; do
99       if ! ovs-vsctl --may-exist add-br ${NET_MAP[$network]}; then
100        echo -e "${red}ERROR: Failed to create ovs bridge ${NET_MAP[$network]}${reset}"
101        exit 1
102       fi
103       echo "${blue}INFO: Creating Virsh Network: $network${reset}"
104       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
105 <network ipv6='yes'>
106 <name>$network</name>
107 <forward mode='bridge'/>
108 <bridge name='${NET_MAP[$network]}'/>
109 <virtualport type='openvswitch'/>
110 </network>
111 EOF
112       if ! (virsh net-list --all | grep $network > /dev/null); then
113           echo "${red}ERROR: unable to create network: ${network}${reset}"
114           exit 1;
115       fi
116       rm -f ${libvirt_dir}/apex-virsh-net.xml &> /dev/null;
117       virsh net-list | grep -E "$network\s+active" > /dev/null || virsh net-start $network
118       virsh net-list | grep -E "$network\s+active\s+yes" > /dev/null || virsh net-autostart --network $network
119     done
120
121     echo -e "${blue}INFO: Bridges set: ${reset}"
122     ovs-vsctl list-br
123   fi
124
125   echo -e "${blue}INFO: virsh networks set: ${reset}"
126   virsh net-list
127
128   # ensure storage pool exists and is started
129   virsh pool-list --all | grep default > /dev/null || virsh pool-define-as --name default dir --target /var/lib/libvirt/images
130   virsh pool-list | grep -Eo "default\s+active" > /dev/null || (virsh pool-autostart default; virsh pool-start default)
131
132   # Virt flag check is Arch dependent on x86
133   if [ "$(uname -i)" == 'x86_64' ]; then
134       if ! egrep '^flags.*(vmx|svm)' /proc/cpuinfo > /dev/null; then
135         echo "${red}virtualization extensions not found, kvm kernel module insertion may fail.\n  \
136 Are you sure you have enabled vmx in your bios or hypervisor?${reset}"
137       fi
138
139       if ! lsmod | grep kvm > /dev/null; then modprobe kvm; fi
140       if ! lsmod | grep kvm_intel > /dev/null; then modprobe kvm_intel; fi
141
142       if ! lsmod | grep kvm > /dev/null; then
143         echo "${red}kvm kernel modules not loaded!${reset}"
144         return 1
145       fi
146
147       # try to enabled nested kvm
148       if [ "$virtual" == "TRUE" ]; then
149         nested_kvm=`cat /sys/module/kvm_intel/parameters/nested`
150         if [ "$nested_kvm" != "Y" ]; then
151           # try to enable nested kvm
152           echo 'options kvm-intel nested=1' > /etc/modprobe.d/kvm_intel.conf
153           if rmmod kvm_intel; then
154             modprobe kvm_intel
155           fi
156           nested_kvm=`cat /sys/module/kvm_intel/parameters/nested`
157         fi
158         if [ "$nested_kvm" != "Y" ]; then
159           echo "${red}Cannot enable nested kvm, falling back to qemu for deployment${reset}"
160           DEPLOY_OPTIONS+=" --libvirt-type qemu"
161         else
162           echo "${blue}Nested kvm enabled, deploying with kvm acceleration${reset}"
163         fi
164       fi
165   fi
166
167   ##sshkeygen for root
168   if [ ! -e ~/.ssh/id_rsa.pub ]; then
169     ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
170   fi
171
172   echo "${blue}All dependencies installed and running${reset}"
173 }