-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·1084 lines (963 loc) · 37.9 KB
/
run.sh
File metadata and controls
executable file
·1084 lines (963 loc) · 37.9 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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Breenix Interactive Runner
# ===========================
# Runs Breenix with a graphical display
#
# Usage:
# ./run.sh # ARM64 with native cocoa display (default)
# ./run.sh --clean # Full rebuild (userspace + ext2 disk + kernel), then run
# ./run.sh --x86 # x86_64 with VNC display
# ./run.sh --headless # ARM64 with serial output only
# ./run.sh --x86 --headless # x86_64 with serial output only
# ./run.sh --no-build # Skip all builds, use existing artifacts
# ./run.sh --resolution 1920x1080 # Custom resolution
# ./run.sh --parallels # Build and boot on Parallels Desktop
# ./run.sh --parallels --no-build # Deploy existing build to Parallels
# ./run.sh --parallels --test # Build, boot, wait, screenshot, exit
# ./run.sh --parallels --test 60 # Same with custom wait (default: 35s)
# ./run.sh --ahci # ARM64 with AHCI (SATA) disk instead of virtio-blk
# ./run.sh --ahci --headless # ARM64 AHCI with serial output only
# ./run.sh --btrt # ARM64 BTRT structured boot test
# ./run.sh --btrt --x86 # x86_64 BTRT structured boot test
#
# Display:
# ARM64: Native window (cocoa) - no VNC needed
# x86_64: VNC at localhost:5900
#
# Both architectures run QEMU natively on the host.
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BREENIX_ROOT="$SCRIPT_DIR"
cd "$BREENIX_ROOT"
# Defaults: ARM64 with graphics
ARCH="arm64"
HEADLESS=false
CLEAN=false
NO_BUILD=false
BTRT=false
PARALLELS=false
PARALLELS_VM="breenix-dev"
PARALLELS_TEST=false
PARALLELS_TEST_WAIT=35
VMWARE=false
DEBUG=false
REBUILD_HOME=false
RESOLUTION=""
USE_AHCI=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--x86|--x86_64|--amd64)
ARCH="x86_64"
shift
;;
--arm64|--aarch64)
ARCH="arm64"
shift
;;
--clean)
CLEAN=true
shift
;;
--no-build)
NO_BUILD=true
shift
;;
--parallels)
PARALLELS=true
ARCH="arm64"
shift
;;
--vmware)
VMWARE=true
ARCH="arm64"
shift
;;
--test)
PARALLELS_TEST=true
# Optional: next arg is wait seconds if it's a number
if [[ "${2:-}" =~ ^[0-9]+$ ]]; then
PARALLELS_TEST_WAIT="$2"
shift
fi
shift
;;
--vm)
PARALLELS_VM="$2"
shift 2
;;
--btrt)
BTRT=true
shift
;;
--debug)
DEBUG=true
shift
;;
--ahci)
USE_AHCI=true
shift
;;
--rebuild-home)
REBUILD_HOME=true
shift
;;
--resolution)
RESOLUTION="$2"
shift 2
;;
--headless|--serial)
HEADLESS=true
shift
;;
--graphics|--vnc)
HEADLESS=false
shift
;;
-h|--help)
echo "Usage: ./run.sh [options]"
echo ""
echo "Options:"
echo " --clean Full rebuild: userspace, system ext2, kernel"
echo " --rebuild-home Recreate home disk (destroys user data)"
echo " --no-build Skip all builds, use existing artifacts"
echo " --x86, --x86_64, --amd64 Run x86_64 kernel (default: ARM64)"
echo " --arm64, --aarch64 Run ARM64 kernel (default)"
echo " --parallels Build and boot on Parallels Desktop VM"
echo " --parallels --test [N] Build, boot, wait N sec (default 35), screenshot, exit"
echo " --vmware Build and boot on VMware Fusion VM"
echo " --vm NAME Parallels VM name (default: breenix-dev)"
echo " --headless, --serial Run without display (serial only)"
echo " --graphics, --vnc Run with VNC display (default)"
echo " --btrt Run BTRT structured boot test"
echo " --ahci Use AHCI (SATA) disk instead of virtio-blk (ARM64)"
echo " --debug Enable GDB stub (port 1234) for debugging"
echo " --resolution WxH Set display resolution (e.g. 1920x1080)"
echo " Default: auto-detect from screen"
echo " -h, --help Show this help"
echo ""
echo "Debugging:"
echo " QMP socket always at: /tmp/breenix-qmp.sock"
echo " GDB (--debug): target remote :1234"
echo " Forensics: scripts/forensic-capture.sh"
echo ""
echo "Display:"
echo " ARM64: Native window (cocoa)"
echo " x86_64: VNC at localhost:5900"
exit 0
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# BTRT mode: delegate to xtask and exit
if [ "$BTRT" = true ]; then
if [ "$ARCH" = "arm64" ]; then
BTRT_ARCH="arm64"
else
BTRT_ARCH="x86_64"
fi
echo ""
echo "========================================="
echo "Breenix BTRT Boot Test ($BTRT_ARCH)"
echo "========================================="
echo ""
exec cargo run -p xtask -- boot-test-btrt --arch "$BTRT_ARCH"
fi
# Parallels mode: build, deploy, boot on Parallels Desktop
if [ "$PARALLELS" = true ]; then
echo ""
echo "========================================="
echo "Breenix on Parallels Desktop"
echo "========================================="
echo ""
if ! command -v prlctl &>/dev/null; then
echo "ERROR: prlctl not found. Is Parallels Desktop installed?"
exit 1
fi
if ! command -v prl_disk_tool &>/dev/null; then
echo "ERROR: prl_disk_tool not found. Is Parallels Desktop installed?"
exit 1
fi
PARALLELS_DIR="$BREENIX_ROOT/target/parallels"
SERIAL_LOG="/tmp/breenix-parallels-serial.log"
HDD_DIR="$PARALLELS_DIR/breenix-efi.hdd"
EXT2_HDD_DIR="$PARALLELS_DIR/breenix-ext2.hdd"
EXT2_DISK="$BREENIX_ROOT/target/ext2-aarch64.img"
# Stop any running breenix VMs before build — if a VM is still running,
# rm -rf on the HDD directory may fail to clean locked .hds files.
for OLD_VM in $(prlctl list --all 2>/dev/null | grep 'breenix-' | awk '{print $NF}'); do
VM_STATUS=$(prlctl status "$OLD_VM" 2>/dev/null | awk '{print $NF}')
if [ "$VM_STATUS" = "running" ] || [ "$VM_STATUS" = "paused" ] || [ "$VM_STATUS" = "suspended" ]; then
echo "Stopping $OLD_VM before build..."
prlctl stop "$OLD_VM" --kill 2>/dev/null || true
fi
done
if [ "$NO_BUILD" = true ]; then
echo "Skipping builds (--no-build)"
if [ ! -d "$HDD_DIR" ]; then
echo "ERROR: No Parallels disk found at $HDD_DIR"
echo "Run without --no-build first to create it."
exit 1
fi
else
# Clean build caches if --clean was specified
if [ "$CLEAN" = true ]; then
echo "[0/6] Cleaning build caches..."
cargo clean -p kernel 2>/dev/null || true
cargo clean -p parallels-loader 2>/dev/null || true
fi
# Build the UEFI loader
echo "[1/6] Building UEFI loader..."
cargo build --release --target aarch64-unknown-uefi -p parallels-loader
# Build the kernel
echo ""
echo "[2/6] Building kernel..."
cargo build --release --target aarch64-breenix.json \
-Z build-std=core,alloc \
-Z build-std-features=compiler-builtins-mem \
-p kernel --bin kernel-aarch64
LOADER_EFI="$BREENIX_ROOT/target/aarch64-unknown-uefi/release/parallels-loader.efi"
KERNEL_ELF="$BREENIX_ROOT/target/aarch64-breenix/release/kernel-aarch64"
if [ ! -f "$LOADER_EFI" ]; then
echo "ERROR: UEFI loader not found at $LOADER_EFI"
exit 1
fi
if [ ! -f "$KERNEL_ELF" ]; then
echo "ERROR: Kernel not found at $KERNEL_ELF"
exit 1
fi
# Build userspace binaries and create ext2 data disk
echo ""
echo "[3/6] Building userspace binaries (aarch64)..."
if "$BREENIX_ROOT/userspace/programs/build.sh" --arch aarch64; then
echo " Userspace build successful"
else
echo " WARNING: Userspace build failed (rust-fork may not be set up)"
echo " Continuing without userspace binaries — ext2 will still have test files"
fi
echo ""
echo "[4/6] Creating ext2 data disk image..."
if ! "$BREENIX_ROOT/scripts/create_ext2_disk.sh" --arch aarch64; then
echo " WARNING: ext2 disk creation failed (Docker may not be running)"
echo " Continuing without ext2 disk"
fi
echo ""
echo "[5/6] Creating FAT32 EFI disk image..."
mkdir -p "$PARALLELS_DIR"
# Create GPT+FAT32 disk image using hdiutil (native macOS, no mtools needed)
DMG_PATH="$PARALLELS_DIR/efi-temp.dmg"
rm -f "$DMG_PATH"
hdiutil create -size 64m -fs FAT32 -volname BREENIX -layout GPTSPUD "$DMG_PATH" >/dev/null 2>&1
# Mount the DMG, copy loader + kernel to ESP layout
VOLUME=$(hdiutil attach "$DMG_PATH" 2>/dev/null | grep -o '/Volumes/[^ ]*' | head -1)
if [ -z "$VOLUME" ] || [ ! -d "$VOLUME" ]; then
echo "ERROR: Failed to mount FAT32 disk image"
rm -f "$DMG_PATH"
exit 1
fi
mkdir -p "$VOLUME/EFI/BOOT"
mkdir -p "$VOLUME/EFI/BREENIX"
cp "$LOADER_EFI" "$VOLUME/EFI/BOOT/BOOTAA64.EFI"
cp "$KERNEL_ELF" "$VOLUME/EFI/BREENIX/KERNEL"
hdiutil detach "$VOLUME" >/dev/null 2>&1
echo " Loader: $(stat -f%z "$LOADER_EFI") bytes"
echo " Kernel: $(stat -f%z "$KERNEL_ELF") bytes"
# Convert DMG to raw disk image
echo ""
echo "[6/6] Creating Parallels disks..."
RAW_IMG="$PARALLELS_DIR/efi-raw.img"
rm -f "$RAW_IMG" "${RAW_IMG}.cdr"
hdiutil convert "$DMG_PATH" -format UDTO -o "$RAW_IMG" >/dev/null 2>&1
mv "${RAW_IMG}.cdr" "$RAW_IMG"
rm -f "$DMG_PATH"
# Patch GPT partition type from "Microsoft Basic Data" to "EFI System Partition"
# so UEFI firmware recognizes the ESP and auto-boots BOOTAA64.EFI
python3 "$BREENIX_ROOT/scripts/parallels/patch-gpt-esp.py" "$RAW_IMG"
# Wrap EFI disk in Parallels .hdd format
rm -rf "$HDD_DIR"
prl_disk_tool create --hdd "$HDD_DIR" --size 64M >/dev/null 2>&1
HDS_FILE=$(find "$HDD_DIR" -name "*.hds" | head -1)
if [ -z "$HDS_FILE" ]; then
echo "ERROR: No .hds file found in $HDD_DIR"
rm -f "$RAW_IMG"
exit 1
fi
cp "$RAW_IMG" "$HDS_FILE"
rm -f "$RAW_IMG"
echo " EFI disk: $HDD_DIR"
# Wrap ext2 data disk in Parallels .hdd format
if [ -f "$EXT2_DISK" ]; then
EXT2_SIZE_MB=$(( $(stat -f%z "$EXT2_DISK") / 1048576 ))
rm -rf "$EXT2_HDD_DIR"
prl_disk_tool create --hdd "$EXT2_HDD_DIR" --size "${EXT2_SIZE_MB}M" >/dev/null 2>&1
HDS_FILE=$(find "$EXT2_HDD_DIR" -name "*.hds" | head -1)
if [ -z "$HDS_FILE" ]; then
echo "WARNING: No .hds file in $EXT2_HDD_DIR, ext2 disk won't be attached"
else
cp "$EXT2_DISK" "$HDS_FILE"
echo " ext2 disk: $EXT2_HDD_DIR (${EXT2_SIZE_MB}MB)"
fi
else
echo "WARNING: ext2 disk not found at $EXT2_DISK"
fi
fi
echo ""
echo "--- Configuring Parallels VM ---"
# Use a unique VM name each time to avoid Parallels stale state issues.
# Parallels can get stuck in "stopping"/"suspended" states that require
# sudo service restarts. Using unique names sidesteps this entirely —
# old VMs are cleaned up in the background, never blocking new launches.
PARALLELS_VM="breenix-$(date +%s)"
echo "VM name: $PARALLELS_VM"
# Clean up any previous breenix-* VMs (best-effort, don't block on stuck ones)
for OLD_VM in $(prlctl list --all 2>/dev/null | grep 'breenix-' | awk '{print $NF}'); do
if [ "$OLD_VM" != "$PARALLELS_VM" ]; then
echo " Cleaning up old VM: $OLD_VM"
prlctl stop "$OLD_VM" --kill 2>/dev/null || true
# Try to delete — if stuck in stopping, just move on
prlctl delete "$OLD_VM" 2>/dev/null || true
fi
done
echo "Creating fresh VM '$PARALLELS_VM'..."
prlctl create "$PARALLELS_VM" --ostype linux --distribution linux --no-hdd
prlctl set "$PARALLELS_VM" --memsize 8192
prlctl set "$PARALLELS_VM" --cpus 8
# Disable auto-suspend: default is "suspend" which halts the VM when Parallels
# detects no display activity. Breenix runs headless/serial; we want it to
# stay running until explicitly stopped.
prlctl set "$PARALLELS_VM" --autostop shutdown 2>/dev/null || true
# Configure VM: EFI boot, attach our disks
prlctl set "$PARALLELS_VM" --efi-boot on 2>/dev/null || true
# VirtIO GPU with 3D acceleration (required for VirGL)
prlctl set "$PARALLELS_VM" --3d-accelerate highest 2>/dev/null || true
prlctl set "$PARALLELS_VM" --videosize 2048 2>/dev/null || true
prlctl set "$PARALLELS_VM" --high-resolution on 2>/dev/null || true
prlctl set "$PARALLELS_VM" --vertical-sync on 2>/dev/null || true
# Remove any default devices Parallels created (cdrom, hdd, serial) to avoid conflicts
for dev in hdd0 hdd1 hdd2 cdrom0 cdrom1 serial0 serial1; do
prlctl set "$PARALLELS_VM" --device-del "$dev" 2>/dev/null || true
done
# Attach EFI boot disk as hdd0 (sata:0)
prlctl set "$PARALLELS_VM" --device-add hdd --image "$HDD_DIR" --type plain --position 0
# Attach ext2 data disk as hdd1 (sata:1) — kernel probes all AHCI devices for ext2 magic
if [ -d "$EXT2_HDD_DIR" ]; then
prlctl set "$PARALLELS_VM" --device-add hdd --image "$EXT2_HDD_DIR" --type plain --position 1
echo " hdd0: EFI boot disk (FAT32) at sata:0"
echo " hdd1: ext2 data disk at sata:1"
else
echo " hdd0: EFI boot disk (FAT32) at sata:0"
echo " WARNING: No ext2 disk to attach (run without --no-build to create it)"
fi
# Set boot order: hard disk first (default boots from cdrom which hangs)
prlctl set "$PARALLELS_VM" --device-bootorder "hdd0" 2>/dev/null || true
# Configure serial port output to file
prlctl set "$PARALLELS_VM" --device-add serial --output "$SERIAL_LOG" 2>/dev/null || true
# Serial port must be explicitly connected or it stays disconnected
prlctl set "$PARALLELS_VM" --device-set serial0 --connect 2>/dev/null || true
echo ""
echo "--- Starting VM ---"
rm -f "$SERIAL_LOG" # Remove so VMware creates fresh (avoids append/replace prompt)
prlctl start "$PARALLELS_VM"
echo ""
echo "========================================="
echo "Breenix running on Parallels"
echo "========================================="
echo "VM: $PARALLELS_VM"
echo "Serial: $SERIAL_LOG"
echo "Stop: prlctl stop $PARALLELS_VM --kill"
echo ""
if [ "$PARALLELS_TEST" = true ]; then
# Test mode: wait, screenshot, exit
SCREENSHOT="/tmp/breenix-screenshot.png"
echo "Test mode: waiting ${PARALLELS_TEST_WAIT}s for boot..."
sleep "$PARALLELS_TEST_WAIT"
echo ""
echo "=== Screenshot ==="
SCREENSHOT_SCRIPT="$BREENIX_ROOT/scripts/parallels/screenshot-vm.sh"
if [ -x "$SCREENSHOT_SCRIPT" ]; then
if "$SCREENSHOT_SCRIPT" "$PARALLELS_VM" "$SCREENSHOT" \
|| prlctl capture "$PARALLELS_VM" --file "$SCREENSHOT" 2>/dev/null; then
echo "Screenshot: $SCREENSHOT"
else
echo "Screenshot failed (VM may not be displaying yet)"
fi
else
prlctl capture "$PARALLELS_VM" --file "$SCREENSHOT" 2>/dev/null \
&& echo "Screenshot: $SCREENSHOT" \
|| echo "Screenshot failed (VM may not be displaying yet)"
fi
echo ""
echo "Serial log: $SERIAL_LOG"
echo "Screenshot: $SCREENSHOT"
else
# Interactive mode: tail serial forever
echo "Tailing serial output (Ctrl+C to detach)..."
echo ""
# Monitor Parallels VM log for VCPU exceptions in background
VM_DIR="$HOME/Parallels/${PARALLELS_VM}.pvm"
VM_LOG="$VM_DIR/parallels.log"
if [ -f "$VM_LOG" ]; then
(
tail -f "$VM_LOG" 2>/dev/null | while IFS= read -r line; do
case "$line" in
*VCPU*|*Exception*|*Synchronous*|*fault*|*FAULT*|*recursive*)
echo "[parallels-log] $line" ;;
esac
done
) &
LOGMON_PID=$!
trap "kill $LOGMON_PID 2>/dev/null" EXIT
fi
# Wait a moment for the VM to start producing output, then tail
sleep 1
tail -f "$SERIAL_LOG"
fi
exit 0
fi
# VMware Fusion mode: build, convert to VMDK, launch
if [ "$VMWARE" = true ]; then
echo ""
echo "========================================="
echo "Breenix on VMware Fusion"
echo "========================================="
echo ""
VMRUN="/Applications/VMware Fusion.app/Contents/Public/vmrun"
if [ ! -x "$VMRUN" ]; then
echo "ERROR: vmrun not found. Is VMware Fusion installed?"
exit 1
fi
VMWARE_DIR="$BREENIX_ROOT/target/vmware"
SERIAL_LOG="/tmp/breenix-vmware-serial.log"
EXT2_DISK="$BREENIX_ROOT/target/ext2-aarch64.img"
VM_MACHINES="$HOME/Virtual Machines.localized"
if [ "$NO_BUILD" = true ]; then
echo "Skipping builds (--no-build)"
if [ ! -f "$VMWARE_DIR/boot.vmdk" ]; then
echo "ERROR: No boot VMDK found at $VMWARE_DIR/boot.vmdk"
echo "Run without --no-build first to create it."
exit 1
fi
else
# Clean build caches if --clean
if [ "$CLEAN" = true ]; then
echo "[0/6] Cleaning build caches..."
cargo clean -p kernel 2>/dev/null || true
cargo clean -p parallels-loader 2>/dev/null || true
fi
# Build UEFI loader
echo "[1/6] Building UEFI loader..."
cargo build --release --target aarch64-unknown-uefi -p parallels-loader
# Build kernel
echo ""
echo "[2/6] Building kernel..."
cargo build --release --target aarch64-breenix.json \
-Z build-std=core,alloc \
-Z build-std-features=compiler-builtins-mem \
-p kernel --bin kernel-aarch64
LOADER_EFI="$BREENIX_ROOT/target/aarch64-unknown-uefi/release/parallels-loader.efi"
KERNEL_ELF="$BREENIX_ROOT/target/aarch64-breenix/release/kernel-aarch64"
if [ ! -f "$LOADER_EFI" ]; then
echo "ERROR: UEFI loader not found at $LOADER_EFI"
exit 1
fi
if [ ! -f "$KERNEL_ELF" ]; then
echo "ERROR: Kernel not found at $KERNEL_ELF"
exit 1
fi
# Build userspace
echo ""
echo "[3/6] Building userspace binaries (aarch64)..."
if "$BREENIX_ROOT/userspace/programs/build.sh" --arch aarch64; then
echo " Userspace build successful"
else
echo " WARNING: Userspace build failed"
echo " Continuing without userspace binaries"
fi
# Create ext2 data disk
echo ""
echo "[4/6] Creating ext2 data disk image..."
if ! "$BREENIX_ROOT/scripts/create_ext2_disk.sh" --arch aarch64; then
echo " WARNING: ext2 disk creation failed"
fi
# Create FAT32 EFI disk image
echo ""
echo "[5/6] Creating FAT32 EFI disk image..."
mkdir -p "$VMWARE_DIR"
DMG_PATH="$VMWARE_DIR/efi-temp.dmg"
rm -f "$DMG_PATH"
hdiutil create -size 64m -fs FAT32 -volname BREENIX -layout GPTSPUD "$DMG_PATH" >/dev/null 2>&1
VOLUME=$(hdiutil attach "$DMG_PATH" 2>/dev/null | grep -o '/Volumes/[^ ]*' | head -1)
if [ -z "$VOLUME" ] || [ ! -d "$VOLUME" ]; then
echo "ERROR: Failed to mount FAT32 disk image"
rm -f "$DMG_PATH"
exit 1
fi
mkdir -p "$VOLUME/EFI/BOOT"
mkdir -p "$VOLUME/EFI/BREENIX"
cp "$LOADER_EFI" "$VOLUME/EFI/BOOT/BOOTAA64.EFI"
cp "$KERNEL_ELF" "$VOLUME/EFI/BREENIX/KERNEL"
hdiutil detach "$VOLUME" >/dev/null 2>&1
echo " Loader: $(stat -f%z "$LOADER_EFI") bytes"
echo " Kernel: $(stat -f%z "$KERNEL_ELF") bytes"
# Convert to raw then to VMDK
echo ""
echo "[6/6] Converting to VMDK..."
RAW_IMG="$VMWARE_DIR/efi-raw.img"
rm -f "$RAW_IMG" "${RAW_IMG}.cdr"
hdiutil convert "$DMG_PATH" -format UDTO -o "$RAW_IMG" >/dev/null 2>&1
mv "${RAW_IMG}.cdr" "$RAW_IMG"
rm -f "$DMG_PATH"
# Patch GPT partition type to EFI System Partition
python3 "$BREENIX_ROOT/scripts/parallels/patch-gpt-esp.py" "$RAW_IMG"
# Convert raw to VMDK
qemu-img convert -f raw -O vmdk "$RAW_IMG" "$VMWARE_DIR/boot.vmdk"
rm -f "$RAW_IMG"
echo " Boot VMDK: $VMWARE_DIR/boot.vmdk"
# Convert ext2 data disk to VMDK
if [ -f "$EXT2_DISK" ]; then
qemu-img convert -f raw -O vmdk "$EXT2_DISK" "$VMWARE_DIR/ext2-data.vmdk"
echo " Data VMDK: $VMWARE_DIR/ext2-data.vmdk"
fi
fi
echo ""
echo "--- Configuring VMware VM ---"
# Use a unique VM name to avoid stale state
VM_NAME="breenix-$(date +%s)"
VM_BUNDLE="$VM_MACHINES/$VM_NAME.vmwarevm"
echo "VM name: $VM_NAME"
# Clean up old breenix-* VMs (best-effort)
for OLD_VM_DIR in "$VM_MACHINES"/breenix-*.vmwarevm; do
[ -d "$OLD_VM_DIR" ] || continue
OLD_VMX=$(find "$OLD_VM_DIR" -name "*.vmx" -maxdepth 1 | head -1)
if [ -n "$OLD_VMX" ]; then
"$VMRUN" stop "$OLD_VMX" hard >/dev/null 2>&1 || true
fi
rm -rf "$OLD_VM_DIR"
echo " Cleaned up: $(basename "$OLD_VM_DIR")"
done
# Create VM bundle
mkdir -p "$VM_BUNDLE"
# Copy VMDKs into the bundle
cp "$VMWARE_DIR/boot.vmdk" "$VM_BUNDLE/boot.vmdk"
EXT2_VMDK_ARG=""
if [ -f "$VMWARE_DIR/ext2-data.vmdk" ]; then
cp "$VMWARE_DIR/ext2-data.vmdk" "$VM_BUNDLE/ext2-data.vmdk"
EXT2_VMDK_ARG="$VM_BUNDLE/ext2-data.vmdk"
fi
# Generate VMX config inline (avoid subshell issues with command substitution)
VMX_FILE="$VM_BUNDLE/$VM_NAME.vmx"
cat > "$VMX_FILE" <<VMXEOF
.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "22"
displayName = "$VM_NAME"
guestOS = "arm-ubuntu-64"
firmware = "efi"
numvcpus = "4"
memsize = "2048"
cpuid.coresPerSocket = "4"
pciBridge0.present = "TRUE"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge4.functions = "8"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
nvme0.present = "TRUE"
nvme0:0.present = "TRUE"
nvme0:0.fileName = "$VM_BUNDLE/boot.vmdk"
sata0.present = "TRUE"
VMXEOF
if [ -n "$EXT2_VMDK_ARG" ]; then
cat >> "$VMX_FILE" <<VMXEOF
sata0:0.present = "TRUE"
sata0:0.fileName = "$EXT2_VMDK_ARG"
VMXEOF
fi
cat >> "$VMX_FILE" <<VMXEOF
serial0.present = "TRUE"
serial0.fileType = "file"
serial0.fileName = "$SERIAL_LOG"
serial0.yieldOnMsrRead = "TRUE"
ethernet0.present = "TRUE"
ethernet0.connectionType = "nat"
ethernet0.virtualDev = "e1000e"
ethernet0.addressType = "generated"
ethernet0.linkStatePropagation.enable = "TRUE"
usb.present = "TRUE"
usb_xhci.present = "TRUE"
usb_xhci:4.present = "TRUE"
usb_xhci:4.deviceType = "hid"
usb_xhci:4.port = "4"
usb_xhci:6.present = "TRUE"
usb_xhci:6.deviceType = "mouse"
usb_xhci:6.port = "6"
svga.vramSize = "268435456"
mks.enable3d = "TRUE"
vmci0.present = "TRUE"
tools.syncTime = "FALSE"
floppy0.present = "FALSE"
hpet0.present = "TRUE"
msg.autoAnswer = "TRUE"
VMXEOF
echo " VMX: $VMX_FILE ($(wc -c < "$VMX_FILE") bytes)"
echo ""
echo "--- Starting VM ---"
rm -f "$SERIAL_LOG" # Remove so VMware creates fresh (avoids append/replace prompt)
"$VMRUN" start "$VMX_FILE" gui 2>&1 || {
echo "vmrun start failed with GUI, trying nogui..."
"$VMRUN" start "$VMX_FILE" nogui 2>&1 || true
}
echo ""
echo "========================================="
echo "Breenix running on VMware Fusion"
echo "========================================="
echo "VM: $VM_NAME"
echo "Serial: $SERIAL_LOG"
echo "VMX: $VMX_FILE"
echo "Stop: \"$VMRUN\" stop \"$VMX_FILE\" hard"
echo ""
echo "Tailing serial output (Ctrl+C to detach)..."
echo ""
# Monitor vmware.log for CPU exceptions in background
VM_LOG="$VM_BUNDLE/vmware.log"
(
sleep 2 # Wait for vmware.log to appear
if [ -f "$VM_LOG" ]; then
tail -f "$VM_LOG" 2>/dev/null | while IFS= read -r line; do
case "$line" in
*vcpu*exception*|*PANIC*|*fault*|*FAULT*|*Guest:*)
echo "[vmware-log] $line" ;;
esac
done
fi
) &
LOGMON_PID=$!
trap "kill $LOGMON_PID 2>/dev/null" EXIT
sleep 1
tail -f "$SERIAL_LOG"
exit 0
fi
# Route to architecture-specific runner
if [ "$ARCH" = "arm64" ]; then
# ARM64 path - direct kernel boot
KERNEL="$BREENIX_ROOT/target/aarch64-breenix/release/kernel-aarch64"
EXT2_DISK="$BREENIX_ROOT/target/ext2-aarch64.img"
HOME_DISK="$BREENIX_ROOT/target/ext2-home-aarch64.img"
# Build command for ARM64
BUILD_CMD="cargo build --release --features boot_tests --target aarch64-breenix.json -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem -p kernel --bin kernel-aarch64"
else
# x86_64 path - uses UEFI boot
EXT2_DISK="$BREENIX_ROOT/target/ext2.img"
HOME_DISK="$BREENIX_ROOT/target/ext2-home.img"
VNC_PORT=5900
# Build command for x86_64
BUILD_CMD="cargo build --release --features testing,external_test_bins,interactive --bin qemu-uefi"
# x86_64 needs to find UEFI image
UEFI_IMG=$(ls -t "$BREENIX_ROOT/target/release/build/breenix-"*/out/breenix-uefi.img 2>/dev/null | head -1)
KERNEL="$UEFI_IMG" # Reuse KERNEL var for existence check
fi
# Resolve display resolution
if [ -z "$RESOLUTION" ] && [ "$HEADLESS" = false ]; then
# Auto-detect: use macOS screen size minus menu bar (37px)
if [ "$(uname)" = "Darwin" ]; then
SCREEN_INFO=$(system_profiler SPDisplaysDataType 2>/dev/null | grep "Resolution:" | head -1)
if [[ "$SCREEN_INFO" =~ ([0-9]+)\ x\ ([0-9]+) ]]; then
NATIVE_W="${BASH_REMATCH[1]}"
NATIVE_H="${BASH_REMATCH[2]}"
# Retina displays: divide by 2 for effective resolution
if echo "$SCREEN_INFO" | grep -q "Retina"; then
NATIVE_W=$((NATIVE_W / 2))
NATIVE_H=$((NATIVE_H / 2))
fi
# Subtract menu bar height
RES_W="$NATIVE_W"
RES_H=$((NATIVE_H - 37))
RESOLUTION="${RES_W}x${RES_H}"
fi
fi
fi
# Fallback default
if [ -z "$RESOLUTION" ]; then
RESOLUTION="1280x800"
fi
# Parse WxH
RES_W="${RESOLUTION%%x*}"
RES_H="${RESOLUTION##*x}"
echo ""
echo "========================================="
echo "Breenix Interactive Mode"
echo "========================================="
echo ""
echo "Architecture: $ARCH"
echo "Resolution: ${RES_W}x${RES_H}"
echo "Mode: $([ "$HEADLESS" = true ] && echo "headless (serial only)" || echo "graphics")"
if [ "$NO_BUILD" = true ]; then
echo "Skipping all builds (--no-build)"
elif [ "$CLEAN" = true ]; then
# --clean: full rebuild of userspace, system ext2 disk, and kernel
echo ""
echo "Clean build: rebuilding everything..."
echo ""
if [ "$ARCH" = "arm64" ]; then
echo "[1/3] Building userspace binaries (aarch64)..."
"$BREENIX_ROOT/userspace/programs/build.sh" --arch aarch64
echo ""
echo "[2/3] Creating system ext2 disk image..."
"$BREENIX_ROOT/scripts/create_ext2_disk.sh" --arch aarch64
echo ""
echo "[3/3] Building kernel..."
else
echo "[1/3] Building userspace binaries (x86_64)..."
"$BREENIX_ROOT/userspace/programs/build.sh"
echo ""
echo "[2/3] Creating system ext2 disk image..."
"$BREENIX_ROOT/scripts/create_ext2_disk.sh"
echo ""
echo "[3/3] Building kernel..."
fi
eval $BUILD_CMD
echo ""
else
# Always rebuild to ensure correct features (boot_tests, etc.)
# Cargo is incremental — this is fast if nothing changed
echo "Building kernel..."
eval $BUILD_CMD
fi
# Create home disk if it doesn't exist (never recreated unless --rebuild-home)
if [ "$REBUILD_HOME" = true ] || [ ! -f "$HOME_DISK" ]; then
if [ "$REBUILD_HOME" = true ] && [ -f "$HOME_DISK" ]; then
echo ""
echo "Rebuilding home disk (--rebuild-home)..."
elif [ ! -f "$HOME_DISK" ]; then
echo ""
echo "Creating home disk (first run)..."
fi
if [ "$ARCH" = "arm64" ]; then
"$BREENIX_ROOT/scripts/create_home_disk.sh" --arch aarch64
else
"$BREENIX_ROOT/scripts/create_home_disk.sh"
fi
fi
if [ ! -f "$KERNEL" ]; then
echo "Error: Kernel not found after build"
exit 1
fi
echo "Kernel: $KERNEL"
# Create output directory
OUTPUT_DIR=$(mktemp -d)
echo "Serial output: $OUTPUT_DIR/serial.txt"
# Add ext2 disk if it exists (writable copy to allow filesystem writes)
# Use a known path so extraction scripts can find the session disk
DISK_OPTS=""
EXT2_SESSION="$BREENIX_ROOT/target/ext2-session.img"
if [ -f "$EXT2_DISK" ]; then
echo "Disk image: $EXT2_DISK"
EXT2_WRITABLE="$EXT2_SESSION"
if [ "$CLEAN" = true ] || [ ! -f "$EXT2_WRITABLE" ]; then
echo "Creating fresh session disk from $EXT2_DISK"
cp "$EXT2_DISK" "$EXT2_WRITABLE"
else
echo "Reusing existing session disk (use --clean to reset)"
fi
if [ "$ARCH" = "arm64" ]; then
if [ "$USE_AHCI" = true ]; then
# AHCI (SATA) disk — reproduces Parallels AHCI interrupt behavior on QEMU
DISK_OPTS="-device ahci,id=ahci0 -device ide-hd,drive=ext2disk,bus=ahci0.0 -drive id=ext2disk,file=$EXT2_WRITABLE,format=raw,if=none"
echo " Using AHCI controller for ext2 disk"
else
DISK_OPTS="-device virtio-blk-device,drive=ext2disk -drive if=none,id=ext2disk,format=raw,file=$EXT2_WRITABLE"
fi
else
# x86_64 uses virtio-blk-pci for UEFI compatibility
DISK_OPTS="-drive if=none,id=ext2disk,format=raw,file=$EXT2_WRITABLE -device virtio-blk-pci,drive=ext2disk,disable-modern=on,disable-legacy=off"
fi
else
echo "Disk image: (none - shell commands will be limited)"
if [ "$ARCH" = "arm64" ]; then
if [ "$USE_AHCI" = true ]; then
DISK_OPTS="-device ahci,id=ahci0 -device ide-hd,drive=hd0,bus=ahci0.0 -drive id=hd0,file=/dev/null,format=raw,if=none"
else
DISK_OPTS="-device virtio-blk-device,drive=hd0 -drive if=none,id=hd0,format=raw,file=/dev/null"
fi
fi
fi
# Add home disk if it exists (writable copy for user data persistence)
HOME_DISK_OPTS=""
HOME_SESSION="$BREENIX_ROOT/target/ext2-home-session.img"
if [ -f "$HOME_DISK" ]; then
echo "Home disk: $HOME_DISK"
if [ "$REBUILD_HOME" = true ] || [ ! -f "$HOME_SESSION" ]; then
echo "Creating fresh home session disk from $HOME_DISK"
cp "$HOME_DISK" "$HOME_SESSION"
else
echo "Reusing existing home session disk (use --rebuild-home to reset)"
fi
if [ "$ARCH" = "arm64" ]; then
if [ "$USE_AHCI" = true ]; then
# Home disk on AHCI port 1 (same controller as ext2 disk on port 0)
HOME_DISK_OPTS="-device ide-hd,drive=homedisk,bus=ahci0.1 -drive id=homedisk,file=$HOME_SESSION,format=raw,if=none"
else
HOME_DISK_OPTS="-device virtio-blk-device,drive=homedisk -drive if=none,id=homedisk,format=raw,file=$HOME_SESSION"
fi
else
HOME_DISK_OPTS="-drive if=none,id=homedisk,format=raw,file=$HOME_SESSION -device virtio-blk-pci,drive=homedisk,disable-modern=on,disable-legacy=off"
fi
fi
# Build display options based on architecture and headless mode
if [ "$ARCH" = "arm64" ]; then
# ARM64: Always add GPU and keyboard devices (needed for VirtIO enumeration)
# The -display option only controls whether a window appears
# Resolution is configured by the kernel's virtio-gpu driver via fw_cfg
if [ "$HEADLESS" = true ]; then
DISPLAY_OPTS="-display none -device virtio-gpu-device -device virtio-keyboard-device -device virtio-tablet-device"
else
DISPLAY_OPTS="-display cocoa -device virtio-gpu-device -device virtio-keyboard-device -device virtio-tablet-device"
fi
else
# x86_64 uses virtio-vga
if [ "$HEADLESS" = true ]; then
DISPLAY_OPTS="-display none"
else
DISPLAY_OPTS="-device virtio-vga -vnc :0 -k en-us"
fi
fi
if [ "$HEADLESS" = true ]; then
echo ""
echo "Running in headless mode. Serial output will appear below."
echo "Press Ctrl+C to stop."
echo ""
else
echo ""
if [ "$ARCH" = "arm64" ]; then
echo "Opening native window (cocoa display)..."
else
echo "VNC available at: localhost:$VNC_PORT"
fi
echo "Press Ctrl+C to stop."
echo ""
fi
# Audio options (VirtIO sound device)
AUDIO_OPTS="-audiodev coreaudio,id=audio0"
if [ "$ARCH" = "arm64" ]; then
AUDIO_OPTS="$AUDIO_OPTS -device virtio-sound-device,audiodev=audio0"
else
AUDIO_OPTS="$AUDIO_OPTS -device virtio-sound-pci,audiodev=audio0"
fi
# QMP socket for programmatic VM control (always enabled)
QMP_SOCK="/tmp/breenix-qmp.sock"
rm -f "$QMP_SOCK"
QMP_OPTS="-qmp unix:${QMP_SOCK},server,nowait"
# GDB stub (--debug flag)
# -s = GDB server on localhost:1234
# -S = halt CPU at startup (wait for GDB to continue)
GDB_OPTS=""
if [ "$DEBUG" = true ]; then
GDB_OPTS="-s -S"
echo "GDB stub: target remote :1234 (CPU halted, waiting for GDB)"
fi
# Pass resolution to kernel via fw_cfg
FW_CFG_OPTS="-fw_cfg name=opt/breenix/resolution,string=${RES_W}x${RES_H}"
# Build the full QEMU command based on architecture
if [ "$ARCH" = "arm64" ]; then
# ARM64 QEMU invocation (native)
# NOTE: Home disk must come BEFORE system disk because QEMU virt assigns
# MMIO addresses in reverse command-line order. The kernel discovers devices
# by scanning MMIO addresses low-to-high, so the LAST device here gets the
# lowest address and becomes device 0 (the system/root disk).
qemu-system-aarch64 \
-M virt -cpu cortex-a72 \
-smp 4 \
-m 512M \
-kernel "$KERNEL" \
$DISPLAY_OPTS \
$HOME_DISK_OPTS \
$DISK_OPTS \
-device virtio-net-device,netdev=net0 \