blob: 5fb1480603cdfef0be6413b3832725c64bfbc623 (
plain) (
tree)
|
|
###########################################
# Include: Set hardware related variables #
###########################################
# New way of handling MAC address prefixes to support
# nested and concurrent run-virt invocations.
# Using a list of predefined MAC prefixes to use for VMs,
# check if the host's MAC address prefix is in that list.
# If not, use the first prefix in the list. If it is
# use the next prefix in the list. If the host's prefix
# is the last in the list, use the first one again.
# This way we support up to 10 nested/concurrent VMs.
# Use: 00:FF:00 for firtual machines ;)
declare -gA MAC_PREFIXES
for i in {0..9}; do
# make an associate array, it will make our life easier later
MAC_PREFIXES["00:FF:0$i"]="$i"
done
## Functions ##
# Sets the VM's hostname to the original hostname prefixed with a fixed string and its ID
set_virt_hostname() {
declare -rg HOSTNAME="virt${VM_ID}-$(hostname)"
writelog "\tVM Hostname:\t\t$HOSTNAME"
}
set_virt_cpu() {
# Make sure CPU_CORES is not empty
declare -g CPU_CORES=${CPU_CORES:-"1"}
}
# Derives the amount of memory allocated to the VM from the
# host's total memory (previously determined by systemd-run_virt_env)
set_virt_memory() {
# Make sure we have a VM_ID and HOST_MEM_TOTAL (read from virtualization.conf)
if isempty HOST_MEM_TOTAL; then
writelog "HOST_MEM_TOTAL is empty! Was '$VMCHOOSER_CONF_DIR/virtualization.conf' sourced?"
EXIT_TYPE="internal" EXIT_REASON="Konnte die Größe des Arbeitsspeichers nicht ermitteln!" cleanexit 1
fi
# Amount of memory for the VM. Be generous if diff is written to HDD
local min=768
local reserve max
if mount | grep -q '^/dev/sd.*on.*/tmp'; then
reserve=20
max=1800
else
reserve=65
max=8192
fi
# Calculate absulute amount of RAM that should stay available to the host
local reserve="$(( ( HOST_MEM_TOTAL * reserve ) / 100 ))"
# Respect some upper and lower bounds for the host amount
[ "$reserve" -lt "$min" ] && reserve="$min"
[ "$reserve" -gt "$max" ] && reserve="$max"
# Get a result which can be divided by 4
declare -g VM_MEM="$(( ( ( HOST_MEM_TOTAL - reserve ) / 4 ) * 4 ))"
declare -g HOST_MEM_REMAINING="$(( HOST_MEM_TOTAL - VM_MEM ))"
# NOTE: removed old code that evaluated 'mainvirtmem'
# as it did nothing and we don't know what the idea was...
}
# New way to generate MAC addresses:
# MAC_PREFIXES is a statically declared list of prefixes which
# can be used for VMs. To support nested VMs, we just look for
# the MAC prefix of the host and if it is present in the list,
# we just take the next one in the list. Currently support up to 10.
# If none are found, we just take the first prefix.
# Suffix template is taken from /opt/openslx/vmchooser/config/virtualization.conf
# and will have the VMID inserted as first byte.
set_virt_mac() {
# First, determine prefix of the host's MAC address
isset HOSTMACADDR || writelog "Host's MAC address was not defined in ${VMCHOOSER_CONF_DIR}/virtualizer.conf"
local HOST_MAC_PREFIX=$(echo "$HOSTMACADDR" | tr '[a-z]' '[A-Z]' | grep -o -E '^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}')
unset MACADDRPREFIX
if notempty HOST_MAC_PREFIX && notempty MAC_PREFIXES[${HOST_MAC_PREFIX}]; then
# the host's prefix was in our list, so use the next one in the list
local -i HOST_MAC_PREFIX_INDEX="${MAC_PREFIXES[${HOST_MAC_PREFIX}]}" # this will be at least 0
# increment if its between 0 and 8
if [[ $HOST_MAC_PREFIX_INDEX =~ ^[0-8]$ ]]; then
declare -g MACADDRPREFIX="${HOST_MAC_PREFIX%?}$(( ++HOST_MAC_PREFIX_INDEX ))"
fi
fi
# host's mac prefix could not be determined (practically this should never happen)
# or the prefix is not in our mac address list or we reached the end of the list.
# Either way, use first mac prefix in the list
notempty MACADDRPREFIX || declare -g MACADDRPREFIX="$(echo ${!MAC_PREFIXES[@]} | awk '{print $1}')"
# Fill in VM_ID
local MACADDRSUFFIX=${MACADDRSUFFIX//%VMID%/"${VM_ID}"}
if ! echo "$MACADDRPREFIX" | grep -q -E '^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$'; then
slxlog "virt-mac" "Could not properly generate mac address prefix (got $MACADDRPREFIX)"
fi
if ! echo "$MACADDRSUFFIX" | grep -q -E '^[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}$'; then
slxlog "virt-mac" "Could not properly generate mac address suffix (got $MACADDRSUFFIX)"
fi
declare -g VM_MAC_ADDR="$MACADDRPREFIX:$MACADDRSUFFIX"
}
# Virtual fd/cd/dvd and drive devices, floppy b: for configuration
# NOTE: This uses bash's variable expansion magic to set
# CDROM/FLOPPY variables evaluated later by the virtualizers...
# If the variables in virtualization.conf is set, "TRUE" is assigned,
# "FALSE" otherwise.
check_optical_drives() {
local TESTVAR=${FLOPPY_0:+"TRUE"}
declare -rg FLOPPY0=${TESTVAR:-"FALSE"}
TESTVAR=${CDROM_0:+"TRUE"}
declare -rg CDROM0=${TESTVAR:-"FALSE"}
TESTVAR=${CDROM_1:+"TRUE"}
declare -rg CDROM1=${TESTVAR:-"FALSE"}
}
## MAIN ##
call_post_source \
set_virt_cpu \
set_virt_memory \
set_virt_mac \
set_virt_hostname \
check_optical_drives
|