summaryrefslogtreecommitdiffstats
path: root/core/modules/vbox/data/opt/openslx/vmchooser/virtualbox/run-virt.include
blob: 59dd712d9008f8233cb96f8b4620971441332138 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -----------------------------------------------------------------------------
# Copyright (c) 2009..2011 - RZ Uni Freiburg
# Copyright (c) 2009..2013 - OpenSLX GmbH
#
# This program/file is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org
# -----------------------------------------------------------------------------
# run-virt.include
#    - component for virtualbox of the vmchooser plugin vmchooser-run_virt
################################################################################

[ -r /run/hwinfo ] && source /run/hwinfo

################################################################################
### declaration of default variables
################################################################################

if ! isset VM_NAME_CLEAN IMG_BASENAME SRC_IMG_ABSOLUTE VM_OS_TYPE; then
  env=$(mktemp)
  env > "$env"
  slxlog -d "virt-vbox-init" "A required variable is not set." "$env"
  writelog "Incomplete variable set passed from main run-virt"
  error_user "Wichtige variablen, die das VBox Plugin benötigt, wurden nicht
gesetzt. Kann die VM nicht starten."
  cleanexit 1
fi

declare -rg VMCHOOSERVBOX="/opt/openslx/vmchooser/${self}"
declare -rg VBOX_ROOT="${TMPDIR}/vbox"	# dir for configs
declare -rg VBOX_MACHINES_DIR="${VBOX_ROOT}/Machines"
declare -rg VBOX_SNAPSHOT_DIR="${VBOX_MACHINES_DIR}/${VM_NAME_CLEAN}/Snapshots"
declare -rg VBOX_HDD_DIR="${VBOX_ROOT}/HardDisks"
declare -rg VBOX_HDD_LINK="${VBOX_HDD_DIR}/${IMG_BASENAME}"

export VBOX_USER_HOME="${VBOX_ROOT}"		# instead of $HOME/.VirtualBox

# use VM_NAME_CLEAN for dir and config names since VM_DISPLAYNAME can be very long
VBOX_MACHINE_CONFIG="${VBOX_MACHINES_DIR}/${VM_NAME_CLEAN}/${VM_NAME_CLEAN}.xml"
mkdir -p "${VBOX_HDD_DIR}" "${VBOX_SNAPSHOT_DIR}" 2>/dev/null
enable2d="true"				# default setting for accelerated 2D grapics (OS dependent)
vram="128"				# set default graphics ram
snapshotuuid="34f617be-192a-46b3-a8ae-bce1029e093f"	# snapshot UUID is static

# link to image, determine extension (type)
imgfmt=$(echo ${SRC_IMG_ABSOLUTE##*.} | tr '[a-z]' '[A-Z]')
if [ -n "$VM_DISKFILE_RW" ]; then
  ln -sf "${VM_DISKFILE_RW}" "${VBOX_HDD_LINK}"
else
  ln -sf "${VM_DISKFILE_RO}" "${VBOX_HDD_LINK}"
  # image is RO, we only support VDI?
  if [ "$imgfmt" != "VDI" ]; then
    writelog "Image is not VDI, cannot create snapshot.."
    error_log "Die gewählte VM nutzt VBox, hat aber kein VDI image (sondern '$imgfmt'), kann VM nicht starten."
    cleanexit 1
  fi
fi


# Setting some default variables
shfolders="TRUE"
enablevt="true"

# set some base configuration depending on the guest operating system
case "${VM_OS_TYPE}" in
  Windows31)
    mem="32"
    vram="16"
    cpu_cores=1
    enablevt="false"
    shfolders="FALSE"
    ;;
  WindowsNT)
    mem="4096"
    vram="16"
    cpu_cores=4
    enablevt="false"
    shfolders="FALSE"
    ;;
  Windows95)
    mem="128"
    vram="32"
    cpu_cores=1
    enablevt="false"
    shfolders="FALSE"
    ;;
  Windows98)
    mem="256"
    vram="64"
    cpu_cores=1
    shfolders="FALSE"
    ;;
  WindowsMe)
    mem="256"
    vram="64"
    cpu_cores=1
    shfolders="FALSE"
    ;;
  WindowsXP)
    ;;
  WindowsVista)
    ;;
  Windows7)
    ;;
  Windows8)
    ;;
  OS2eCS)
    mem="256"
    cpu_cores=1
    enable2d="false"
    ;;
  MacOS)
    enable2d="false"
    ;;
  Linux*)
    ;;
  *)
    VM_OS_TYPE="Other"
    enable2d="false"
    ;;
esac

if [ "$HW_KVM" != "ENABLED" ]; then
  enablevt="false"
  cpu_cores=1 # check for VT, if not available only 1 cpu supported
fi

writelog "Directories:"
writelog "\tConfig dir:\t\t$VBOX_ROOT"
writelog "\tMachines dir:\t\t$VBOX_MACHINES_DIR"
writelog "\tMachine config:\t\t$VBOX_MACHINE_CONFIG"

################################################################################
### Hardware checks
################################################################################

# remove ':' from MAC addr for vbox and generate a VDE (virtual device ethernet)
# MAC addr from the first one (put in prefix 00DE)
macaddr=$(echo ${macaddr} | sed 's/://g')
vdemacaddr=$(echo ${macaddr} | sed 's/^..../00DE/g')

machineuuid="00000000-0000-0000-0000-${macaddr}"	# machine UUID, MAC addr part of it
machineuuid=$(echo ${machineuuid} | tr "[A-Z]" "[a-z]")	# cosmetical, since UUID in lower case

# get UUID of VBox image
diskuuid=$(VBoxManage -q showvdiinfo ${VBOX_HDD_LINK} \
            | grep UUID | grep -v "Parent UUID" | awk '{print $2}' \
            | grep -v use )	# nec. for VBox >= 4


if [ -n "$VM_DISKFILE_RW" ]; then
  # Image is already RW - easy
  imgtype="Normal"
  imageuuid=${diskuuid}
else
  # use temp disk as snapshot to get CoW
  imgtype="Immutable"					# make disk immutable
  imageuuid=${snapshotuuid}
  < "${VMCHOOSERVBOX}/empty-diff.vdi.gz" gunzip > "${VBOX_SNAPSHOT_DIR}/{${snapshotuuid}}.vdi"
  # patch the disk file uuid into the snapshot vdi file:
  # We read from offset 424 in the source HDD and write to offset 392 in out prepared
  # CoW container. 16 bytes will be copied, which is the binary UUID
  # TODO: This requires the image to be a vdi/vdh; Afaik, vbox supports vmdk,
  # so this code would break in that case. Can we use the diskuuid we got via
  # VBoxManage, convert to binary, and patch it
  dd if=${VBOX_HDD_LINK} of="${VBOX_SNAPSHOT_DIR}/{${snapshotuuid}}.vdi" seek=424 \
    skip=392 bs=16 count=1 conv=notrunc
fi

# 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

boot="HardDisk"

writelog "\tSnapshots dir:\t\t$VBOX_SNAPSHOT_DIR"
writelog "Diskimage:"
writelog "\tDisk file:\t\t$VBOX_HDD_LINK"
writelog "\tDisk type:\t\t$imgtype"
writelog "\tVMostype:\t\t$VM_OS_TYPE"
writelog "\tMachine UUID:\t\t$machineuuid"
writelog "\tDisk UUID:\t\t$diskuuid"
writelog "Virtual Hardware:"
writelog "\tCPU cores:\t\t${cpu_cores}\c"
writelog ""
[ "$HW_KVM" = "ENABLED" ] && writelog " (VT enabled CPU)"
writelog "\tGuest RAM:\t\t${mem} MB"
# echo nur wenn hostmem gesetzt
[ -n "${hostmem}" ] && writelog "\tHost RAM:\t\t${hostmem} MB"
writelog "\tMAC address:\t\t$macaddr"
writelog "\tNetwork card:\t\t${vb_network_card}"
writelog "\tNetwork kind:\t\t${network_kind}"
writelog "\tBooting from:\t\t${boot}\c"
writelog ""
writelog "\tCD-ROM1:\t\t${cdrom0}"
#writelog "\tCD-ROM2:\t\t${cdrom1}"
writelog "\tFloppy_A:\t\t${floppy0}"
writelog "\tFloppy_B:\t\t${floppy1}"
# defined in run-virt.sh and run-vmgrid.sh
writelog "\tShared Folders '${sharename}':\t${sharepath}"

################################################################################
### Pepare and configure virtual machine and disk image
################################################################################

# create Virtualbox.xml

# Shares given?
if [ "x$shfolders" != "xFALSE" ]; then
  sharelist="<SharedFolders>
        <SharedFolder name=\"${homesharename}\" hostPath=\"${homesharepath}\" writable=\"true\"/>
        <SharedFolder name=\"${commonsharename}\" hostPath=\"${commonsharepath}\" writable=\"true\"/>
      </SharedFolders>"
else
  sharelist=''
fi

source "${VMCHOOSERVBOX}/virtualbox.include"  # create Virtualbox.xml

# remove snapshot disk when using rw images
if [ "${imgtype}" != "Immutable" ]; then
  sed -i "/${snapshotuuid}/d" "${VBOX_ROOT}/VirtualBox.xml"
fi

# TODO: add rawdisk if requested
#"raw.vmdk" format="VMDK" type="Writethrough"/>

source "${VMCHOOSERVBOX}/machine.include" # create machine.xml

# remove CD-ROM if not available
if [ "${cdrom0}" != "TRUE" ]; then
  sed -i "/HostDrive/d" "${VBOX_MACHINE_CONFIG}"
  sed -i '/AttachedDevice.*type="DVD"/d' "${VBOX_MACHINE_CONFIG}"
  sed -i "/<!-- PLEASE DO NOT REMOVE THIS COMMENT (CD)!!! -->/d" "${VBOX_MACHINE_CONFIG}"
fi

################################################################################
### Set env for vmchooser-run_virt
################################################################################

# wait for a certain command to settle
# get the PID of the right process
# kill PID, seems to work
VBMANPID=$(pstree -p | grep VBoxXPCOMIPCD | grep -ivE "VirtualBox|VBoxHeadless"\
  | sed -e "s/.*VBoxXPCOMIPCD(\(.*\)).*/\1/")
for i in $(echo ${VBMANPID}); do
  kill -9 ${VBMANPID} >/dev/null 2>&1
done

# set the variables appropriately (several interfaces with different names)
VIRTCMD=$(which VirtualBox 2>/dev/null)
VIRTCMDOPTS="--startvm ${machineuuid} --start-running --fullscreen"

# set headless mode (-v off to disable vrdp)
VIRTCMDHL=$(which VBoxHeadless 2>/dev/null)
VIRTCMDOPTSHL="-s ${machineuuid}"