-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqemu-quickboot.sh
More file actions
804 lines (678 loc) · 32.4 KB
/
Copy pathqemu-quickboot.sh
File metadata and controls
804 lines (678 loc) · 32.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#!/bin/bash
# QEMU QuickBoot — main launcher (uses YAD for GUI dialogs)
# Requires: yad, qemu-system-x86_64 or qemu-system-aarch64, socat
# Companion script: quickboot-settings.sh (VM Session Settings panel)
#
# 2026 update — applied via review:
# * Format autodetection for primary boot source and extra disks
# * ISO selected as primary boot source now boots via -cdrom (not -drive)
# * USB controllers gated on the "Enable USB" checkbox
# * Inner restart loop so the session panel can request an IPv4 reconfig
# restart without losing the user's other settings
# Set the GTK theme to dark
export GTK_THEME=Orchis:dark
# Calculate window sizes
original_width=450
original_height=320
smaller_width=$(awk "BEGIN {printf \"%.0f\n\", $original_width * 0.5}")
smaller_height=$(awk "BEGIN {printf \"%.0f\n\", $original_height * 0.7}")
bigger_width=$(awk "BEGIN {printf \"%.0f\n\", $original_width * 1.3}")
# Icon for YAD windows
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
ICON="$SCRIPT_DIR/qemu-quickboot.png"
YAD_ICON=""
[ -f "$ICON" ] && YAD_ICON="--window-icon=$ICON"
# --- Host architecture autodetection -----------------------------------------
# Sets QEMU_BIN and QEMU_MACHINE_ARGS based on `uname -m` so the launcher
# works on both x86_64 and aarch64 hosts. Anything else is unsupported.
HOST_ARCH="$(uname -m)"
case "$HOST_ARCH" in
x86_64|amd64)
QEMU_BIN="qemu-system-x86_64"
QEMU_MACHINE_ARGS="-enable-kvm -cpu host"
;;
aarch64|arm64)
QEMU_BIN="qemu-system-aarch64"
QEMU_MACHINE_ARGS="-machine virt -enable-kvm -cpu host"
;;
*)
echo "Unsupported architecture: $HOST_ARCH" >&2
exit 1
;;
esac
export QEMU_BIN QEMU_MACHINE_ARGS
extra_disks=""
usb_hotplug_enabled=1
headless_enabled=0
# --- Detect QEMU drive format from path or block device ---
# Block devices and unknown extensions default to raw. ISOs return "iso" so
# the caller can decide whether to attach as -drive or -cdrom.
detect_format() {
local path="$1"
if [ -b "$path" ]; then echo "raw"; return; fi
local ext="${path##*.}"
case "${ext,,}" in
qcow2|qcow) echo "qcow2" ;;
vmdk) echo "vmdk" ;;
vdi) echo "vdi" ;;
vhdx) echo "vhdx" ;;
vhd|vpc) echo "vpc" ;;
iso) echo "iso" ;;
*) echo "raw" ;;
esac
}
# --- Detect OVMF (UEFI firmware) location across distros ---
# Returns the first existing path from a list of well-known locations.
# Order matters: modern Arch first, then Debian/Ubuntu, then Fedora.
detect_ovmf() {
local candidates=()
# Choose firmware candidates based on host architecture.
if [ "${HOST_ARCH:-$(uname -m)}" = "aarch64" ] || [ "${HOST_ARCH:-}" = "arm64" ]; then
candidates=(
# Debian / Ubuntu — qemu-efi-aarch64 package
"/usr/share/AAVMF/AAVMF_CODE.fd"
"/usr/share/AAVMF/AAVMF_CODE.ms.fd"
# Fedora / RHEL — edk2-aarch64
"/usr/share/edk2/aarch64/QEMU_EFI.fd"
"/usr/share/edk2/aarch64/QEMU_EFI-silent-pflash.raw"
# Arch — edk2-armvirt
"/usr/share/edk2-armvirt/aarch64/QEMU_EFI.fd"
# Generic
"/usr/share/qemu-efi-aarch64/QEMU_EFI.fd"
)
else
candidates=(
# Arch — modern edk2 layout (edk2-ovmf package)
"/usr/share/edk2/x64/OVMF.4m.fd"
"/usr/share/edk2/x64/OVMF_CODE.4m.fd"
"/usr/share/edk2-ovmf/x64/OVMF.4m.fd"
"/usr/share/edk2-ovmf/x64/OVMF_CODE.4m.fd"
# Older Arch layouts
"/usr/share/edk2-ovmf/x64/OVMF.fd"
"/usr/share/edk2-ovmf/x64/OVMF_CODE.fd"
"/usr/share/ovmf/x64/OVMF.fd"
# Debian / Ubuntu — legacy single-file (ovmf package)
"/usr/share/qemu/OVMF.fd"
"/usr/share/OVMF/OVMF_CODE.fd"
# Fedora
"/usr/share/edk2/ovmf/OVMF_CODE.fd"
)
fi
local f
for f in "${candidates[@]}"; do
[ -f "$f" ] && { echo "$f"; return 0; }
done
return 1
}
# --- Helper: prompt for boot mode + USB hotplug toggle in a single form ---
# Sets global variables: boot_mode, usb_hotplug_enabled
# Returns 1 if user cancels.
#
# UI shape: one form dialog with a single "Enable USB Support" checkbox.
# Firmware choice is made by pressing one of three dialog buttons:
# Cancel -> exit code 1
# BIOS -> exit code 10
# UEFI -> exit code 12
# This keeps BIOS/UEFI as big, visible buttons (no dropdown) and still lets
# us bundle the USB toggle into the same window.
prompt_boot_mode_and_usb() {
local result rc
result=$(yad --form $YAD_ICON \
--title="Select Boot Mode" \
--width="$smaller_width" --height="$smaller_height" \
--text="Choose firmware and VM options:" \
--separator="|" \
--field="Enable USB Support:CHK" "TRUE" \
--field="Headless Mode (no display):CHK" "FALSE" \
--button="Cancel:1" --button="BIOS:10" --button="UEFI:12")
rc=$?
case "$rc" in
10) boot_mode="BIOS" ;;
12) boot_mode="UEFI" ;;
*) return 1 ;;
esac
local usb_flag headless_flag
usb_flag=$(echo "$result" | cut -d'|' -f1)
headless_flag=$(echo "$result" | cut -d'|' -f2)
if [ "$usb_flag" = "TRUE" ]; then
usb_hotplug_enabled=1
else
usb_hotplug_enabled=0
fi
if [ "$headless_flag" = "TRUE" ]; then
headless_enabled=1
else
headless_enabled=0
fi
return 0
}
while true; do
# Option to choose boot source using YAD - escape the & character
boot_source_choice=$(yad --list $YAD_ICON \
--title="Select VM Boot Source" \
--width="$original_width" --height="$smaller_height" \
--column="Option" \
--text="Select items from the list below." \
--button="Cancel:1" --button="OK:0" \
"Boot from connected device" \
"Boot from file (.vhd, .img, .iso)" \
"ISO & Drive (Virtual disk or Physical Device)")
if [ $? -ne 0 ]; then
exit 1
fi
# Remove YAD's trailing | from output
boot_source_choice=$(echo "$boot_source_choice" | sed 's/|$//')
case "$boot_source_choice" in
"Boot from connected device")
# Simple approach - build the YAD command with all drives as arguments
yad_args=("--list" "--title=Select Disk" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a disk:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
selected_drive=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
exit 1
fi
# Extract just the drive name (first column)
selected_drive=$(echo "$selected_drive" | cut -d'|' -f1)
# Prompt for extra disks
add_extra_disk=$(yad --list $YAD_ICON \
--title="Add Extra Disk" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Do you want to add an extra disk?" \
--button="Cancel:1" --button="OK:0" \
"Yes" "No")
if [ $? -ne 0 ]; then
exit 1
fi
add_extra_disk=$(echo "$add_extra_disk" | sed 's/|$//')
if [ "$add_extra_disk" == "Yes" ]; then
extra_disk_id=1
while true; do
extra_disk_choice=$(yad --list $YAD_ICON \
--title="Select Extra Disk Type" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Choose the type of extra disk:" \
--button="Cancel:1" --button="OK:0" \
"Select Virtual Disk File" \
"Select Physical Device" \
"Done")
if [ $? -ne 0 ]; then
break
fi
extra_disk_choice=$(echo "$extra_disk_choice" | sed 's/|$//')
if [ "$extra_disk_choice" == "Select Virtual Disk File" ]; then
extra_disk=$(yad --file $YAD_ICON \
--title="Select Extra Virtual Disk File" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
continue
fi
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Select Physical Device" ]; then
yad_args=("--list" "--title=Select Physical Device" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a physical device:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
extra_disk=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
continue
fi
extra_disk=$(echo "$extra_disk" | cut -d'|' -f1)
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Done" ]; then
break
fi
done
fi
# Use a single form dialog for boot mode + USB toggle.
prompt_boot_mode_and_usb || exit 1
;;
"Boot from file (.vhd, .img, .iso)")
# Select disk image file
selected_drive=$(yad --file $YAD_ICON \
--title="Select Disk Image" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
continue
fi
# Prompt for extra disks
add_extra_disk=$(yad --list $YAD_ICON \
--title="Add Extra Disk" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Do you want to add an extra disk?" \
--button="Cancel:1" --button="OK:0" \
"Yes" "No")
if [ $? -ne 0 ]; then
exit 1
fi
add_extra_disk=$(echo "$add_extra_disk" | sed 's/|$//')
if [ "$add_extra_disk" == "Yes" ]; then
extra_disk_id=1
while true; do
extra_disk_choice=$(yad --list $YAD_ICON \
--title="Select Extra Disk Type" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Choose the type of extra disk:" \
--button="Cancel:1" --button="OK:0" \
"Select Virtual Disk File" \
"Select Physical Device" \
"Done")
if [ $? -ne 0 ]; then
break
fi
extra_disk_choice=$(echo "$extra_disk_choice" | sed 's/|$//')
if [ "$extra_disk_choice" == "Select Virtual Disk File" ]; then
extra_disk=$(yad --file $YAD_ICON \
--title="Select Extra Virtual Disk File" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
continue
fi
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Select Physical Device" ]; then
yad_args=("--list" "--title=Select Physical Device" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a physical device:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
extra_disk=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
continue
fi
extra_disk=$(echo "$extra_disk" | cut -d'|' -f1)
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Done" ]; then
break
fi
done
fi
# Use a single form dialog for boot mode + USB toggle.
prompt_boot_mode_and_usb || exit 1
;;
"ISO & Drive..."|"ISO & Drive (Virtual disk or Physical Device)"|"ISO & Drive (Virtual disk or Physical Device)")
# First select ISO file
iso_path=$(yad --file $YAD_ICON \
--title="Select ISO File" \
--width="$bigger_width" --height="$smaller_height" \
--file-filter="ISO files | *.iso")
if [ $? -ne 0 ]; then
continue
fi
# Then select drive type
drive_type=$(yad --list $YAD_ICON \
--title="Select Drive Type" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Choose drive type:" \
--button="Cancel:1" --button="OK:0" \
"Virtual Disk" \
"Physical Device")
if [ $? -ne 0 ]; then
exit 1
fi
drive_type=$(echo "$drive_type" | sed 's/|$//')
if [ "$drive_type" == "Virtual Disk" ]; then
selected_drive=$(yad --file $YAD_ICON \
--title="Select Virtual Disk File" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
exit 1
fi
# Prompt for extra disks
add_extra_disk=$(yad --list $YAD_ICON \
--title="Add Extra Disk" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Do you want to add an extra disk?" \
--button="Cancel:1" --button="OK:0" \
"Yes" "No")
if [ $? -ne 0 ]; then
exit 1
fi
add_extra_disk=$(echo "$add_extra_disk" | sed 's/|$//')
if [ "$add_extra_disk" == "Yes" ]; then
extra_disk_id=1
while true; do
extra_disk_choice=$(yad --list $YAD_ICON \
--title="Select Extra Disk Type" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Choose the type of extra disk:" \
--button="Cancel:1" --button="OK:0" \
"Select Virtual Disk File" \
"Select Physical Device" \
"Done")
if [ $? -ne 0 ]; then
break
fi
extra_disk_choice=$(echo "$extra_disk_choice" | sed 's/|$//')
if [ "$extra_disk_choice" == "Select Virtual Disk File" ]; then
extra_disk=$(yad --file $YAD_ICON \
--title="Select Extra Virtual Disk File" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
continue
fi
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Select Physical Device" ]; then
yad_args=("--list" "--title=Select Physical Device" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a physical device:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
extra_disk=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
continue
fi
extra_disk=$(echo "$extra_disk" | cut -d'|' -f1)
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Done" ]; then
break
fi
done
fi
elif [ "$drive_type" == "Physical Device" ]; then
yad_args=("--list" "--title=Select Physical Device" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a physical device:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
selected_drive=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
exit 1
fi
selected_drive=$(echo "$selected_drive" | cut -d'|' -f1)
# Prompt for extra disks
add_extra_disk=$(yad --list $YAD_ICON \
--title="Add Extra Disk" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Do you want to add an extra disk?" \
--button="Cancel:1" --button="OK:0" \
"Yes" "No")
if [ $? -ne 0 ]; then
exit 1
fi
add_extra_disk=$(echo "$add_extra_disk" | sed 's/|$//')
if [ "$add_extra_disk" == "Yes" ]; then
extra_disk_id=1
while true; do
extra_disk_choice=$(yad --list $YAD_ICON \
--title="Select Extra Disk Type" \
--width="$smaller_width" --height="$smaller_height" \
--column="Option" \
--text="Choose the type of extra disk:" \
--button="Cancel:1" --button="OK:0" \
"Select Virtual Disk File" \
"Select Physical Device" \
"Done")
if [ $? -ne 0 ]; then
break
fi
extra_disk_choice=$(echo "$extra_disk_choice" | sed 's/|$//')
if [ "$extra_disk_choice" == "Select Virtual Disk File" ]; then
extra_disk=$(yad --file $YAD_ICON \
--title="Select Extra Virtual Disk File" \
--width="$bigger_width" --height="$smaller_height")
if [ $? -ne 0 ]; then
continue
fi
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Select Physical Device" ]; then
yad_args=("--list" "--title=Select Physical Device" "--width=$bigger_width" "--height=$smaller_height" "--column=Drive" "--column=Size" "--text=Select a physical device:" "--button=Cancel:1" "--button=OK:0")
while IFS= read -r line; do
if [ -n "$line" ]; then
drive_name=$(echo "$line" | awk '{print $1}')
drive_size=$(echo "$line" | awk '{print $2}')
yad_args+=("$drive_name" "$drive_size")
fi
done <<< "$(lsblk -o NAME,SIZE -lnp -d -e 7,11)"
extra_disk=$(yad "${yad_args[@]}")
if [ $? -ne 0 ]; then
continue
fi
extra_disk=$(echo "$extra_disk" | cut -d'|' -f1)
extra_fmt=$(detect_format "$extra_disk")
extra_disks="$extra_disks -drive file=\"$extra_disk\",format=$extra_fmt,id=extra_drive$extra_disk_id"
extra_disk_id=$((extra_disk_id + 1))
elif [ "$extra_disk_choice" == "Done" ]; then
break
fi
done
fi
fi
# Use a single form dialog for boot mode + USB toggle.
prompt_boot_mode_and_usb || exit 1
;;
*)
yad --error $YAD_ICON --title="Invalid Choice" --text="Please select a valid option" --width="$smaller_width" --height="$smaller_height"
continue
;;
esac
# Prompt for RAM size
ram_size=$(yad --entry $YAD_ICON \
--title="Set RAM Size" \
--width="$smaller_width" --height="$smaller_height" \
--text="Enter RAM size in MB:" \
--entry-text="2048" \
--button="Cancel:1" --button="OK:0")
if [ $? -ne 0 ]; then
continue
fi
if ! [[ "$ram_size" =~ ^[0-9]+$ ]] || [ "$ram_size" -lt 256 ]; then
yad --error $YAD_ICON --title="Invalid Input" --text="Please enter a valid RAM size in MB" --width="$smaller_width" --height="$smaller_height"
continue
fi
# Print the selected drive, extra disks, boot mode, and RAM size for debugging
echo -e "\nSelected Drive: $selected_drive"
echo "Extra Disks: $extra_disks"
echo "Boot Mode: $boot_mode"
echo "RAM Size: $ram_size MB"
if [ "$usb_hotplug_enabled" = "1" ]; then
echo "USB Hotplug: ENABLED"
else
echo "USB Hotplug: disabled"
fi
if [ "$headless_enabled" = "1" ]; then
echo "Display: HEADLESS (no GUI)"
fi
if [ -n "$iso_path" ]; then
echo "ISO Path: $iso_path"
fi
# --- Launch / restart loop ---
# The session panel can request a VM restart (e.g. for IPv4 reconfig) by
# writing /tmp/qemu-quickboot-restart and sending 'quit' to the monitor.
# We re-enter this inner loop with the new netdev fragment appended,
# reusing every other launch parameter so the user doesn't redo prompts.
net_config_extras=""
rm -f /tmp/qemu-quickboot-restart
while true; do
# Clean any stale monitor socket and panel state files from a previous run
rm -f /tmp/qemu-monitor.sock
rm -f /tmp/vm-storage-devices.list
rm -f /tmp/hotplug-devices.list
# Detect format of the primary source. ISOs become -cdrom, everything
# else (block device or disk image) becomes -drive with that format.
main_format=$(detect_format "$selected_drive")
if [ "$main_format" = "iso" ] && [ -z "$iso_path" ]; then
# User picked an ISO via "Boot from file" — boot it as a CD.
primary_args="-cdrom \"$selected_drive\" -boot order=d"
elif [ -n "$iso_path" ]; then
# ISO + Drive mode: drive is primary, ISO is supplementary.
primary_args="-drive file=\"$selected_drive\",format=$main_format -cdrom \"$iso_path\" -boot order=dc"
else
primary_args="-drive file=\"$selected_drive\",format=$main_format"
fi
# USB controllers only when the user enabled USB at launch time.
usb_args=""
if [ "$usb_hotplug_enabled" = "1" ]; then
usb_args="-usb -device usb-ehci,id=ehci -device qemu-xhci,id=xhci"
fi
# Headless mode: suppress the QEMU display window entirely.
# VGA stays emulated so the guest boots normally; -display none is
# used instead of -nographic to keep -monitor stdio intact.
display_args=""
if [ "$headless_enabled" = "1" ]; then
display_args="-display none"
fi
# SSH-forward host port. Persist within a session so a restart for
# IPv4 reconfig keeps the same SSH endpoint.
if [ -z "$random_port" ]; then
random_port=$((RANDOM % 1000 + 2222))
fi
# Network: SLIRP user-mode, optional IPv4/subnet override appended
# by the session panel via the restart loop.
netdev_arg="user,id=net0,hostfwd=tcp::${random_port}-:22"
if [ -n "$net_config_extras" ]; then
netdev_arg="${netdev_arg},${net_config_extras}"
fi
network_args="-netdev ${netdev_arg} -device e1000,netdev=net0"
bios_args=""
if [ "$boot_mode" = "UEFI" ]; then
ovmf_path=$(detect_ovmf)
if [ -z "$ovmf_path" ]; then
yad --error $YAD_ICON \
--title="OVMF firmware not found" \
--width="$bigger_width" --height="$smaller_height" \
--text="UEFI firmware (OVMF) not found in any standard location.\n\nInstall it for your distro:\n Debian/Ubuntu : sudo apt install ovmf\n Arch / CachyOS: sudo pacman -S edk2-ovmf\n Fedora : sudo dnf install edk2-ovmf\n\nFalling back to BIOS for this VM." \
--button="OK:0"
boot_mode="BIOS"
else
bios_args="-bios $ovmf_path"
echo "OVMF firmware: $ovmf_path"
fi
fi
qemu_command="$QEMU_BIN $QEMU_MACHINE_ARGS -smp 4 -m ${ram_size}M $primary_args $usb_args $display_args -device virtio-scsi-pci,id=scsi0 -monitor stdio -monitor unix:/tmp/qemu-monitor.sock,server,nowait $bios_args $network_args $extra_disks"
echo "Running: $qemu_command"
echo "SSH port forwarding: localhost:${random_port} -> VM:22"
if [ -n "$net_config_extras" ]; then
echo "Custom network: $net_config_extras"
fi
export QEMU_QUICKBOOT_SSH_PORT="$random_port"
export QEMU_QUICKBOOT_HEADLESS="$headless_enabled"
# Resolve script dir for hotplug helper
SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
SETTINGS_PANEL="$SCRIPT_DIR/quickboot-settings.sh"
# Compute desired hotplug window position (upper-right area of screen so
# it does NOT land on top of the QEMU window, which opens centered).
HOTPLUG_WIDTH=400
screen_w=""
if command -v xdpyinfo &>/dev/null; then
screen_w=$(xdpyinfo 2>/dev/null | awk '/dimensions:/ {print $2}' | cut -d'x' -f1)
fi
[ -z "$screen_w" ] && screen_w=1920
HOTPLUG_POSX=$(( screen_w - HOTPLUG_WIDTH - 40 ))
[ "$HOTPLUG_POSX" -lt 0 ] && HOTPLUG_POSX=40
HOTPLUG_POSY=120
export HOTPLUG_POSX HOTPLUG_POSY
# Launch QEMU in the background so we can supervise the helper.
bash -c "$qemu_command" &
qemu_pid=$!
export QEMU_PID="$qemu_pid"
hotplug_pid=""
hotplug_pgid=""
# The session panel launches when USB hotplug is enabled, OR always in
# headless mode (it is then the only way to control / stop the VM).
if [ "$usb_hotplug_enabled" = "1" ] || [ "$headless_enabled" = "1" ]; then
# Helper runs in its own process group via setsid; killing the
# group reaches yad and any descendants when QEMU dies.
setsid bash -c '
for i in $(seq 1 15); do
[ -S /tmp/qemu-monitor.sock ] && break
kill -0 "'"$qemu_pid"'" 2>/dev/null || exit 0
sleep 1
done
if [ -S /tmp/qemu-monitor.sock ] && kill -0 "'"$qemu_pid"'" 2>/dev/null; then
if [ -f "'"$SETTINGS_PANEL"'" ]; then
exec bash "'"$SETTINGS_PANEL"'"
else
yad --error '"$YAD_ICON"' --title="VM Session Settings" --text="quickboot-settings.sh not found at:\n'"$SETTINGS_PANEL"'" --button="OK:0"
fi
fi
' </dev/null &
hotplug_pid=$!
hotplug_pgid="$hotplug_pid"
(
while kill -0 "$qemu_pid" 2>/dev/null; do
sleep 1
done
if [ -n "$hotplug_pgid" ]; then
kill -TERM -- "-$hotplug_pgid" 2>/dev/null
sleep 0.3
kill -KILL -- "-$hotplug_pgid" 2>/dev/null
fi
) &
fi
wait "$qemu_pid"
if [ -n "$hotplug_pgid" ]; then
kill -TERM -- "-$hotplug_pgid" 2>/dev/null
sleep 0.2
kill -KILL -- "-$hotplug_pgid" 2>/dev/null
fi
rm -f /tmp/qemu-monitor.sock
# Did the panel ask for a restart with new settings?
if [ -f /tmp/qemu-quickboot-restart ]; then
PENDING_NET_CONFIG=""
# shellcheck disable=SC1091
source /tmp/qemu-quickboot-restart
rm -f /tmp/qemu-quickboot-restart
if [ -n "$PENDING_NET_CONFIG" ]; then
net_config_extras="$PENDING_NET_CONFIG"
continue
fi
fi
break
done
# Ask to run another VM
yad --question $YAD_ICON \
--title="QEMU - QuickBoot" \
--width="$smaller_width" --height="$smaller_height" \
--text="QuickBoot another VM?" \
--button="No:1" --button="Yes:0"
if [ $? -ne 0 ]; then
break
fi
# Reset variables for next iteration
extra_disks=""
iso_path=""
usb_hotplug_enabled=1
headless_enabled=0
random_port=""
done
# End of script