3 #Script that install prerequisites
4 #author: Szilard Cserey (szilard.cserey@ericsson.com)
6 #Installs qemu-kvm, libvirt and prepares networking for Fuel VM
13 private_interface='enp6s0'
14 public_interface='enp8s0'
16 fuel_gw_ip='10.20.0.1/16'
20 ###check whether qemu-kvm is installed, otherwise install it
22 echo "${blue}Checking whether qemu-kvm is installed, otherwise install it${reset}"
23 if ! rpm -qa | grep -iE 'qemu-kvm'; then
24 echo "${blue}qemu-kvm is not installed, installing...${reset}"
25 yum -y install qemu-kvm
27 echo "${green}OK!${reset}"
31 ###check whether libvirt is installed, otherwise install it
33 echo "${blue}Checking whether libvirt is installed, otherwise install it${reset}"
34 if ! rpm -qa | grep -iE 'libvirt'; then
35 echo "${blue}libvirt is not installed, installing...${reset}"
36 yum -y install libvirt
38 echo "${green}OK!${reset}"
42 ###check whether kvm kernel module is loaded, otherwise load it
43 load_kvm_kernel_mod() {
44 echo "${blue}Checking whether kvm kernel module is loaded, otherwise load it${reset}"
45 if ! lsmod | grep -iE 'kvm'; then
46 if [[ `lscpu | grep 'Vendor ID' | awk 'BEGIN { FS = ":" } ; {print $2}' | tr -d ' '` == 'GenuineIntel' ]]; then
47 echo "${blue}Intel processor identified, loading kernel module kvm-intel${reset}"
48 kernel_mod='kvm-intel'
49 modprobe ${kernel_mod}
51 if [[ `lscpu | grep 'Vendor ID' | awk 'BEGIN { FS = ":" } ; {print $2}' | tr -d ' '` == 'AuthenticAMD' ]]; then
52 echo "${blue}AMD processor identified, loading kernel module kvm-amd${reset}"
54 modprobe ${kernel_mod}
56 if ! lsmod | grep -iE 'kvm'; then
57 echo "${red}Failed to load kernel module ${kernel_mod}!${reset}"
61 echo "${green}OK!${reset}"
65 ###check whether libvirtd service is running otherwise start it
66 start_libvirtd_service() {
67 echo "${blue}Checking whether libvirtd service is running otherwise start it${reset}"
68 if ! sudo systemctl status libvirtd | grep -iE 'active \(running\)'; then
69 echo "${blue}starting libvirtd service${reset}"
70 systemctl start libvirtd
71 if ! sudo systemctl status libvirtd | grep -iE 'active \(running\)'; then
72 echo "${red}Failed to start libvirtd service!${reset}"
76 echo "${green}OK!${reset}"
80 #Check whether interface is UP
83 echo "${red}Cannot bring UP, No interface specified${reset}"
87 echo "${blue}Checking whether interface ${interface} is UP${reset}"
88 link_state=$(ip link show ${interface} | grep -oP 'state \K[^ ]+')
89 if [[ ${link_state} != 'UP' ]]; then
90 echo "${blue}${interface} state is ${link_state}. Bringing it UP!${reset}"
91 ip link set dev ${interface} up
93 link_state=$(ip link show ${interface} | grep -oP 'state \K[^ ]+')
94 if [[ ${link_state} == 'DOWN' ]]; then
95 echo "${red}Could not bring UP interface ${interface} link state is ${link_state}${reset}"
99 echo "${green}OK!${reset}"
104 #Check whether private interface exists
105 echo "${blue}Checking whether private interface ${private_interface} exists${reset}"
106 if ! ip link show ${private_interface}; then
107 echo "${red}Private interface ${private_interface} does not exists!${reset}"
110 echo "${green}OK!${reset}"
113 #Check whether private interface is UP
114 check_interface ${private_interface}
117 pxe_interface="${private_interface}.${pxe_vid}"
119 #Check whether VLAN 0 (PXE) interface exists
120 echo "${blue}Checking whether VLAN 0 (PXE) interface ${pxe_interface} exists${reset}"
121 if ! ip link show ${pxe_interface}; then
122 echo "${blue}Creating VLAN 0 (PXE) interface ${pxe_interface}${reset}"
123 ip link add link ${private_interface} name ${pxe_interface} type vlan id ${pxe_vid}
125 echo "${green}OK!${reset}"
128 #Check whether VLAN 0 (PXE) interface is UP
129 check_interface ${pxe_interface}
131 #Check whether PXE bridge exists
132 echo "${blue}Checking whether PXE bridge ${pxe_bridge} exists${reset}"
133 if brctl show ${pxe_bridge} 2>&1 | grep 'No such device'; then
134 echo "${blue}Creating PXE bridge ${pxe_bridge}${reset}"
135 brctl addbr ${pxe_bridge}
137 echo "${green}OK!${reset}"
140 #Add VLAN 0 (PXE) interface to PXE bridge
141 echo "${blue}Checking whether VLAN 0 (PXE) interface ${pxe_interface} is added to PXE bridge ${pxe_bridge} exists${reset}"
142 if ! brctl show ${pxe_bridge} 2>&1 | grep ${pxe_interface}; then
143 echo "${blue}Adding VLAN 0 (PXE) interface ${pxe_interface} to PXE bridge ${pxe_bridge}${reset}"
144 brctl addif ${pxe_bridge} ${pxe_interface}
145 if ! brctl show ${pxe_bridge} 2>&1 | grep ${pxe_interface}; then
146 echo "${red}Could not add VLAN 0 (PXE) interface ${pxe_interface} to PXE bridge ${pxe_bridge}${reset}"
150 echo "${green}OK!${reset}"
153 #Check whether PXE bridge is UP
154 check_interface ${pxe_bridge}
156 #Add Fuel Gateway IP Address to PXE bridge
157 echo "${blue}Checking whether Fuel Gateway IP Address ${fuel_gw_ip} is assigned to PXE bridge ${pxe_bridge}${reset}"
158 if ! ip addr show ${pxe_bridge} | grep ${fuel_gw_ip}; then
159 echo "${blue}Adding Fuel Gateway IP Address ${fuel_gw_ip} to PXE bridge ${pxe_bridge}${reset}"
160 sudo ip addr add ${fuel_gw_ip} dev ${pxe_bridge}
161 if ! ip addr show ${pxe_bridge} | grep ${fuel_gw_ip}; then
162 echo "${red}Could not add Fuel Gateway IP Address ${fuel_gw_ip} to PXE bridge ${pxe_bridge}${reset}"
166 echo "${green}OK!${reset}"
169 ###check whether access to public network is granted
170 check_access_enabled_to_public_network() {
171 #Check whether public interface exists
172 echo "${blue}Checking whether public interface ${public_interface} exists${reset}"
173 if ! ip link show ${public_interface}; then
174 echo "${red}Public interface ${public_interface} does not exists!${reset}"
177 echo "${green}OK!${reset}"
180 #Check whether public interface ${public_interface} is UP
181 check_interface ${public_interface}
183 echo "${blue}Checking whether access is granted to public network through interface ${public_interface}${reset}"
184 if ! sudo iptables -t nat -L POSTROUTING -v | grep "MASQUERADE.*${public_interface}.*anywhere.*anywhere"; then
185 echo "${blue}Enable access to public network through interface ${public_interface}${reset}"
186 iptables -t nat -A POSTROUTING -o ${public_interface} -j MASQUERADE
188 echo "${green}OK!${reset}"
197 start_libvirtd_service
199 check_access_enabled_to_public_network