summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/data/opt/openslx/vmchooser/plugins/qemukvm/run-virt.include
blob: 391ba5a55f79b2b0f4b4b94fffc7e85b393b84a2 (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
# -----------------------------------------------------------------------------
#
# 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")

	# setup RW image access for operation
	local vm_diskfile
	if 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

	# set path to QEMU firmware specification files
	local firmware_path="/usr/share/qemu/firmware"

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

		# set configuration for virt-manager
		local GSETTINGS_SCHEMA_NAME_PREFIX="org.virt-manager.virt-manager"
		local GSETTINGS_SCHEMA_PATH_PREFIX="/org/virt-manager/virt-manager"

		# per VM relocatable virt-manager settings
		# scale a VM's output in fullscreen to fit the virt-manager's console size
		gsettings set "${GSETTINGS_SCHEMA_NAME_PREFIX}.vm:${GSETTINGS_SCHEMA_PATH_PREFIX}/vm/" "scaling" "1"
		# automatic resize of VM resolution to virt-manager's console resolution
		gsettings set "${GSETTINGS_SCHEMA_NAME_PREFIX}.vm:${GSETTINGS_SCHEMA_PATH_PREFIX}/vm/" "resize-guest" "1"
	fi

	# set device passthrough debug mode
	local debug_pth="false"

	# Use cat here instead of redirect because of globbing
	local cputhreads
	cputhreads="$( cat /sys/devices/system/cpu/cpu*/topology/core_cpus_list | awk '!a[$1]{if(b)printf";";printf $1;a[$1]=1;b=1}' )"
	# Try legacy name
	[ -z "$cputhreads" ] && cputhreads="$( cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | awk '!a[$1]{if(b)printf";";printf $1;a[$1]=1;b=1}' )"

	# 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 debug_pth         && VIRTCMDOPTS+=( "-debugpth"    "${debug_pth}" )
	notempty firmware_path     && VIRTCMDOPTS+=( "-firmware"    "${firmware_path}" )
	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}" )
	if notempty cputhreads; then
		VIRTCMDOPTS+=( "-cputopo"     "${cputhreads}" )
	elif notempty HW_THREADS; then
		VIRTCMDOPTS+=( "-vmncpus"     "${HW_THREADS}" )
	fi
	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 floppy_img        && VIRTCMDOPTS+=( "-vmfloppy1"   "${floppy_img}" )
	notempty CDROM_0           && VIRTCMDOPTS+=( "-vmcdrom0"    "${CDROM_0}" )
	notempty CDROM_1           && VIRTCMDOPTS+=( "-vmcdrom1"    "${CDROM_1}" )
	notempty SERIAL0 && [ -w "${SERIAL0}" ] \
	                           && VIRTCMDOPTS+=( "-vmserial0"   "${SERIAL0}" )
	notempty PARALLEL0 && [ -w "${PARALLEL0}" ] \
	                           && VIRTCMDOPTS+=( "-vmparallel0" "${PARALLEL0}" )
	notempty pt_gpu_mdev_id    && VIRTCMDOPTS+=( "-vmilmdevid0" "${pt_gpu_mdev_id}" )

	if $DEBUG; then
		VIRTCMDOPTS+=( "-xmledit" )
	fi

	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
}