summaryrefslogtreecommitdiffstats
path: root/core/modules/run-virt/data/opt/openslx/scripts/systemd-run_virt_env
blob: 04c1aff513aef9a2c4468c305d769627fa77a460 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/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