| 1 | #!/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 "$(uname -s)" in |
| 25 | Darwin) DIST=Darwin ;; # macOS |
| 26 | Linux) |
| 27 | if [ -e "$PREFIX/etc/initramfs-tools/modules" ]; then |
| 28 | DIST=Debian |
| 29 | else |
| 30 | DIST=Arch |
| 31 | fi |
| 32 | ;; |
| 33 | *) DIST=Unknown |
| 34 | echo "WARNING: Unsupported system" |
| 35 | ;; |
| 36 | esac |
| 37 | |
| 38 | bin=$(dirname $0) |
| 39 | p=$(basename $0) |
| 40 | |
| 41 | if command -v brew 2>&1 >/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/null || { 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 | 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 [ ! -f "$F" ]; then |
| 67 | 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 [ -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 [ ! -f "$F" ]; then |
| 76 | 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 [ ! -d "$F" ]; then |
| 95 | 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 [ -f "$bin/../config.pl" ]; then |
| 101 | # using local configuration |
| 102 | echo "$bin/../config.pl" |
| 103 | config_pl=$(realpath "$bin/../config.pl") |
| 104 | else |
| 105 | F="$PREFIX/etc/qemu-web-desktop/config.pl" |
| 106 | config_pl="$F" |
| 107 | if [ ! -f "$F" ]; then |
| 108 | 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 [ -x "$bin/../cgi-bin/qemu-web-desktop.pl" ]; then |
| 114 | cgi=$(realpath "$bin/../cgi-bin/qemu-web-desktop.pl") |
| 115 | elif [ -x "$cgi/qemu-web-desktop.pl" ]; then |
| 116 | # local (git) |
| 117 | cgi="$cgi/qemu-web-desktop.pl" |
| 118 | elif [ -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 [ -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_html() { |
| 134 | local s="$1" |
| 135 | s="${s//&/&}" |
| 136 | s="${s//</<}" |
| 137 | s="${s//>/>}" |
| 138 | s="${s//\"/"}" #' |
| 139 | s="${s//\'/'}" |
| 140 | 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_sections() { |
| 149 | local f="$1" |
| 150 | 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_get() { |
| 167 | local f="$1" section="$2" key="$3" |
| 168 | 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_status() { |
| 202 | |
| 203 | echo "$p status" |
| 204 | echo " config: $config_pl" |
| 205 | echo " $conf_d/" |
| 206 | echo " machines: $machine_conf" |
| 207 | echo " $machine_html" |
| 208 | echo " $qwdprefix/machines/" |
| 209 | echo " landing: $index_html" |
| 210 | if [[ "$DIST" == "Darwin" ]]; then |
| 211 | ip_address=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -1) |
| 212 | else |
| 213 | ip_address=$(hostname -I | awk '{print $1}') |
| 214 | fi |
| 215 | echo " service: http://$ip_address/qemu-web-desktop/" |
| 216 | 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 | [ -f "$F" ] || continue |
| 224 | # Extract active sections |
| 225 | while IFS= read -r section; do |
| 226 | active_sections+=("$section") |
| 227 | done < <(grep -E '^\[[^]]+\]' "$F" | sed -E 's/^\[([^]]+)\]/\1/') |
| 228 | |
| 229 | # Extract inactive sections |
| 230 | while IFS= read -r section; do |
| 231 | inactive_sections+=("$section") |
| 232 | done < <(grep -E '^;\[[^]]+\]' "$F" | sed -E 's/^;\[([^]]+)\]/\1/') |
| 233 | done |
| 234 | |
| 235 | if [ -z "${active_sections[*]}" ]; then |
| 236 | echo "Enabled machines (use '$p enable VM|FILE|URL' to add one):" |
| 237 | echo " None" |
| 238 | else |
| 239 | echo "Enabled machines (disable with '$p disable VM'):" |
| 240 | printf ' %s\n' "${active_sections[@]}" |
| 241 | fi |
| 242 | echo |
| 243 | if [ ! -z "${inactive_sections[*]}" ]; then |
| 244 | echo "Disabled machines (enable with '$p enable VM')" |
| 245 | printf ' %s\n' "${inactive_sections[@]}" |
| 246 | echo |
| 247 | fi |
| 248 | local orphan=0 |
| 249 | echo "Orphan machine files (not enabled/disabled, purge with '$p purge VM'):" |
| 250 | for file in "$qwdprefix/machines"/*; do |
| 251 | [ -f "$file" ] || continue |
| 252 | filename=$(basename "$file") |
| 253 | # Check if filename is not in active or inactive lists |
| 254 | if ! printf '%s\n' "${active_sections[@]}" "${inactive_sections[@]}" | grep -q "^${filename}$"; then |
| 255 | orphan=1 |
| 256 | echo " $filename" |
| 257 | fi |
| 258 | done |
| 259 | if [ $orphan == 0 ]; then echo " None"; fi |
| 260 | echo " " |
| 261 | echo "Running sessions:" |
| 262 | # Header for the table |
| 263 | printf "%-8s | %5s | %-15s | %4s | %6s | %-7s | %s\n" "PID" "CPU/%" "User name" "#CPU" "MEM/GB" "GPU" "Full name" |
| 264 | printf "%s\n" "------------------------------------------------------------------------" |
| 265 | |
| 266 | # Get PIDs for qemu-system, excluding grep and sh -c processes |
| 267 | ps auxww | grep 'qemu-system' | grep -v 'grep' | grep -v 'sh -c' | while read -r line; do |
| 268 | # Extract PID and CPU usage |
| 269 | pid=$(echo "$line" | awk '{print $2}') |
| 270 | cpu_usage=$(echo "$line" | awk '{print $3"%"}') |
| 271 | |
| 272 | # Extract the full command (from field 11 onwards) |
| 273 | cmd=$(echo "$line" | awk '{for(i=11;i<=NF;i++) printf $i" "; print ""}') |
| 274 | |
| 275 | # Use GNU grep for Perl regex support |
| 276 | full_name=$(echo "$cmd" | $GREP -oP '(?<=-name ).*?(?= -|$)') |
| 277 | nb_cpu=$(echo "$cmd" | $GREP -oP '(?<=-smp ).*?(?= -|$)') |
| 278 | mem_mb=$(echo "$cmd" | $GREP -oP '(?<=-m ).*?(?= -|$)') |
| 279 | mem_gb=$(awk -v mem_mb="${mem_mb:-0}" 'BEGIN {printf "%.2f", mem_mb / 1024}') |
| 280 | user_name=$(echo "$full_name" | cut -d: -f2) |
| 281 | pci=$(echo "$cmd" | $GREP -oP '(?<=host=)[^, ]+' || echo "") |
| 282 | |
| 283 | # Print the row |
| 284 | 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_section() { |
| 291 | local action="$1" marker="$2" t=$(mktemp -t "index_html.XXXXXX") |
| 292 | chmod 644 "$t" |
| 293 | |
| 294 | 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 && mv $t "$index_html" || { echo "ERROR: Failed to move $t" >&2; exit 1; } |
| 318 | echo "$action '$marker' in $index_html" |
| 319 | } |
| 320 | |
| 321 | MAX_INPUT_LEN=256 |
| 322 | validate_input() { |
| 323 | [[ ${#1} -le $MAX_INPUT_LEN ]] || { echo "ERROR: Input too long" >&2; return 1; } |
| 324 | return 0 |
| 325 | } |
| 326 | |
| 327 | validate_gpu_id() { |
| 328 | [[ "$1" =~ ^([0-9a-fA-F]{4}:[0-9a-fA-F]{4},?)+$ ]] || return 1 |
| 329 | } |
| 330 | |
| 331 | # ------------------------------------------------------------------------------ |
| 332 | |
| 333 | if [ -z "$1" ]; then |
| 334 | 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 | echo "Usage: $p [help|download|refresh|status|start VM|stop|gpu|gpu-unlock|enable|disable|purge|edit] ..." |
| 345 | echo " " |
| 346 | echo " controls the availability of virtual machines for the " |
| 347 | echo " qemu-web-desktop." |
| 348 | echo " In addition, the status of the running sessions can be displayed" |
| 349 | echo " and it is possible to start virtual machines manually and display" |
| 350 | echo " them in a browser." |
| 351 | echo |
| 352 | echo "OPTIONS:" |
| 353 | echo " --help|help|-h" |
| 354 | echo " show this help." |
| 355 | echo |
| 356 | echo " --disable|disable VM|one-shot|gpu" |
| 357 | echo " deactivate a [VM] entry." |
| 358 | echo " '$p --disable one-shot' disables the one-shot section in $index_html." |
| 359 | echo " '$p --disable gpu' disables the GPU support (same as --gpu-unlock)." |
| 360 | echo |
| 361 | echo " --download|download|update" |
| 362 | echo " scan the $machine_conf file for [name.ext] and download them when URL are given." |
| 363 | echo " a '$p --refresh' is then performed. Virtual machine images are stored into $qwdprefix/machines." |
| 364 | echo " Additional machine descriptions can be given in $conf_d/*.ini files." |
| 365 | echo " " |
| 366 | echo " --edit|edit [machines|config|web|VM]" |
| 367 | echo " edit the machines list, the service configuration file or the service web page." |
| 368 | echo " In the case of the machines list, the '$p --download' command is triggered automatically after edit." |
| 369 | echo " When given a VM name, the file holding the [VM] entry is edited." |
| 370 | echo " Set the \$EDITOR variable to select the text editor to use." |
| 371 | echo " In case the '$p edit machines' has no effect on the index.html service landing page," |
| 372 | echo " specify set 'QWDCTL_INSERT=yes' or option --machine-insert." |
| 373 | echo " It is recommended to use the --enable and --disable to manage the VM/machine list." |
| 374 | echo |
| 375 | echo " --enable|enable VM|FILE|URL|one-shot|gpu {'DESCRIPTION'}" |
| 376 | echo " add or (re)activate a [VM] entry, file or URL. Optional description is given as a string." |
| 377 | echo " '$p --enable one-shot' activates the one-shot section in $index_html." |
| 378 | echo " '$p --enable gpu' activates the GPU support (same as --gpu)." |
| 379 | echo |
| 380 | echo " --gpu|gpu VENDOR:MODEL" |
| 381 | echo " configure GPU for pass-through. The GPU_ids are e.g. '10de:1d01'." |
| 382 | echo " " |
| 383 | echo " --gpu-unlock|gpu-unlock" |
| 384 | echo " unlock/re-attach all GPU's to server (uninstall pass-through)." |
| 385 | echo " " |
| 386 | echo " --machine-insert" |
| 387 | echo " used with --download/--refresh, force to modify the $index_html" |
| 388 | echo " landing page instead of using an include." |
| 389 | echo " Same as QWDCTL_INSERT=yes shell variable." |
| 390 | echo |
| 391 | echo " --refresh|refresh" |
| 392 | echo " scan the $machine_conf file, and generate the $machine_html that lists" |
| 393 | echo " available images to show in the qemu-web-desktop main form." |
| 394 | echo " Additional machine descriptions can be given in $conf_d/*.ini files." |
| 395 | echo " " |
| 396 | echo " --status|status" |
| 397 | echo " list running sessions, as well as enabled and disabled machines." |
| 398 | echo " " |
| 399 | echo " --start|start VM ..." |
| 400 | echo " start the given VM or ISO file (full path) in a browser. Connect to it with" |
| 401 | echo " the displayed URL. Further arguments are passed to the service (see config.pl)" |
| 402 | echo " e.g. --snapshot_alloc_mem=1 (in GB) and --snapshot_alloc_cpu=2." |
| 403 | echo " Changes are lost except when specifying option --snapshot_use_master=1," |
| 404 | echo " requiring write access. When running ISO's you may also specify" |
| 405 | echo " --snapshot_alloc_disk=40 (in GB)." |
| 406 | echo " Use '$p --start' to get the full list of default VM settings." |
| 407 | echo " " |
| 408 | echo " --stop|stop TOKEN" |
| 409 | echo " stop sessions matching TOKEN. Some snapshot files may be left-over." |
| 410 | echo " " |
| 411 | echo " --purge|purge {VMNAME}" |
| 412 | echo " remove VM files which are inactive and not used. When VM argument is " |
| 413 | echo " given, only files starting by 'VM' are checked." |
| 414 | echo |
| 415 | echo " --version|version|-v" |
| 416 | echo " show $p version." |
| 417 | echo |
| 418 | echo " --yes|-y" |
| 419 | echo " assume yes to questions. Same as QWDCTL_YES=1 shell variable." |
| 420 | echo |
| 421 | echo "AUTHOR:" |
| 422 | echo " Written by Roland Mas and Emmanuel Farhi." |
| 423 | echo |
| 424 | echo "FILES:" |
| 425 | echo " The QWD service configuration is defined in:" |
| 426 | echo " - $config_pl" |
| 427 | echo " - $conf_d/*.pl" |
| 428 | echo " " |
| 429 | echo " The QWD available VM's are defined in:" |
| 430 | echo " - $qwdprefix/machines" |
| 431 | echo " - $machine_conf" |
| 432 | echo " - $conf_d/*.ini" |
| 433 | echo " Entries should contain lines:" |
| 434 | echo " [name.ext or URL]" |
| 435 | echo " description=<name of machine to appear in the form>" |
| 436 | echo " In addition, any line with:" |
| 437 | echo " url=<link>" |
| 438 | echo " will retrieve the given file with: $p download" |
| 439 | echo " When the description is missing, the VM file name is used." |
| 440 | echo " Supported virtual machine formats: ISO, QCOW2, VDI, VMDK, RAW, VHD/VHDX, QED" |
| 441 | echo " " |
| 442 | echo " The QWD landing/login page is in:" |
| 443 | echo " - $index_html" |
| 444 | echo " - $machine_html" |
| 445 | echo " " |
| 446 | echo " The QWD service script is in:" |
| 447 | echo " - $cgi" |
| 448 | echo |
| 449 | echo " - https://gitlab.com/soleil-data-treatment/soleil-software-projects/qemu-web-desktop" |
| 450 | echo " " |
| 451 | echo "ENVIRONMENT VARIABLES:" |
| 452 | echo " EDITOR Set the text editor to use, e.g. nano. Default is to use 'nano' or \$EDITOR." |
| 453 | echo " QWDCTL_INSERT=yes is equivalent to setting --machine-insert." |
| 454 | echo " QWDCTL_YES=1 is equivalent to setting --yes (skip confirm)." |
| 455 | echo " " |
| 456 | |
| 457 | exit 0 |
| 458 | ;; |
| 459 | # ------------------------------------------------------------------------------ |
| 460 | --status|status) |
| 461 | 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 [ "x$2" = "x" ]; then |
| 470 | echo "Usage: $p $1 VM [options]" |
| 471 | echo "Configuration: $config_pl $conf_d/" |
| 472 | echo "Script: $cgi" |
| 473 | echo "Availabe options are:" |
| 474 | $cgi -h |
| 475 | exit 0 |
| 476 | fi |
| 477 | validate_input "$2" || exit 1 |
| 478 | DIR=$(dirname "$2") |
| 479 | DIR=$(realpath "$DIR") |
| 480 | VM=$(basename "$2") |
| 481 | shift 2 |
| 482 | if [ ! -f "$DIR/$VM" -a -f "$qwdprefix/machines/$VM" ]; then |
| 483 | DIR=$qwdprefix/machines |
| 484 | fi |
| 485 | |
| 486 | # snapshot_alloc_cpu snapshot_alloc_mem |
| 487 | echo "$p: Launching $DIR/$VM $@" |
| 488 | log_file=$(mktemp -t "qwdctl_${VM}_XXXXXX.log") |
| 489 | chmod 644 "$log_file" |
| 490 | 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 | tee $log_file | awk '/URL:/ { print $4 " " $5 }' |
| 492 | echo "Cleaning remaining sessions..." |
| 493 | $cgi --service_purge=1 --dir_snapshots=/tmp --dir_cfg=/tmp 2>&1 | $GREP -i "stop" |
| 494 | 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 echo "$p $1: Please do NOT run as root (using brew)."; exit 1; fi |
| 509 | else |
| 510 | if [[ $(id -u) -ne 0 ]]; then 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_input "$2" || exit 1 |
| 520 | if [ "$VM" = "" ]; then |
| 521 | 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_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 | [ -f "$F" ] || continue |
| 539 | echo "$p: Reading $F" |
| 540 | b=$(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 [ ! "$v" = "" ]; then |
| 548 | echo "$p: Comment [$v] in $F" |
| 549 | tmpfile=$(mktemp -t qwd_comment.XXXXXX) |
| 550 | chmod 644 "$tmpfile" |
| 551 | 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 | mv "$tmpfile" "$F" || { 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 | mkdir -p $qwdprefix/machines |
| 568 | |
| 569 | for F in "$machine_conf" "$conf_d/*.ini" "$conf_d/*.conf"; do |
| 570 | if [ ! -f "$F" ]; then continue; fi |
| 571 | sections=$(ini_sections "$F") |
| 572 | for i in $sections ; do |
| 573 | b=$(basename "$i") |
| 574 | if [ -f "$i" ]; then |
| 575 | if [ ! -f "$qwdprefix/machines/downloads/$b" ] && [ ! -f "$qwdprefix/machines/$b" ]; then |
| 576 | # copy local VM file |
| 577 | echo "$p: Copy $i into $qwdprefix/machines/downloads/$b" |
| 578 | mkdir -p "$qwdprefix/machines/downloads/$b" |
| 579 | cp "$i" $qwdprefix/machines/downloads/$b/ || { 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=$(basename "$u") |
| 586 | else |
| 587 | u=$(ini_get "$F" "$i" url) |
| 588 | fi |
| 589 | mkdir -p $qwdprefix/machines/downloads/$i |
| 590 | # download http stuff |
| 591 | if [ "$u" ] ; then |
| 592 | local_file="$qwdprefix/machines/downloads/$i/$(basename "$u")" |
| 593 | if [ ! -f "$local_file" ]; then |
| 594 | 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=$(ls -t $qwdprefix/machines/downloads/$i/* 2>/dev/null | head -1) |
| 600 | if [ -e "$vm" ] ; then |
| 601 | 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_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 | [ -f "$F" ] || continue |
| 617 | if $GREP -q "\[$2\]" "$F"; then |
| 618 | FILE="$F" |
| 619 | 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 | 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 | echo "$p: Edit config: WARNING: rather edit local files in $conf_d" |
| 634 | FILE=$config_pl |
| 635 | ;; |
| 636 | esac |
| 637 | [ -f "$FILE" -o ! -z "$FILE" ] || { echo "$p: ERROR: Nothing to edit." >&2; exit 1; } |
| 638 | 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_input "$2" || exit 1 |
| 649 | if [ "$VM" = "" ]; then |
| 650 | 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 | 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_section "enable" "one-shot session checkbox" |
| 661 | exit |
| 662 | ;; |
| 663 | gpu) |
| 664 | $0 --gpu |
| 665 | exit |
| 666 | ;; |
| 667 | esac |
| 668 | DESCR="$3" |
| 669 | b=$(basename "$VM") |
| 670 | if [ "$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 | [ -f "$F" ] || continue |
| 679 | if $GREP -q "\[$VM\]" "$F"; then |
| 680 | # uncomment found $VM in $FILE |
| 681 | FILE="$F" |
| 682 | echo "$p: Uncomment [$VM] in $FILE" |
| 683 | tmpfile=$(mktemp -t qwd_uncomment.XXXXXX) |
| 684 | chmod 644 $tmpfile |
| 685 | 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 | mv "$tmpfile" "$FILE" || { echo "ERROR: Failed to move $tmpfile" >&2; exit 1; } |
| 692 | fi # found |
| 693 | done |
| 694 | |
| 695 | u= # remote URL if relevant |
| 696 | if [ -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=$(basename "$u") |
| 704 | elif [ -f "$VM" ]; then |
| 705 | # copy local VM file |
| 706 | echo "$p: Copy $VM into $qwdprefix/machines/" |
| 707 | cp -- "$VM" "$qwdprefix/machines/" || { echo "ERROR: Failed to copy $VM" >&2; exit 1; } |
| 708 | VM=$(basename "$VM") |
| 709 | fi |
| 710 | echo "$p: Create $FILE" |
| 711 | 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 [ "$u" ]; then |
| 733 | # add URL |
| 734 | 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 | echo "$p: GPU pass-through is not supported on $DIST." >&2 |
| 745 | exit 1 |
| 746 | fi |
| 747 | |
| 748 | validate_input "$2" || exit 1 |
| 749 | GPU_ids="$2" |
| 750 | |
| 751 | # get CPU model |
| 752 | CPU_type= |
| 753 | $GREP -qi "intel" /proc/cpuinfo && CPU_type=intel_iommu |
| 754 | $GREP -qi "amd" /proc/cpuinfo && CPU_type=amd_iommu |
| 755 | if [ "x$CPU_type" = "x" ]; then |
| 756 | 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 | echo "$p: Available GPUs. The GPU_ids are usually shown as [vendor:model]" |
| 762 | echo " " |
| 763 | GPU_avail=`lspci -nnv | $GREP -i "VGA\|3d controller"` |
| 764 | echo "$GPU_avail" |
| 765 | echo " " |
| 766 | GPU_nb=$(echo "$GPU_avail" | wc -l) |
| 767 | if [ "$GPU_nb" -lt "2" ]; then |
| 768 | 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 | echo "$p: NOTE: Make sure to keep one GPU for your current display." |
| 771 | if [ "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 [ "x$GPU_ids" = "x" ]; then |
| 775 | echo "Usage: $p gpu GPU_ids"; |
| 776 | echo " The GPU_ids should be e.g. '10de:1d01' or '10de:13bc,10de:0fbc'" |
| 777 | exit 1 |
| 778 | fi |
| 779 | |
| 780 | GPU_firstID=`echo $GPU_ids | cut -d ',' -f1` |
| 781 | echo $GPU_avail | $GREP -q "$GPU_firstID" || GPU_avail=no |
| 782 | if [ "x$GPU_avail" = "xno" ]; then |
| 783 | echo "$p: ERROR: GPU $GPU_ids model not available." >&2 |
| 784 | exit 1 |
| 785 | fi |
| 786 | |
| 787 | # display message and wait for confirmation |
| 788 | echo "Ready to configure GPU $GPU_ids for $CPU_type (set VFIO pass-through)." |
| 789 | echo "All these GPU models will be detached from the server and made usable for virtualization." |
| 790 | echo "WARNING: Make sure you keep a display for your system." |
| 791 | echo "The following files will be modified ($DIST):" |
| 792 | echo " /etc/default/grub (GRUB_CMDLINE_LINUX_DEFAULT) update" |
| 793 | echo " /etc/security/limits.conf append" |
| 794 | echo " /etc/modprobe.d/vfio.conf create" |
| 795 | echo " /etc/udev/rules.d/10-qemu-hw-users.rules create" |
| 796 | if [ "x$DIST" = "xDebian" ]; then |
| 797 | echo " /etc/initramfs-tools/modules append" |
| 798 | echo " /etc/systemd/system/apache2.service.d/override.conf create" |
| 799 | else |
| 800 | echo " /etc/mkinitcpio.conf append" |
| 801 | echo " /etc/systemd/system/httpd.service.d/override.conf create" |
| 802 | echo " NOTE: require package 'mkinitcpio'" |
| 803 | fi |
| 804 | if [ -z "$QWDCTL_YES" ]; then |
| 805 | read -p ">> Continue (y/N)?" choice |
| 806 | case "$choice" in |
| 807 | y|Y ) echo "Proceeding";; |
| 808 | *) 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 | echo "$p; WARNING: $FILE seems to already contain IOMMU keyword." |
| 816 | echo "$p: Please check content and/or clean above files. You may use: $p gpu_unlock" |
| 817 | 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_id "$GPU_ids" || { echo "ERROR: Invalid GPU ID format" >&2; exit 1; } |
| 825 | DATE=$(date +"%Y-%m-%d-%H-%M") |
| 826 | echo "$p: Updating $FILE (old stored as $FILE.$DATE)" |
| 827 | cp $FILE $FILE.$DATE || { echo "ERROR: Failed to copy $FILE" >&2; exit 1; } |
| 828 | 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 | echo "$p: Creating $FILE" |
| 835 | 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 [ "x$DIST" = "xDebian" ]; then |
| 842 | FILE=$PREFIX/etc/initramfs-tools/modules |
| 843 | MODULES=$(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/null; then |
| 855 | echo "$p: ERROR: Missing mkinitcpio. Install it, and then retry." >&2 |
| 856 | exit 1 |
| 857 | fi |
| 858 | MODULES=$(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 | echo "$p: Appending $FILE" |
| 867 | if [ -e "$FILE" ]; then cp $FILE $FILE.$DATE || { 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" || echo "$MODULES" >> "$FILE" |
| 870 | |
| 871 | # udev Rules |
| 872 | FILE=$PREFIX/etc/udev/rules.d/10-qemu-hw-users.rules |
| 873 | if [ -e "$FILE" ]; then |
| 874 | echo "$p: WARNING: $FILE is kept (already there)." |
| 875 | else |
| 876 | echo "$p: Creating $FILE" |
| 877 | 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 | echo "$p: Appending $FILE" |
| 886 | LIMITS=$(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" || echo "$LIMITS" >> "$FILE" |
| 896 | |
| 897 | # update apache2 mem allocation limit |
| 898 | if [ -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 | mkdir -p $DIR |
| 904 | FILE=$DIR/override.conf |
| 905 | echo "$p: Creating $FILE" |
| 906 | dd status=none of=${FILE} << EOF |
| 907 | # $FILE for qemu-web-desktop |
| 908 | [Service] |
| 909 | LimitMEMLOCK=infinity |
| 910 | EOF |
| 911 | |
| 912 | echo "$p: Updating GRUB boot, kernel modules, rules" |
| 913 | if [ "x$DIST" = "xDebian" ]; then |
| 914 | update-initramfs -u |
| 915 | update-grub |
| 916 | else |
| 917 | mkinitcpio -p linux |
| 918 | grub-mkconfig -o /boot/grub/grub.cfg || 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_section "enable" "GPU checkbox" |
| 926 | echo "-----------------------------------------------------------------" |
| 927 | echo "Check the above files, as well as:" |
| 928 | echo " $index_html" |
| 929 | echo "To release the GPU back to the server use: '$p gpu_unlock'" |
| 930 | echo "Now please reboot to activate changes." |
| 931 | 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 | echo "$p: GPU pass-through is not supported on $DIST." >&2 |
| 939 | exit 1 |
| 940 | fi |
| 941 | |
| 942 | echo "$p: Cleaning existing GPU virtualization (remove VFIO pass-through)." |
| 943 | echo "All detached/locked GPUs will be returned to the server." |
| 944 | echo "The following files will be modified:" |
| 945 | echo " /etc/default/grub (GRUB_CMDLINE_LINUX_DEFAULT) update" |
| 946 | echo " /etc/security/limits.conf kept" |
| 947 | echo " /etc/modprobe.d/vfio.conf remove" |
| 948 | echo " /etc/udev/rules.d/10-qemu-hw-users.rules remove" |
| 949 | if [ "x$DIST" = "xDebian" ]; then |
| 950 | echo " /etc/initramfs-tools/modules update" |
| 951 | echo " /etc/systemd/system/apache2.service.d/override.conf remove" |
| 952 | else |
| 953 | echo " /etc/mkinitcpio.conf update" |
| 954 | echo " /etc/systemd/system/httpd.service.d/override.conf remove" |
| 955 | echo " NOTE: require package 'mkinitcpio'" |
| 956 | fi |
| 957 | if [ -z "$QWDCTL_YES" ]; then |
| 958 | read -p ">> Continue (y/N)?" choice |
| 959 | case "$choice" in |
| 960 | y|Y ) echo "Proceeding";; |
| 961 | *) echo "Aborting." && exit 1;; |
| 962 | esac |
| 963 | fi |
| 964 | |
| 965 | # restore default GRUB options |
| 966 | FILE=$PREFIX/etc/default/grub |
| 967 | DATE=$(date +"%Y-%m-%d-%H-%M") |
| 968 | echo "$p: Updating $FILE (old stored as $FILE.$DATE)" |
| 969 | cp $FILE $FILE.$DATE || { echo "ERROR: Failed to copy $FILE" >&2; exit 1; } |
| 970 | sed -i "s/^GRUB_CMDLINE_LINUX_DEFAULT/#GRUB_CMDLINE_LINUX_DEFAULT/g" $FILE |
| 971 | echo "GRUB_CMDLINE_LINUX_DEFAULT=\"quiet splash\"" >> $FILE |
| 972 | |
| 973 | # comment modules |
| 974 | if [ "x$DIST" = "xDebian" ]; then |
| 975 | FILE=$PREFIX/etc/initramfs-tools/modules |
| 976 | else |
| 977 | if ! command -v mkinitcpio 2>&1 >/dev/null; then |
| 978 | 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 [ -e "$FILE" ]; then |
| 984 | echo "$p: Updating $FILE (old stored as $FILE.$DATE)" |
| 985 | cp $FILE $FILE.$DATE || { echo "ERROR: Failed to copy $FILE" >&2; exit 1; } |
| 986 | if [ "x$DIST" = "xDebian" ]; then |
| 987 | sed -i "s/^vfio/#vfio/g" $FILE |
| 988 | sed -i "s/^vhost-netdev/#vhost-netdev/g" $FILE |
| 989 | else |
| 990 | sed -i "s/^MODULES+=/#MODULES+=/g" $FILE |
| 991 | sed -i "s/^HOOKS+=/#HOOKS+=/g" $FILE |
| 992 | fi |
| 993 | fi |
| 994 | |
| 995 | rm -f $PREFIX/etc/modprobe.d/vfio.conf |
| 996 | rm -f $PREFIX/etc/udev/rules.d/10-qemu-hw-users.rules |
| 997 | if [ "x$DIST" = "xDebian" ]; then |
| 998 | rm -f $PREFIX/etc/systemd/system/apache2.service.d/override.conf |
| 999 | else |
| 1000 | rm -f $PREFIX/etc/systemd/system/httpd.service.d/override.conf |
| 1001 | fi |
| 1002 | |
| 1003 | if [ "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 || 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_section "disable" "GPU checkbox" |
| 1016 | echo "-----------------------------------------------------------------" |
| 1017 | echo "Check the above files, as well as:" |
| 1018 | echo " $index_html" |
| 1019 | echo "You may use '$p edit web' to do so." |
| 1020 | echo "Now please reboot to activate changes." |
| 1021 | 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 | mkdir -p $qwdprefix/snapshots |
| 1031 | if [[ "$DIST" != "Darwin" ]] && getent passwd _qemu-web-desktop > /dev/null 2>&1; then |
| 1032 | chown _qemu-web-desktop $qwdprefix/snapshots # when using MPM ITK |
| 1033 | fi |
| 1034 | mkdir -p $qwdprefix/machines |
| 1035 | cd $qwdprefix/machines |
| 1036 | echo "$p: $1 -> $machine_html" |
| 1037 | |
| 1038 | # list of machines |
| 1039 | t=$(mktemp -t "machine_html.XXXXXX") |
| 1040 | has_vm=no |
| 1041 | chmod 644 "$t" |
| 1042 | chmod 755 "$qwdprefix" |
| 1043 | files=("$machine_conf" "$conf_d"/*.ini "$conf_d"/*.conf) |
| 1044 | for F in "${files[@]}"; do |
| 1045 | if [ ! -f "$F" ]; then continue; fi |
| 1046 | echo "$p: Reading $F" |
| 1047 | sections=$(ini_sections "$F") |
| 1048 | for i in $sections; do |
| 1049 | d=$(ini_get "$F" "$i" description) |
| 1050 | if [ "x$d" = "x" ]; then # when no description is given, use file name |
| 1051 | d="$i" |
| 1052 | fi |
| 1053 | u="$i" |
| 1054 | i=$(basename "$i") |
| 1055 | if [ -e "$i" ] ; then |
| 1056 | if [ "$d" ]; then |
| 1057 | # add entry when VM file and descr are given |
| 1058 | if [ "$i" = "$u" ]; then |
| 1059 | echo "Found $i '$d'" |
| 1060 | else |
| 1061 | echo "Found $i '$d' (from $u)" |
| 1062 | fi |
| 1063 | i_escaped=$(escape_html "$i") |
| 1064 | d_escaped=$(escape_html "$d") |
| 1065 | echo "<option value='$i_escaped'>$d_escaped</option>" >> "$t" |
| 1066 | has_vm=yes |
| 1067 | fi |
| 1068 | else |
| 1069 | echo "$p: WARNING: $F [$i] is not available." |
| 1070 | fi |
| 1071 | done |
| 1072 | done |
| 1073 | if [ "$has_vm" = no ]; then |
| 1074 | echo "$p: WARNING: No VM is configured. Use 'qwdctl enable VM|FILE|URL' to add entries." |
| 1075 | echo '<option value="monitor">[ No VM has been configured. Use "qwdctl enable VM" as admin ]</option>' > $t |
| 1076 | fi |
| 1077 | mv "$t" "$machine_html" || { 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 [ "x$QWDCTL_INSERT" = "xyes" ]; then |
| 1083 | echo "$p: Updating $index_html" |
| 1084 | t=$(mktemp -t "index_html.XXXXXX") |
| 1085 | chmod 644 "$t" |
| 1086 | # Use awk to find and replace content between markers |
| 1087 | replacement=$(cat "$machine_html") |
| 1088 | 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 | mv "$t" "$index_html" || { echo "ERROR: Failed to move $t" >&2; exit 1; } |
| 1121 | 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 [ "x$2" = "x" ]; then |
| 1130 | 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_input "$2" || exit 1 |
| 1135 | t=$(ps aux | $GREP -F qemu-web-desktop | $GREP -F -- "$2") |
| 1136 | name=$(echo "$t" | $GREP -oP '(?<=\-name )[^ ]*' ) |
| 1137 | pid=$( echo "$t" | awk '{ print $2 }' ) |
| 1138 | [[ -n "$pid" ]] || { echo "$p: ERROR: No matching process found" >&2; exit 1; } |
| 1139 | table=$(printf '%s\n' "$name" | pr -1 -Ts'\t') |
| 1140 | u=$(echo "$table" | uniq ) |
| 1141 | echo "$p: Stopping:" |
| 1142 | echo "$u" |
| 1143 | 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_input "$2" || exit 1 |
| 1151 | # scan VM files in $qwdprefix/machines |
| 1152 | for F in "$qwdprefix/machines"/*; do |
| 1153 | b=$(basename "$F") |
| 1154 | if [ ! -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=$(ps -u _qemu-web-desktop -o comm= | $GREP -qF -- "$b") |
| 1162 | else |
| 1163 | t=$(ps auxw | $GREP -qF -- "$b") |
| 1164 | fi |
| 1165 | if [ "$t" ]; then |
| 1166 | 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 [ ! -f "$C" ]; then continue; fi |
| 1173 | l=$($GREP -v '^;' $C) |
| 1174 | if [[ "$l" == *"$b"* ]];then |
| 1175 | FOUND=yes |
| 1176 | echo "$p: VM $b is enabled in $C" |
| 1177 | continue |
| 1178 | fi |
| 1179 | done |
| 1180 | if [ "$FOUND" = "yes" ]; then |
| 1181 | continue |
| 1182 | fi |
| 1183 | # now remove $F |
| 1184 | if [ -z "$QWDCTL_YES" ]; then |
| 1185 | read -p ">> Remove inactive $F (y/N)?" choice |
| 1186 | case "$choice" in |
| 1187 | y|Y ) echo "Proceeding";; |
| 1188 | *) echo "Keeping." && continue;; |
| 1189 | esac |
| 1190 | fi |
| 1191 | rm -f -- "$F" |
| 1192 | done |
| 1193 | ;; |
| 1194 | |
| 1195 | # ------------------------------------------------------------------------------ |
| 1196 | *) |
| 1197 | |
| 1198 | 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 |