-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsystemmanager.cpp
More file actions
1745 lines (1507 loc) · 64.9 KB
/
systemmanager.cpp
File metadata and controls
1745 lines (1507 loc) · 64.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
#include "systemmanager.h"
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QStandardPaths>
#include <QDateTime>
#include <QRegularExpression>
SystemManager::SystemManager(QObject *parent)
: QObject(parent)
, m_currentProcess(nullptr)
, m_currentOperation("")
, m_progressTimer(new QTimer(this))
, m_simulatedProgress(0)
{
connect(m_progressTimer, &QTimer::timeout, [this]() {
m_simulatedProgress += 2;
if (m_simulatedProgress <= 95) {
emit progressUpdated(m_simulatedProgress);
}
});
}
void SystemManager::extractDrivers()
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "extract_drivers";
emit statusUpdated("Starting Orange Pi 5+ driver extraction...");
if (!checkPrerequisites()) {
emit operationCompleted(false, "Prerequisites check failed");
return;
}
// Detect GPU drivers in /gpu directory
QString gpuPath = detectGpuDrivers();
// Check for upgrade files in local directories
QString upgradeBase = "/home/snake/Arm-Pi-Tweaker/upgrade";
emit statusUpdated("Scanning upgrade directories for kernel files...");
// Search for kernel files in upgrade subdirectories
QStringList kernelPatterns = {"vmlinuz*", "initrd*", "config-*", "System.map-*"};
QStringList kernelFiles = findFilesInDirectory(upgradeBase, kernelPatterns);
// Search for device tree files
QStringList dtPatterns = {"*.dtb", "*.dts"};
QStringList dtFiles = findFilesInDirectory(upgradeBase, dtPatterns);
// Search for module files
QStringList modulePatterns = {"*.ko", "modules.*"};
QStringList moduleFiles = findFilesInDirectory(upgradeBase, modulePatterns);
emit statusUpdated(QString("Found %1 kernel files, %2 device tree files, %3 module files")
.arg(kernelFiles.size()).arg(dtFiles.size()).arg(moduleFiles.size()));
if (kernelFiles.isEmpty() && dtFiles.isEmpty() && moduleFiles.isEmpty() && gpuPath.isEmpty()) {
emit operationCompleted(false,
"No extractable files found in /gpu or /upgrade directories. "
"Please ensure upgrade.img is extracted or kernel files are present.");
return;
}
// Create destination directory structure
QString destPath = "/home/snake/Arm-Pi-Tweaker/extracted_drivers";
QDir().mkpath(destPath + "/boot");
QDir().mkpath(destPath + "/lib/modules");
QDir().mkpath(destPath + "/lib/firmware");
QDir().mkpath(destPath + "/usr/lib/aarch64-linux-gnu");
QDir().mkpath(destPath + "/etc/X11");
QDir().mkpath(destPath + "/gpu");
// Start extraction process
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
connect(m_currentProcess, &QProcess::readyReadStandardError, this, &SystemManager::onProcessOutput);
// Create comprehensive extraction script
QString script = QString(
"#!/bin/bash\n"
"set -e\n"
"UPGRADE_BASE='%1'\n"
"GPU_PATH='%2'\n"
"DEST='%3'\n"
"COPIED_COUNT=0\n"
"\n"
"log_copy() {\n"
" echo \"[$(date '+%%H:%%M:%%S')] $1\"\n"
"}\n"
"\n"
"safe_copy_file() {\n"
" local src=\"$1\"\n"
" local dst_dir=\"$2\"\n"
" local desc=\"$3\"\n"
" \n"
" if [ -f \"$src\" ]; then\n"
" mkdir -p \"$dst_dir\"\n"
" cp -v \"$src\" \"$dst_dir/\" && COPIED_COUNT=$((COPIED_COUNT + 1))\n"
" log_copy \"Copied $desc: $src -> $dst_dir/\"\n"
" return 0\n"
" fi\n"
" return 1\n"
"}\n"
"\n"
"safe_copy_dir() {\n"
" local src=\"$1\"\n"
" local dst=\"$2\"\n"
" local desc=\"$3\"\n"
" \n"
" if [ -d \"$src\" ]; then\n"
" mkdir -p \"$dst\"\n"
" cp -rv \"$src\"/* \"$dst/\" && COPIED_COUNT=$((COPIED_COUNT + 1))\n"
" log_copy \"Copied $desc: $src -> $dst\"\n"
" return 0\n"
" fi\n"
" return 1\n"
"}\n"
"\n"
"log_copy \"🔍 Starting comprehensive Orange Pi 5+ extraction...\"\n"
"\n"
"# Extract GPU drivers from /gpu directory\n"
"if [ -n \"$GPU_PATH\" ] && [ -d \"$GPU_PATH\" ]; then\n"
" log_copy \"📱 Extracting GPU drivers from $GPU_PATH...\"\n"
" \n"
" # Copy all GPU subdirectories\n"
" find \"$GPU_PATH\" -mindepth 1 -maxdepth 1 -type d | while read gpu_subdir; do\n"
" subdir_name=$(basename \"$gpu_subdir\")\n"
" log_copy \"Processing GPU driver: $subdir_name\"\n"
" safe_copy_dir \"$gpu_subdir\" \"$DEST/gpu/$subdir_name\" \"$subdir_name GPU drivers\"\n"
" done\n"
" \n"
" # Copy individual GPU files\n"
" find \"$GPU_PATH\" -maxdepth 1 -name '*.deb' -o -name 'libmali*' -o -name '*.so*' | while read gpu_file; do\n"
" safe_copy_file \"$gpu_file\" \"$DEST/gpu\" \"GPU driver file\"\n"
" done\n"
"fi\n"
"\n"
"# Extract kernel and system files from upgrade directory\n"
"if [ -d \"$UPGRADE_BASE\" ]; then\n"
" log_copy \"🐧 Extracting kernel files from $UPGRADE_BASE...\"\n"
" \n"
" # Find and copy kernel files\n"
" find \"$UPGRADE_BASE\" -name 'vmlinuz*' -type f | while read kernel; do\n"
" safe_copy_file \"$kernel\" \"$DEST/boot\" \"kernel image\"\n"
" done\n"
" \n"
" find \"$UPGRADE_BASE\" -name 'initrd*' -type f | while read initrd; do\n"
" safe_copy_file \"$initrd\" \"$DEST/boot\" \"initrd image\"\n"
" done\n"
" \n"
" find \"$UPGRADE_BASE\" -name 'config-*' -type f | while read config; do\n"
" safe_copy_file \"$config\" \"$DEST/boot\" \"kernel config\"\n"
" done\n"
" \n"
" find \"$UPGRADE_BASE\" -name 'System.map-*' -type f | while read sysmap; do\n"
" safe_copy_file \"$sysmap\" \"$DEST/boot\" \"kernel symbols\"\n"
" done\n"
" \n"
" # Find and copy device tree files\n"
" log_copy \"🌳 Extracting device tree files...\"\n"
" find \"$UPGRADE_BASE\" -name '*.dtb' -o -name '*.dts' | while read dt_file; do\n"
" safe_copy_file \"$dt_file\" \"$DEST/boot/dtb\" \"device tree file\"\n"
" done\n"
" \n"
" # Find and copy module directories\n"
" log_copy \"🔧 Extracting kernel modules...\"\n"
" find \"$UPGRADE_BASE\" -path '*/lib/modules/*' -type d -name '[0-9]*' | while read module_dir; do\n"
" module_version=$(basename \"$module_dir\")\n"
" safe_copy_dir \"$module_dir\" \"$DEST/lib/modules/$module_version\" \"kernel modules $module_version\"\n"
" done\n"
" \n"
" # Find and copy firmware\n"
" log_copy \"💾 Extracting firmware...\"\n"
" find \"$UPGRADE_BASE\" -path '*/lib/firmware' -type d | while read fw_dir; do\n"
" safe_copy_dir \"$fw_dir\" \"$DEST/lib/firmware\" \"firmware files\"\n"
" done\n"
"fi\n"
"\n"
"# Create extraction manifest\n"
"MANIFEST=\"$DEST/extraction_manifest.txt\"\n"
"echo \"# Arm-Pi Tweaker Extraction Manifest\" > \"$MANIFEST\"\n"
"echo \"Extraction Date: $(date)\" >> \"$MANIFEST\"\n"
"echo \"GPU Path: $GPU_PATH\" >> \"$MANIFEST\"\n"
"echo \"Upgrade Base: $UPGRADE_BASE\" >> \"$MANIFEST\"\n"
"echo \"Items Copied: $COPIED_COUNT\" >> \"$MANIFEST\"\n"
"echo \"\" >> \"$MANIFEST\"\n"
"echo \"Extracted Files:\" >> \"$MANIFEST\"\n"
"find \"$DEST\" -type f | sort >> \"$MANIFEST\"\n"
"\n"
"log_copy \"✅ Extraction completed - $COPIED_COUNT items copied\"\n"
"log_copy \"📄 Manifest: $MANIFEST\"\n"
"log_copy \"📁 Files extracted to: $DEST\"\n"
).arg(upgradeBase, gpuPath, destPath);
// Write extraction script
QString scriptPath = "/tmp/extract_armpi_drivers.sh";
QFile scriptFile(scriptPath);
if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&scriptFile);
out << script;
scriptFile.close();
// Make script executable
QProcess::execute("chmod", QStringList() << "+x" << scriptPath);
// Start extraction with progress tracking
m_simulatedProgress = 0;
m_progressTimer->start(1000);
emit progressUpdated(0);
m_currentProcess->start("bash", QStringList() << scriptPath);
} else {
emit operationCompleted(false, "Failed to create extraction script");
}
}
void SystemManager::runUbuntuUpgrade()
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "ubuntu_upgrade";
emit statusUpdated("Preparing Ubuntu upgrade to 24.10...");
// Check prerequisites first
if (!checkUpgradePrerequisites()) {
emit operationCompleted(false, "Prerequisites check failed for Ubuntu upgrade");
return;
}
// Prepare system for upgrade
if (!prepareSystemForUpgrade()) {
emit operationCompleted(false, "Failed to prepare system for upgrade");
return;
}
emit statusUpdated("Starting Ubuntu upgrade to 24.10...");
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
connect(m_currentProcess, &QProcess::readyReadStandardError, this, &SystemManager::onProcessOutput);
m_simulatedProgress = 0;
m_progressTimer->start(2000); // Slower progress for long upgrade
emit progressUpdated(0);
// Set environment for non-interactive upgrade
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("DEBIAN_FRONTEND", "noninteractive");
env.insert("DEBIAN_PRIORITY", "critical");
m_currentProcess->setProcessEnvironment(env);
// Run the actual Ubuntu upgrade
m_currentProcess->start("bash", QStringList() << "-c" <<
"sudo DEBIAN_FRONTEND=noninteractive do-release-upgrade -f DistUpgradeViewNonInteractive -d");
}
void SystemManager::patchSystem()
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "patch_system";
emit statusUpdated("Preparing to patch system with Orange Pi 5+ support...");
// Verify upgrade files exist
QString upgradeDir = "/home/snake/Arm-Pi-Tweaker/upgrade";
if (!QDir(upgradeDir).exists()) {
emit operationCompleted(false, "Upgrade directory not found - run driver extraction first");
return;
}
// Create backup before patching
createBackup();
emit statusUpdated("Patching system with Orange Pi 5+ support...");
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
connect(m_currentProcess, &QProcess::readyReadStandardError, this, &SystemManager::onProcessOutput);
// Create comprehensive patching script
QString script = QString(
"#!/bin/bash\n"
"set -e\n"
"UPGRADE_DIR='%1'\n"
"PATCHED_COUNT=0\n"
"\n"
"log_patch() {\n"
" echo \"[$(date '+%%H:%%M:%%S')] $1\"\n"
"}\n"
"\n"
"safe_patch() {\n"
" local src=\"$1\"\n"
" local dst=\"$2\"\n"
" local desc=\"$3\"\n"
" \n"
" if [ -e \"$src\" ]; then\n"
" log_patch \"Installing $desc: $src -> $dst\"\n"
" sudo cp -rv \"$src\" \"$dst\" && PATCHED_COUNT=$((PATCHED_COUNT + 1))\n"
" return 0\n"
" else\n"
" log_patch \"Not found: $src (skipping $desc)\"\n"
" return 1\n"
" fi\n"
"}\n"
"\n"
"safe_patch_glob() {\n"
" local pattern=\"$1\"\n"
" local dst=\"$2\"\n"
" local desc=\"$3\"\n"
" local found=false\n"
" \n"
" for file in $pattern; do\n"
" if [ -e \"$file\" ]; then\n"
" safe_patch \"$file\" \"$dst\" \"$desc\"\n"
" found=true\n"
" fi\n"
" done\n"
" \n"
" if [ \"$found\" = false ]; then\n"
" log_patch \"No files found matching: $pattern\"\n"
" fi\n"
"}\n"
"\n"
"log_patch \"🚀 Starting Orange Pi 5+ system patching...\"\n"
"\n"
"# Install kernel files\n"
"log_patch \"📦 Installing kernel files...\"\n"
"safe_patch_glob \"$UPGRADE_DIR/boot/vmlinuz*\" \"/boot/\" \"kernel image\"\n"
"safe_patch_glob \"$UPGRADE_DIR/boot/initrd*\" \"/boot/\" \"initrd image\"\n"
"safe_patch_glob \"$UPGRADE_DIR/boot/config-*\" \"/boot/\" \"kernel config\"\n"
"safe_patch_glob \"$UPGRADE_DIR/boot/System.map-*\" \"/boot/\" \"kernel symbols\"\n"
"\n"
"# Install device tree files\n"
"log_patch \"🌳 Installing device tree files...\"\n"
"if [ -d \"$UPGRADE_DIR/boot/dtbs\" ]; then\n"
" safe_patch \"$UPGRADE_DIR/boot/dtbs\" \"/boot/\" \"device tree files\"\n"
"fi\n"
"if [ -d \"$UPGRADE_DIR/boot/dtb\" ]; then\n"
" safe_patch \"$UPGRADE_DIR/boot/dtb\" \"/boot/\" \"device tree files\"\n"
"fi\n"
"\n"
"# Install kernel modules\n"
"log_patch \"🔧 Installing kernel modules...\"\n"
"if [ -d \"$UPGRADE_DIR/lib/modules\" ]; then\n"
" for module_dir in \"$UPGRADE_DIR\"/lib/modules/*; do\n"
" if [ -d \"$module_dir\" ]; then\n"
" module_name=$(basename \"$module_dir\")\n"
" safe_patch \"$module_dir\" \"/lib/modules/\" \"kernel modules for $module_name\"\n"
" fi\n"
" done\n"
"fi\n"
"\n"
"# Install firmware\n"
"log_patch \"💾 Installing firmware...\"\n"
"if [ -d \"$UPGRADE_DIR/lib/firmware\" ]; then\n"
" # Create firmware directory if it doesn't exist\n"
" sudo mkdir -p /lib/firmware\n"
" safe_patch \"$UPGRADE_DIR/lib/firmware/.\" \"/lib/firmware/\" \"firmware files\"\n"
"fi\n"
"\n"
"# Install GPU drivers\n"
"log_patch \"🎮 Installing GPU drivers...\"\n"
"if [ -d \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu\" ]; then\n"
" sudo mkdir -p /usr/lib/aarch64-linux-gnu\n"
" safe_patch_glob \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu/libmali*\" \"/usr/lib/aarch64-linux-gnu/\" \"Mali GPU drivers\"\n"
" safe_patch_glob \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu/libEGL*\" \"/usr/lib/aarch64-linux-gnu/\" \"EGL libraries\"\n"
" safe_patch_glob \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu/libGLES*\" \"/usr/lib/aarch64-linux-gnu/\" \"GLES libraries\"\n"
" \n"
" if [ -d \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu/dri\" ]; then\n"
" safe_patch \"$UPGRADE_DIR/usr/lib/aarch64-linux-gnu/dri\" \"/usr/lib/aarch64-linux-gnu/\" \"DRI drivers\"\n"
" fi\n"
"fi\n"
"\n"
"# Install X11 configuration\n"
"log_patch \"🖥️ Installing X11 configuration...\"\n"
"if [ -d \"$UPGRADE_DIR/etc/X11\" ]; then\n"
" sudo mkdir -p /etc/X11\n"
" if [ -d \"$UPGRADE_DIR/etc/X11/xorg.conf.d\" ]; then\n"
" safe_patch \"$UPGRADE_DIR/etc/X11/xorg.conf.d\" \"/etc/X11/\" \"X11 configuration directory\"\n"
" fi\n"
" if [ -f \"$UPGRADE_DIR/etc/X11/xorg.conf\" ]; then\n"
" safe_patch \"$UPGRADE_DIR/etc/X11/xorg.conf\" \"/etc/X11/\" \"X11 configuration file\"\n"
" fi\n"
"fi\n"
"\n"
"# Update system configuration\n"
"log_patch \"⚙️ Updating system configuration...\"\n"
"\n"
"# Update initramfs for all installed kernels\n"
"log_patch \"🔄 Updating initramfs...\"\n"
"if sudo update-initramfs -u -k all; then\n"
" log_patch \"✅ Initramfs updated successfully\"\n"
"else\n"
" log_patch \"⚠️ Initramfs update failed, trying specific kernel...\"\n"
" # Try updating for current kernel\n"
" CURRENT_KERNEL=$(uname -r)\n"
" sudo update-initramfs -u -k \"$CURRENT_KERNEL\" || log_patch \"❌ Failed to update initramfs\"\n"
"fi\n"
"\n"
"# Update GRUB bootloader\n"
"log_patch \"🥾 Updating GRUB bootloader...\"\n"
"if sudo update-grub; then\n"
" log_patch \"✅ GRUB updated successfully\"\n"
"else\n"
" log_patch \"❌ GRUB update failed\"\n"
"fi\n"
"\n"
"# Update library cache\n"
"log_patch \"📚 Updating library cache...\"\n"
"sudo ldconfig\n"
"\n"
"# Create patch manifest\n"
"MANIFEST_FILE=\"/home/snake/Arm-Pi-Tweaker/patch_manifest_$(date +%%Y%%m%%d_%%H%%M%%S).txt\"\n"
"echo \"# Orange Pi 5+ System Patch Manifest\" > \"$MANIFEST_FILE\"\n"
"echo \"Patch Date: $(date)\" >> \"$MANIFEST_FILE\"\n"
"echo \"Files Patched: $PATCHED_COUNT\" >> \"$MANIFEST_FILE\"\n"
"echo \"Kernel Version: $(uname -r)\" >> \"$MANIFEST_FILE\"\n"
"echo \"Ubuntu Version: $(lsb_release -d | cut -f2)\" >> \"$MANIFEST_FILE\"\n"
"echo \"\" >> \"$MANIFEST_FILE\"\n"
"echo \"Installed Files:\" >> \"$MANIFEST_FILE\"\n"
"find /boot -name '*orange*' -o -name '*rk3588*' -o -name '*mali*' 2>/dev/null | sort >> \"$MANIFEST_FILE\" || true\n"
"find /lib/modules -name '*rk3588*' -o -name '*mali*' 2>/dev/null | head -20 >> \"$MANIFEST_FILE\" || true\n"
"find /lib/firmware -name '*rk3588*' -o -name '*mali*' 2>/dev/null | head -20 >> \"$MANIFEST_FILE\" || true\n"
"\n"
"log_patch \"✅ Orange Pi 5+ system patching completed!\"\n"
"log_patch \"📊 Total files patched: $PATCHED_COUNT\"\n"
"log_patch \"📄 Patch manifest: $MANIFEST_FILE\"\n"
"log_patch \"🔄 Please reboot to complete the installation\"\n"
).arg(upgradeDir);
QString scriptPath = "/tmp/patch_opi5_system.sh";
QFile scriptFile(scriptPath);
if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&scriptFile);
out << script;
scriptFile.close();
QProcess::execute("chmod", QStringList() << "+x" << scriptPath);
m_simulatedProgress = 0;
m_progressTimer->start(1000);
emit progressUpdated(0);
m_currentProcess->start("bash", QStringList() << scriptPath);
} else {
emit operationCompleted(false, "Failed to create patching script");
}
}
void SystemManager::rollbackUpgrade()
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "rollback";
emit statusUpdated("Rolling back upgrade...");
QString backupDir = "/home/snake/Arm-Pi-Tweaker/backup";
if (!QDir(backupDir).exists()) {
emit operationCompleted(false, "No backup found to rollback to");
return;
}
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
// Create rollback script
QString script = QString(
"#!/bin/bash\n"
"set -e\n"
"BACKUP_DIR='%1'\n"
"echo 'Restoring from backup...'\n"
"sudo cp -rv \"$BACKUP_DIR\"/boot/* /boot/\n"
"sudo cp -rv \"$BACKUP_DIR\"/lib/* /lib/\n"
"echo 'Updating initramfs...'\n"
"sudo update-initramfs -u\n"
"echo 'Updating GRUB...'\n"
"sudo update-grub\n"
"echo 'Rollback completed successfully'\n"
).arg(backupDir);
QString scriptPath = "/tmp/rollback.sh";
QFile scriptFile(scriptPath);
if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&scriptFile);
out << script;
scriptFile.close();
QProcess::execute("chmod", QStringList() << "+x" << scriptPath);
m_simulatedProgress = 0;
m_progressTimer->start(500);
emit progressUpdated(0);
m_currentProcess->start("bash", QStringList() << scriptPath);
} else {
emit operationCompleted(false, "Failed to create rollback script");
}
}
void SystemManager::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
m_progressTimer->stop();
emit progressUpdated(100);
QString operation = m_currentOperation;
m_currentOperation.clear();
if (m_currentProcess) {
m_currentProcess->deleteLater();
m_currentProcess = nullptr;
}
if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
QString message;
if (operation == "extract_drivers") {
message = "✅ Orange Pi 5+ drivers extracted successfully";
} else if (operation == "ubuntu_upgrade") {
message = "✅ Ubuntu upgrade to 24.10 completed successfully";
} else if (operation == "patch_system") {
message = "✅ Orange Pi 5+ support patched successfully";
} else if (operation == "rollback") {
message = "✅ Rollback completed successfully";
} else {
message = "✅ Operation completed successfully";
}
emit statusUpdated(message);
emit operationCompleted(true, message);
} else {
QString message = QString("❌ Operation failed with exit code %1").arg(exitCode);
emit statusUpdated(message);
emit operationCompleted(false, message);
}
}
void SystemManager::onProcessError(QProcess::ProcessError error)
{
m_progressTimer->stop();
QString errorMessage;
switch (error) {
case QProcess::FailedToStart:
errorMessage = "Process failed to start";
break;
case QProcess::Crashed:
errorMessage = "Process crashed";
break;
case QProcess::Timedout:
errorMessage = "Process timed out";
break;
default:
errorMessage = "Unknown process error";
break;
}
emit statusUpdated(QString("❌ %1").arg(errorMessage));
emit operationCompleted(false, errorMessage);
if (m_currentProcess) {
m_currentProcess->deleteLater();
m_currentProcess = nullptr;
}
}
void SystemManager::onProcessOutput()
{
if (m_currentProcess) {
QByteArray data = m_currentProcess->readAllStandardOutput();
QString output = QString::fromUtf8(data).trimmed();
if (!output.isEmpty()) {
emit statusUpdated(output);
}
}
}
bool SystemManager::checkPrerequisites()
{
// Check if running as root or can sudo
QProcess process;
process.start("id", QStringList() << "-u");
process.waitForFinished();
if (process.readAllStandardOutput().trimmed() != "0") {
// Not root, check if sudo is available
QProcess sudoCheck;
sudoCheck.start("sudo", QStringList() << "-n" << "true");
sudoCheck.waitForFinished();
if (sudoCheck.exitCode() != 0) {
emit statusUpdated("⚠️ Root privileges required. Please run with sudo or configure passwordless sudo.");
return false;
}
}
return true;
}
QString SystemManager::getUpgradeSourcePath()
{
// Check for mounted upgrade image first
if (QDir("/mnt/upgrade").exists()) {
return "/mnt/upgrade";
}
// Check for local upgrade directory
QString localUpgrade = "/home/snake/Arm-Pi-Tweaker/upgrade";
if (QDir(localUpgrade).exists()) {
return localUpgrade;
}
return QString();
}
QString SystemManager::detectGpuDrivers()
{
QString gpuDir = "/home/snake/Arm-Pi-Tweaker/gpu";
emit statusUpdated("Scanning GPU driver directory...");
if (!QDir(gpuDir).exists()) {
emit statusUpdated("⚠️ GPU directory not found: " + gpuDir);
return QString();
}
// Look for common GPU driver types
QStringList driverTypes;
QDir gpu(gpuDir);
QStringList subdirs = gpu.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const QString &subdir : subdirs) {
QString fullPath = gpuDir + "/" + subdir;
QDir driverDir(fullPath);
// Check for common driver files
QStringList driverFiles = driverDir.entryList(QStringList() << "*.deb" << "libmali*" << "*.so*", QDir::Files);
if (!driverFiles.isEmpty()) {
driverTypes.append(subdir + " (" + QString::number(driverFiles.size()) + " files)");
emit statusUpdated(QString("Found GPU drivers in: %1 - %2 files").arg(fullPath).arg(driverFiles.size()));
}
}
if (driverTypes.isEmpty()) {
emit statusUpdated("⚠️ No GPU drivers found in " + gpuDir);
return QString();
}
emit statusUpdated(QString("✅ Detected GPU driver types: %1").arg(driverTypes.join(", ")));
return gpuDir;
}
QStringList SystemManager::findFilesInDirectory(const QString &directory, const QStringList &patterns)
{
QStringList foundFiles;
QDir dir(directory);
if (!dir.exists()) {
return foundFiles;
}
// Search recursively through subdirectories
QDirIterator it(directory, patterns, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
foundFiles.append(it.next());
}
return foundFiles;
}
bool SystemManager::checkUpgradePrerequisites()
{
emit statusUpdated("Checking upgrade prerequisites...");
// Check disk space (need at least 10GB free)
if (!checkDiskSpace()) {
emit statusUpdated("❌ Insufficient disk space for upgrade");
return false;
}
// Check internet connectivity
QProcess networkCheck;
networkCheck.start("ping", QStringList() << "-c" << "1" << "archive.ubuntu.com");
networkCheck.waitForFinished(5000);
if (networkCheck.exitCode() != 0) {
emit statusUpdated("❌ No internet connection to Ubuntu repositories");
return false;
}
// Check current Ubuntu version
QProcess versionCheck;
versionCheck.start("lsb_release", QStringList() << "-r" << "-s");
versionCheck.waitForFinished(2000);
QString currentVersion = versionCheck.readAllStandardOutput().trimmed();
if (!currentVersion.startsWith("22.04")) {
emit statusUpdated(QString("❌ Current version %1 is not supported for upgrade").arg(currentVersion));
return false;
}
emit statusUpdated("✅ Prerequisites check passed");
return true;
}
bool SystemManager::prepareSystemForUpgrade()
{
emit statusUpdated("Preparing system for upgrade...");
// Update package lists
if (!updatePackageLists()) {
return false;
}
// Fix any broken packages
if (!fixBrokenPackages()) {
return false;
}
// Install update-manager-core if not present
QProcess checkManager;
checkManager.start("dpkg", QStringList() << "-l" << "update-manager-core");
checkManager.waitForFinished(3000);
if (checkManager.exitCode() != 0) {
emit statusUpdated("Installing update-manager-core...");
QProcess installManager;
installManager.start("sudo", QStringList() << "apt" << "install" << "-y" << "update-manager-core");
installManager.waitForFinished(60000);
if (installManager.exitCode() != 0) {
emit statusUpdated("❌ Failed to install update-manager-core");
return false;
}
}
// Enable development release upgrades
QProcess enableDevel;
enableDevel.start("sudo", QStringList() << "sed" << "-i" << "s/Prompt=lts/Prompt=normal/" << "/etc/update-manager/release-upgrades");
enableDevel.waitForFinished(3000);
emit statusUpdated("✅ System prepared for upgrade");
return true;
}
bool SystemManager::checkDiskSpace()
{
QProcess dfCheck;
dfCheck.start("df", QStringList() << "/" << "--output=avail" << "-B1G");
dfCheck.waitForFinished(3000);
QString output = dfCheck.readAllStandardOutput();
QStringList lines = output.split('\n');
if (lines.size() >= 2) {
bool ok;
int availableGB = lines[1].trimmed().toInt(&ok);
if (ok && availableGB >= 10) {
emit statusUpdated(QString("✅ Sufficient disk space: %1GB available").arg(availableGB));
return true;
} else {
emit statusUpdated(QString("❌ Insufficient disk space: %1GB available, need 10GB").arg(availableGB));
return false;
}
}
emit statusUpdated("⚠️ Could not determine disk space, proceeding anyway");
return true;
}
bool SystemManager::updatePackageLists()
{
emit statusUpdated("Updating package lists...");
QProcess aptUpdate;
aptUpdate.start("sudo", QStringList() << "apt" << "update");
aptUpdate.waitForFinished(120000); // 2 minutes timeout
if (aptUpdate.exitCode() != 0) {
QString error = aptUpdate.readAllStandardError();
emit statusUpdated(QString("❌ Failed to update package lists: %1").arg(error));
return false;
}
emit statusUpdated("✅ Package lists updated");
return true;
}
bool SystemManager::fixBrokenPackages()
{
emit statusUpdated("Checking and fixing broken packages...");
// First check if there are broken packages
QProcess checkBroken;
checkBroken.start("apt", QStringList() << "list" << "--broken");
checkBroken.waitForFinished(10000);
QString brokenOutput = checkBroken.readAllStandardOutput();
if (brokenOutput.contains("WARNING: apt does not have a stable CLI interface")) {
// No broken packages found (just the warning)
emit statusUpdated("✅ No broken packages found");
return true;
}
// Fix broken packages
QProcess fixBroken;
fixBroken.start("sudo", QStringList() << "apt" << "--fix-broken" << "install" << "-y");
fixBroken.waitForFinished(300000); // 5 minutes timeout
if (fixBroken.exitCode() != 0) {
QString error = fixBroken.readAllStandardError();
emit statusUpdated(QString("❌ Failed to fix broken packages: %1").arg(error));
return false;
}
emit statusUpdated("✅ Broken packages fixed");
return true;
}
void SystemManager::createBackup()
{
QString backupDir = QString("/home/snake/Arm-Pi-Tweaker/backup_%1")
.arg(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss"));
emit statusUpdated(QString("Creating backup to %1...").arg(backupDir));
QDir().mkpath(backupDir);
QDir().mkpath(backupDir + "/boot");
QDir().mkpath(backupDir + "/lib");
// Create backup of important system files
QProcess bootBackup;
bootBackup.start("sudo", QStringList() << "cp" << "-r" << "/boot/." << backupDir + "/boot/");
bootBackup.waitForFinished(60000);
QProcess modulesBackup;
modulesBackup.start("sudo", QStringList() << "cp" << "-r" << "/lib/modules" << backupDir + "/lib/");
modulesBackup.waitForFinished(60000);
QProcess firmwareBackup;
firmwareBackup.start("sudo", QStringList() << "cp" << "-r" << "/lib/firmware" << backupDir + "/lib/");
firmwareBackup.waitForFinished(60000);
// Backup sources.list
QProcess sourcesBackup;
sourcesBackup.start("sudo", QStringList() << "cp" << "/etc/apt/sources.list" << backupDir + "/sources.list");
sourcesBackup.waitForFinished(5000);
emit statusUpdated(QString("💾 Backup created: %1").arg(backupDir));
}
// GPU Management Implementation
void SystemManager::installGpuDriver(const QString &driverPath)
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "install_gpu_driver";
emit statusUpdated(QString("Installing GPU driver: %1").arg(QFileInfo(driverPath).fileName()));
if (!QFile::exists(driverPath)) {
emit operationCompleted(false, "Driver file not found");
return;
}
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
// Create GPU driver installation script
QString script = QString(
"#!/bin/bash\n"
"set -e\n"
"DRIVER_PATH='%1'\n"
"DRIVER_NAME=$(basename \"$DRIVER_PATH\")\n"
"\n"
"log_gpu() {\n"
" echo \"[$(date '+%%H:%%M:%%S')] $1\"\n"
"}\n"
"\n"
"log_gpu \"🎮 Installing GPU driver: $DRIVER_NAME\"\n"
"\n"
"# Stop display manager if running\n"
"if systemctl is-active --quiet display-manager; then\n"
" log_gpu \"Stopping display manager...\"\n"
" sudo systemctl stop display-manager\n"
"fi\n"
"\n"
"# Install .deb package\n"
"if [[ \"$DRIVER_PATH\" == *.deb ]]; then\n"
" log_gpu \"Installing .deb package...\"\n"
" sudo dpkg -i \"$DRIVER_PATH\" || sudo apt-get install -f -y\n"
"elif [[ \"$DRIVER_PATH\" == *.tar.* ]]; then\n"
" log_gpu \"Extracting and installing from archive...\"\n"
" TEMP_DIR=$(mktemp -d)\n"
" tar -xf \"$DRIVER_PATH\" -C \"$TEMP_DIR\"\n"
" \n"
" # Look for install script\n"
" if [ -f \"$TEMP_DIR/install.sh\" ]; then\n"
" cd \"$TEMP_DIR\" && sudo bash install.sh\n"
" else\n"
" # Manual installation\n"
" find \"$TEMP_DIR\" -name '*.so*' | while read lib; do\n"
" sudo cp \"$lib\" /usr/lib/aarch64-linux-gnu/\n"
" done\n"
" fi\n"
" \n"
" rm -rf \"$TEMP_DIR\"\n"
"else\n"
" log_gpu \"❌ Unsupported driver format\"\n"
" exit 1\n"
"fi\n"
"\n"
"# Update library cache\n"
"log_gpu \"Updating library cache...\"\n"
"sudo ldconfig\n"
"\n"
"# Create/update GPU configuration\n"
"log_gpu \"Configuring GPU...\"\n"
"sudo mkdir -p /etc/X11/xorg.conf.d\n"
"\n"
"# Restart display manager\n"
"if systemctl list-unit-files | grep -q display-manager; then\n"
" log_gpu \"Restarting display manager...\"\n"
" sudo systemctl start display-manager\n"
"fi\n"
"\n"
"log_gpu \"✅ GPU driver installation completed\"\n"
"log_gpu \"Please reboot to ensure all changes take effect\"\n"
).arg(driverPath);
QString scriptPath = "/tmp/install_gpu_driver.sh";
QFile scriptFile(scriptPath);
if (scriptFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&scriptFile);
out << script;
scriptFile.close();
QProcess::execute("chmod", QStringList() << "+x" << scriptPath);
m_simulatedProgress = 0;
m_progressTimer->start(1000);
emit progressUpdated(0);
m_currentProcess->start("bash", QStringList() << scriptPath);
} else {
emit operationCompleted(false, "Failed to create installation script");
}
}
void SystemManager::removeGpuDriver(const QString &driverName)
{
if (m_currentProcess && m_currentProcess->state() != QProcess::NotRunning) {
emit statusUpdated("Another operation is already running");
return;
}
m_currentOperation = "remove_gpu_driver";
emit statusUpdated(QString("Removing GPU driver: %1").arg(driverName));
m_currentProcess = new QProcess(this);
connect(m_currentProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &SystemManager::onProcessFinished);
connect(m_currentProcess, &QProcess::errorOccurred, this, &SystemManager::onProcessError);
connect(m_currentProcess, &QProcess::readyReadStandardOutput, this, &SystemManager::onProcessOutput);
// Create GPU driver removal script
QString script = QString(
"#!/bin/bash\n"
"set -e\n"
"DRIVER_NAME='%1'\n"
"\n"
"log_gpu() {\n"
" echo \"[$(date '+%%H:%%M:%%S')] $1\"\n"