Debian Codegraph @

bin/qwdctl

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 #Usage: /usr/bin/qwdctl controls the availability of virtual machines for the qemu-web-desktop.
4 #
5 # Each entry in the configuration file '/etc/qemu-web-desktop/machines.conf'
6 # spans on 1 to 3 lines:
7 #
8 # - [name.ext or URL or file path]
9 # - url=[URL to ISO, QCOW2, VDI, VMDK, RAW, VHD/VHDX, QED virtual machine disk, optional]
10 # - description=[description to be shown in the service page]
11 #
12 # Images listed in the configuration file without a 'url=' parameter are
13 # expected to be downloaded by hand and installed into
14 # '/var/lib/qemu-web-desktop/machines' by the local administrator. Then, just
15 # specify the [name.ext] and description.
16 # When the description is missing, the VM file name is used.
17 # Additional machine descriptions can be given in '/etc/qemu-web-desktop/config.d/*.ini' files.
18 #
19 # use: 'qwdctl help' to get help about usage and commands/options.
20
21 set -eo pipefail
22
23 # identify type of system
24 case "$(unamescip-shell system . . uname. -s)" in
25 Darwin) DIST=Darwin ;; # macOS
26 Linux)
27 if [scip-shell system . . `[`. -e "$PREFIX/etc/initramfs-tools/modules" ]; then
28 DIST=Debian
29 else
30 DIST=Arch
31 fi
32 ;;
33 *) DIST=Unknown
34 echoscip-shell system . . echo. "WARNING: Unsupported system"
35 ;;
36 esac
37
38 bin=$(dirnamescip-shell system . . dirname. $0)
39 p=$(basenamescip-shell system . . basename. $0)
40
41 if command -v brew 2>&1 >/dev/nullscip-shell filesystem . . `/dev/null`.; then
42 # using brew / Apple Mac
43 PREFIX=`brew --prefix`
44 GREP=$(command -v ggrep || command -v grep) # Use GNU grep if available
45 EDITOR=nano
46 else
47 GREP=grep
48 fi
49
50 # check required binaries
51 for cmd in wget sed awk "$GREP" "${EDITOR:-nano}"; do
52 command -v "$cmd" >/dev/nullscip-shell filesystem . . `/dev/null`. || { echoscip-shell system . . echo. "$p: ERROR: $cmd not found"; exit 1; }
53 done
54
55 # After setting PREFIX
56 if [[ -z "$PREFIX" ]]; then
57 PREFIX=""
58 elif [[ ! "$PREFIX" =~ ^/ ]]; then
59 echoscip-shell system . . echo. "$p: ERROR: Invalid PREFIX: $PREFIX" >&2
60 exit 1
61 fi
62
63 # files to process
64 F="$PREFIX/etc/qemu-web-desktop/machines.conf"
65 machine_conf="$F"
66 if [scip-shell system . . `[`. ! -f "$F" ]; then
67 echoscip-shell system . . echo. "$p: WARNING: can not find QWD machine.conf file ($F)."
68 fi
69 conf_d="$PREFIX/etc/qemu-web-desktop/config.d"
70
71 F="$PREFIX/usr/share/qemu-web-desktop/html/desktop/index.html"
72 index_html="$F"
73 if [scip-shell system . . `[`. -f $PREFIX/var/www/qemu-web-desktop/html/desktop/index.html ]; then
74 index_html="$PREFIX/var/www/qemu-web-desktop/html/desktop/index.html"
75 elif [scip-shell system . . `[`. ! -f "$F" ]; then
76 echoscip-shell system . . echo. "$p: WARNING: can not find QWD index.html file ($F)."
77 fi
78
79 # set to yes if apache/httpd include_module does not properly show list in index.html
80 for i in $*; do
81 case "$i" in
82 --machine-insert|--machine_insert|machine-insert|machine_insert)
83 QWDCTL_INSERT=yes # for refresh/download
84 ;;
85 --yes|-y)
86 QWDCTL_YES=1 # assume Yes for gpu and purge
87 ;;
88 esac
89 done
90
91 # generated files, should be linked into /usr/share/qemu-web-desktop/html/desktop/
92 F="$PREFIX/var/lib/qemu-web-desktop"
93 qwdprefix="$F"
94 if [scip-shell system . . `[`. ! -d "$F" ]; then
95 echoscip-shell system . . echo. "$p: WARNING: can not find QWD machines directory ($F)."
96 fi
97 machine_html="$qwdprefix/machines.html"
98
99 # search for local config
100 if [scip-shell system . . `[`. -f "$bin/../config.pl" ]; then
101 # using local configuration
102 echoscip-shell system . . echo. "$bin/../config.pl"
103 config_pl=$(realpathscip-shell system . . realpath. "$bin/../config.pl")
104 else
105 F="$PREFIX/etc/qemu-web-desktop/config.pl"
106 config_pl="$F"
107 if [scip-shell system . . `[`. ! -f "$F" ]; then
108 echoscip-shell system . . echo. "$p: WARNING: can not find QWD config.pl file ($F)."
109 fi
110 fi
111
112 # search for local cgi/.pl script
113 if [scip-shell system . . `[`. -x "$bin/../cgi-bin/qemu-web-desktop.pl" ]; then
114 cgi=$(realpathscip-shell system . . realpath. "$bin/../cgi-bin/qemu-web-desktop.pl")
115 elif [scip-shell system . . `[`. -x "$cgi/qemu-web-desktop.pl" ]; then
116 # local (git)
117 cgi="$cgi/qemu-web-desktop.pl"
118 elif [scip-shell system . . `[`. -x "$PREFIX/srv/http/cgi-bin/qemu-web-desktop.pl" ]; then
119 # Arch/httpd default
120 cgi="$PREFIX/srv/http/cgi-bin/qemu-web-desktop.pl"
121 elif [scip-shell system . . `[`. -x "$PREFIX/var/www/cgi-bin/qemu-web-desktop.pl" ]; then
122 # RedHat/httpd and Brew default
123 cgi="$PREFIX/var/www/cgi-bin/qemu-web-desktop.pl"
124 else
125 # Debian default
126 cgi="$PREFIX/usr/lib/cgi-bin/qemu-web-desktop.pl"
127 fi
128
129 # ------------------------------------------------------------------------------
130
131 # Clean HTML string
132 # ret=$(escape_html "string")
133 escape_htmlscip-shell shell shell-project 0.0.0 escape_html().() {
134 local s="$1"
135 s="${s//&/&}"
136 s="${s//</&lt;}"
137 s="${s//>/&gt;}"
138 s="${s//\"/&quot;}" #'
139 s="${s//\'/&#39;}"
140 printfscip-shell system . . printf. '%s' "$s"
141 }
142
143 # List all section names in an INI file (one per line)
144 # sections=$(ini_sections "ini_file")
145 #
146 # Replaces: confget -f $F -q sections
147 # Replaces: crudini --get $F
148 ini_sectionsscip-shell shell shell-project 0.0.0 ini_sections().() {
149 local f="$1"
150 awkscip-shell system . . awk. '
151 /^[ \t]*[;#]/ { next } # skip comment lines
152 /^[ \t]*\[.*\][ \t]*$/ {
153 line=$0
154 gsub(/^[ \t]*\[[ \t]*/, "", line)
155 gsub(/[ \t]*\][ \t]*$/, "", line)
156 print line
157 }
158 ' "$f"
159 }
160
161 # Get the value of key "$3" in section "$2" of file "$1"
162 # value = $(ini_get "ini_file" "section" "key")
163 #
164 # Replaces: confget -f $F -s $i url
165 # Replaces: crudini --get $F $i url
166 ini_getscip-shell shell shell-project 0.0.0 ini_get().() {
167 local f="$1" section="$2" key="$3"
168 awkscip-shell system . . awk. -v section="$section" -v key="$key" '
169 function trim(s) {
170 gsub(/^[ \t]+|[ \t]+$/, "", s)
171 return s
172 }
173 /^[ \t]*[;#]/ { next } # skip comment lines
174 /^[ \t]*\[.*\][ \t]*$/ {
175 line=$0
176 gsub(/^[ \t]*\[[ \t]*/, "", line)
177 gsub(/[ \t]*\][ \t]*$/, "", line)
178 in_sec = (line == section)
179 next
180 }
181 in_sec {
182 line = $0
183 eq = index(line, "=")
184 if (eq > 0) {
185 k = trim(substr(line, 1, eq-1))
186 if (k == key) {
187 v = trim(substr(line, eq+1))
188 # strip surrounding quotes if present, like crudini does
189 if (v ~ /^".*"$/ || v ~ /^\x27.*\x27$/) {
190 v = substr(v, 2, length(v)-2)
191 }
192 print v
193 exit
194 }
195 }
196 }
197 ' "$f"
198 }
199
200 # List running sessions
201 qwdctl_statusscip-shell shell shell-project 0.0.0 qwdctl_status().() {
202
203 echoscip-shell system . . echo. "$p status"
204 echoscip-shell system . . echo. " config: $config_pl"
205 echoscip-shell system . . echo. " $conf_d/"
206 echoscip-shell system . . echo. " machines: $machine_conf"
207 echoscip-shell system . . echo. " $machine_html"
208 echoscip-shell system . . echo. " $qwdprefix/machines/"
209 echoscip-shell system . . echo. " landing: $index_html"
210 if [[ "$DIST" == "Darwin" ]]; then
211 ip_address=$(ifconfig | grepscip-shell system . . grep. "inet " | grepscip-shell system . . grep. -v 127.0.0.1 | awkscip-shell system . . awk. '{print $2}' | headscip-shell system . . head. -1)
212 else
213 ip_address=$(hostnamescip-shell system . . hostname. -I | awkscip-shell system . . awk. '{print $1}')
214 fi
215 echoscip-shell system . . echo. " service: http://$ip_address/qemu-web-desktop/"
216 echoscip-shell system . . echo. " "
217 # Loop through machine lists
218 local active_sections=()
219 local inactive_sections=()
220 local files=("$machine_conf" "$conf_d"/*.ini "$conf_d"/*.conf)
221 for F in "${files[@]}"; do
222 # Skip if the file does not exist (e.g., no .ini or .conf files)
223 [scip-shell system . . `[`. -f "$F" ] || continue
224 # Extract active sections
225 while IFS= read -r section; do
226 active_sections+=("$section")
227 done < <(grepscip-shell system . . grep. -E '^\[[^]]+\]' "$F" | sedscip-shell system . . sed. -E 's/^\[([^]]+)\]/\1/')
228
229 # Extract inactive sections
230 while IFS= read -r section; do
231 inactive_sections+=("$section")
232 done < <(grepscip-shell system . . grep. -E '^;\[[^]]+\]' "$F" | sedscip-shell system . . sed. -E 's/^;\[([^]]+)\]/\1/')
233 done
234
235 if [scip-shell system . . `[`. -z "${active_sections[*]}" ]; then
236 echoscip-shell system . . echo. "Enabled machines (use '$p enable VM|FILE|URL' to add one):"
237 echoscip-shell system . . echo. " None"
238 else
239 echoscip-shell system . . echo. "Enabled machines (disable with '$p disable VM'):"
240 printfscip-shell system . . printf. ' %s\n' "${active_sections[@]}"
241 fi
242 echoscip-shell system . . echo.
243 if [scip-shell system . . `[`. ! -z "${inactive_sections[*]}" ]; then
244 echoscip-shell system . . echo. "Disabled machines (enable with '$p enable VM')"
245 printfscip-shell system . . printf. ' %s\n' "${inactive_sections[@]}"
246 echoscip-shell system . . echo.
247 fi
248 local orphan=0
249 echoscip-shell system . . echo. "Orphan machine files (not enabled/disabled, purge with '$p purge VM'):"
250 for file in "$qwdprefix/machines"/*; do
251 [scip-shell system . . `[`. -f "$file" ] || continue
252 filename=$(basenamescip-shell system . . basename. "$file")
253 # Check if filename is not in active or inactive lists
254 if ! printfscip-shell system . . printf. '%s\n' "${active_sections[@]}" "${inactive_sections[@]}" | grepscip-shell system . . grep. -q "^${filename}$"; then
255 orphan=1
256 echoscip-shell system . . echo. " $filename"
257 fi
258 done
259 if [scip-shell system . . `[`. $orphan == 0 ]; then echoscip-shell system . . echo. " None"; fi
260 echoscip-shell system . . echo. " "
261 echoscip-shell system . . echo. "Running sessions:"
262 # Header for the table
263 printfscip-shell system . . printf. "%-8s | %5s | %-15s | %4s | %6s | %-7s | %s\n" "PID" "CPU/%" "User name" "#CPU" "MEM/GB" "GPU" "Full name"
264 printfscip-shell system . . printf. "%s\n" "------------------------------------------------------------------------"
265
266 # Get PIDs for qemu-system, excluding grep and sh -c processes
267 psscip-shell system . . ps. auxww | grepscip-shell system . . grep. 'qemu-system' | grepscip-shell system . . grep. -v 'grep' | grepscip-shell system . . grep. -v 'sh -c' | while read -r line; do
268 # Extract PID and CPU usage
269 pid=$(echoscip-shell system . . echo. "$line" | awkscip-shell system . . awk. '{print $2}')
270 cpu_usage=$(echoscip-shell system . . echo. "$line" | awkscip-shell system . . awk. '{print $3"%"}')
271
272 # Extract the full command (from field 11 onwards)
273 cmd=$(echoscip-shell system . . echo. "$line" | awkscip-shell system . . awk. '{for(i=11;i<=NF;i++) printf $i" "; print ""}')
274
275 # Use GNU grep for Perl regex support
276 full_name=$(echoscip-shell system . . echo. "$cmd" | $GREP -oP '(?<=-name ).*?(?= -|$)')
277 nb_cpu=$(echoscip-shell system . . echo. "$cmd" | $GREP -oP '(?<=-smp ).*?(?= -|$)')
278 mem_mb=$(echoscip-shell system . . echo. "$cmd" | $GREP -oP '(?<=-m ).*?(?= -|$)')
279 mem_gb=$(awkscip-shell system . . awk. -v mem_mb="${mem_mb:-0}" 'BEGIN {printf "%.2f", mem_mb / 1024}')
280 user_name=$(echoscip-shell system . . echo. "$full_name" | cutscip-shell system . . cut. -d: -f2)
281 pci=$(echoscip-shell system . . echo. "$cmd" | $GREP -oP '(?<=host=)[^, ]+' || echoscip-shell system . . echo. "")
282
283 # Print the row
284 printfscip-shell system . . printf. "%-8s | %5s | %-15s | %4s | %6s | %-7s | %s\n" "$pid" "$cpu_usage" "$user_name" "$nb_cpu" "$mem_gb" "$pci" "$full_name"
285 done
286 }
287
288 # Enable/disable a section in $index_html
289 # toggle_section "enable"|"disable" "GPU checkbox"|"one-shot session checkbox"
290 toggle_sectionscip-shell shell shell-project 0.0.0 toggle_section().() {
291 local action="$1" marker="$2" t=$(mktempscip-shell system . . mktemp. -t "index_html.XXXXXX")
292 chmodscip-shell system . . chmod. 644 "$t"
293
294 awkscip-shell system . . awk. -v action="$action" -v marker="$marker" '
295 {
296 if (in_section) {
297 if ($0 ~ /^<!--[[:space:]]*-+[[:space:]]*-->$/) {
298 in_section = 0
299 }
300 else if (skip_lines > 0) {
301 skip_lines--
302 }
303 else if ($0 ~ /</) {
304 if (action == "enable") {
305 sub(/^<!--/, "")
306 sub(/ -->$/, "")
307 } else {
308 if ($0 !~ /^<!--/) $0 = "<!--" $0
309 if ($0 !~ / -->$/) $0 = $0 " -->"
310 }
311 }
312 } else if ($0 ~ "<!-- \\[X\\] " marker) {
313 in_section = 1
314 skip_lines = 2
315 }
316 print $0
317 }' "$index_html" > $t && mvscip-shell system . . mv. $t "$index_html" || { echoscip-shell system . . echo. "ERROR: Failed to move $t" >&2; exit 1; }
318 echoscip-shell system . . echo. "$action '$marker' in $index_html"
319 }
320
321 MAX_INPUT_LEN=256
322 validate_inputscip-shell shell shell-project 0.0.0 validate_input().() {
323 [[ ${#1} -le $MAX_INPUT_LEN ]] || { echoscip-shell system . . echo. "ERROR: Input too long" >&2; return 1; }
324 return 0
325 }
326
327 validate_gpu_idscip-shell shell shell-project 0.0.0 validate_gpu_id().() {
328 [[ "$1" =~ ^([0-9a-fA-F]{4}:[0-9a-fA-F]{4},?)+$ ]] || return 1
329 }
330
331 # ------------------------------------------------------------------------------
332
333 if [scip-shell system . . `[`. -z "$1" ]; then
334 qwdctl_statusscip-shell shell shell-project 0.0.0 qwdctl_status().
335 exit 0
336 fi
337
338 # user level actions
339 case "$1" in
340 # ------------------------------------------------------------------------------
341 --help|help|-h)
342 # show help.
343
344 echoscip-shell system . . echo. "Usage: $p [help|download|refresh|status|start VM|stop|gpu|gpu-unlock|enable|disable|purge|edit] ..."
345 echoscip-shell system . . echo. " "
346 echoscip-shell system . . echo. " controls the availability of virtual machines for the "
347 echoscip-shell system . . echo. " qemu-web-desktop."
348 echoscip-shell system . . echo. " In addition, the status of the running sessions can be displayed"
349 echoscip-shell system . . echo. " and it is possible to start virtual machines manually and display"
350 echoscip-shell system . . echo. " them in a browser."
351 echoscip-shell system . . echo.
352 echoscip-shell system . . echo. "OPTIONS:"
353 echoscip-shell system . . echo. " --help|help|-h"
354 echoscip-shell system . . echo. " show this help."
355 echoscip-shell system . . echo.
356 echoscip-shell system . . echo. " --disable|disable VM|one-shot|gpu"
357 echoscip-shell system . . echo. " deactivate a [VM] entry."
358 echoscip-shell system . . echo. " '$p --disable one-shot' disables the one-shot section in $index_html."
359 echoscip-shell system . . echo. " '$p --disable gpu' disables the GPU support (same as --gpu-unlock)."
360 echoscip-shell system . . echo.
361 echoscip-shell system . . echo. " --download|download|update"
362 echoscip-shell system . . echo. " scan the $machine_conf file for [name.ext] and download them when URL are given."
363 echoscip-shell system . . echo. " a '$p --refresh' is then performed. Virtual machine images are stored into $qwdprefix/machines."
364 echoscip-shell system . . echo. " Additional machine descriptions can be given in $conf_d/*.ini files."
365 echoscip-shell system . . echo. " "
366 echoscip-shell system . . echo. " --edit|edit [machines|config|web|VM]"
367 echoscip-shell system . . echo. " edit the machines list, the service configuration file or the service web page."
368 echoscip-shell system . . echo. " In the case of the machines list, the '$p --download' command is triggered automatically after edit."
369 echoscip-shell system . . echo. " When given a VM name, the file holding the [VM] entry is edited."
370 echoscip-shell system . . echo. " Set the \$EDITOR variable to select the text editor to use."
371 echoscip-shell system . . echo. " In case the '$p edit machines' has no effect on the index.html service landing page,"
372 echoscip-shell system . . echo. " specify set 'QWDCTL_INSERT=yes' or option --machine-insert."
373 echoscip-shell system . . echo. " It is recommended to use the --enable and --disable to manage the VM/machine list."
374 echoscip-shell system . . echo.
375 echoscip-shell system . . echo. " --enable|enable VM|FILE|URL|one-shot|gpu {'DESCRIPTION'}"
376 echoscip-shell system . . echo. " add or (re)activate a [VM] entry, file or URL. Optional description is given as a string."
377 echoscip-shell system . . echo. " '$p --enable one-shot' activates the one-shot section in $index_html."
378 echoscip-shell system . . echo. " '$p --enable gpu' activates the GPU support (same as --gpu)."
379 echoscip-shell system . . echo.
380 echoscip-shell system . . echo. " --gpu|gpu VENDOR:MODEL"
381 echoscip-shell system . . echo. " configure GPU for pass-through. The GPU_ids are e.g. '10de:1d01'."
382 echoscip-shell system . . echo. " "
383 echoscip-shell system . . echo. " --gpu-unlock|gpu-unlock"
384 echoscip-shell system . . echo. " unlock/re-attach all GPU's to server (uninstall pass-through)."
385 echoscip-shell system . . echo. " "
386 echoscip-shell system . . echo. " --machine-insert"
387 echoscip-shell system . . echo. " used with --download/--refresh, force to modify the $index_html"
388 echoscip-shell system . . echo. " landing page instead of using an include."
389 echoscip-shell system . . echo. " Same as QWDCTL_INSERT=yes shell variable."
390 echoscip-shell system . . echo.
391 echoscip-shell system . . echo. " --refresh|refresh"
392 echoscip-shell system . . echo. " scan the $machine_conf file, and generate the $machine_html that lists"
393 echoscip-shell system . . echo. " available images to show in the qemu-web-desktop main form."
394 echoscip-shell system . . echo. " Additional machine descriptions can be given in $conf_d/*.ini files."
395 echoscip-shell system . . echo. " "
396 echoscip-shell system . . echo. " --status|status"
397 echoscip-shell system . . echo. " list running sessions, as well as enabled and disabled machines."
398 echoscip-shell system . . echo. " "
399 echoscip-shell system . . echo. " --start|start VM ..."
400 echoscip-shell system . . echo. " start the given VM or ISO file (full path) in a browser. Connect to it with"
401 echoscip-shell system . . echo. " the displayed URL. Further arguments are passed to the service (see config.pl)"
402 echoscip-shell system . . echo. " e.g. --snapshot_alloc_mem=1 (in GB) and --snapshot_alloc_cpu=2."
403 echoscip-shell system . . echo. " Changes are lost except when specifying option --snapshot_use_master=1,"
404 echoscip-shell system . . echo. " requiring write access. When running ISO's you may also specify"
405 echoscip-shell system . . echo. " --snapshot_alloc_disk=40 (in GB)."
406 echoscip-shell system . . echo. " Use '$p --start' to get the full list of default VM settings."
407 echoscip-shell system . . echo. " "
408 echoscip-shell system . . echo. " --stop|stop TOKEN"
409 echoscip-shell system . . echo. " stop sessions matching TOKEN. Some snapshot files may be left-over."
410 echoscip-shell system . . echo. " "
411 echoscip-shell system . . echo. " --purge|purge {VMNAME}"
412 echoscip-shell system . . echo. " remove VM files which are inactive and not used. When VM argument is "
413 echoscip-shell system . . echo. " given, only files starting by 'VM' are checked."
414 echoscip-shell system . . echo.
415 echoscip-shell system . . echo. " --version|version|-v"
416 echoscip-shell system . . echo. " show $p version."
417 echoscip-shell system . . echo.
418 echoscip-shell system . . echo. " --yes|-y"
419 echoscip-shell system . . echo. " assume yes to questions. Same as QWDCTL_YES=1 shell variable."
420 echoscip-shell system . . echo.
421 echoscip-shell system . . echo. "AUTHOR:"
422 echoscip-shell system . . echo. " Written by Roland Mas and Emmanuel Farhi."
423 echoscip-shell system . . echo.
424 echoscip-shell system . . echo. "FILES:"
425 echoscip-shell system . . echo. " The QWD service configuration is defined in:"
426 echoscip-shell system . . echo. " - $config_pl"
427 echoscip-shell system . . echo. " - $conf_d/*.pl"
428 echoscip-shell system . . echo. " "
429 echoscip-shell system . . echo. " The QWD available VM's are defined in:"
430 echoscip-shell system . . echo. " - $qwdprefix/machines"
431 echoscip-shell system . . echo. " - $machine_conf"
432 echoscip-shell system . . echo. " - $conf_d/*.ini"
433 echoscip-shell system . . echo. " Entries should contain lines:"
434 echoscip-shell system . . echo. " [name.ext or URL]"
435 echoscip-shell system . . echo. " description=<name of machine to appear in the form>"
436 echoscip-shell system . . echo. " In addition, any line with:"
437 echoscip-shell system . . echo. " url=<link>"
438 echoscip-shell system . . echo. " will retrieve the given file with: $p download"
439 echoscip-shell system . . echo. " When the description is missing, the VM file name is used."
440 echoscip-shell system . . echo. " Supported virtual machine formats: ISO, QCOW2, VDI, VMDK, RAW, VHD/VHDX, QED"
441 echoscip-shell system . . echo. " "
442 echoscip-shell system . . echo. " The QWD landing/login page is in:"
443 echoscip-shell system . . echo. " - $index_html"
444 echoscip-shell system . . echo. " - $machine_html"
445 echoscip-shell system . . echo. " "
446 echoscip-shell system . . echo. " The QWD service script is in:"
447 echoscip-shell system . . echo. " - $cgi"
448 echoscip-shell system . . echo.
449 echoscip-shell system . . echo. " - https://gitlab.com/soleil-data-treatment/soleil-software-projects/qemu-web-desktop"
450 echoscip-shell system . . echo. " "
451 echoscip-shell system . . echo. "ENVIRONMENT VARIABLES:"
452 echoscip-shell system . . echo. " EDITOR Set the text editor to use, e.g. nano. Default is to use 'nano' or \$EDITOR."
453 echoscip-shell system . . echo. " QWDCTL_INSERT=yes is equivalent to setting --machine-insert."
454 echoscip-shell system . . echo. " QWDCTL_YES=1 is equivalent to setting --yes (skip confirm)."
455 echoscip-shell system . . echo. " "
456
457 exit 0
458 ;;
459 # ------------------------------------------------------------------------------
460 --status|status)
461 qwdctl_statusscip-shell shell shell-project 0.0.0 qwdctl_status().
462 exit 0
463 ;;
464 # ------------------------------------------------------------------------------
465 --start|start)
466 # start the given VM or ISO file (full path) in a browser.
467
468 # 2nd arg is the VM
469 if [scip-shell system . . `[`. "x$2" = "x" ]; then
470 echoscip-shell system . . echo. "Usage: $p $1 VM [options]"
471 echoscip-shell system . . echo. "Configuration: $config_pl $conf_d/"
472 echoscip-shell system . . echo. "Script: $cgi"
473 echoscip-shell system . . echo. "Availabe options are:"
474 $cgi -h
475 exit 0
476 fi
477 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
478 DIR=$(dirnamescip-shell system . . dirname. "$2")
479 DIR=$(realpathscip-shell system . . realpath. "$DIR")
480 VM=$(basenamescip-shell system . . basename. "$2")
481 shift 2
482 if [scip-shell system . . `[`. ! -f "$DIR/$VM" -a -f "$qwdprefix/machines/$VM" ]; then
483 DIR=$qwdprefix/machines
484 fi
485
486 # snapshot_alloc_cpu snapshot_alloc_mem
487 echoscip-shell system . . echo. "$p: Launching $DIR/$VM $@"
488 log_file=$(mktempscip-shell system . . mktemp. -t "qwdctl_${VM}_XXXXXX.log")
489 chmodscip-shell system . . chmod. 644 "$log_file"
490 echoscip-shell system . . echo. "Connect to (log in $log_file):"
491 $cgi --dir_snapshots=/tmp --dir_cfg=/tmp --dir_machines="$DIR" --machine="$VM" --oneshot=1 --service_max_mem_fraction_nb_per_user=0.8 --service_max_cpu_fraction_nb_per_user=0.8 --snapshot_alloc_disk=30 $@ 2>&1 | teescip-shell system . . tee. $log_file | awkscip-shell system . . awk. '/URL:/ { print $4 " " $5 }scip-shell filesystem . . `/URL:/ { print $4 " " $5 }`.'
492 echoscip-shell system . . echo. "Cleaning remaining sessions..."
493 $cgi --service_purge=1 --dir_snapshots=/tmp --dir_cfg=/tmp 2>&1 | $GREP -i "stop"
494 echoscip-shell system . . echo. "Done $VM"
495 exit 0
496 ;;
497 # ------------------------------------------------------------------------------
498 --version|version|-v)
499 # show qemu-web-desktop version.
500
501 $cgi -v
502 exit 0
503 ;;
504 esac
505
506 # require root for further actions (except Darwin/macos using brew)
507 if [[ "$DIST" == "Darwin" ]]; then
508 if [[ $(id -u) -eq 0 ]]; then echoscip-shell system . . echo. "$p $1: Please do NOT run as root (using brew)."; exit 1; fi
509 else
510 if [[ $(id -u) -ne 0 ]]; then echoscip-shell system . . echo. "$p $1: Please run as root."; exit 1; fi
511 fi
512
513 case "$1" in
514
515 # ------------------------------------------------------------------------------
516 --disable|disable|remove)
517 # remove (comment) a VM entry.
518 VM="$2"
519 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
520 if [scip-shell system . . `[`. "$VM" = "" ]; then
521 echoscip-shell system . . echo. "ERROR: usage: $p disable VM|one-shot|gpu" >&2
522 exit 1
523 fi
524 # handle index_html enable/disable
525 case "$VM" in
526 one-shot)
527 toggle_sectionscip-shell shell shell-project 0.0.0 toggle_section(). "disable" "one-shot session checkbox"
528 exit
529 ;;
530 gpu)
531 $0 --gpu-unlock
532 exit
533 ;;
534 esac
535 # search for existing entry
536 files=("$machine_conf" "$conf_d"/*.ini "$conf_d"/*.conf)
537 for F in "${files[@]}"; do
538 [scip-shell system . . `[`. -f "$F" ] || continue
539 echoscip-shell system . . echo. "$p: Reading $F"
540 b=$(basenamescip-shell system . . basename. "$VM")
541 v=
542 if $GREP -q "\[$b\]" "$F"; then
543 v=$b
544 elif $GREP -q "\[$VM\]" "$F"; then
545 v=$VM
546 fi
547 if [scip-shell system . . `[`. ! "$v" = "" ]; then
548 echoscip-shell system . . echo. "$p: Comment [$v] in $F"
549 tmpfile=$(mktempscip-shell system . . mktemp. -t qwd_comment.XXXXXX)
550 chmodscip-shell system . . chmod. 644 "$tmpfile"
551 awkscip-shell system . . awk. -v vm="$v" '
552 $0 == "[" vm "]" { sub(/^/, ";"); in_section=1 }
553 in_section && /^$/ { in_section=0 }
554 in_section { sub(/^/, ";") }
555 { print }
556 ' "$F" > "$tmpfile"
557 mvscip-shell system . . mv. "$tmpfile" "$F" || { echoscip-shell system . . echo. "ERROR: Failed to move $tmpfile" >&2; exit 1; }
558 fi # found
559 done
560 $p download
561 ;;
562
563 # ------------------------------------------------------------------------------
564 --download|--update|download|update)
565 # scan the machines.conf + INI file for [name.ext] and download them when URL are given.
566
567 mkdirscip-shell system . . mkdir. -p $qwdprefix/machines
568
569 for F in "$machine_conf" "$conf_d/*.ini" "$conf_d/*.conf"; do
570 if [scip-shell system . . `[`. ! -f "$F" ]; then continue; fi
571 sections=$(ini_sectionsscip-shell shell shell-project 0.0.0 ini_sections(). "$F")
572 for i in $sections ; do
573 b=$(basenamescip-shell system . . basename. "$i")
574 if [scip-shell system . . `[`. -f "$i" ]; then
575 if [scip-shell system . . `[`. ! -f "$qwdprefix/machines/downloads/$b" ] && [scip-shell system . . `[`. ! -f "$qwdprefix/machines/$b" ]; then
576 # copy local VM file
577 echoscip-shell system . . echo. "$p: Copy $i into $qwdprefix/machines/downloads/$b"
578 mkdirscip-shell system . . mkdir. -p "$qwdprefix/machines/downloads/$b"
579 cpscip-shell system . . cp. "$i" $qwdprefix/machines/downloads/$b/ || { echoscip-shell system . . echo. "ERROR: Failed to copy $i" >&2; exit 1; }
580 i="$b"
581 fi
582 elif [[ "$i" =~ ^"http" ]]; then
583 # identify distant VM given as "name.ext"
584 u="$i"
585 i=$(basenamescip-shell system . . basename. "$u")
586 else
587 u=$(ini_getscip-shell shell shell-project 0.0.0 ini_get(). "$F" "$i" url)
588 fi
589 mkdirscip-shell system . . mkdir. -p $qwdprefix/machines/downloads/$i
590 # download http stuff
591 if [scip-shell system . . `[`. "$u" ] ; then
592 local_file="$qwdprefix/machines/downloads/$i/$(basenamescip-shell system . . basename. "$u")"
593 if [scip-shell system . . `[`. ! -f "$local_file" ]; then
594 echoscip-shell system . . echo. "$p: Download $u"
595 (cd $qwdprefix/machines/downloads/$i; wget --timeout=30 --tries=3 --no-check-certificate --no-cookies -N -- "$u")
596 fi
597 fi
598 # create link if relevant
599 vm=$(lsscip-shell system . . ls. -t $qwdprefix/machines/downloads/$i/* 2>/dev/nullscip-shell filesystem . . `/dev/null`. | headscip-shell system . . head. -1)
600 if [scip-shell system . . `[`. -e "$vm" ] ; then
601 lnscip-shell system . . ln. -sf -- "$vm" "$qwdprefix/machines/$i"
602 fi
603 done
604 done
605 $p refresh
606 ;;
607
608 # ------------------------------------------------------------------------------
609 --edit|edit)
610 # edit the VM/machine list, the service configuration file or the service web page.
611
612 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
613 # check if arg is a VM file, then find its config.
614 FILE=
615 for F in "$machine_conf" "$conf_d/*.ini" "$conf_d/*.conf"; do
616 [scip-shell system . . `[`. -f "$F" ] || continue
617 if $GREP -q "\[$2\]" "$F"; then
618 FILE="$F"
619 echoscip-shell system . . echo. "$p: Found [$2] in $F"
620 break
621 fi
622 done
623
624 case "$2" in
625 machine|machines|vm|list)
626 FILE=$machine_conf
627 echoscip-shell system . . echo. "$p: Edit machines: WARNING: rather use '$p enable|disable VM'"
628 ;;
629 landing|web|index|html)
630 FILE=$index_html
631 ;;
632 config|service)
633 echoscip-shell system . . echo. "$p: Edit config: WARNING: rather edit local files in $conf_d"
634 FILE=$config_pl
635 ;;
636 esac
637 [scip-shell system . . `[`. -f "$FILE" -o ! -z "$FILE" ] || { echoscip-shell system . . echo. "$p: ERROR: Nothing to edit." >&2; exit 1; }
638 echoscip-shell system . . echo. "$p: Edit $FILE"
639 "${EDITOR:-nano}" "$FILE"
640 # trigger 'download' command
641 $p download
642 ;;
643
644 # ------------------------------------------------------------------------------
645 --enable|enable|add)
646 # add/enable/uncomment a VM as a config.d/ini file.
647 VM="$2"
648 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
649 if [scip-shell system . . `[`. "$VM" = "" ]; then
650 echoscip-shell system . . echo. "ERROR: usage: $p enable VMNAME|FILE|URL|one-shot|gpu {'DESCRIPTION'}" >&2
651 exit 1
652 fi
653 if [[ ! "$VM" =~ ^[a-zA-Z0-9._-]+$ ]]; then
654 echoscip-shell system . . echo. "ERROR: $p: Invalid VM name" >&2
655 exit 1
656 fi
657 # handle index_html enable/disable
658 case "$VM" in
659 one-shot)
660 toggle_sectionscip-shell shell shell-project 0.0.0 toggle_section(). "enable" "one-shot session checkbox"
661 exit
662 ;;
663 gpu)
664 $0 --gpu
665 exit
666 ;;
667 esac
668 DESCR="$3"
669 b=$(basenamescip-shell system . . basename. "$VM")
670 if [scip-shell system . . `[`. "$DESCR" = "" ]; then
671 DESCR="$b"
672 fi
673 FILE=
674
675 # search for existing entry
676 files=("$machine_conf" "$conf_d"/*.ini "$conf_d"/*.conf)
677 for F in "${files[@]}"; do
678 [scip-shell system . . `[`. -f "$F" ] || continue
679 if $GREP -q "\[$VM\]" "$F"; then
680 # uncomment found $VM in $FILE
681 FILE="$F"
682 echoscip-shell system . . echo. "$p: Uncomment [$VM] in $FILE"
683 tmpfile=$(mktempscip-shell system . . mktemp. -t qwd_uncomment.XXXXXX)
684 chmodscip-shell system . . chmod. 644 $tmpfile
685 awkscip-shell system . . awk. -v vm="$VM" '
686 $0 ~ ";\\[" vm "\\]" { gsub(/^;+/, ""); in_section=1 }
687 in_section && /^$/ { in_section=0 }
688 in_section && /^;/ { sub(/^;/, "") }
689 { print }
690 ' "$FILE" > "$tmpfile"
691 mvscip-shell system . . mv. "$tmpfile" "$FILE" || { echoscip-shell system . . echo. "ERROR: Failed to move $tmpfile" >&2; exit 1; }
692 fi # found
693 done
694
695 u= # remote URL if relevant
696 if [scip-shell system . . `[`. -z "$FILE" ]; then
697 # new file if does not exist
698 FILE="$conf_d/$b.ini"
699 # check for remote URL
700 if [[ "$VM" =~ ^"http" ]]; then
701 # identify distant VM given as "name.ext"
702 u="$VM"
703 VM=$(basenamescip-shell system . . basename. "$u")
704 elif [scip-shell system . . `[`. -f "$VM" ]; then
705 # copy local VM file
706 echoscip-shell system . . echo. "$p: Copy $VM into $qwdprefix/machines/"
707 cpscip-shell system . . cp. -- "$VM" "$qwdprefix/machines/" || { echoscip-shell system . . echo. "ERROR: Failed to copy $VM" >&2; exit 1; }
708 VM=$(basenamescip-shell system . . basename. "$VM")
709 fi
710 echoscip-shell system . . echo. "$p: Create $FILE"
711 ddscip-shell system . . dd. status=none of=${FILE} << EOF
712 ; $FILE
713 ; This file lists virtual machines to be inserted in the qemu-web-desktop login page.
714 ;
715 ; Supported virtual machine formats include: ISO, QCOW2, VDI, VMDK, RAW, VHD/VHDX, QED
716 ;
717 ; Each entry in the configuration file '/etc/qemu-web-desktop/machines.conf'
718 ; spans on 3 lines:
719 ; - [name.ext or FILEPATH or URL]
720 ; - url=[URL to virtual machine disk, optional]
721 ; - description=[description to be shown in the service page]
722 ;
723 ; Images listed in the configuration file without a 'url=' parameter are
724 ; expected to be downloaded by hand and installed into
725 ; '/var/lib/qemu-web-desktop/machines' by the local administrator. Then, just
726 ; specify the [name.ext] and description.
727 ;
728 ; Then run 'qwdctl download' or 'qwdctl refresh' commands.
729 [$VM]
730 description=$DESCR
731 EOF
732 if [scip-shell system . . `[`. "$u" ]; then
733 # add URL
734 echoscip-shell system . . echo. "url=$u" >> $FILE
735 fi
736 fi
737 $p download
738 ;;
739
740 # ------------------------------------------------------------------------------
741 --gpu|gpu|gpu-lock)
742 # configure GPU for pass-through. The GPU_ids are e.g. '10de:1d01'.
743 if [[ "$DIST" == "Darwin" ]]; then
744 echoscip-shell system . . echo. "$p: GPU pass-through is not supported on $DIST." >&2
745 exit 1
746 fi
747
748 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
749 GPU_ids="$2"
750
751 # get CPU model
752 CPU_type=
753 $GREP -qi "intel" /proc/cpuinfoscip-shell filesystem . . `/proc/cpuinfo`. && CPU_type=intel_iommu
754 $GREP -qi "amd" /proc/cpuinfoscip-shell filesystem . . `/proc/cpuinfo`. && CPU_type=amd_iommu
755 if [scip-shell system . . `[`. "x$CPU_type" = "x" ]; then
756 echoscip-shell system . . echo. "$p: ERROR: GPU support can currently only be configured for AMD and Intel CPUs." >&2
757 exit 1
758 fi
759
760 # list available GPUs
761 echoscip-shell system . . echo. "$p: Available GPUs. The GPU_ids are usually shown as [vendor:model]"
762 echoscip-shell system . . echo. " "
763 GPU_avail=`lspci -nnv | $GREP -i "VGA\|3d controller"`
764 echoscip-shell system . . echo. "$GPU_avail"
765 echoscip-shell system . . echo. " "
766 GPU_nb=$(echoscip-shell system . . echo. "$GPU_avail" | wcscip-shell system . . wc. -l)
767 if [scip-shell system . . `[`. "$GPU_nb" -lt "2" ]; then
768 echoscip-shell system . . echo. "$p: WARNING: you need at least 2 GPU models, and keep one for your display. PROCEEDING MAY LEAD TO NO-DISPLAY."
769 fi
770 echoscip-shell system . . echo. "$p: NOTE: Make sure to keep one GPU for your current display."
771 if [scip-shell system . . `[`. "x$GPU_ids" = "x" ]; then
772 read -p ">> Enter vendor:model ID e.g. 10de:13bc,10de:0fbc (all parts of IOMMU group. Ctrl-C to abort): " GPU_ids
773 fi
774 if [scip-shell system . . `[`. "x$GPU_ids" = "x" ]; then
775 echoscip-shell system . . echo. "Usage: $p gpu GPU_ids";
776 echoscip-shell system . . echo. " The GPU_ids should be e.g. '10de:1d01' or '10de:13bc,10de:0fbc'"
777 exit 1
778 fi
779
780 GPU_firstID=`echoscip-shell system . . echo. $GPU_ids | cutscip-shell system . . cut. -d ',' -f1`
781 echoscip-shell system . . echo. $GPU_avail | $GREP -q "$GPU_firstID" || GPU_avail=no
782 if [scip-shell system . . `[`. "x$GPU_avail" = "xno" ]; then
783 echoscip-shell system . . echo. "$p: ERROR: GPU $GPU_ids model not available." >&2
784 exit 1
785 fi
786
787 # display message and wait for confirmation
788 echoscip-shell system . . echo. "Ready to configure GPU $GPU_ids for $CPU_type (set VFIO pass-through)."
789 echoscip-shell system . . echo. "All these GPU models will be detached from the server and made usable for virtualization."
790 echoscip-shell system . . echo. "WARNING: Make sure you keep a display for your system."
791 echoscip-shell system . . echo. "The following files will be modified ($DIST):"
792 echoscip-shell system . . echo. " /etc/default/grub (GRUB_CMDLINE_LINUX_DEFAULT) update"
793 echoscip-shell system . . echo. " /etc/security/limits.conf append"
794 echoscip-shell system . . echo. " /etc/modprobe.d/vfio.conf create"
795 echoscip-shell system . . echo. " /etc/udev/rules.d/10-qemu-hw-users.rules create"
796 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
797 echoscip-shell system . . echo. " /etc/initramfs-tools/modules append"
798 echoscip-shell system . . echo. " /etc/systemd/system/apache2.service.d/override.conf create"
799 else
800 echoscip-shell system . . echo. " /etc/mkinitcpio.conf append"
801 echoscip-shell system . . echo. " /etc/systemd/system/httpd.service.d/override.conf create"
802 echoscip-shell system . . echo. " NOTE: require package 'mkinitcpio'"
803 fi
804 if [scip-shell system . . `[`. -z "$QWDCTL_YES" ]; then
805 read -p ">> Continue (y/N)?" choice
806 case "$choice" in
807 y|Y ) echoscip-shell system . . echo. "Proceeding";;
808 *) echoscip-shell system . . echo. "Aborting." && exit 1;;
809 esac
810 fi
811
812 # GRUB
813 FILE=$PREFIX/etc/default/grub
814 if $GREP -i "^GRUB_CMDLINE_LINUX_DEFAULT" "$FILE" | $GREP "_iommu"; then
815 echoscip-shell system . . echo. "$p; WARNING: $FILE seems to already contain IOMMU keyword."
816 echoscip-shell system . . echo. "$p: Please check content and/or clean above files. You may use: $p gpu_unlock"
817 catscip-shell system . . cat. $FILE
818 read -p ">> Press 'N' to ignore this step, 'y' to proceed anyway, or Ctrl-C to ABORT." choice
819 else
820 choice=y
821 fi
822 case "$choice" in
823 y|Y )
824 validate_gpu_idscip-shell shell shell-project 0.0.0 validate_gpu_id(). "$GPU_ids" || { echoscip-shell system . . echo. "ERROR: Invalid GPU ID format" >&2; exit 1; }
825 DATE=$(datescip-shell system . . date. +"%Y-%m-%d-%H-%M")
826 echoscip-shell system . . echo. "$p: Updating $FILE (old stored as $FILE.$DATE)"
827 cpscip-shell system . . cp. $FILE $FILE.$DATE || { echoscip-shell system . . echo. "ERROR: Failed to copy $FILE" >&2; exit 1; }
828 sedscip-shell system . . sed. -i "s|^GRUB_CMDLINE_LINUX_DEFAULT=|&$CPU_type=on iommu=pt vfio-pci.ids=$GPU_ids|g" "$FILE"
829 ;;
830 esac
831
832 # VFIO modules
833 FILE=$PREFIX/etc/modprobe.d/vfio.conf
834 echoscip-shell system . . echo. "$p: Creating $FILE"
835 ddscip-shell system . . dd. status=none of=${FILE} << EOF
836 # $FILE
837 options vfio-pci ids=$GPU_ids disable_vga=1
838 EOF
839
840 # Initramfs modules
841 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
842 FILE=$PREFIX/etc/initramfs-tools/modules
843 MODULES=$(catscip-shell system . . cat. << EOF
844 # $FILE for qemu-web-desktop
845 vfio
846 vfio_iommu_type1
847 vfio_pci
848 vfio_virqfd
849 vhost-netdev
850 EOF
851 )
852 else
853 FILE=$PREFIX/etc/mkinitcpio.conf
854 if ! command -v mkinitcpio 2>&1 >/dev/nullscip-shell filesystem . . `/dev/null`.; then
855 echoscip-shell system . . echo. "$p: ERROR: Missing mkinitcpio. Install it, and then retry." >&2
856 exit 1
857 fi
858 MODULES=$(catscip-shell system . . cat. << EOF
859 # $FILE for qemu-web-desktop
860 MODULES+=(vfio vfio_iommu_type1 vfio_pci)
861 HOOKS+=(modconf)
862 EOF
863 )
864 fi
865 # append only if token not found in FILE
866 echoscip-shell system . . echo. "$p: Appending $FILE"
867 if [scip-shell system . . `[`. -e "$FILE" ]; then cpscip-shell system . . cp. $FILE $FILE.$DATE || { echoscip-shell system . . echo. "ERROR: Failed to copy $FILE" >&2; exit 1; }; fi
868 # append only if token not found in FILE
869 $GREP -q "vfio_pci" "$FILE" || echoscip-shell system . . echo. "$MODULES" >> "$FILE"
870
871 # udev Rules
872 FILE=$PREFIX/etc/udev/rules.d/10-qemu-hw-users.rules
873 if [scip-shell system . . `[`. -e "$FILE" ]; then
874 echoscip-shell system . . echo. "$p: WARNING: $FILE is kept (already there)."
875 else
876 echoscip-shell system . . echo. "$p: Creating $FILE"
877 ddscip-shell system . . dd. status=none of=${FILE} << EOF
878 # $FILE for qemu-web-desktop
879 SUBSYSTEM=="vfio", OWNER="root", GROUP="kvm"
880 EOF
881 fi
882
883 # limits: append at end
884 FILE=$PREFIX/etc/security/limits.conf
885 echoscip-shell system . . echo. "$p: Appending $FILE"
886 LIMITS=$(catscip-shell system . . cat. << EOF
887 # $FILE for qemu-web-desktop
888 * soft memlock 20000000
889 * hard memlock 20000000
890 @kvm soft memlock unlimited
891 @kvm hard memlock unlimited
892 EOF
893 )
894 # append only if token not found in FILE
895 $GREP -q "kvm soft memlock unlimited" "$FILE" || echoscip-shell system . . echo. "$LIMITS" >> "$FILE"
896
897 # update apache2 mem allocation limit
898 if [scip-shell system . . `[`. -e "$PREFIX/etc/systemd/system/httpd.service" ]; then
899 DIR=$PREFIX/etc/systemd/system/httpd.service.d/
900 else
901 DIR=$PREFIX/etc/systemd/system/apache2.service.d/
902 fi
903 mkdirscip-shell system . . mkdir. -p $DIR
904 FILE=$DIR/override.conf
905 echoscip-shell system . . echo. "$p: Creating $FILE"
906 ddscip-shell system . . dd. status=none of=${FILE} << EOF
907 # $FILE for qemu-web-desktop
908 [Service]
909 LimitMEMLOCK=infinity
910 EOF
911
912 echoscip-shell system . . echo. "$p: Updating GRUB boot, kernel modules, rules"
913 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
914 update-initramfs -u
915 update-grub
916 else
917 mkinitcpio -p linux
918 grub-mkconfig -o /boot/grub/grub.cfgscip-shell filesystem . . `/boot/grub/grub.cfg`. || echoscip-shell system . . echo. "WARNING: Failed to update GRUB"
919 fi
920 udevadm control --reload-rules
921 udevadm trigger
922 systemctl daemon-reload
923
924 # request to uncomment GPU section in index.html
925 toggle_sectionscip-shell shell shell-project 0.0.0 toggle_section(). "enable" "GPU checkbox"
926 echoscip-shell system . . echo. "-----------------------------------------------------------------"
927 echoscip-shell system . . echo. "Check the above files, as well as:"
928 echoscip-shell system . . echo. " $index_html"
929 echoscip-shell system . . echo. "To release the GPU back to the server use: '$p gpu_unlock'"
930 echoscip-shell system . . echo. "Now please reboot to activate changes."
931 echoscip-shell system . . echo. "-----------------------------------------------------------------"
932 ;;
933
934 # ------------------------------------------------------------------------------
935 --gpu-unlock|gpu-unlock)
936 # unlock/re-attach all GPU's to server (uninstall pass-through).
937 if [[ "$DIST" == "Darwin" ]]; then
938 echoscip-shell system . . echo. "$p: GPU pass-through is not supported on $DIST." >&2
939 exit 1
940 fi
941
942 echoscip-shell system . . echo. "$p: Cleaning existing GPU virtualization (remove VFIO pass-through)."
943 echoscip-shell system . . echo. "All detached/locked GPUs will be returned to the server."
944 echoscip-shell system . . echo. "The following files will be modified:"
945 echoscip-shell system . . echo. " /etc/default/grub (GRUB_CMDLINE_LINUX_DEFAULT) update"
946 echoscip-shell system . . echo. " /etc/security/limits.conf kept"
947 echoscip-shell system . . echo. " /etc/modprobe.d/vfio.conf remove"
948 echoscip-shell system . . echo. " /etc/udev/rules.d/10-qemu-hw-users.rules remove"
949 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
950 echoscip-shell system . . echo. " /etc/initramfs-tools/modules update"
951 echoscip-shell system . . echo. " /etc/systemd/system/apache2.service.d/override.conf remove"
952 else
953 echoscip-shell system . . echo. " /etc/mkinitcpio.conf update"
954 echoscip-shell system . . echo. " /etc/systemd/system/httpd.service.d/override.conf remove"
955 echoscip-shell system . . echo. " NOTE: require package 'mkinitcpio'"
956 fi
957 if [scip-shell system . . `[`. -z "$QWDCTL_YES" ]; then
958 read -p ">> Continue (y/N)?" choice
959 case "$choice" in
960 y|Y ) echoscip-shell system . . echo. "Proceeding";;
961 *) echoscip-shell system . . echo. "Aborting." && exit 1;;
962 esac
963 fi
964
965 # restore default GRUB options
966 FILE=$PREFIX/etc/default/grub
967 DATE=$(datescip-shell system . . date. +"%Y-%m-%d-%H-%M")
968 echoscip-shell system . . echo. "$p: Updating $FILE (old stored as $FILE.$DATE)"
969 cpscip-shell system . . cp. $FILE $FILE.$DATE || { echoscip-shell system . . echo. "ERROR: Failed to copy $FILE" >&2; exit 1; }
970 sedscip-shell system . . sed. -i "s/^GRUB_CMDLINE_LINUX_DEFAULT/#GRUB_CMDLINE_LINUX_DEFAULT/g" $FILE
971 echoscip-shell system . . echo. "GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash\"" >> $FILE
972
973 # comment modules
974 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
975 FILE=$PREFIX/etc/initramfs-tools/modules
976 else
977 if ! command -v mkinitcpio 2>&1 >/dev/nullscip-shell filesystem . . `/dev/null`.; then
978 echoscip-shell system . . echo. "$p: ERROR: Missing mkinitcpio to proceed. Install it, and then retry." >&2
979 exit 1
980 fi
981 FILE=$PREFIX/etc/mkinitcpio.conf
982 fi
983 if [scip-shell system . . `[`. -e "$FILE" ]; then
984 echoscip-shell system . . echo. "$p: Updating $FILE (old stored as $FILE.$DATE)"
985 cpscip-shell system . . cp. $FILE $FILE.$DATE || { echoscip-shell system . . echo. "ERROR: Failed to copy $FILE" >&2; exit 1; }
986 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
987 sedscip-shell system . . sed. -i "s/^vfio/#vfio/g" $FILE
988 sedscip-shell system . . sed. -i "s/^vhost-netdev/#vhost-netdev/g" $FILE
989 else
990 sedscip-shell system . . sed. -i "s/^MODULES+=/#MODULES+=/g" $FILE
991 sedscip-shell system . . sed. -i "s/^HOOKS+=/#HOOKS+=/g" $FILE
992 fi
993 fi
994
995 rmscip-shell system . . rm. -f $PREFIX/etc/modprobe.d/vfio.conf
996 rmscip-shell system . . rm. -f $PREFIX/etc/udev/rules.d/10-qemu-hw-users.rules
997 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
998 rmscip-shell system . . rm. -f $PREFIX/etc/systemd/system/apache2.service.d/override.conf
999 else
1000 rmscip-shell system . . rm. -f $PREFIX/etc/systemd/system/httpd.service.d/override.conf
1001 fi
1002
1003 if [scip-shell system . . `[`. "x$DIST" = "xDebian" ]; then
1004 update-initramfs -u
1005 update-grub
1006 else
1007 mkinitcpio -p linux
1008 grub-mkconfig -o $PREFIX/boot/grub/grub.cfg || echoscip-shell system . . echo. "$p: WARNING: Failed to update GRUB"
1009 fi
1010 udevadm control --reload-rules
1011 udevadm trigger
1012 systemctl daemon-reload
1013
1014 # request to comment GPU section in index.html
1015 toggle_sectionscip-shell shell shell-project 0.0.0 toggle_section(). "disable" "GPU checkbox"
1016 echoscip-shell system . . echo. "-----------------------------------------------------------------"
1017 echoscip-shell system . . echo. "Check the above files, as well as:"
1018 echoscip-shell system . . echo. " $index_html"
1019 echoscip-shell system . . echo. "You may use '$p edit web' to do so."
1020 echoscip-shell system . . echo. "Now please reboot to activate changes."
1021 echoscip-shell system . . echo. "-----------------------------------------------------------------"
1022 ;;
1023
1024
1025 # ------------------------------------------------------------------------------
1026 --refresh|refresh)
1027 # scan the machines.conf file, and generate the machines.html that lists
1028 # available images to show in the qemu-web-desktop main form.
1029
1030 mkdirscip-shell system . . mkdir. -p $qwdprefix/snapshots
1031 if [[ "$DIST" != "Darwin" ]] && getentscip-shell system . . getent. passwd _qemu-web-desktop > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
1032 chownscip-shell system . . chown. _qemu-web-desktop $qwdprefix/snapshots # when using MPM ITK
1033 fi
1034 mkdirscip-shell system . . mkdir. -p $qwdprefix/machines
1035 cd $qwdprefix/machines
1036 echoscip-shell system . . echo. "$p: $1 -> $machine_html"
1037
1038 # list of machines
1039 t=$(mktempscip-shell system . . mktemp. -t "machine_html.XXXXXX")
1040 has_vm=no
1041 chmodscip-shell system . . chmod. 644 "$t"
1042 chmodscip-shell system . . chmod. 755 "$qwdprefix"
1043 files=("$machine_conf" "$conf_d"/*.ini "$conf_d"/*.conf)
1044 for F in "${files[@]}"; do
1045 if [scip-shell system . . `[`. ! -f "$F" ]; then continue; fi
1046 echoscip-shell system . . echo. "$p: Reading $F"
1047 sections=$(ini_sectionsscip-shell shell shell-project 0.0.0 ini_sections(). "$F")
1048 for i in $sections; do
1049 d=$(ini_getscip-shell shell shell-project 0.0.0 ini_get(). "$F" "$i" description)
1050 if [scip-shell system . . `[`. "x$d" = "x" ]; then # when no description is given, use file name
1051 d="$i"
1052 fi
1053 u="$i"
1054 i=$(basenamescip-shell system . . basename. "$i")
1055 if [scip-shell system . . `[`. -e "$i" ] ; then
1056 if [scip-shell system . . `[`. "$d" ]; then
1057 # add entry when VM file and descr are given
1058 if [scip-shell system . . `[`. "$i" = "$u" ]; then
1059 echoscip-shell system . . echo. "Found $i '$d'"
1060 else
1061 echoscip-shell system . . echo. "Found $i '$d' (from $u)"
1062 fi
1063 i_escaped=$(escape_htmlscip-shell shell shell-project 0.0.0 escape_html(). "$i")
1064 d_escaped=$(escape_htmlscip-shell shell shell-project 0.0.0 escape_html(). "$d")
1065 echoscip-shell system . . echo. "<option value='$i_escaped'>$d_escaped</option>" >> "$t"
1066 has_vm=yes
1067 fi
1068 else
1069 echoscip-shell system . . echo. "$p: WARNING: $F [$i] is not available."
1070 fi
1071 done
1072 done
1073 if [scip-shell system . . `[`. "$has_vm" = no ]; then
1074 echoscip-shell system . . echo. "$p: WARNING: No VM is configured. Use 'qwdctl enable VM|FILE|URL' to add entries."
1075 echoscip-shell system . . echo. '<option value="monitor">[ No VM has been configured. Use "qwdctl enable VM" as admin ]</option>' > $t
1076 fi
1077 mvscip-shell system . . mv. "$t" "$machine_html" || { echoscip-shell system . . echo. "ERROR: Failed to move $t" >&2; exit 1; }
1078 # handle case when apache mod_include does not work...
1079 if [[ "$DIST" == "Darwin" ]]; then
1080 QWDCTL_INSERT=yes # module include does not work on apache/macos
1081 fi
1082 if [scip-shell system . . `[`. "x$QWDCTL_INSERT" = "xyes" ]; then
1083 echoscip-shell system . . echo. "$p: Updating $index_html"
1084 t=$(mktempscip-shell system . . mktemp. -t "index_html.XXXXXX")
1085 chmodscip-shell system . . chmod. 644 "$t"
1086 # Use awk to find and replace content between markers
1087 replacement=$(catscip-shell system . . cat. "$machine_html")
1088 awkscip-shell system . . awk. -v start="<!--BEGIN_MACHINE_LIST-->" \
1089 -v end="<!--END_MACHINE_LIST-->" \
1090 -v replacement="$replacement" '
1091 BEGIN { in_block = 0 }
1092
1093 # Check for the START marker
1094 $0 == start {
1095 in_block = 1
1096 print start # Print the start marker
1097 print replacement
1098 replacement = ""
1099 next
1100 }
1101
1102 # Check for the END marker
1103 $0 == end {
1104 in_block = 0
1105 print end # Print the end marker
1106 next
1107 }
1108
1109 # If inside the block, print the replacement content and skip original line
1110 in_block {
1111 # the replqcement has already been printed after START
1112 next # Skip printing the original content
1113 }
1114
1115 # If not in the block, print the line normally
1116 !in_block {
1117 print $0
1118 }
1119 ' "$index_html" > "$t"
1120 mvscip-shell system . . mv. "$t" "$index_html" || { echoscip-shell system . . echo. "ERROR: Failed to move $t" >&2; exit 1; }
1121 chmodscip-shell system . . chmod. a+r "$index_html"
1122 fi
1123 ;;
1124
1125 # ------------------------------------------------------------------------------
1126 --stop|stop)
1127 # stop sessions matching TOKEN. Some snapshot files may be left-over.
1128
1129 if [scip-shell system . . `[`. "x$2" = "x" ]; then
1130 echoscip-shell system . . echo. "$p: Stopping all local VMs"
1131 $cgi --service_purge=1 --dir_snapshots=/tmp --dir_cfg=/tmp 2>&1 | $GREP -i "stop"
1132 exit 0
1133 fi
1134 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
1135 t=$(psscip-shell system . . ps. aux | $GREP -F qemu-web-desktop | $GREP -F -- "$2")
1136 name=$(echoscip-shell system . . echo. "$t" | $GREP -oP '(?<=\-name )[^ ]*' )
1137 pid=$( echoscip-shell system . . echo. "$t" | awkscip-shell system . . awk. '{ print $2 }' )
1138 [[ -n "$pid" ]] || { echoscip-shell system . . echo. "$p: ERROR: No matching process found" >&2; exit 1; }
1139 table=$(printfscip-shell system . . printf. '%s\n' "$name" | prscip-shell system . . pr. -1 -Ts'\t')
1140 u=$(echoscip-shell system . . echo. "$table" | uniqscip-shell system . . uniq. )
1141 echoscip-shell system . . echo. "$p: Stopping:"
1142 echoscip-shell system . . echo. "$u"
1143 killscip-shell system . . kill. -- "$pid"
1144 ;;
1145
1146 # ------------------------------------------------------------------------------
1147 --purge|purge)
1148 # clean unused and disabled VM files.
1149 VM="$2" # optional name of VM file to match
1150 validate_inputscip-shell shell shell-project 0.0.0 validate_input(). "$2" || exit 1
1151 # scan VM files in $qwdprefix/machines
1152 for F in "$qwdprefix/machines"/*; do
1153 b=$(basenamescip-shell system . . basename. "$F")
1154 if [scip-shell system . . `[`. ! -f "$F" ]; then continue; fi
1155 # when VM is given, we must only check when F == VM
1156 if [[ -n "$VM" && ! "$b" == "$VM"* ]]; then
1157 continue
1158 fi
1159 # continue $F is currently running
1160 if [[ "$DIST" != "Darwin" ]]; then
1161 t=$(psscip-shell system . . ps. -u _qemu-web-desktop -o comm= | $GREP -qF -- "$b")
1162 else
1163 t=$(psscip-shell system . . ps. auxw | $GREP -qF -- "$b")
1164 fi
1165 if [scip-shell system . . `[`. "$t" ]; then
1166 echoscip-shell system . . echo. "$p: VM $b is running as: $t"
1167 continue
1168 fi
1169 # or $F if is active (not commented) in machine conf
1170 FOUND=no
1171 for C in $machine_conf $conf_d/*.ini $conf_d/*.conf; do
1172 if [scip-shell system . . `[`. ! -f "$C" ]; then continue; fi
1173 l=$($GREP -v '^;' $C)
1174 if [[ "$l" == *"$b"* ]];then
1175 FOUND=yes
1176 echoscip-shell system . . echo. "$p: VM $b is enabled in $C"
1177 continue
1178 fi
1179 done
1180 if [scip-shell system . . `[`. "$FOUND" = "yes" ]; then
1181 continue
1182 fi
1183 # now remove $F
1184 if [scip-shell system . . `[`. -z "$QWDCTL_YES" ]; then
1185 read -p ">> Remove inactive $F (y/N)?" choice
1186 case "$choice" in
1187 y|Y ) echoscip-shell system . . echo. "Proceeding";;
1188 *) echoscip-shell system . . echo. "Keeping." && continue;;
1189 esac
1190 fi
1191 rmscip-shell system . . rm. -f -- "$F"
1192 done
1193 ;;
1194
1195 # ------------------------------------------------------------------------------
1196 *)
1197
1198 echoscip-shell system . . echo. "Usage: $p [help|download|refresh|status|start VM|stop|gpu|gpu-unlock|enable VM|disable VM|purge|edit] ..."
1199 exit 0
1200 esac
1201
1202 # ------------------------------------------------------------------------------
1203