#!/usr/bin/env bash #Script that install prerequisites #author: Szilard Cserey (szilard.cserey@ericsson.com) # #Installs qemu-kvm, libvirt and prepares networking for Fuel VM ##VARS reset=`tput sgr0` blue=`tput setaf 4` red=`tput setaf 1` green=`tput setaf 2` private_interface='enp6s0' public_interface='enp8s0' pxe_bridge='pxebr' fuel_gw_ip='10.20.0.1/16' management_vid=300 management_interface="${private_interface}.${management_vid}" ##END VARS ##FUNCTIONS ###check whether qemu-kvm is installed, otherwise install it install_qemu_kvm() { echo "${blue}Checking whether qemu-kvm is installed, otherwise install it${reset}" if ! rpm -qa | grep -iE 'qemu-kvm'; then echo "${blue}qemu-kvm is not installed, installing...${reset}" yum -y install qemu-kvm else echo "${green}OK!${reset}" fi } ###check whether libvirt is installed, otherwise install it install_libvirt() { echo "${blue}Checking whether libvirt is installed, otherwise install it${reset}" if ! rpm -qa | grep -iE 'libvirt'; then echo "${blue}libvirt is not installed, installing...${reset}" yum -y install libvirt else echo "${green}OK!${reset}" fi } ###check whether kvm kernel module is loaded, otherwise load it load_kvm_kernel_mod() { echo "${blue}Checking whether kvm kernel module is loaded, otherwise load it${reset}" if ! lsmod | grep -iE 'kvm'; then if [[ `lscpu | grep 'Vendor ID' | awk 'BEGIN { FS = ":" } ; {print $2}' | tr -d ' '` == 'GenuineIntel' ]]; then echo "${blue}Intel processor identified, loading kernel module kvm-intel${reset}" kernel_mod='kvm-intel' modprobe ${kernel_mod} fi if [[ `lscpu | grep 'Vendor ID' | awk 'BEGIN { FS = ":" } ; {print $2}' | tr -d ' '` == 'AuthenticAMD' ]]; then echo "${blue}AMD processor identified, loading kernel module kvm-amd${reset}" kernel_mod='kvm-amd' modprobe ${kernel_mod} fi if ! lsmod | grep -iE 'kvm'; then echo "${red}Failed to load kernel module ${kernel_mod}!${reset}" exit 1 fi else echo "${green}OK!${reset}" fi } ###check whether libvirtd service is running otherwise start it start_libvirtd_service() { echo "${blue}Checking whether libvirtd service is running otherwise start it${reset}" if ! sudo systemctl status libvirtd | grep -iE 'active \(running\)'; then echo "${blue}starting libvirtd service${reset}" systemctl start libvirtd if ! sudo systemctl status libvirtd | grep -iE 'active \(running\)'; then echo "${red}Failed to start libvirtd service!${reset}" exit 1 fi else echo "${green}OK!${reset}" fi } #Check whether interface exists check_interface_exists() { if [ -z $1 ]; then echo "${red}Cannot check whether interface exists! No interface specified!${reset}" exit 1 fi local interface=$1 #Check whether interface exists echo "${blue}Checking whether interface ${interface} exists${reset}" if ! ip link show ${interface}; then echo "${red}Interface ${interface} does not exists!${reset}" exit 1 else echo "${green}OK!${reset}" fi } #Check whether interface is UP check_interface_up() { if [ -z $1 ]; then echo "${red}Cannot check whether interface is UP! No interface specified!${reset}" exit 1 fi local interface=$1 #Check whether interface is UP echo "${blue}Checking whether interface ${interface} is UP${reset}" link_state=$(ip link show ${interface} | grep -oP 'state \K[^ ]+') if [[ ${link_state} != 'UP' ]]; then echo "${blue}${interface} state is ${link_state}. Bringing it UP!${reset}" ip link set dev ${interface} up sleep 5 link_state=$(ip link show ${interface} | grep -oP 'state \K[^ ]+') if [[ ${link_state} == 'DOWN' ]]; then echo "${red}Could not bring UP interface ${interface} link state is ${link_state}${reset}" exit 1 fi else echo "${green}OK!${reset}" fi } #Create VLAN interface create_vlan_interface() { if [ -z $1 ]; then echo "${red}Cannot create VLAN interface. No base interface specified!${reset}" exit 1 fi if [ -z $2 ]; then echo "${red}Cannot create VLAN interface. No VLAN ID specified!${reset}" exit 1 fi local base_interface=$1 local vid=$2 local interface="${base_interface}.${vid}" echo "${blue}Checking whether VLAN ${vid} interface ${interface} exists, otherwise create it${reset}" if ! ip link show ${interface}; then echo "${blue}Creating VLAN ${vid} interface ${interface}${reset}" ip link add link ${base_interface} name ${interface} type vlan id ${vid} else echo "${green}OK!${reset}" fi #Check whether VLAN interface is UP check_interface_up ${interface} } ###setup PXE Bridge setup_pxe_bridge() { pxe_vid=0