summaryrefslogtreecommitdiffstats
path: root/core/modules/vbox-src/data/opt/openslx/vmchooser/plugins/virtualbox/includes/finalize_machine_config.inc
blob: 178da0e7dfd202c232bb7d7c2b1aea0270621b9c (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
################################################################################
# Include: write final machine configuration file                              #
################################################################################
finalize_machine_config() {
	# Expected path to the final vbox file
	VBOX_MACHINE_CONFIG="${VBOX_MACHINES_DIR}/${VM_CLEANNAME}/${VM_CLEANNAME}.xml"

	# remove ':' from MAC addr for vbox and generate a VDE (virtual device ethernet)
	VM_MAC_ADDR="$(sed 's/://g' <<< ${VM_MAC_ADDR})"

	# translate network kinds (nat, bridged, host-only)
	# TODO: Server should prepare this in returned xml
	case "${network_kind}" in
		bridge*)
			network_kind='HostOnlyInterface name="vboxnet0"'
			;;
		host*)
			network_kind='HostOnlyInterface name="vboxnet2"'
			;;
		*)
			network_kind='HostOnlyInterface name="vboxnet1"'
	esac

	sed -i "s,%VM_DISK_REDOLOGDIR%,$VBOX_SNAPSHOT_DIR,g" $TMPCONFIG
	sed -i "s,%VM_DISK_PATH%,${VBOX_HDD_LINK},g" $TMPCONFIG
	sed -i "s/%OpenSLX_MUUID%/{${MACHINE_UUID}}/g" $TMPCONFIG
	sed -i "0,/%OpenSLX_HDDUUID_0/ s/%OpenSLX_HDDUUID_0%/{${HDD_UUID}}/" $TMPCONFIG
	sed -i "0,/%OpenSLX_HDDUUID_0/ s/%OpenSLX_HDDUUID_0%/{${SNAPSHOT_UUID}}/" $TMPCONFIG
	sed -i 's,%OpenSLX_CPU%,'"${CPU_CORES}"',g' $TMPCONFIG
	sed -i 's,%OpenSLX_MEMORY%,'"${VM_MEM}"',g' $TMPCONFIG
	
	# Add a HardDisk node for the snapshot
	add_node \
		"/VirtualBox/Machine/MediaRegistry/HardDisks/HardDisk" "HardDisk" \
		"uuid={${SNAPSHOT_UUID}}" \
		"location=$VBOX_SNAPSHOT_DIR/{${SNAPSHOT_UUID}}.vdi" \
		"format=VDI" \
		"type=Normal"

	# add storage controller and 2 floppies to it
	add_node \
		"/VirtualBox/Machine/StorageControllers" "StorageController" \
		"name=Floppy" \
		"type=I82078" \
		"PortCount=1" \
		"useHostIOCache=true"
	add_node \
		'/VirtualBox/Machine/StorageControllers/StorageController[@name="Floppy"]' "AttachedDevice" \
		"type=Floppy" \
		"hotpluggable=false" \
		"port=0" \
		"device=0"
	add_node \
		'/VirtualBox/Machine/StorageControllers/StorageController[@name="Floppy"]' "AttachedDevice" \
		"type=Floppy" \
		"hotpluggable=false" \
		"port=0" \
		"device=1"

	# add the slx floppy to the second drive
	add_node \
		'/VirtualBox/Machine/StorageControllers/StorageController/AttachedDevice[@device="1"]' "Image" \
		"uuid={${SLX_FLOPPY_UUID}}"


	# Add a node for the SharedFolder
	add_node "/VirtualBox/Machine/Hardware" "SharedFolders"
	if [ -n "${HOME_SHARE_NAME}" -a -n "${HOME_SHARE_PATH}" -a -d "${HOME_SHARE_PATH}" ]; then
		add_node \
			"/VirtualBox/Machine/Hardware/SharedFolders" "SharedFolder" \
			"name=${HOME_SHARE_NAME}" \
			"hostPath=${HOME_SHARE_PATH}" \
			"writable=true" \
			"autoMount=true"
	fi

	# set the MAC address
	set_attr "/VirtualBox/Machine/Hardware/Network/Adapter" "MACAddress" "${VM_MAC_ADDR}"

	# check if KVM is available and activate it if so
	if source /run/hwinfo && [ "${HW_KVM}" = "ENABLED" ]; then
		set_attr "/VirtualBox/Machine/Hardware/Paravirt" "provider" "KVM"
	fi

	# activate IOAPIC needed for multi core (?)
	if [ $CPU_CORES -gt 1 ]; then
		set_attr "/VirtualBox/Machine/Hardware/BIOS/IOAPIC" "enabled" "true"
	fi

	detect_cpu_flag() {
		grep -m1 '^flags\s*:' /proc/cpuinfo | grep -qw -e "$1"
	}

	# PAE support?
	local PAE_SUPPORT="false"
	detect_cpu_flag "pae" && PAE_SUPPORT="true"
	set_attr "/VirtualBox/Machine/Hardware/CPU/PAE" "enabled" "${PAE_SUPPORT}"

	# LongMode?
	local LM_SUPPORT="false"
	detect_cpu_flag "lm" && LM_SUPPORT="true"
	set_attr "/VirtualBox/Machine/Hardware/CPU/LongMode" "enabled" "${LM_SUPPORT}"

	# Page size extensions?
	local PSE_SUPPORT="false"
	detect_cpu_flag "pse" && PSE_SUPPORT="true"
	set_attr "/VirtualBox/Machine/Hardware/CPU/HardwareVirtExLargePages" "enabled" "${PSE_SUPPORT}"

	# EXPERIMENTAL: check for USB 3 support
	local XHCI_OK="$(lsusb -t | grep xhci)"
	if [ -n "$XHCI_OK" ]; then
		del_node "/VirtualBox/Machine/Hardware/USB"
		add_node "/VirtualBox/Machine/Hardware/USB/Controllers" "Controller" "name=xHCI" "type=XHCI"
	fi

	cp $TMPCONFIG /tmp/vbox-last-config
	cp $TMPCONFIG $VBOX_MACHINE_CONFIG
}

call_post_source finalize_machine_config