On my Arch Linux system I have created a script that should do the following on system startup:
- detect all network interfaces
- check if physical interfaces are up
- check if bridge interfaces are up
- determine if physical interfaces have IPv4 addresses and delete them
- check if physical interfaces have IPv6 addresses
- delete global IP addresses from physical interfaces
- delete link local IP addresses from physical interfaces
- check if wan bridge interface has an IPv4 address
- check if wan bridge interface has global IPv6 addresses
- delete global IPv6 addresses from wan bridge interface
- check if wan bridge interface has link local IPv6 addresses
- delete link local IPv6 addresses from wan bridge interface
- check if other bridge interfaces except the lan bridge interface have an IPv4 address
- delete IPv4 addresses from those interfaces
- check if other bridge interfaces except the lan bridge
- interface have IPv6 addresses
- delete IPv6 addresses from those interfaces
- check if spezific vm is running
- start specific vm if it is not running
- check if this specific vm is now running
- change network configuration for lan bridge interface to use dhcp
- restart systemd-networkd to reflect new network configuration for lan bridge interface
- verify that lan bridge interface got IPv4 and IPv6 addresses
- start other vms on purpose
- verify that other vms are running
- log everything to a log file
and another one to restore the startup network configuration on shutdown.
Due to the long list of tasks especially the first script became very complex and long (over 1500 lines). To make it worse I do not like programming and normally avoid doing this. So my knowledge is on a very basic level. Although the scripts seem to be working I must written most of them in a fit of deliriousness. When I now review them I do not understand most of the stuff I have written there.
So please review the script for any bugs and possible optimisations. They should be as foolproof and general (i.e. the names of the network interfaces should be assigned to variables) as possible.
### uncomment to send all output to vmrun.log file
### save stdout and stderr to file descriptors 3 and 4, then
### redirect them to /var/log/vmrun.log
###
exec 3>&1 4>&2 >/var/log/vmrun.log 2>&1
### declare global variables
# equivalent for DOS pause command
declare -r pausearg="Press any key to continue..."
# naming convention, bridge interfaces should start with br
declare -r bridgeinterfacestart="br"
# lan bridge interface
declare -r lanbr="brlan"
# string variable for IPv4 address of lan bridge interface
declare -r lanbr_ipv4_addr="192.168.3.4"
# wan bridge interface
declare -r wanbr="brwan"
# dmz bridge interface
declare -r dmzbr="brdmz"
# amount of seconds to sleep
declare -r sleeptime=3
# max counter value for loops
declare -r maxcounter=10
# string varibale for vm state shut off when locale is not set or C
declare -r vmstate_shutoff="shut off"
# string varibale for vm state running when locale is not set or C
declare -r vmstate_running="running"
# To handle the state of the vms correctly you need to set the
# variables vmstate_localized_shutoff and vmstate_localized_running in
# the "Global variables" section to the values the command
# "virsh list --all" returns for machines in shut down and running state
# for your locale
# In this script they are set for german language
# string variable for vm state shut off when locale set to german language
declare -r vmstate_shutoff_localized="ausschalten"
# string variable for vm state running when locale set to german language
declare -r vmstate_running_localized="laufend"
# name of first vm
declare -r vm1_name="lede"
# IPv4 address of vm1 for ping
declare -r vm1_ipv4_addr="192.168.3.1"
# boolean variable vor vm status
declare vmstate=0
# string variable for name of startup network config file for openvswitch
# lan bridge interface
declare -r brlan_config="/etc/systemd/network/50-ovs-brlan.network"
# string variable for name of safety copy of network config file for
# openvswitch lan bridge interface
declare -r brlan_config_startup="/etc/systemd/network/50-ovs-brlan.startup"
# string variable for name of network config file for openvswitch lan
# bridge interface to use after virtual machine vm1_name started up
declare -r brlan_config_aftervm="/etc/systemd/network/50-ovs-brlan.aftervm"
# string variable for md5 checksumfile for network config files for
# openvswitch lan bridge interface
declare -r brlan_configs_md5="/opt/vmchecks/50-ovs-brlan.md5"
# string variable for sha checksumfile for network config files for
# openvswitch lan bridge interface
declare -r brlan_configs_sha="/opt/vmchecks/50-ovs-brlan.sha"
# array with variable names of network config files
declare -r -a arr_brlan_config_files=(brlan_config brlan_config_startup brlan_config_aftervm)
# array with variable names of checksum files
declare -r -a arr_checksum_brlan_config_files=(brlan_configs_md5 brlan_configs_sha)
# array with the lines from the checksum files
declare -a arr_checksum_lines
# string variable for config file name
declare conf_file=""
# string variable for checksum file name
declare checksum_file=""
# string variable for checksum filename from array arr_checksum_lines
declare checksum_filename_from_arr
# string variable for saving current IFS
declare IFS_SAVE=""
# array with needed external programs
declare -a arr_ext_programs_needed=(md5sum sha512sum ip awk sed lshw grep ethtool systemctl cp diff virsh sleep date cat sysctl cut)
# array for list of all interfaces
declare -a arr_iface_all=()
# array for list of physical interfaces
declare -a arr_iface_phy=()
# array for list of virtual interfaces
declare -a arr_iface_virt=()
# array for list of virtual bridge interfaces
declare -a arr_iface_virt_bridge=()
# array for list of virtual openvswitch bridge interfaces
declare -a arr_iface_virt_ovsbridge=()
# array for list of virtual nonbridge interfaces
declare -a arr_iface_virt_nonbridge=()
# array for interfaces with ip address
declare -a arr_iface_withip=()
### declare functions
# function to emulate DOS pause command
declare -f func_pause
# function to check if all needed external programs are installed
declare -f func_needed_ext_progs
# function to get an array with all known interfaces
declare -f func_arr_iface_all
# function to get an array with all physical interfaces
declare -f func_arr_iface_phy
# function to get an array with all virtual interfaces
declare -f func_arr_iface_virt
# function to build array with virtual bridge interfaces
declare -f func_arr_iface_virt_bridge
# function to build array with virtual openvswitch bridge interfaces
declare -f func_arr_iface_virt_ovsbridge
# function to build array with virtual nonbridge interfaces
declare -f func_arr_iface_virt_nonbridge
# function to test if all physical interfaces are connected
declare -f func_iface_phy_carrier
# function to check if an interface is up
declare -f func_iface_up
# function to check if an interface has an IPv4 address
declare -f func_iface_ipv4
# function to check if an interface has an IPv6 address
declare -f func_iface_ipv6
# function to delete IPv4 addresses from an interface
declare -f func_iface_del_ipv4
# function to delete IPv6 addresses not starting with fe80: from an
# interface
declare -f func_iface_del_ipv6
# function to delete IPv6 addresses starting with fe80: from an interface
declare -f func_iface_del_ipv6_fe80
# function to test if IPv4 addresses were deleted from an interface
declare -f func_iface_del_test_ipv4
# function to test if all IPv6 addresses not starting with fe80: were
# deleted from an interface
declare -f func_iface_del_test_ipv6
# function to disable IPv6 on physical interfaces
declare -f func_iface_ipv6_disable
# function to test if IPv6 is disabled on an inteface
declare -f func_iface_ipv6_disabled_test
# function to get state of a virtual machine
declare -f func_vm_state
# function to start a virtual machine
declare -f func_vm_start
# function to verify that a virtual machine is running
declare -f func_vm_start_verify
# function to wait for a virtual machine to fully start up
declare -f func_vm_start_completed
# function to check if file exists
declare -f func_file_exists
### name: func_pause
### function emulate the DOS pause command
### @param
### no arguments required
### @return
### returns nothing
function func_pause
{
read -n1 -r -p "$*"
}
### name: func_needed_ext_progs
### function to check if all needed external programs are installed
### @param
### no arguments required
### @return
### returns nothing
function func_needed_ext_progs
{
for ((i=0; i<${#arr_ext_programs_needed[@]}; i++))
do
if ! command -v "${arr_ext_programs_needed[i]}" &>/dev/null
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t I require ${arr_ext_programs_needed[i]} but could not find it. Aborting."
exit 1
fi
done
}
### name: func_arr_iface_all
### function to get an array with all known interfaces excluding
### loopback (lo) and ovs-system interface
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_all
{
#define local variables
# temporary array for interfaces list
local -a arr_iface_tmp=()
# counter variable
local -i i=0
# length of tremporary array with all known interfaces
local -i arr_iface_tmp_length=0
# get list of existing interfaces
arr_iface_tmp=( "$( ip -o link | awk '{ print $2 }' | sed 's/.$//' )" )
# get length of array
arr_iface_tmp_length=${#arr_iface_tmp[@]}
# delete loopback (lo) interface and ovs-system interface from array
# and build final array without them
for ((i=0; i<arr_iface_tmp_length; i++))
do
if [ "${arr_iface_tmp[i]}" == "lo" ] || [ "${arr_iface_tmp[i]}" == "ovs-system" ]
then
unset 'arr_iface_tmp[i]'
fi
done
IFS_SAVE=$IFS
IFS=$'\n'
read -d '' -r -a arr_iface_all <<< "${arr_iface_tmp[@]}"
IFS=$IFS_SAVE
}
### name: func_arr_iface_phy
### function to get an array with all physical interfaces
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_phy
{
local -a arr_tmp
# get list of physical interfaces identified by lshw -c network -businfo
# assuming only physical interfaces have a non empty businfo field
arr_tmp="$( lshw -c network -businfo | grep '^[^ B=]' | awk '{ print $2 }' )"
IFS_SAVE=$IFS
IFS=$'\n'
read -d '' -r -a arr_iface_phy <<< "${arr_tmp[@]}"
IFS=$IFS_SAVE
}
### name: func_arr_iface_virt
### function to get an array with all virtual interfaces by deleting
### all physical interfaces from array of all known interfaces
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_virt
{
local -a arr_iface_tmp=("${arr_iface_all[@]}")
local arr_iface_tmp_length=${#arr_iface_tmp[@]}
local arr_iface_phy_length=${#arr_iface_phy[@]}
local -i i=0
local -i j=0
for ((i=0; i<arr_iface_tmp_length; i++))
do
for ((j=0; j<arr_iface_phy_length; j++))
do
if [ "${arr_iface_tmp[i]}" == "${arr_iface_phy[j]}" ]
then
unset 'arr_iface_tmp[i]'
fi
done
done
arr_iface_virt=("${arr_iface_tmp[@]}")
}
### name: func_arr_iface_virt_bridge
### function to get an array with all virtual bridge interfaces by
### deleting all virtual non bridge interfaces from array of all
### virtual interfaces
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_virt_bridge
{
local -a arr_iface_virt_bridge_tmp=("${arr_iface_virt[@]}")
local arr_iface_virt_bridge_tmp_length=${#arr_iface_virt_bridge_tmp[@]}
local -i i=0
# extract bridge interfaces from virtual interfaces
for ((i=0; i<arr_iface_virt_bridge_tmp_length; i++))
do
if [[ "${arr_iface_virt_bridge_tmp[i]}" != $bridgeinterfacestart* ]]
then
unset 'arr_iface_virt_bridge_tmp[i]'
fi
done
arr_iface_virt_bridge=("${arr_iface_virt_bridge_tmp[@]}")
}
### name: func_arr_iface_virt_nonbridge
### function to get an array with all virtual nonbridge interfaces
### by deleting all virtual bridge interfaces from array of all
### virtual interfaces
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_virt_nonbridge
{
local -a arr_iface_virt_nonbridge_tmp=("${arr_iface_virt[@]}")
local arr_iface_virt_nonbridge_tmp_length=${#arr_iface_virt_nonbridge_tmp[@]}
local -i i=0
# extract nonbridge interfaces from virtual interfaces
for ((i=0; i<arr_iface_virt_nonbridge_tmp_length; i++))
do
if [[ "${arr_iface_virt_nonbridge_tmp[i]}" == $bridgeinterfacestart* ]]
then
unset 'arr_iface_virt_nonbridge_tmp[i]'
fi
done
arr_iface_virt_nonbridge=("${arr_iface_virt_nonbridge_tmp[@]}")
}
### name: func_arr_iface_virt_ovsbridge
### function to get an array with all virtual openvswitch bridge
### interfaces by deleting all non openvswitch virtual bridge
### interfaces from array of all virtual bridge interfaces
### @param
### no arguments required
### @return
### returns nothing
function func_arr_iface_virt_ovsbridge
{
local -a arr_iface_virt_ovsbridge_tmp=("${arr_iface_virt_bridge[@]}")
local arr_iface_virt_ovsbridge_tmp_length=${#arr_iface_virt_ovsbridge_tmp[@]}
local -i i=0
# build list of virtual openvswitch bridge interfaces
for ((i=0; i<arr_iface_virt_ovsbridge_tmp_length; i++))
do
if ethtool -i "${arr_iface_virt_ovsbridge_tmp[i]}" | grep -q openvswitch
then
unset 'arr_iface_virt_ovsbridge_tmp[i]'
fi
done
arr_iface_virt_ovsbridge=("${arr_iface_virt_ovsbridge_tmp[@]}")
}
### name: func_iface_phy_carrier
### function to test if all physical interfaces are connected
### @param
### no arguments required
### @return
### returns nothing
function func_iface_phy_carrier
{
local loc_iface
local -i loc_maxcount
local -i i=0
loc_maxcount=$((maxcounter-5))
for loc_iface in "${arr_iface_phy[@]}"
do
if ip link show "$loc_iface" | grep -q '\bNO-CARRIER\b'
then
for ((i=0; i<=loc_maxcount; i++))
do
sleep "$sleeptime"s
if ip link show "$loc_iface" | grep -q '\bNO-CARRIER\b'
then
break
fi
done
if [[ $i -gt $loc_maxcount ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t I require physical interface ${loc_iface} connected but it's not. Aborting."
exit 1
fi
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Physical interface ${loc_iface} is connected. OK."
fi
done
}
### name: func_iface_up
### function to test if an interface is up
### @param
### interface name required
### @return
### returns nothing
function func_iface_up
{
if ! ip link show "$1" | grep -q '\bUP\b'
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t I require interface ${1} is UP but it's not. Aborting."
exit 1
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Interface ${1} is UP. OK."
fi
}
### name: func_iface_ipv4
### function to test if an interface has an IPv4 address
### @param
### interface name required
### @return
### returns nothing
function func_iface_ipv4
{
if [[ "$(ip -4 addr show "$1")" ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Interface $1 has an IPv4 address."
arr_iface_withip+=( "$1" )
fi
}
### name: func_iface_del_ipv4
### function to delete IPv4 address from an interface
### @param
### interface name required
### @return
### returns nothing
function func_iface_del_ipv4
{
local -a loc_arr_addr=()
local addr
loc_arr_addr=("$(ip -4 addr show "$1" | grep '\binet\b' | awk '{ print $2 }')")
for addr in "${loc_arr_addr[@]}"
do
ip addr del "$addr" dev "$1"
done
}
### name: func_iface_del__test_ipv4
### function to test if IPv4 addresses were deleted from interface
### @param
### interface name required
### @return
### returns nothing
function func_iface_del_test_ipv4
{
if [[ -z $(ip -4 addr show "$1" | grep '\binet\b' | awk '{ print $2 }') ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv4 Addresses successfully deleted from $1."
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv4 Addresses not deleted from $1. Aborting."
exit 1
fi
}
### name: func_iface_ipv6
### function to test if an interface has an IPv6 address
### @param
### interface name required
### @return
### returns nothing
function func_iface_ipv6
{
if [ "$(ip -6 addr show "$1")" ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Interface $1 has an IPv6 address."
arr_iface_withip+=( "$1" )
fi
}
### name: func_iface_del_ipv6
### function to delete IPv6 addresses except the ones starting with
### fe80: from an interface
### @param
### interface name required
### @return
### returns nothing
function func_iface_del_ipv6
{
local -a loc_arr_addr=()
local addr
loc_arr_addr=("$(ip -6 addr show "$1" | grep '\binet6\b' | awk '{ print $2 }')")
for addr in "${loc_arr_addr[@]}"
do
if [[ $addr != fe80:* ]]
then
ip -6 addr del "$addr" dev "$1"
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv6 address on interface $1 starts with fe80:. I will not delete it."
fi
done
}
### name: func_iface_del_ipv6_fe80
### function to delete IPv6 addresses starting with fe80: from an
### interface
### @param
### interface name required
### @return
### returns nothing
function func_iface_del_ipv6_fe80
{
local -a loc_arr_addr=()
local addr
loc_arr_addr=("$(ip -6 addr show "$1" | grep '\binet6\b' | awk '{ print $2 }')")
for addr in "${loc_arr_addr[@]}"
do
if [[ $addr = fe80:* ]]
then
ip -6 addr del "$addr" dev "$1"
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv6 address on interface $1 does not start with fe80:. I will not delete it."
fi
done
}
### name: func_iface_del__test_ipv6 ###
### function to test if all IPv6 addresses not starting with fe80: ###
### were deleted from interface
### @param ###
### interface name required
### @return ###
### returns nothing
function func_iface_del_test_ipv6
{
local -a loc_arr_addr=()
local -a loc_arr_addr_tmp=()
local addr=""
local -i loc_arr_addr_tmp_length=0
local -i i=0
loc_arr_addr_tmp=("$(ip -6 addr show "$1" | grep '\binet6\b' | awk '{ print $2 }')")
loc_arr_addr_tmp_length=${#loc_arr_addr_tmp[@]}
for ((i=0; i<loc_arr_addr_tmp_length; i++))
do
if [[ "${loc_arr_addr_tmp[i]}" == fe80:* ]]
then
unset 'loc_arr_addr_tmp[i]'
fi
done
loc_arr_addr=("${loc_arr_addr_tmp[@]}")
if [ ${#loc_arr_addr[@]} != 0 ]
then
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t Not all IPv6 addresses not starting with fe80: were deleted from interface $1. Aborting."
exit 1
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t All IPv6 addresses not starting with fe80: were deleted from interface $1. OK."
fi
}
########################################################################
### ###
### name: func_iface_del__test_ipv6_fe80 ###
### function to test if all IPv6 addresses starting with fe80: were ###
### deleted from interface ###
### ###
### @param ###
### interface name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_iface_del_test_ipv6_fe80
{
local -a loc_arr_addr=()
local -a loc_arr_addr_tmp=()
local addr=""
local -i loc_arr_addr_tmp_length=0
local -i i=0
loc_arr_addr_tmp=("$(ip -6 addr show "$1" | grep '\binet6\b' | awk '{ print $2 }')")
loc_arr_addr_tmp_length=${#loc_arr_addr_tmp[@]}
for ((i=0; i<loc_arr_addr_tmp_length; i++))
do
if [[ "${loc_arr_addr_tmp[i]}" != fe80:* ]]
then
unset 'loc_arr_addr_tmp[i]'
fi
done
loc_arr_addr=("${loc_arr_addr_tmp[@]}")
if [ ${#loc_arr_addr[@]} != 0 ]
then
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t Not all IPv6 addresses starting with fe80: were deleted from interface $1. Aborting."
exit 1
else
echo -e "$(date '+%Y-%m-%d %H:%M:%S')\\t All IPv6 addresses starting with fe80: were deleted from interface $1. OK."
fi
}
########################################################################
### ###
### name: func_iface_ipv6_disable ###
### function to disable IPv6 on an interface ###
### ###
### @param ###
### interface name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_iface_ipv6_disable
{
sysctl -w net.ipv6.conf."$1".disable_ipv6=1
}
########################################################################
### ###
### name: func_iface_ipv6_disabled_test ###
### function to test if IPv6 is disabled on an interface ###
### ###
### @param ###
### interface name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_iface_ipv6_disabled_test
{
if [ "$( cat /proc/sys/net/ipv6/conf/"$1"/disable_ipv6 )" -eq 1 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv6 is disabled on $1."
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv6 is not disabled on $1. Aborting"
exit 1
fi
}
########################################################################
### ###
### name: func_vm_state ###
### function to get status of a virtual machine ###
### ###
### @param ###
### vm name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_vm_state
{
local loc_vmstatus
local -i i=1
unset vmstate
for ((i=1; i<=maxcounter; i++))
do
loc_vmstatus=$( virsh list --all | grep "$1" | awk '{print $3}' )
case "$loc_vmstatus" in
"$vmstate_shutoff"|"$vmstate_shutoff_localized")
vmstate=0
break
;;
"$vmstate_running"|"$vmstate_running_localized")
vmstate=1
break
;;
*)
vmstate=2
;;
esac
sleep "$sleeptime"s
done
if [ $vmstate -eq 2 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Could not get status of vm $1 after "$((maxcounter * sleeptime))"s waiting."
fi
}
########################################################################
### ###
### name: func_vm_start ###
### function to get status of a virtual machine ###
### ###
### @param ###
### vm name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_vm_start
{
local loc_vmstatus
local -i i=0
virsh start "$1" &>/dev/null
for ((i=0; i<=maxcounter; i++))
do
sleep "$sleeptime"s
loc_vmstatus=$( virsh list --all | grep "$1" | awk '{print $3}' )
if [ "$loc_vmstatus" == "$vmstate_running" ] || [ "$loc_vmstatus" == "$vmstate_running_localized" ]
then
break
fi
done
}
########################################################################
### ###
### name: func_vm_start_verify ###
### function to get status of a virtual machine ###
### ###
### @param ###
### vm name required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_vm_start_verify
{
if [ $vmstate -eq 1 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine $1 is running."
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine $1 is not properly running"
fi
}
########################################################################
### ###
### name: func_vm_start_completed ###
### function to wait for a virtual machine to fully start up ###
### assuming it is when it can be pinged ###
### ###
### @param ###
### vm name required ###
### vm IPv4 address required ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_vm_start_completed
{
local loc_ping_success=0
local -i maxcount=maxcounter+5
for ((i=0; i<=maxcount; i++))
do
if ping -4 -w1 -c1 "$2" &>/dev/null
then
loc_ping_success=1
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start up of virtual machine $1 completed."
break
else
loc_ping_success=0
sleep "$sleeptime"s
fi
done
if [ $loc_ping_success -eq 0 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start up of virtual machine $1 did not complete after "$((maxcount * sleeptime))"s. Aborting."
exit 1
fi
}
########################################################################
### ###
### name: func_file_exists ###
### function to check if file exists ###
### ###
### @param ###
### filename with absolute path ###
### ###
### @return ###
### returns nothing ###
### ###
########################################################################
function func_file_exists
{
if ! [ -f "$1" ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $1 file does not exist. Aborting."
exit 1
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $1 file exists."
fi
}
########################################################################
### main program ###
### requires no arguments ###
########################################################################
# check if all needed external programs are installed
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if all needed external programms are installed."
func_needed_ext_progs
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if all needed external programms are installed finished."
# get list of existing interfaces in array variable arr_iface_all
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Get list of all known networkinterfaces."
func_arr_iface_all
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Get list of all known networkinterfaces finished."
# get list of physical interfaces in array variable arr_iface_phy
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Get list of all physical networkinterfaces."
func_arr_iface_phy
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Get list of all physical networkinterfaces finished."
# check if all physical interfaces have a carrier, required
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if physical interfaces have a carrier."
func_iface_phy_carrier
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if physical interfaces have a carrier finished."
# build list of virtual interfaces
func_arr_iface_virt
# build list of virtual bridge interfaces
func_arr_iface_virt_bridge
# build list of virtual non bridge iterfaces
func_arr_iface_virt_nonbridge
# build list of virtual openvswitch bridge interfaces
func_arr_iface_virt_ovsbridge
# check if all physical interfaces are up, required
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if physical interfaces are up."
for iface in "${arr_iface_phy[@]}"
do
func_iface_up "$iface"
done
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if physical interfaces are up finished."
unset iface
# check if all openvswitch bridge interfaces are up, required
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if virtual openvswitch bridge interfaces are up."
for iface in "${arr_iface_virt_ovsbridge[@]}"
do
func_iface_up "$iface"
done
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if virtual openvswitch bridge interfaces are up finished."
unset iface
# in this System physical interfaces should not have an IPv4 address
# check if physical interfaces have an IPv4 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if physical interfaces have an IPv4 address."
arr_iface_withip=()
for iface in "${arr_iface_phy[@]}"
do
func_iface_ipv4 "$iface"
done
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if physical interfaces have an IPv4 address finished."
# deleting IPv4 addresses from physical interfaces
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Some physical interfaces have an IPv4 address, deleting it"
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv4 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv4 addresses from physical interfaces finished"
# test if IPv4 adresses are deleted
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv4 addresses, from physical interfaces are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv4 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv4 addresses, from physical interfaces are deleted finished."
# in this System the openvswitch wan bridge interface should not have an IPv4 address
# check if openvswitch wan bridge interface has an IPv4 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if openvswitch wan bridge has an IPv4 address."
arr_iface_withip=()
func_iface_ipv4 "$wanbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch wan bridge has an IPv4 address finished."
# deleting IPv4 addresses from openvswitch wan bridge interface
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch wan bridge interface has an IPv4 address, deleting it"
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv4 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv4 addresses from openvswitch wan bridge interface finished."
# test if IPv4 adresses are deleted
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if IPv4 addresses from openvswitch wan bridge are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv4 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv4 addresses from openvswitch wan bridge are deleted finished."
# in this System the openvswitch dmz bridge interface should not have an IPv4 address
# check if openvswitch dmz bridge interface has an IPv4 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if openvswitch dmz bridge has an IPv4 address."
arr_iface_withip=()
func_iface_ipv4 "$dmzbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch dmz bridge has an IPv4 address finished."
# deleting IPv4 addresses from openvswitch dmz bridge interface
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch dmz bridge interface has an IPv4 address, deleting it"
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv4 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv4 addresses from openvswitch dmz bridge interface finished."
# test if IPv4 adresses are deleted
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if IPv4 addresses from openvswitch dmz bridge are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv4 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv4 addresses from openvswitch dmz bridge are deleted finished."
arr_iface_withip=()
# in this System physical interfaces should not have an IPv6 address
# check if physical interfaces have an IPv6 address except for the ones
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if physical interfaces have an IPv6 address."
arr_iface_withip=()
for iface in "${arr_iface_phy[@]}"
do
func_iface_ipv6 "$iface"
done
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if physical interfaces have an IPv6 address finished."
# delete IPv6 addresses from physical interfaces except for the ones
# starting with "fe80:"
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Some physical interfaces have IPv6 addresses, deleting them if they start not with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses not starting with fe80: from physical interfaces finished."
# test if IPv6 adresses except the ones starting with fe80: are deleted
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from physical interfaces are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from physical interfaces not starting with fe80: are deleted finished."
# delete IPv6 addresses from physical interfaces starting with "fe80:"
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Some physical interfaces have IPv6 addresses, deleting them if they start with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6_fe80 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses starting with fe80: from physical interfaces finished."
# test if IPv6 adresses starting with fe80: are deleted
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from physical interfaces are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6_fe80 "$iface"
done
fi
unset iface
arr_iface_withip=()
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from physical interfaces starting with fe80: are deleted finished."
# disabling IPv6 on physical interfaces to make sure that no communication via IPv6 is possible
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start disabling IPv6 on physical interfaces"
#for iface in "${arr_iface_phy[@]}"
#do
# func_iface_ipv6_disable "$iface"
#done
#unset iface
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Disabling IPv6 on physical interfaces finished."
# test if IPv6 on physical interfaces is disabled
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on physical interfaces is disabled"
#for iface in "${arr_iface_phy[@]}"
#do
# func_iface_ipv6_disabled_test "$iface"
#done
#unset iface
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on physical interfaces is disabled finished."
# in this System the openvswitch wan bridge interface should not have an
# IPv6 address
# check if the openvswitch wan bridge interface have an IPv6 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if the openvswitch wan bridge interface has an IPv6 address."
arr_iface_withip=()
func_iface_ipv6 "$wanbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch wan bridge have IPv6 addresses finished."
# delete IPv6 addresses from the openvswitch wan bridge interface except
# for the ones starting with "fe80:"
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t The openvswitch wan bridge interface has IPv6 addresses, deleting them if they start not with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses from openvswitch wan bridge interface finished."
# test if IPv6 adresses except the ones starting with fe80: are deleted
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from openvswitch wan bridge interface are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses not starting with fe80: from openvswitch wan bridge interface are deleted finished."
# delete IPv6 addresses from the openvswitch wan bridge interface starting with "fe80:"
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t The openvswitch wan bridge interface has IPv6 addresses, deleting them if they start with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6_fe80 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses starting with fe80: from openvswitch wan bridge interface finished."
# test if IPv6 adresses except the ones starting with fe80: are deleted
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses starting with fe80: from openvswitch wan bridge interface are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6_fe80 "$iface"
done
fi
unset iface
arr_iface_withip=()
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses starting with fe80: from openvswitch wan bridge interface are deleted finished."
# disabling IPv6 on openvswitch wan interfaces to make sure that no communication via IPv6 is possible
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start disabling IPv6 on openvswitch wan interface"
#func_iface_ipv6_disable "$wanbr"
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Disabling IPv6 on openvswitch wan interface finished."
# test if IPv6 on openvswitch wan interface is disabled
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on openvswitch wan interface is disabled"
#func_iface_ipv6_disabled_test "$wanbr"
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on openvswitch wan interface is disabled finished."
# in this System the openvswitch dmz bridge interface should not have an
# IPv6 address
# check if the openvswitch dmz bridge interface have an IPv6 address
# except for the ones starting with "fe80:"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if the openvswitch dmz bridge interface has an IPv6 address."
arr_iface_withip=()
func_iface_ipv6 "$dmzbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch dmz bridge has IPv6 addresses finished."
# delete IPv6 addresses from the openvswitch dmz bridge interface except
# for the ones starting with "fe80:"
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses not starting with fe80: from openvswitch dmz bridge interface."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t The openvswitch dmz bridge interface has IPv6 addresses, deleting them if they start not with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses starting with fe80: from openvswitch dmz bridge interface finished."
# test if IPv6 adresses except the ones starting with fe80: are deleted
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses not starting with fe80: from openvswitch dmz bridge interface are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6 "$iface"
done
fi
unset iface
arr_iface_withip=()
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses not starting with fe80: from openvswitch dmz bridge interface are deleted finished."
# delete IPv6 addresses from the openvswitch dmz bridge interface starting
# with "fe80:"
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses starting with fe80: from openvswitch dmz bridge interface."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t The openvswitch dmz bridge interface has IPv6 addresses, deleting them if they start with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6_fe80 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses starting with fe80: from openvswitch dmz bridge interface finished."
# test if IPv6 adresses starting with fe80: are deleted from openvswitch dmz bridge interface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses starting with fe80: from openvswitch dmz bridge interface are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6_fe80 "$iface"
done
fi
unset iface
arr_iface_withip=()
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses starting with fe80: from openvswitch dmz bridge interface are deleted finished."
arr_iface_withip=()
# disabling IPv6 on openvswitch dmz interfaces to make sure that no communication via IPv6 is possible
# echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start disabling IPv6 on openvswitch dmz interface"
# func_iface_ipv6_disable "$dmzbr"
# echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Disabling IPv6 on openvswitch dmz interface finished."
# test if IPv6 on openvswitch dmz interface is disabled
#echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on openvswitch dmz interface is disabled"
# func_iface_ipv6_disabled_test "$dmzbr"
# echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 on openvswitch dmz interface is disabled finished."
# test if virtual machine vm1_name is running and start it if not
# get status of virtual machine vm1_name
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Get status of virtual machine $vm1_name."
func_vm_state "$vm1_name"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Getting status of virtual machine $vm1_name finished. State is $vmstate."
# starting virtual machine vm1_name if not running, do nothing if already running
# aborting script if not in running or shut down state
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Starting virtual machine $vm1_name if not running and in a state I can handle."
case $vmstate in
0)
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine $vm1_name is not running, starting it."
func_vm_start "$vm1_name"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine $vm1_name started."
;;
1)
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machin $vm1_name is already running."
;;
2)
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine vm1_name is in a status I cannot handle. Aborting."
exit 1
;;
*)
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Something in the function to get the state of the virtual machine $1 went wrong. Aborting."
exit 1
;;
esac
unset vmstate
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Starting of virtual machine $vm1_name finished."
#verifying that virtual machine vm1_name is running
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Verifying that virtual machine $vm1_name is running."
func_vm_state "$vm1_name"
func_vm_start_verify "$vm1_name"
if [ $vmstate -ne 1 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Virtual machine $vm1_name is not running. Aborting."
exit 1
fi
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Verifying that virtual machine $vm1_name is running finished."
# wait for virtual machine vm1_name to fully startup
if [ $vmstate -eq 1 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Wait for virtual machine $vm1_name to fully start up."
func_vm_start_completed "$vm1_name" "$vm1_ipv4_addr"
fi
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Wait for virtual machine $vm1_name to fully start up finished."
# now the the virtual machine vm1_name is running delete all IPv4 and
# IPv6 addresses from openvswitch lan bridge interface and get new ones
# via dhcp from virtual machine vm1_name
# check if openvswitch lan bridge interface has an IPv4 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if openvswitch lan bridge has an IPv4 address."
arr_iface_withip=()
func_iface_ipv4 "$lanbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch lan bridge has an IPv4 address finished."
# deleting IPv4 addresses from openvswitch lan bridge interface
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch lan bridge interface has an IPv4 address, deleting it"
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv4 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv4 addresses from openvswitch lan bridge interface finished."
# test if IPv4 adresses are deleted
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if IPv4 addresses from openvswitch lan bridge are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv4 "$iface"
done
fi
unset iface
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv4 addresses from openvswitch lan bridge are deleted finished."
arr_iface_withip=()
# check if the openvswitch lan bridge interface have an IPv6 address
# except for the ones starting with "fe80:"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if the openvswitch lan bridge interface has an IPv6 address."
arr_iface_withip=()
func_iface_ipv6 "$lanbr"
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch lan bridge has IPv6 addresses finished."
# delete IPv6 addresses from the openvswitch lan bridge interface except
# except for the ones starting with "fe80:"
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses from openvswitch lan bridge interface finished."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t The openvswitch lan bridge interface has IPv6 addresses, deleting them if they start not with fe80:."
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_ipv6 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Deleting IPv6 addresses from openvswitch lan bridge interface finished."
# test if IPv6 adresses except the ones starting with fe80: are deleted
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from openvswitch lan bridge interface are deleted."
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
for iface in "${arr_iface_withip[@]}"
do
func_iface_del_test_ipv6 "$iface"
done
fi
unset iface
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if IPv6 addresses from openvswitch lan bridge interface are deleted finished."
arr_iface_withip=()
# replacing current network config file with the one for use after vm vm1_name is running
# checking if the files exists
# check if needed files exist
# check if needed network config files exist
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Check if network config files exist."
conf_file=""
for conf_file in "${arr_brlan_config_files[@]}"
do
func_file_exists "${!conf_file}"
done
conf_file=""
# check if needed checksum files exist
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Check if checksum files for network config files exist."
checksum_file=""
for checksum_file in "${arr_checksum_brlan_config_files[@]}"
do
func_file_exists "${!checksum_file}"
done
checksum_file=""
#check integrity of files against the checksum files
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Check integrity of network config files against the checksum files."
checksum_file=""
conf_file=""
for checksum_file in "${arr_checksum_brlan_config_files[@]}"
do
IFS_SAVE=$IFS
IFS=$'\n'
read -d '' -r -a arr_checksum < "${!checksum_file}"
IFS=$IFS_SAVE
for arr_checksum_lines in "${arr_checksum[@]}"
do
checksum_filename_from_arr="$(echo "$arr_checksum_lines" | awk '{print $2}')"
if [ "${!checksum_file:(-4)}" == .md5 ]
then
if [ "$(md5sum "$checksum_filename_from_arr" | awk '{print $1}')" == "$(echo "$arr_checksum_lines" | awk '{print $1}')" ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $checksum_filename_from_arr matches md5 checksum stored in ${!checksum_file}."
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $checksum_filename_from_arr does not match md5 checksum stored in ${!checksum_file}. Aborting."
exit 1
fi
fi
if [ "${!checksum_file:(-4)}" == .sha ]
then
if [ "$(sha512sum "$checksum_filename_from_arr" | awk '{print $1}')" == "$(echo "$arr_checksum_lines" | awk '{print $1}')" ]
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $checksum_filename_from_arr matches sha 512 checksum stored in ${!checksum_file}."
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t $checksum_filename_from_arr does not match sha 512 checksum stored in ${!checksum_file}. Aborting."
exit 1
fi
fi
done
done
conf_file=""
checksum_file=""
# copy network config file for openvswitch lan bridge interface for run
# after virtual machine vm1_name startup but only if it differs from
# current network config file and startup network config file and
# current network config file equals startup network config file
if ! diff -q "$brlan_config" "$brlan_config_aftervm" >/dev/null
then
if ! diff -q "$brlan_config_aftervm" "$brlan_config_startup" >/dev/null
then
if diff -q "$brlan_config" "$brlan_config_startup"
then
cp -f "$brlan_config_aftervm" "$brlan_config"
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Network config file for use at system start differ from current network config file. You are in trouble. Aborting."
exit 1
fi
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Network config file for use after starting virtual machine does not differ from startup network config file. You are in trouble. Aborting."
exit 1
fi
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Network config file for use after starting virtual machine does not differ from current network config file. You are in trouble. Aborting."
exit 1
fi
# verifying that network config file was successful copied
if diff -q "$brlan_config" "$brlan_config_aftervm" >/dev/null
then
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Network config file was successful copied."
else
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Network config file was not successful copied. Aborting."
exit 1
fi
# Restarting systemd-networkd with new config file
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Restarting systemd-networkd."
systemctl restart systemd-networkd
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Restarting systemd-networkd finished."
# check if openvswitch lan bridge interface has an IPv4 address
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Start test if openvswitch lan bridge has an IPv4 address."
arr_iface_withip=()
if [ ${#arr_iface_withip[@]} -eq 0 ]
then
for ((i=1; i<=maxcounter; i++))
do
func_iface_ipv4 "$lanbr"
if [ ${#arr_iface_withip[@]} -eq 0 ]
then
sleep "$sleeptime"s
else
break
fi
done
fi
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch lan bridge interface did not get an IPv4 address after"$((maxcounter * sleeptime))"s waiting."
if [ ${#arr_iface_withip[@]} -eq 0 ]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch lan bridge interface does not have an IPv4 address assinging one for emergency"
ip addr add $lanbr_ipv4_addr/24 dev $lanbr
# check if openvswitch lan bridge interface now has an IPv4 address
if [[ $(ip -4 addr show $lanbr | grep inet | awk '{print $2}' | cut -f1 -d"/") == "$lanbr_ipv4_addr" ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch lan bridge interface now has an emergency IPv4 address. Aborting."
exit 1
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Openvswitch lan bridge interface did not accept the emergency IPv4 address. You are in serious trouble. Aborting."
exit 1
fi
fi
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Test if openvswitch lan bridge has an IPv4 address finished."
# check if openvswitch lan bridge interface equals $lanbr_ipv4_addr
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
if [[ $(ip -4 addr show $lanbr | grep inet | awk '{print $2}' | cut -f1 -d"/") == "$lanbr_ipv4_addr" ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Assigned IPv4 address for openvswitch lan bridge interface is correct"
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Assigned IPv4 address for openvswitch lan bridge interface is not correct. Aborting."
exit 1
fi
fi
# check if openvswitch lan bridge interface IPv4 address was assigned via DHCP
if [ ${#arr_iface_withip[@]} -ne 0 ]
then
if [[ $(ip -4 addr show $lanbr | grep inet | awk '{print $7}') == "dynamic" ]]
then
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv4 address of openvswitch lan bridge interface was assigend via DHCP"
else
echo -e >&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t IPv4 address of openvswitch lan bridge interface was not assigend via DHCP"
fi
fi
arr_iface_withip=()
# final message
echo -e <&2 "$(date '+%Y-%m-%d %H:%M:%S')\\t Script finished"
#func_pause "$pausearg"
# restoring original output and closing created file descriptors
exec 1>&3 2>&4 3>&- 4>&-
I already checked the script with shellcheck.
The result:
In line 133:
declare -r pausearg="Press any key to continue..."
^-- SC2034: pausearg appears unused. Verify use (or export if used externally).
This is intentionally left for later use, for debugging:
In /opt/vmchecks/vmrun line 179:
declare -r brlan_configs_md5="/opt/vmchecks/50-ovs-brlan.md5"
^-- SC2034: brlan_configs_md5 appears unused. Verify use (or export if used externally).
In /opt/vmchecks/vmrun line 182:
declare -r brlan_configs_sha="/opt/vmchecks/50-ovs-brlan.sha"
^-- SC2034: brlan_configs_sha appears unused. Verify use (or export if used externally).
This is a false positive; it is used:
In /opt/vmchecks/vmrun line 462:
arr_iface_virt_nonbridge=("${arr_iface_virt_nonbridge_tmp[@]}")
^-- SC2034: arr_iface_virt_nonbridge appears unused. Verify use (or export if used externally).
True. It's currently only initialized with values but not further used. It's kept for a possible later use.
Please review, find bugs and give hints to make them simpler and better.