-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-retro-mac.sh
More file actions
executable file
·1762 lines (1539 loc) · 63.8 KB
/
setup-retro-mac.sh
File metadata and controls
executable file
·1762 lines (1539 loc) · 63.8 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
# Mac OS 9 Emulation Setup Script
# This script is idempotent - safe to run multiple times
# Author: Retro Mac Setup
# Date: $(date +%Y-%m-%d)
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored status messages
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Function to check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
}
# Function to check if a package is installed
is_package_installed() {
dpkg -l "$1" 2>/dev/null | grep -q "^ii"
}
# Function to check if a service is running
is_service_running() {
systemctl is-active --quiet "$1"
}
# Function to download file if not exists
download_if_needed() {
local url="$1"
local dest="$2"
local desc="$3"
if [ -f "$dest" ]; then
print_status "$desc already exists, skipping download"
else
print_status "Downloading $desc..."
if wget -q --show-progress -O "$dest" "$url"; then
print_success "$desc downloaded successfully"
else
print_error "Failed to download $desc"
rm -f "$dest"
return 1
fi
fi
}
# Function to fix existing installation
fix_existing_installation() {
print_status "Checking for existing installation issues..."
# Fix LightDM if it's using root
if [ -f "/etc/lightdm/lightdm.conf" ]; then
if grep -q "autologin-user=root" /etc/lightdm/lightdm.conf; then
print_warning "Found LightDM configured for root auto-login (this doesn't work)"
# Find appropriate user
if id retro >/dev/null 2>&1; then
FIX_USER="retro"
else
FIX_USER=$(getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' | head -1)
fi
if [ -n "$FIX_USER" ]; then
print_status "Fixing auto-login to use $FIX_USER..."
sed -i "s/autologin-user=root/autologin-user=$FIX_USER/g" /etc/lightdm/lightdm.conf
# Fix openbox autostart
USER_HOME=$(getent passwd "$FIX_USER" | cut -d: -f6)
mkdir -p "$USER_HOME/.config/openbox"
cat > "$USER_HOME/.config/openbox/autostart" << 'EOF'
#!/bin/bash
# Start QEMU Mac emulator after X11 starts
sleep 5
export DISPLAY=:0
sudo systemctl start qemu-mac.service &
EOF
chmod +x "$USER_HOME/.config/openbox/autostart"
chown -R "$FIX_USER:$FIX_USER" "$USER_HOME/.config"
# Fix QEMU service
if [ -f "/etc/systemd/system/qemu-mac.service" ]; then
sed -i "s|^User=.*|User=$FIX_USER|" /etc/systemd/system/qemu-mac.service
sed -i "s|^Group=.*|Group=$FIX_USER|" /etc/systemd/system/qemu-mac.service
sed -i "s|Environment=\"HOME=.*\"|Environment=\"HOME=$USER_HOME\"|" /etc/systemd/system/qemu-mac.service
sed -i "s|Environment=\"XAUTHORITY=.*\"|Environment=\"XAUTHORITY=$USER_HOME/.Xauthority\"|" /etc/systemd/system/qemu-mac.service
fi
# Add sudo permissions
if ! grep -q "$FIX_USER.*systemctl.*qemu-mac" /etc/sudoers; then
echo "" >> /etc/sudoers
echo "# Allow $FIX_USER to control QEMU service" >> /etc/sudoers
echo "$FIX_USER ALL=(ALL) NOPASSWD: /bin/systemctl start qemu-mac.service" >> /etc/sudoers
echo "$FIX_USER ALL=(ALL) NOPASSWD: /bin/systemctl stop qemu-mac.service" >> /etc/sudoers
echo "$FIX_USER ALL=(ALL) NOPASSWD: /bin/systemctl restart qemu-mac.service" >> /etc/sudoers
fi
# Fix ownership
chown -R "$FIX_USER:$FIX_USER" /opt/retro-mac/ 2>/dev/null || true
# Create/fix log files with proper permissions
touch /var/log/qemu-mac.log /var/log/qemu-mac-cmd.log
chown "$FIX_USER:$FIX_USER" /var/log/qemu-mac*.log
chmod 664 /var/log/qemu-mac*.log
systemctl daemon-reload
print_success "Fixed configuration to use $FIX_USER"
# Restart LightDM
print_status "Restarting display manager..."
systemctl restart lightdm
return 0
fi
fi
fi
return 1
}
# Function to update existing installation with new features
update_pointer_control() {
print_status "Checking for pointer control updates..."
# Update QEMU configuration with pointer settings if not present
if [ -f "/opt/retro-mac/qemu-config.json" ]; then
# Check if pointer_mode exists in config
if ! jq -e '.pointer_mode' /opt/retro-mac/qemu-config.json >/dev/null 2>&1; then
print_status "Adding pointer control settings to configuration..."
# Add pointer settings to config
jq '. + {"pointer_mode": "usb-tablet", "grab_on_click": true}' /opt/retro-mac/qemu-config.json > /tmp/qemu-config-new.json
mv /tmp/qemu-config-new.json /opt/retro-mac/qemu-config.json
chmod 644 /opt/retro-mac/qemu-config.json
chown www-data:www-data /opt/retro-mac/qemu-config.json 2>/dev/null || true
print_success "Pointer control settings added to configuration"
fi
fi
# Update startup script if it exists and is outdated
if [ -f "/opt/retro-mac/start-mac.sh" ]; then
if ! grep -q "POINTER_MODE" /opt/retro-mac/start-mac.sh; then
print_status "Updating QEMU startup script with pointer control..."
# Backup existing script
cp /opt/retro-mac/start-mac.sh /opt/retro-mac/start-mac.sh.backup-$(date +%Y%m%d)
# Extract the new startup script from this file and update
awk '/^cat > \/opt\/retro-mac\/start-mac.sh << .EOF.$/{flag=1; next} /^EOF$/ && flag{flag=0} flag' "$0" > /opt/retro-mac/start-mac.sh
chmod +x /opt/retro-mac/start-mac.sh
# Set proper ownership
if id retro >/dev/null 2>&1; then
chown retro:retro /opt/retro-mac/start-mac.sh
fi
print_success "QEMU startup script updated with pointer control"
fi
fi
}
# Main setup begins here
main() {
echo "=========================================="
echo "Mac OS 9 Emulation Setup Script"
echo "=========================================="
echo ""
# Check if running as root
check_root
# Check if this is fixing an existing installation
if [ -f "/opt/retro-mac/start-mac.sh" ] && [ -f "/etc/systemd/system/qemu-mac.service" ]; then
print_status "Existing installation detected"
# Try to fix any configuration issues
FIXED=false
if fix_existing_installation; then
FIXED=true
fi
# Update with new features
update_pointer_control
if [ "$FIXED" = true ]; then
echo ""
echo "=========================================="
echo "Existing Installation Updated!"
echo "=========================================="
echo ""
echo "The configuration has been corrected and updated."
echo "New features added:"
echo "• Pointer control settings (USB Tablet/Mouse/PS2)"
echo "• Mouse grab configuration"
echo ""
echo "Access the web control panel to configure pointer settings."
echo ""
exit 0
fi
fi
# Update package lists
print_status "Updating package lists..."
apt-get update -qq
print_success "Package lists updated"
# Install required packages
print_status "Installing required packages..."
# Apache2
if is_package_installed apache2; then
print_status "Apache2 already installed"
else
print_status "Installing Apache2..."
apt-get install -y apache2
print_success "Apache2 installed"
fi
# PHP and Apache PHP module
if is_package_installed php; then
print_status "PHP already installed"
else
print_status "Installing PHP and Apache PHP module..."
apt-get install -y php libapache2-mod-php php-cli
print_success "PHP installed"
fi
# QEMU
if is_package_installed qemu-system-ppc; then
print_status "QEMU PPC already installed"
else
print_status "Installing QEMU for PowerPC emulation..."
apt-get install -y qemu-system-ppc qemu-utils
print_success "QEMU PPC installed"
fi
# Plymouth
if is_package_installed plymouth; then
print_status "Plymouth already installed"
else
print_status "Installing Plymouth..."
apt-get install -y plymouth plymouth-themes plymouth-theme-ubuntu-text
print_success "Plymouth installed"
fi
# X11 and display manager for QEMU
print_status "Installing X11 and display components..."
if is_package_installed xorg; then
print_status "X11 already installed"
else
print_status "Installing X11 and minimal display manager..."
# Install minimal X11 and lightdm for auto-login
apt-get install -y xorg xinit lightdm lightdm-gtk-greeter openbox
print_success "X11 and display manager installed"
fi
# Additional utilities
print_status "Installing additional utilities..."
apt-get install -y wget curl imagemagick jq qemu-utils
print_success "Additional utilities installed"
# Create directories
print_status "Creating required directories..."
mkdir -p /var/www/html
mkdir -p /opt/retro-mac
mkdir -p /opt/retro-mac/images
mkdir -p /usr/share/plymouth/themes/retro-mac
print_success "Directories created"
# Download boot image
print_status "Downloading boot image..."
download_if_needed "https://www.mcchord.net/static/macTest.png" \
"/opt/retro-mac/images/macTest.png" \
"Mac boot image"
# Configure Plymouth theme
print_status "Configuring Plymouth theme..."
# Create Plymouth theme script
cat > /usr/share/plymouth/themes/retro-mac/retro-mac.script << 'EOF'
# Plymouth Theme: Retro Mac
# Shows centered Mac image during boot
Window.SetBackgroundTopColor(0.0, 0.0, 0.0);
Window.SetBackgroundBottomColor(0.0, 0.0, 0.0);
# Load and center the image
logo.image = Image("macTest.png");
logo.sprite = Sprite(logo.image);
# Get screen dimensions
screen.width = Window.GetWidth();
screen.height = Window.GetHeight();
# Center the image
logo.x = screen.width / 2 - logo.image.GetWidth() / 2;
logo.y = screen.height / 2 - logo.image.GetHeight() / 2;
logo.sprite.SetPosition(logo.x, logo.y, 0);
# Progress callback (optional)
fun progress_callback (duration, progress) {
# Keep image centered
}
Plymouth.SetUpdateStatusFunction(progress_callback);
EOF
# Create Plymouth theme configuration
cat > /usr/share/plymouth/themes/retro-mac/retro-mac.plymouth << 'EOF'
[Plymouth Theme]
Name=Retro Mac
Description=Mac OS 9 Boot Screen
ModuleName=script
[script]
ImageDir=/usr/share/plymouth/themes/retro-mac
ScriptFile=/usr/share/plymouth/themes/retro-mac/retro-mac.script
EOF
# Copy image to Plymouth theme directory
if [ -f "/opt/retro-mac/images/macTest.png" ]; then
cp /opt/retro-mac/images/macTest.png /usr/share/plymouth/themes/retro-mac/
print_success "Plymouth theme configured"
else
print_warning "Boot image not found for Plymouth theme"
fi
# Install and set Plymouth theme
if [ -f "/usr/share/plymouth/themes/retro-mac/retro-mac.plymouth" ]; then
# Install the theme
update-alternatives --install /usr/share/plymouth/themes/default.plymouth default.plymouth \
/usr/share/plymouth/themes/retro-mac/retro-mac.plymouth 100
# Set as default theme
plymouth-set-default-theme retro-mac
print_status "Plymouth theme set as default"
# Ensure Plymouth is enabled in kernel parameters
if ! grep -q "splash" /etc/default/grub; then
sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="\(.*\)"/GRUB_CMDLINE_LINUX_DEFAULT="\1 quiet splash"/' /etc/default/grub
print_status "Added splash to kernel parameters"
fi
# Update initramfs with the new theme
print_status "Updating initramfs for Plymouth..."
update-initramfs -u
print_success "Plymouth theme installed and configured"
fi
# Configure GRUB
print_status "Configuring GRUB boot screen..."
# Convert PNG to format suitable for GRUB if image exists
if [ -f "/opt/retro-mac/images/macTest.png" ]; then
print_status "Converting image for GRUB (centered, not stretched)..."
# Create a properly sized image with the original centered (not stretched)
convert /opt/retro-mac/images/macTest.png -background black -gravity center -extent 1024x768 /boot/grub/retro-mac.png
# Backup original GRUB config
if [ ! -f "/etc/default/grub.backup" ]; then
cp /etc/default/grub /etc/default/grub.backup
print_status "GRUB configuration backed up"
fi
# Update GRUB configuration
if ! grep -q "GRUB_BACKGROUND=/boot/grub/retro-mac.png" /etc/default/grub; then
echo "" >> /etc/default/grub
echo "# Retro Mac boot image" >> /etc/default/grub
echo "GRUB_BACKGROUND=/boot/grub/retro-mac.png" >> /etc/default/grub
fi
# Set GRUB graphics mode to prevent stretching
if grep -q "^#GRUB_GFXMODE" /etc/default/grub; then
sed -i 's/^#GRUB_GFXMODE=.*/GRUB_GFXMODE=1024x768/' /etc/default/grub
elif ! grep -q "^GRUB_GFXMODE" /etc/default/grub; then
echo "GRUB_GFXMODE=1024x768" >> /etc/default/grub
fi
# Keep the graphics mode for Linux
if ! grep -q "GRUB_GFXPAYLOAD_LINUX" /etc/default/grub; then
echo "GRUB_GFXPAYLOAD_LINUX=keep" >> /etc/default/grub
fi
# Set GRUB timeout for cleaner boot
sed -i 's/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=2/' /etc/default/grub
# Update GRUB
print_status "Updating GRUB..."
update-grub 2>/dev/null || print_warning "Could not update GRUB"
print_success "GRUB configured"
else
print_warning "Boot image not found for GRUB configuration"
fi
# Download Mac OS 9 ISO
print_status "Downloading Mac OS 9 ISO (this may take a while)..."
download_if_needed "http://www.mcchord.net/static/macos_921_ppc.iso" \
"/opt/retro-mac/macos_921_ppc.iso" \
"Mac OS 9.2.1 ISO"
# Create default QEMU configuration
print_status "Creating default QEMU configuration..."
cat > /opt/retro-mac/qemu-config.json << 'EOF'
{
"ram": "512",
"cpu": "g4",
"machine": "mac99,via=pmu",
"resolution": "1024x768x32",
"fullscreen": true,
"boot_device": "d",
"cdrom": "/opt/retro-mac/macos_921_ppc.iso",
"hard_drives": [],
"custom_args": "",
"network": "user",
"sound": true,
"pointer_mode": "usb-tablet",
"grab_on_click": true
}
EOF
chmod 644 /opt/retro-mac/qemu-config.json
print_success "Default configuration created"
# Create QEMU startup script
print_status "Creating QEMU startup script..."
cat > /opt/retro-mac/start-mac.sh << 'EOF'
#!/bin/bash
# QEMU Mac OS 9 Emulator Script with Configuration Support
CONFIG_FILE="/opt/retro-mac/qemu-config.json"
# Wait for X11 to be available
max_wait=30
wait_count=0
while [ -z "$DISPLAY" ] && [ $wait_count -lt $max_wait ]; do
export DISPLAY=:0
if xset q &>/dev/null; then
break
fi
sleep 1
wait_count=$((wait_count + 1))
done
# Kill any existing QEMU instances
pkill -f qemu-system-ppc || true
# Read configuration
if [ -f "$CONFIG_FILE" ]; then
RAM=$(jq -r '.ram' "$CONFIG_FILE")
CPU=$(jq -r '.cpu' "$CONFIG_FILE")
MACHINE=$(jq -r '.machine' "$CONFIG_FILE")
RESOLUTION=$(jq -r '.resolution' "$CONFIG_FILE")
FULLSCREEN=$(jq -r '.fullscreen' "$CONFIG_FILE")
BOOT=$(jq -r '.boot_device' "$CONFIG_FILE")
CDROM=$(jq -r '.cdrom' "$CONFIG_FILE")
CUSTOM_ARGS=$(jq -r '.custom_args // ""' "$CONFIG_FILE")
NETWORK=$(jq -r '.network // "user"' "$CONFIG_FILE")
SOUND=$(jq -r '.sound // true' "$CONFIG_FILE")
POINTER_MODE=$(jq -r '.pointer_mode // "usb-tablet"' "$CONFIG_FILE")
GRAB_ON_CLICK=$(jq -r '.grab_on_click // true' "$CONFIG_FILE")
else
# Defaults if config doesn't exist
RAM="512"
CPU="g4"
MACHINE="mac99,via=pmu"
RESOLUTION="1024x768x32"
FULLSCREEN="true"
BOOT="d"
CDROM="/opt/retro-mac/macos_921_ppc.iso"
CUSTOM_ARGS=""
NETWORK="user"
SOUND="true"
POINTER_MODE="usb-tablet"
GRAB_ON_CLICK="true"
fi
# Build QEMU command
CMD="qemu-system-ppc"
CMD="$CMD -M $MACHINE"
CMD="$CMD -m $RAM"
CMD="$CMD -cpu $CPU"
CMD="$CMD -g $RESOLUTION"
CMD="$CMD -boot $BOOT"
# Configure pointer/mouse based on settings
if [ "$POINTER_MODE" = "usb-tablet" ]; then
# USB tablet provides absolute positioning (no grab needed)
CMD="$CMD -device usb-tablet"
CMD="$CMD -device usb-kbd"
elif [ "$POINTER_MODE" = "usb-mouse" ]; then
# USB mouse with relative positioning (traditional grab)
CMD="$CMD -device usb-mouse"
CMD="$CMD -device usb-kbd"
elif [ "$POINTER_MODE" = "ps2" ]; then
# PS/2 mouse (legacy)
# PS/2 devices are included by default in mac99
CMD="$CMD -device usb-kbd"
else
# Default to USB tablet
CMD="$CMD -device usb-tablet"
CMD="$CMD -device usb-kbd"
fi
# Add CDROM if specified
if [ -n "$CDROM" ] && [ "$CDROM" != "null" ] && [ -f "$CDROM" ]; then
CMD="$CMD -drive file=$CDROM,format=raw,media=cdrom"
fi
# Add hard drives
if [ -f "$CONFIG_FILE" ]; then
DRIVES=$(jq -r '.hard_drives[]? | @base64' "$CONFIG_FILE")
for drive in $DRIVES; do
DRIVE_PATH=$(echo "$drive" | base64 -d | jq -r '.path')
DRIVE_FORMAT=$(echo "$drive" | base64 -d | jq -r '.format // "qcow2"')
if [ -f "$DRIVE_PATH" ]; then
CMD="$CMD -drive file=$DRIVE_PATH,format=$DRIVE_FORMAT"
fi
done
fi
# Network configuration
if [ "$NETWORK" = "none" ]; then
CMD="$CMD -netdev none,id=none"
else
CMD="$CMD -netdev $NETWORK,id=net0 -device rtl8139,netdev=net0"
fi
# Sound configuration
if [ "$SOUND" = "true" ]; then
CMD="$CMD -device ES1370"
fi
# Fullscreen
if [ "$FULLSCREEN" = "true" ]; then
CMD="$CMD -full-screen"
fi
# Display and grab settings
if [ "$GRAB_ON_CLICK" = "false" ]; then
CMD="$CMD -display sdl,grab-mod=rctrl"
else
CMD="$CMD -display sdl"
fi
# Add custom arguments
if [ -n "$CUSTOM_ARGS" ]; then
CMD="$CMD $CUSTOM_ARGS"
fi
# Create log files if they don't exist and ensure we can write to them
touch /var/log/qemu-mac-cmd.log /var/log/qemu-mac.log 2>/dev/null || true
# Try to write to system logs, fall back to user directory if that fails
if [ -w /var/log/qemu-mac-cmd.log ]; then
echo "Starting QEMU with: $CMD" > /var/log/qemu-mac-cmd.log
exec $CMD 2>/var/log/qemu-mac.log
else
# Fall back to user directory for logs
LOG_DIR="$HOME/.local/share/qemu-mac"
mkdir -p "$LOG_DIR"
echo "Starting QEMU with: $CMD" > "$LOG_DIR/qemu-mac-cmd.log"
exec $CMD 2>"$LOG_DIR/qemu-mac.log"
fi
EOF
chmod +x /opt/retro-mac/start-mac.sh
print_success "QEMU startup script created"
# Create systemd service for QEMU
print_status "Creating QEMU systemd service..."
cat > /etc/systemd/system/qemu-mac.service << 'EOF'
[Unit]
Description=QEMU Mac OS 9 Emulator
After=multi-user.target graphical.target display-manager.service
Wants=display-manager.service
[Service]
Type=simple
# Dynamic user detection - will be updated during installation
User=retro
Group=retro
Environment="HOME=/home/retro"
Environment="USER=retro"
Environment="DISPLAY=:0"
Environment="SDL_VIDEODRIVER=x11"
Environment="XAUTHORITY=/home/retro/.Xauthority"
ExecStartPre=/bin/bash -c 'until xset q &>/dev/null; do sleep 1; done'
ExecStart=/opt/retro-mac/start-mac.sh
Restart=on-failure
RestartSec=10
StartLimitBurst=3
StartLimitInterval=60
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=graphical.target
EOF
# Configure LightDM for auto-login
print_status "Configuring display manager for auto-login..."
if [ -f "/etc/lightdm/lightdm.conf" ] || [ -d "/etc/lightdm" ]; then
# Determine the non-root user to use for auto-login
# Use the first regular user (UID >= 1000) or 'retro' if it exists
if id retro >/dev/null 2>&1; then
AUTO_USER="retro"
else
AUTO_USER=$(getent passwd | awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' | head -1)
if [ -z "$AUTO_USER" ]; then
print_warning "No regular user found for auto-login, skipping auto-login setup"
AUTO_USER=""
fi
fi
if [ -n "$AUTO_USER" ]; then
print_status "Setting up auto-login for user: $AUTO_USER"
# Create or update LightDM configuration
cat > /etc/lightdm/lightdm.conf << EOF
[Seat:*]
autologin-user=$AUTO_USER
autologin-user-timeout=0
user-session=openbox
greeter-show-manual-login=true
greeter-hide-users=false
[SeatDefaults]
autologin-user=$AUTO_USER
autologin-user-timeout=0
EOF
print_success "LightDM configured for auto-login with user $AUTO_USER"
# Create PAM autologin configuration
cat > /etc/pam.d/lightdm-autologin << 'EOF'
# PAM configuration for LightDM autologin
auth requisite pam_nologin.so
auth required pam_succeed_if.so user != root quiet_success
auth required pam_permit.so
@include common-account
session optional pam_keyinit.so force revoke
session required pam_limits.so
@include common-session
@include common-password
EOF
print_success "PAM autologin configured"
# Create openbox autostart for QEMU
USER_HOME=$(getent passwd "$AUTO_USER" | cut -d: -f6)
mkdir -p "$USER_HOME/.config/openbox"
cat > "$USER_HOME/.config/openbox/autostart" << 'EOF'
#!/bin/bash
# Start QEMU Mac emulator after X11 starts
sleep 5
export DISPLAY=:0
sudo systemctl start qemu-mac.service &
EOF
chmod +x "$USER_HOME/.config/openbox/autostart"
chown -R "$AUTO_USER:$AUTO_USER" "$USER_HOME/.config"
print_success "Openbox autostart configured for $AUTO_USER"
# Add sudo permissions for the auto-login user
if ! grep -q "$AUTO_USER.*systemctl.*qemu-mac" /etc/sudoers; then
echo "" >> /etc/sudoers
echo "# Allow $AUTO_USER to control QEMU service" >> /etc/sudoers
echo "$AUTO_USER ALL=(ALL) NOPASSWD: /bin/systemctl start qemu-mac.service" >> /etc/sudoers
echo "$AUTO_USER ALL=(ALL) NOPASSWD: /bin/systemctl stop qemu-mac.service" >> /etc/sudoers
echo "$AUTO_USER ALL=(ALL) NOPASSWD: /bin/systemctl restart qemu-mac.service" >> /etc/sudoers
echo "$AUTO_USER ALL=(ALL) NOPASSWD: /bin/systemctl status qemu-mac.service" >> /etc/sudoers
print_success "Sudo permissions configured for $AUTO_USER"
fi
fi
fi
# Update QEMU service with correct user
if [ -n "$AUTO_USER" ] && [ -f "/etc/systemd/system/qemu-mac.service" ]; then
print_status "Updating QEMU service to run as $AUTO_USER..."
USER_HOME=$(getent passwd "$AUTO_USER" | cut -d: -f6)
# Update the service file with the correct user
sed -i "s|^User=.*|User=$AUTO_USER|" /etc/systemd/system/qemu-mac.service
sed -i "s|^Group=.*|Group=$AUTO_USER|" /etc/systemd/system/qemu-mac.service
sed -i "s|Environment=\"HOME=.*\"|Environment=\"HOME=$USER_HOME\"|" /etc/systemd/system/qemu-mac.service
sed -i "s|Environment=\"USER=.*\"|Environment=\"USER=$AUTO_USER\"|" /etc/systemd/system/qemu-mac.service
sed -i "s|Environment=\"XAUTHORITY=.*\"|Environment=\"XAUTHORITY=$USER_HOME/.Xauthority\"|" /etc/systemd/system/qemu-mac.service
# Ensure user has necessary permissions
usermod -a -G video,audio,input "$AUTO_USER" 2>/dev/null || true
# Set ownership of retro-mac directory
chown -R "$AUTO_USER:$AUTO_USER" /opt/retro-mac/
chmod 755 /opt/retro-mac
# Create log files with proper permissions
touch /var/log/qemu-mac.log /var/log/qemu-mac-cmd.log
chown "$AUTO_USER:$AUTO_USER" /var/log/qemu-mac*.log
chmod 664 /var/log/qemu-mac*.log
print_success "QEMU service configured for user $AUTO_USER"
fi
# Reload systemd and enable service
systemctl daemon-reload
if ! systemctl is-enabled qemu-mac.service >/dev/null 2>&1; then
systemctl enable qemu-mac.service
print_success "QEMU service enabled for boot startup"
else
print_status "QEMU service already enabled"
fi
# Enable LightDM
if systemctl list-unit-files | grep -q lightdm; then
systemctl enable lightdm 2>/dev/null || true
print_status "LightDM display manager enabled"
fi
# Create PHP control panel
print_status "Creating PHP control panel..."
cat > /var/www/html/index.php << 'EOF'
<?php
// Mac OS 9 Emulator Control Panel with Classic Mac OS 9 Interface Design
$config_file = '/opt/retro-mac/qemu-config.json';
$drives_dir = '/opt/retro-mac/drives';
// Create drives directory if it doesn't exist
if (!file_exists($drives_dir)) {
mkdir($drives_dir, 0755, true);
}
// Load configuration
function load_config() {
global $config_file;
if (file_exists($config_file)) {
return json_decode(file_get_contents($config_file), true);
}
return [
'ram' => '512',
'cpu' => 'g4',
'machine' => 'mac99,via=pmu',
'resolution' => '1024x768x32',
'fullscreen' => true,
'boot_device' => 'd',
'cdrom' => '/opt/retro-mac/macos_921_ppc.iso',
'hard_drives' => [],
'custom_args' => '',
'network' => 'user',
'sound' => true,
'pointer_mode' => 'usb-tablet',
'grab_on_click' => true
];
}
// Save configuration
function save_config($config) {
global $config_file;
file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT));
chmod($config_file, 0644);
}
$config = load_config();
$message = '';
$message_type = '';
// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'start':
exec('sudo systemctl start qemu-mac.service 2>&1', $output, $return);
$message = $return === 0 ? 'QEMU started successfully' : 'Failed to start QEMU';
$message_type = $return === 0 ? 'success' : 'error';
break;
case 'stop':
exec('sudo systemctl stop qemu-mac.service 2>&1', $output, $return);
$message = $return === 0 ? 'QEMU stopped successfully' : 'Failed to stop QEMU';
$message_type = $return === 0 ? 'success' : 'error';
break;
case 'restart':
exec('sudo systemctl restart qemu-mac.service 2>&1', $output, $return);
$message = $return === 0 ? 'QEMU restarted successfully' : 'Failed to restart QEMU';
$message_type = $return === 0 ? 'success' : 'error';
break;
case 'save_config':
// Update configuration from form
$config['ram'] = $_POST['ram'] ?? '512';
$config['cpu'] = $_POST['cpu'] ?? 'g4';
$config['resolution'] = $_POST['resolution'] ?? '1024x768x32';
$config['fullscreen'] = isset($_POST['fullscreen']);
$config['boot_device'] = $_POST['boot_device'] ?? 'd';
$config['custom_args'] = $_POST['custom_args'] ?? '';
$config['network'] = $_POST['network'] ?? 'user';
$config['sound'] = isset($_POST['sound']);
$config['pointer_mode'] = $_POST['pointer_mode'] ?? 'usb-tablet';
$config['grab_on_click'] = isset($_POST['grab_on_click']);
save_config($config);
$message = 'Configuration saved successfully';
$message_type = 'success';
break;
case 'create_drive':
$size = $_POST['drive_size'] ?? '2G';
$name = preg_replace('/[^a-zA-Z0-9_-]/', '', $_POST['drive_name'] ?? 'disk');
$format = $_POST['drive_format'] ?? 'qcow2';
if ($name) {
$drive_path = "$drives_dir/$name.$format";
if (!file_exists($drive_path)) {
exec("sudo qemu-img create -f $format $drive_path $size 2>&1", $output, $return);
if ($return === 0) {
$config['hard_drives'][] = ['path' => $drive_path, 'format' => $format];
save_config($config);
$message = "Drive '$name' created successfully";
$message_type = 'success';
} else {
$message = 'Failed to create drive: ' . implode(' ', $output);
$message_type = 'error';
}
} else {
$message = 'Drive already exists';
$message_type = 'error';
}
}
break;
case 'remove_drive':
$index = intval($_POST['drive_index'] ?? -1);
if ($index >= 0 && isset($config['hard_drives'][$index])) {
array_splice($config['hard_drives'], $index, 1);
save_config($config);
$message = 'Drive removed from configuration';
$message_type = 'success';
}
break;
}
}
}
// Reload config after changes
$config = load_config();
// Get system information
$hostname = gethostname();
$uptime = shell_exec('uptime -p');
$load = sys_getloadavg();
$memory = shell_exec("free -h | grep Mem | awk '{print $3 \" / \" $2}'");
$disk = shell_exec("df -h / | tail -1 | awk '{print $3 \" / \" $2 \" (\" $5 \" used)\"}'");
// Check QEMU status
$qemu_status = trim(shell_exec('systemctl is-active qemu-mac.service'));
$qemu_running = ($qemu_status === 'active');
// Get CPU info
$cpu_info = shell_exec("lscpu | grep 'Model name' | cut -d: -f2 | xargs");
$cpu_cores = shell_exec("nproc");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mac OS 9 Control Panel</title>
<style>
@font-face {
font-family: 'Chicago';
src: local('Chicago'), local('Geneva'), local('Helvetica'), sans-serif;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Chicago', 'Geneva', 'Helvetica', sans-serif;
font-size: 12px;
background: #DDDDDD;
background-image:
repeating-linear-gradient(
0deg,
transparent,
transparent 1px,
#CCCCCC 1px,
#CCCCCC 2px
);
color: #000000;
padding: 0;
margin: 0;
min-height: 100vh;
}
/* Mac OS 9 Desktop Menu Bar */
.menu-bar {
background: linear-gradient(to bottom, #FFFFFF 0%, #DDDDDD 50%, #BBBBBB 100%);
border-bottom: 1px solid #000000;
height: 20px;
display: flex;
align-items: center;
padding: 0 10px;
font-weight: bold;
font-size: 12px;
}
.menu-bar .apple-menu {
margin-right: 20px;
font-size: 14px;
}
.menu-bar .menu-item {
margin-right: 15px;
cursor: default;
}
.menu-bar .right-menu {
margin-left: auto;
display: flex;
align-items: center;
}
/* Container */
.desktop {
padding: 20px;
display: flex;
flex-wrap: wrap;
gap: 20px;
justify-content: center;
}
/* Classic Mac OS 9 Window */
.window {
background: #DDDDDD;
border: 1px solid #000000;
box-shadow: 2px 2px 0px #000000;
min-width: 400px;
max-width: 600px;
}
/* Window Title Bar */
.window-titlebar {
background: linear-gradient(
to bottom,
#FFFFFF 0%,
#DDDDDD 45%,
#BBBBBB 50%,
#999999 100%
);
background-size: 100% 4px;
border-bottom: 1px solid #000000;
height: 20px;
display: flex;
align-items: center;
padding: 0 8px;
position: relative;
cursor: default;
}
.window-titlebar.active {
background: repeating-linear-gradient(
to bottom,
#FFFFFF 0px,
#FFFFFF 1px,
#000000 1px,
#000000 2px,
#FFFFFF 2px,
#FFFFFF 3px,
#BBBBBB 3px,
#BBBBBB 4px
);
}
/* Window Controls */
.window-controls {
display: flex;
gap: 8px;