#!/bin/ash # ----------------------------------------------------------------------------- # # Copyright (c) 2007..2018 bwLehrpool-Projektteam # # This program/file is free software distributed under the GPL version 2. # See https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html # # If you have any feedback please consult https://bwlehrpool.de and # send your feedback to support@bwlehrpool.de. # # General information about bwLehrpool can be found at https://bwlehrpool.de # # ----------------------------------------------------------------------------- # systemd-vmchooser_env # - This is the preparation script for the configuration of the virtuali- # zation tools used later on. # - This creates '$VMCHOOSER_CONF_DIR/virtualization.conf' that is later # evaluated by the runvirt script. ################################################################################ ################################################################################ ### Define default dirs / get configs ################################################################################ # source config file with vars (e.g. ipaddr, macaddr, HOSTNAME) . /opt/openslx/config # from plugins/vmchooser/XX_vmchooser.sh VMCHOOSER_DIR="/opt/openslx/vmchooser" VMCHOOSER_CONF_DIR="${VMCHOOSER_DIR}/config" VMCHOOSER_VIRT_CONF="${VMCHOOSER_CONF_DIR}/virtualization.conf" echo "# Generated $(date)" > "$VMCHOOSER_VIRT_CONF" # setup more scratch space for virtual machines, if configured for DIR in /tmp/virt /var/log/samba /run/samba; do if ! mkdir -p "$DIR"; then echo "Could not create '$DIR'!" exit 1 fi done ################################################################################ echo "Determine host's hardware configuration" ################################################################################ # general machine architecture (32/64bit) echo "HOST_ARCH=$(uname -m)" >> "${VMCHOOSER_VIRT_CONF}" # generated mac address parts from the host's mac address if [ -n "$SLX_PXE_MAC" ]; then HOSTMACADDR="${SLX_PXE_MAC}" else ## Fallback: HOSTMACADDR="$(ip a | grep ether | grep -o -E -i '([0-9a-f]{2}:){5}[0-9a-f]{2}' | head -n 1)" fi echo "HOSTMACADDR='$HOSTMACADDR'" >> "${VMCHOOSER_VIRT_CONF}" # NG: we define 10 fixed prefixes (00:FF:00 -> 00:FF:09) to use for VMs # First, check if the first 3 bytes of the host's mac address is # contained in our list. If it is, use the next one in the list. # If not, or the host's prefix is the last one in the list, use the first one. # This lays the groundwork to avoid mac addresses collisions in nested operations. MACADDRPREFIX= for i in 0 1 2 3 4 5 6 7 8; do if [ "x${HOSTMACADDR#00:FF:0$i}" != "x${HOSTMACADDR}" ]; then # current prefix is indeed part of the host's mac address # use the next prefix if != 9 MACADDRPREFIX="00:FF:0$(( i + 1 ))" break fi done # if it is still unset, then either the host's prefix did not match any in the list # or it matched the 9th. Either way, use the first prefix for this level of VMs. [ -n "${MACADDRPREFIX}" ] || MACADDRPREFIX='00:FF:00' # grab the last 2 bytes of the host's mac and prepend our VMID # placeholder (later replaced used by run-virt). MACADDRSUFFIX="$( printf "%s" "$HOSTMACADDR" | awk -F ":" '{print "%VMID%:" $(NF-1) ":" $NF}' | tr 'a-z' 'A-Z' )" { # ### stdout redirect start echo "MACADDRPREFIX='$MACADDRPREFIX'" echo "MACADDRSUFFIX='$MACADDRSUFFIX'" # read in ip address echo "HOSTIP=${SLX_PXE_CLIENT_IP}" # hostname echo "HOSTNAME='$( hostname -s )'" # get further hw information and check for traces of available optical and floppy drives j=0 for CDROM in $(dmesg|grep -i "Attached scsi CD-ROM sr" | sed "s,.*Attached scsi CD-ROM ,,"); do echo "CDROM_${j}=/dev/${CDROM}" j="$(( j + 1 ))" done j=0 for FLOPPY in $(dmesg|grep -i "Floppy drive"|sed "s,.*(s): ,,;s, is .*,,"); do echo "FLOPPY_${j}=/dev/${FLOPPY}" j="$(( j + 1 ))" done # Serial ports echo "SERIAL_PORTS='$(dmesg | grep -Eo 'ttyS[0-9]+' | sed 's,^,/dev/,' | tr '\n' ' ')'" } >> "${VMCHOOSER_VIRT_CONF}" # ### redirect end # Parallel ports modprobe parport_pc ################################################################################ echo "Setup VM networking" ################################################################################ # there will be three types of network interfaces for virtual machines available # all will be configured for every type of virtualization tool # a) br0 is the direct host bridge into the local LAN (br0 already exists) # vmnet0 for VMware # vboxnet0 for VirtualBox # kvmnet0* for Qemu/KVM # b) nat1 is an internal bridge which "NATes" into the local LAN (with DHCP) # vmnet1 for VMware # vboxnet1 for VirtualBox # kvmnet1* for Qemu/KVM # c) vsw2 is an internal host bridge to connect software defined LANs to # vmnet2 for VMware # vboxnet2 for VirtualBox # kvmnet2* for Qemu/KVM # creating and configuring nat0 # 192.168.101.0/24 is vm nat. If you ever change this there are a couple of other files # where you'd need to make changes, so think twice before doing so. ;) brctl addbr nat1 ip link set dev nat1 up ip addr add 192.168.101.1/24 dev nat1 echo "1" >/proc/sys/net/ipv4/conf/nat1/forwarding echo "1" >/proc/sys/net/ipv4/conf/br0/forwarding 2>/dev/null # iptables masquerade rule is now inserted by /opt/openslx/iptables/rules.d/50-virt-nat1-masquerading # Whitelist all additionally bridged nics for forwarding extra= echo "#!/bin/ash" > "/tmp/nic-forward.tmp" for nic in /sys/class/net/br-nic-*; do [ -d "$nic" ] || continue extra=1 echo "iptables -A FORWARD -i ${nic##*/} -j ACCEPT" echo "iptables -A FORWARD -o ${nic##*/} -j ACCEPT" done >> "/tmp/nic-forward.tmp" if [ -n "$extra" ]; then chmod +x "/tmp/nic-forward.tmp" mv "/tmp/nic-forward.tmp" "/opt/openslx/iptables/rules.d/10-forward-additional-nics" else unlink "/tmp/nic-forward.tmp" fi # creating and configuring vsw2 brctl addbr vsw2 ip link set dev vsw2 up echo "1" >/proc/sys/net/ipv4/conf/vsw2/forwarding # Write proper DHCPd config for VM network /opt/openslx/scripts/runvirt-start_dhcpd # estimate free mem during clean boot # TODO: Does that even make sense? We determine available memory during bootup and then blindly use # this value to configure the VM's memory size later on. Shouldn't we query this right before starting the VM? MEM_TOTAL="$( awk '{if ($1 == "MemAvailable:") { print $2; exit; }}' /proc/meminfo )" [ -z "$MEM_TOTAL" ] && MEM_TOTAL=2048000 echo "HOST_MEM_TOTAL=$(( MEM_TOTAL / 1024 ))" >> "${VMCHOOSER_VIRT_CONF}" exit 0