summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/data/opt/openslx/vmchooser/plugins/qemukvm/run-virt.include
blob: 893835e783ea6b906ad9214fdd009886b5258bb9 (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
# -----------------------------------------------------------------------------
#
# Copyright (c) 2009..2021 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
#
# -----------------------------------------------------------------------------
# run-virt.include
#    - qemu plugin for vmchooser run-virt
################################################################################

# BASH_SOURCE[0] contains the file being sourced, namely this one
declare -rg QEMU_PLUGIN_DIR="$(dirname "${BASH_SOURCE[0]}")"
declare -rg QEMU_INCLUDE_DIR="${QEMU_PLUGIN_DIR}/includes"

# Define which features the QEMU plugin supports
declare -rg PLUGIN_FEATURES="firewall printer usb slxfloppy sound netshares"

run_plugin() {
	# include kernel command line utils and functions
	$(safesource "${QEMU_INCLUDE_DIR}/kernel-cmdln.inc")
	# include PCI passthrough utils and functions
	$(safesource "${QEMU_INCLUDE_DIR}/passthrough-pci.inc")
	# include mediated device passthrough utils and functions
	$(safesource "${QEMU_INCLUDE_DIR}/passthrough-mdev.inc")
	# include function for creating the rw qcow2 image
	$(safesource "${QEMU_INCLUDE_DIR}/create-rw-diskimage.inc")

	local vm_diskfile
	# If admin mode checkbox is set in the vmchooser, create the rw disk for the snapshot
	if [ "${VMCHOOSER_ADMIN_MODE}" = TRUE ]; then
		# Create tmp dir that survives cleanup
		mkdir /tmp/upload

		# Activate cleanup function for uploading
		create_rw_diskimage

		# Create snapshot file in upload dir that doesn't get deleted on cleanup
		if [ -f /var/lib/lightdm/editsession.id ]; then
			EDITID=$(cat /var/lib/lightdm/editsession.id)
			vm_diskfile="/tmp/upload/$EDITID.qcow2"
		else
			writelog "No editid was found at /var/lib/lightdm/editsession.id, using IMGUUID as fallback"
			vm_diskfile="/tmp/upload/$IMGUUID.qcow2"
		fi
		local vm_diskfile_type="$(qemu-img info --output=json ${VM_DISKFILE_RO} | jq -r '.format')"
		qemu-img create -F "${vm_diskfile_type}" -b "${VM_DISKFILE_RO}" -f qcow2 "${vm_diskfile}"
	# setup RW image access for operation
	elif notempty VM_DISKFILE_RO; then
		# create copy-on-write layer for readonly image
		vm_diskfile="${TMPDIR}/$(basename ${VM_DISKFILE_RO}).cow.qcow2"
		local vm_diskfile_type="$(qemu-img info --output=json ${VM_DISKFILE_RO} | jq -r '.format')"
		qemu-img create -F "${vm_diskfile_type}" -b "${VM_DISKFILE_RO}" -f qcow2 "${vm_diskfile}"
	elif notempty VM_DISKFILE_RW; then
		# use supplied RW disk image from run-virt
		vm_diskfile="${VM_DISKFILE_RW}"
	fi

	# setup GPU passthrough and obtain PCI addresses and IDs if passthrough is enabled
	pt_gpu_pci_ids=($(passthrough_pci_setup))
	if [ "${?}" -ne 0 ]; then
		writelog "Failed to set up PCI devices for GPU passthrough!"
	fi

	# setup GPU mediated device passthrough and obtain mediated device address
	pt_gpu_mdev_id="$(passthrough_mdev_setup)"
	if [ "${?}" -ne 0 ]; then
		writelog "Failed to set up mediated device (Intel GVT-g) for GPU passthrough!"
	fi

	# write finalized config in debug mode to temporary folder for debugging purposes
	if [ "${DEBUG}" = "true" ]; then
		local vm_final_config="/tmp/qemu-last-config.xml"
	fi

	# call the Libvirt Java tool to finalize configuration and start VM
	declare -rg VIRTCMD="java"
	VIRTCMDOPTS=( "-jar" "${QEMU_PLUGIN_DIR}/runvirt-plugin-qemu.jar" )

	notempty DEBUG             && VIRTCMDOPTS+=( "-debug"       "${DEBUG}" )
	notempty VM_CLEANNAME      && VIRTCMDOPTS+=( "-vmname"      "${VM_CLEANNAME}" )
	notempty VM_DISPLAYNAME    && VIRTCMDOPTS+=( "-vmdsplname"  "${VM_DISPLAYNAME}" )
	notempty VM_OS_TYPE        && VIRTCMDOPTS+=( "-vmos"        "${VM_OS_TYPE}" )
	notempty TMPCONFIG         && VIRTCMDOPTS+=( "-vmcfginp"    "${TMPCONFIG}" )
	notempty vm_final_config   && VIRTCMDOPTS+=( "-vmcfgout"    "${vm_final_config}" )
	notempty IMGUUID           && VIRTCMDOPTS+=( "-vmuuid"      "${IMGUUID}" )
	notempty HW_CORES          && VIRTCMDOPTS+=( "-vmncpus"     "${HW_CORES}" )
	notempty VM_MEM            && VIRTCMDOPTS+=( "-vmmem"       "${VM_MEM}" )
	notempty VM_MAC_ADDR       && VIRTCMDOPTS+=( "-vmmac0"      "${VM_MAC_ADDR}" )
	notempty vm_diskfile       && VIRTCMDOPTS+=( "-vmhdd0"      "${vm_diskfile}" )
	                              VIRTCMDOPTS+=( "-vmfloppy0"   "${FLOPPY_0}" )
	notempty SLX_FLOPPY_IMG    && VIRTCMDOPTS+=( "-vmfloppy1"   "${SLX_FLOPPY_IMG}" )
	notempty CDROM_0           && VIRTCMDOPTS+=( "-vmcdrom0"    "${CDROM_0}" )
	notempty CDROM_1           && VIRTCMDOPTS+=( "-vmcdrom1"    "${CDROM_1}" )
	notempty SERIAL0           && VIRTCMDOPTS+=( "-vmserial0"   "${SERIAL0}" )
	notempty PARALLEL0         && VIRTCMDOPTS+=( "-vmparallel0" "${PARALLEL0}" )
	notempty pt_gpu_mdev_id    && VIRTCMDOPTS+=( "-vmilmdevid0" "${pt_gpu_mdev_id}" )

	if [ "${SHARE_REMAP_MODE}" -gt 1 ]; then
		notempty HOME_SHARE_PATH   && VIRTCMDOPTS+=( "-vmfssrc0"    "${HOME_SHARE_PATH}" )
		notempty HOME_SHARE_NAME   && VIRTCMDOPTS+=( "-vmfstgt0"    "${HOME_SHARE_NAME}" )
		notempty COMMON_SHARE_PATH && VIRTCMDOPTS+=( "-vmfssrc1"    "${COMMON_SHARE_PATH}" )
		notempty COMMON_SHARE_NAME && VIRTCMDOPTS+=( "-vmfstgt1"    "${COMMON_SHARE_NAME}" )
	fi

	if notempty pt_gpu_pci_ids; then
		for pt_gpu_pci_id in "${pt_gpu_pci_ids[@]}"; do
			VIRTCMDOPTS+=( "-vmnvgpuids0" "${pt_gpu_pci_id}" )
		done
	fi
}