summaryrefslogtreecommitdiffstats
path: root/core/modules/vbox-src/data/opt/openslx/scripts/systemd-vbox_env
blob: a1711f0ad786e69744a964f42fe966a81beca680 (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
#!/bin/bash
# -----------------------------------------------------------------------------
#
# 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-vbox_env
#    - This is the preparation script for the configuration of VirtualBox.
################################################################################

## sanity checks
VBOX_BASE_DIR="/usr/lib/virtualbox"
VBOX_KMOD_DIR="/lib/modules/vbox"
VBOX_MANAGE="${VBOX_BASE_DIR}/VBoxManage"

. /opt/openslx/config
exit_code=0

# Runtime critical checks first
# VBoxManage should be under /usr/lib/virtualbox
if ! [ -d "${VBOX_BASE_DIR}" ] || ! [ -x "${VBOX_MANAGE}" ] || ! [ -d "${VBOX_KMOD_DIR}" ]; then
	echo "Failed to find VirtualBox installation at expected paths."
	exit 1
fi

# load vbox kernel modules
if ! cd "${VBOX_KMOD_DIR}"; then
	exit_code=1
else
	for MOD in *; do
		if ! lsmod | grep -q "^${MOD%%.*} " && ! insmod "${MOD}"; then
			slxlog "vbox-setup" "Loading of ${MOD} failed."
			exit_code=1
		fi
	done
fi

# check/create vboxusers group
echo "Handling group"
getent group vboxusers &> /dev/null || addgroup --system vboxusers
[[ " $( id -Gn demo ) " = *" vboxusers "* ]] || adduser demo vboxusers

# set their permissions
echo "Setting up /dev"
chmod 0600 /dev/vboxdrv /dev/vboxnetctl
chmod 0666 /dev/vboxdrvu
mkdir -p /dev/vboxusb
chmod 0750 /dev/vboxusb
chown root:vboxusers /dev/vboxusb

# create required standard directories
mkdir -p "/tmp/virt/virtualbox" -m 1777

if ! [ -e "/run/openslx/dmsetup.state" ]; then
	# reload udev rules since aufs'ing the layer on top do not trigger its inotify watch
	echo "Reloading udev rules"
	udevadm control --reload
fi

# pretty dumb, you can only create host-only interfaces,
# but not assign a specific name/number
echo "Setting up vbox network interfaces"

if ! [ -e "/sys/class/net/vboxnet0" ]; then
	${VBOX_MANAGE} hostonlyif create || exit_code=1
	ip link set dev vboxnet0 up
	[ "$SLX_JUMBO_FRAMES" = "yes" ] && ip link set dev vboxnet0 mtu 9000
	brctl addif br0 vboxnet0 || exit_code=1
fi

if ! [ -e "/sys/class/net/vboxnet1" ]; then
	${VBOX_MANAGE} hostonlyif create || exit_code=1
	ip link set dev vboxnet1 up
	brctl addif nat1 vboxnet1 || exit_code=1
fi

if ! [ -e "/sys/class/net/vboxnet2" ]; then
	${VBOX_MANAGE} hostonlyif create || exit_code=1
	ip link set dev vboxnet2 up
	brctl addif vsw2 vboxnet2 || exit_code=1
fi

if [ "$SLX_BRIDGE_OTHER_NICS" = "yes" ]; then
	# These will have been set up in our init, or by bridge-other-nics.service
	echo "Setting up additional bridged network interfaces"
	vboxnet=3
	for nic in /sys/class/net/br-nic-*; do
		nic="${nic##*-}"
		if ! [ "$nic" -ge 0 ]; then
			slxlog "vbox-other-nics" "NaN: br-nic-X has X='$nic'"
			continue
		fi
		# Already done?
		[ -e "/sys/class/net/vboxnet${vboxnet}" ] && continue
		# create vboxnet3 - vboxnetN for these
		${VBOX_MANAGE} hostonlyif create
		brctl addif "br-nic-${nic}" "vboxnet${vboxnet}"
		vboxnet="$(( vboxnet + 1 ))"
	done
fi

# trigger reload of iptables stuff (it's using inotify)
touch /opt/openslx/iptables/rules.d/empty

exit "$exit_code"