summaryrefslogtreecommitdiffstats
path: root/core/modules/qemukvm/data/opt/openslx/vmchooser/plugins/qemukvm/includes/setup_network.inc
blob: 73fb2518c43d1e98572669e15199b2a1a5c3fbc4 (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
###################################
# qemu/kvm include: Network setup #
###################################
# This now makes use of the qemu's bridge helper
# which creates a tap device and adds it to the
# bridge corresponding to the network type
# TODO configurable network type
setup_network() {
	# list available models with:
	#		qemu-system-x86_64 -net nic,model=?
	# e.g. as of 2.0.0:
	#		ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio
	declare -rg NIC_MODEL="e1000"

	# add MAC address and network card model
	VIRTCMDOPTS+=( "-device" "${NIC_MODEL},mac=${VM_MAC_ADDR},netdev=guestnet0" )

	# TODO support different network kinds for lectures in bwlehrpool-suite, just NAT for now
	declare -g NETWORK_MODE="nat"

	# detect if qemu's bridge helper binary is available
	declare -g QEMU_BRIDGE_HELPER=
	for HELPER_PATH in /usr/lib/qemu-bridge-helper /usr/local/libexec/qemu-bridge-helper; do
		if [ -x "${HELPER_PATH}" ] && [ -u "${HELPER_PATH}" ]; then
			QEMU_BRIDGE_HELPER="${HELPER_PATH}"
			readonly QEMU_BRIDGE_HELPER
			break
		fi
	done
	if isempty QEMU_BRIDGE_HELPER; then
		writelog "Could not find qemu-bridge-helper on this machine. Setting network mode to user."
		# Even though falling back to creating tap devices ourselves, we should instead
		# garantee the existance of qemu's helper on minilinux build time.
		# qemu's user network mode allows tcp/udp connections in a nat-fashion and
		# it allows access to the web which seems suffisant for a fallback.
		NETWORK_MODE="user"
	fi

	case "${NETWORK_MODE}" in
	nat*)
		VIRTCMDOPTS+=( "-netdev" "bridge,br=nat1,id=guestnet0,helper=${QEMU_BRIDGE_HELPER}" )
		;;
	bridge*)
		VIRTCMDOPTS+=( "-netdev" "bridge,br=br0,id=guestnet0,helper=${QEMU_BRIDGE_HELPER}" )
		;;
	host*)
		VIRTCMDOPTS+=( "-netdev" "bridge,br=vsw2,id=guestnet0,helper=${QEMU_BRIDGE_HELPER}" )
		;;
	user*|*)
		VIRTCMDOPTS+=( "-netdev" "user,id=guestnet0" )
		;;
	esac
}

## MAIN ##
call_post_source setup_network