summaryrefslogtreecommitdiffstats
path: root/remote/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/get_xml_file_variables.inc
blob: cf0d545b81a1fafc86b752a784a229d22168030e (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
############################################
# Include: Get needed values from XML file #
############################################

writelog "Parsing XML..."

declare -rg VMSTORE_PATH=/mnt/vmstore

# Need some lean and mean xml parser some day in the far future
get_xml () {
	xmlextract "//settings/eintrag/${1}/@param" "${xmlfile}"
}

IMGUUID=$(get_xml "uuid")

# # Name of the virt image
SRC_IMG_ABSOLUTE=$(get_xml "image_path")
SRC_IMG_RELATIVE=$(get_xml "image_name")

if [ -z "${SRC_IMG_ABSOLUTE}${SRC_IMG_RELATIVE}" ]; then
	writelog "Neither relative nor absolute path for image found in xml"
	cleanexit 1
fi

if [ -n "$SRC_IMG_ABSOLUTE" ] && [ "${SRC_IMG_ABSOLUTE:0:1}" != "/" ]; then
	writelog "Error parsing XML for absolute image path: given value doesn't start with '/': '$SRC_IMG_ABSOLUTE'"
	cleanexit 1
fi

if [ -z "$SRC_IMG_ABSOLUTE" ]; then
	SRC_IMG_ABSOLUTE="${VMSTORE_PATH}/${SRC_IMG_RELATIVE}"
fi

IMG_BASENAME=$(basename "$SRC_IMG_ABSOLUTE")
writelog "Virtual image file name: $IMG_BASENAME"

VM_DISPLAYNAME=$(get_xml "short_description")
[ -z "$VM_DISPLAYNAME" ] && VM_DISPLAYNAME="${IMG_BASENAME}"

# Define VM_NAME_CLEAN since VM_DISPLAYNAME can be long and contain weird characters
VM_NAME_CLEAN=$(echo "${VM_DISPLAYNAME:0:32}" | sed -r 's/[^0-9a-zA-Z_-\.]+/_/g')

# image is for the following virtual machine
PLUGIN_ID=$(grep -o 'virtualmachine param=.*"' "${xmlfile}" \
	| sed -e "s/&.*;/; /g" | awk -F '"' '{print $2}')

# Extracting OS type (VM_OS_TYPE) from xml file. We don't care here whether VM_OS_TYPE is empty, as then 
# it will yield the default entries later on.
VM_OS_TYPE=$(get_xml "os")

readonly IMGUUID
readonly SRC_IMG_ABSOLUTE SRC_IMG_RELATIVE
readonly IMG_BASENAME
readonly VM_DISPLAYNAME VM_NAME_CLEAN
readonly PLUGIN_ID
readonly VM_OS_TYPE

writelog "VM UUID:                $IMGUUID"
writelog "Virtualization plugin:  $PLUGIN_ID"
writelog "VM name:                $VM_DISPLAYNAME"
writelog "VM short name:          $VM_NAME_CLEAN"
writelog "VM OS:                  $VM_OS_TYPE"
writelog "Done parsing XML."