summaryrefslogblamecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/set_runvirt_hardware_variables.inc
blob: f94137467c987418f1eb48880f76fac4c0193a0f (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
           


                                           








                                                         
 

















                                                                                                                       
 









                                                                            
 











                                                                                 
 










                                                                                                                    
                                                                                                                     











                                                                                                       

                                                                  













                                                                     
 






                            
 
#!/bin/bash
###########################################
# 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 ;)

## 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"
	isset MACADDRPREFIX || writelog "No MAC address prefix was defined in ${VMCHOOSER_CONF_DIR}/virtualizer.conf"

	# 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