-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathHelpers.php
More file actions
1832 lines (1599 loc) · 64.9 KB
/
Helpers.php
File metadata and controls
1832 lines (1599 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
<?PHP
/* Copyright 2005-2025, Lime Technology
* Copyright 2012-2025, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
?>
<?
$docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Wrappers.php";
require_once "$docroot/webGui/include/Secure.php";
// Helper functions
function my_scale($value, &$unit, $decimals=NULL, $scale=NULL, $kilo=1000) {
global $display, $language;
$scale = $scale ?? $display['scale'];
$number = _var($display,'number','.,');
$units = explode(' ', ' '.($kilo==1000 ? ($language['prefix_SI'] ?? 'K M G T P E Z Y') : ($language['prefix_IEC'] ?? 'Ki Mi Gi Ti Pi Ei Zi Yi')));
$size = count($units);
if ($scale == 0 && ($decimals === NULL || $decimals < 0)) {
$decimals = 0;
$unit = '';
} else {
$base = $value ? intval(floor(log($value, $kilo))) : 0;
if ($scale > 0 && $base > $scale) $base = $scale;
if ($base > $size) $base = $size - 1;
$value /= pow($kilo, $base);
if ($decimals === NULL) $decimals = $value >= 100 ? 0 : ($value >= 10 ? 1 : (round($value*100)%100 === 0 ? 0 : 2));
elseif ($decimals < 0) $decimals = $value >= 100 || round($value*10)%10 === 0 ? 0 : abs($decimals);
if ($scale < 0 && round($value,-1) == 1000) {$value = 1; $base++;}
$unit = $units[$base]._('B');
}
return number_format($value, $decimals, $number[0], $value > 9999 ? $number[1] : '');
}
function my_number($value) {
global $display;
$number = _var($display,'number','.,');
return number_format($value, 0, $number[0], ($value >= 10000 ? $number[1] : ''));
}
function my_time($time, $fmt=NULL) {
global $display;
if (!$fmt) $fmt = _var($display,'date').(_var($display,'date')!='%c' ? ", "._var($display,'time') : "");
return $time ? my_date($fmt, $time) : _('unknown');
}
function my_temp($value) {
global $display;
$unit = _var($display,'unit','C');
$number = _var($display,'number','.,');
return is_numeric($value) ? (($unit == 'F' ? fahrenheit($value) : str_replace('.', $number[0], $value)).' °'.$unit) : $value;
}
function my_disk($name, $raw=false) {
global $display;
return _var($display,'raw') || $raw ? $name : ucfirst(preg_replace('/(\d+)$/',' $1',$name));
}
function my_disks($disk) {
return strpos(_var($disk,'status'),'_NP') === false;
}
function my_hyperlink($text, $link) {
return str_replace(['[',']'],["<a href=\"$link\">","</a>"],$text);
}
function main_only($disk) {
return _var($disk,'type') == 'Parity' || _var($disk,'type') == 'Data';
}
function parity_only($disk) {
return _var($disk,'type') == 'Parity';
}
function data_only($disk) {
return _var($disk,'type') == 'Data';
}
function cache_only($disk) {
return _var($disk,'type') == 'Cache';
}
function boot_only($disk) {
return _var($disk,'type') == 'Boot';
}
function luks_only($disk) {
return _var($disk,'type') == 'Data' || _var($disk,'type') == 'Cache';
}
function main_filter($disks) {
return array_filter($disks, 'main_only');
}
function parity_filter($disks) {
return array_filter($disks, 'parity_only');
}
function data_filter($disks) {
return array_filter($disks, 'data_only');
}
function cache_filter($disks) {
return array_filter($disks, 'cache_only');
}
function boot_filter($disks) {
return array_filter($disks, 'boot_only');
}
function luks_filter($disks) {
return array_filter($disks, 'luks_only');
}
function pools_filter($disks) {
$cache_pools = array_keys(cache_filter($disks));
return array_unique(array_map('prefix', $cache_pools));
}
function flash_filter($disks) {
$boot_pools = array_keys(boot_filter($disks));
return array_unique(array_map('prefix', $boot_pools));
}
function my_id($id) {
global $display;
$len = strlen($id);
$wwn = substr($id,-18);
return (_var($display,'wwn') || substr($wwn,0,2) != '_3' || preg_match('/.[_-]/',$wwn)) ? $id : substr($id,0,$len-18);
}
function my_word($num) {
$words = ['zero','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen','twenty','twenty-one','twenty-two','twenty-three','twenty-four','twenty-five','twenty-six','twenty-seven','twenty-eight','twenty-nine','thirty'];
return $num < count($words) ? _($words[$num],1) : $num;
}
function my_usage() {
global $disks, $var, $display;
$arraysize = 0;
$arrayfree = 0;
foreach ($disks as $disk) {
if (strpos(_var($disk,'name'),'disk') !== false) {
$arraysize += _var($disk,'sizeSb',0);
$arrayfree += _var($disk,'fsFree',0);
}
}
if (_var($var,'fsNumMounted',0) > 0) {
$used = $arraysize ? 100-round(100*$arrayfree/$arraysize) : 0;
echo "<div class='usage-bar'><span style='width:{$used}%' class='".usage_color($display,$used,false)."'>{$used}%</span></div>";
} else {
echo "<div class='usage-bar'><span style='text-align:center'>".($var['fsState']=='Started'?'Maintenance':'offline')."</span></div>";
}
}
function usage_color(&$disk, $limit, $free) {
global $display;
if (_var($display,'text',0) == 1 || intval(_var($display,'text',0)/10) == 1) return '';
$critical = _var($disk,'critical') >= 0 ? $disk['critical'] : (_var($display,'critical') >= 0 ? $display['critical'] : 0);
$warning = _var($disk,'warning') >= 0 ? $disk['warning'] : (_var($display,'warning') >= 0 ? $display['warning'] : 0);
if (!$free) {
if ($critical > 0 && $limit >= $critical) return 'redbar';
if ($warning > 0 && $limit >= $warning) return 'orangebar';
return 'greenbar';
} else {
if ($critical > 0 && $limit <= 100-$critical) return 'redbar';
if ($warning > 0 && $limit <= 100-$warning) return 'orangebar';
return 'greenbar';
}
}
function my_check($time, $speed) {
if (!$time) return _('unavailable (no parity-check entries logged)');
$days = floor($time/86400);
$hmss = $time-$days*86400;
$hour = floor($hmss/3600);
$mins = floor($hmss/60)%60;
$secs = $hmss%60;
return plus($days,'day',($hour|$mins|$secs) == 0).plus($hour,'hour',($mins|$secs) == 0).plus($mins,'minute',$secs == 0).plus($secs,'second',true).". "._('Average speed').": ".(is_numeric($speed) ? my_scale($speed,$unit,1)." $unit/s" : $speed);
}
function my_error($code) {
switch ($code) {
case -4:
return "<em>"._('aborted')."</em>";
default:
return "<strong>$code</strong>";
}
}
function mk_option($select, $value, $text, $extra="") {
return "<option value='$value'".($value == $select ? " selected" : "").(strlen($extra) ? " $extra" : "").">$text</option>";
}
function mk_option_check($name, $value, $text="") {
if ($text) {
$checked = in_array($value,explode(',',$name)) ? " selected" : "";
return "<option value='$value'$checked>$text</option>";
}
if (strpos($name,'disk') !== false) {
$checked = in_array($name,explode(',',$value)) ? " selected" : "";
return "<option value='$name'$checked>".my_disk($name)."</option>";
}
}
function mk_option_luks($name, $value, $luks) {
if (strpos($name,'disk') !== false) {
$checked = in_array($name,explode(',',$value)) ? " selected" : "";
return "<option luks='$luks' value='$name'$checked>".my_disk($name)."</option>";
}
}
function day_count($time) {
global $var;
if (!$time) return;
$datetz = new DateTimeZone($var['timeZone']);
$date = new DateTime("now", $datetz);
$offset = $datetz->getOffset($date);
$now = new DateTime("@".intval((time()+$offset)/86400)*86400);
$last = new DateTime("@".intval(($time+$offset)/86400)*86400);
$days = date_diff($last,$now)->format('%a');
switch (true) {
case ($days < 0):
return;
case ($days == 0):
return " <span class='green-text'>("._('today').")</span>";
case ($days == 1):
return " <span class='green-text'>("._('yesterday').")</span>";
case ($days <= 31):
return " <span class='green-text'>(".sprintf(_('%s days ago'),my_word($days)).")</span>";
case ($days <= 61):
return " <span class='orange-text'>(".sprintf(_('%s days ago'),$days).")</span>";
case ($days > 61):
return " <span class='red-text'>(".sprintf(_('%s days ago'),$days).")</span>";
}
}
function plus($val, $word, $last) {
return $val > 0 ? (($val || $last) ? ($val.' '._($word.($val != 1 ? 's' : '')).($last ? '' : ', ')) : '') : '';
}
function compress($name, $size=18, $end=6) {
return mb_strlen($name) <= $size ? $name : mb_substr($name, 0, $size-($end ? $end+3 : 0)).'...'.($end ? mb_substr($name,-$end) : '');
}
function escapestring($name) {
return "\"$name\"";
}
function tail($file, $rows=1) {
$file = new SplFileObject($file);
$file->seek(PHP_INT_MAX);
$file->seek($file->key()-$rows);
$echo = [];
while (!$file->eof()) {
$echo[] = $file->current();
$file->next();
}
return implode($echo);
}
/* Get the last parity check from the parity history. */
function last_parity_log() {
$log = '/boot/config/parity-checks.log';
if (file_exists($log)) {
[$date, $duration, $speed, $status, $error, $action, $size] = my_explode('|', tail($log), 7);
} else {
[$date, $duration, $speed, $status, $error, $action, $size] = array_fill(0, 7, 0);
}
if ($date) {
[$y, $m, $d, $t] = my_preg_split('/ +/', $date, 4);
$date = strtotime("$d-$m-$y $t");
}
return [$date, $duration, $speed, $status, $error, $action, $size];
}
/* Get the last parity check from Unraid. */
function last_parity_check() {
global $var;
/* Files for the latest parity check. */
$stamps = '/var/tmp/stamps.ini';
$resync = '/var/tmp/resync.ini';
/* Get the latest parity information from Unraid. */
$synced = file_exists($stamps) ? explode(',',file_get_contents($stamps)) : [];
$sbSynced = array_shift($synced) ?: _var($var,'sbSynced',0);
$idle = [];
while (count($synced) > 1) {
$idle[] = array_pop($synced) - array_pop($synced);
}
$action = _var($var, 'mdResyncAction');
$size = _var($var, 'mdResyncSize', 0);
if (file_exists($resync)) {
list($action, $size) = my_explode(',', file_get_contents($resync));
}
$duration = $var['sbSynced2']-$sbSynced-array_sum($idle);
$status = _var($var,'sbSyncExit');
$speed = $status==0 ? round($size*1024/$duration) : 0;
$error = _var($var,'sbSyncErrs',0);
return [$duration, $speed, $status, $error, $action, $size];
}
function urlencode_path($path) {
return str_replace("%2F", "/", urlencode($path));
}
function check_deprecated_filesystem($disk) {
$fsType = _var($disk, 'fsType', '');
$name = _var($disk, 'name', '');
$warnings = [];
// Check for ReiserFS
if (stripos($fsType, 'reiserfs') !== false) {
$warnings[] = [
'type' => 'reiserfs',
'severity' => 'critical',
'message' => _('ReiserFS is deprecated and is no longer supported in Unraid. You will need to downgrade to Unraid 7.2 to take action.')
];
}
// Check for XFS v4 (lacks CRC checksums)
if (stripos($fsType, 'xfs') !== false) {
// Check if disk is mounted to determine XFS version
$mountPoint = "/mnt/$name";
if (is_dir($mountPoint) && exec("mountpoint -q " . escapeshellarg($mountPoint) . " 2>/dev/null", $output, $ret) && $ret == 0) {
// Check for crc=0 which indicates XFS v4
$xfsInfo = shell_exec("xfs_info " . escapeshellarg($mountPoint) . " 2>/dev/null");
if ($xfsInfo && strpos($xfsInfo, 'crc=0') !== false) {
$warnings[] = [
'type' => 'xfs_v4',
'severity' => 'critical',
'message' => _('XFS v4 is deprecated and will not be supported in future Unraid releases. Please migrate to XFS v5 immediately.')
];
}
}
}
return $warnings;
}
function get_filesystem_warning_icon($warnings) {
if (empty($warnings)) return '';
$hasCritical = false;
$messages = [];
foreach ($warnings as $warning) {
if ($warning['severity'] == 'critical') {
$hasCritical = true;
}
$messages[] = $warning['message'];
}
$icon = $hasCritical ? 'exclamation-triangle' : 'exclamation-circle';
$color = $hasCritical ? 'red-text' : 'orange-text';
$tooltip = implode('. ', $messages);
return " <i class='fa fa-$icon $color' title='$tooltip'></i>";
}
function pgrep($process_name, $escape_arg=true) {
$pid = exec('pgrep --ns $$ '.($escape_arg ? escapeshellarg($process_name) : $process_name), $output, $retval);
return $retval == 0 ? $pid : false;
}
function is_block($path) {
return (@filetype(realpath($path)) == 'block');
}
function autov($file, $ret=false) {
global $docroot;
$path = $docroot.$file;
clearstatcache(true, $path);
$time = file_exists($path) ? filemtime($path) : 'autov_fileDoesntExist';
$newFile = "$file?v=".$time;
if ($ret)
return $newFile;
else
echo $newFile;
}
function transpose_user_path($path) {
if (strpos($path,'/mnt/user/') === 0 && file_exists($path)) {
$realdisk = trim(shell_exec("getfattr --absolute-names --only-values -n system.LOCATION ".escapeshellarg($path)." 2>/dev/null"));
if (!empty($realdisk))
$path = str_replace('/mnt/user/', "/mnt/$realdisk/", $path);
}
return $path;
}
function cpu_list() {
exec('cat /sys/devices/system/cpu/*/topology/thread_siblings_list|sort -nu', $cpus);
return $cpus;
}
function my_explode($split, $text, $count=2) {
return array_pad(explode($split, $text??"", $count), $count, '');
}
function my_preg_split($split, $text, $count=2) {
return array_pad(preg_split($split, $text, $count), $count, '');
}
function delete_file(...$file) {
array_map('unlink', array_filter($file,'file_exists'));
}
function my_mkdir($dirname, $permissions=0777, $recursive=false, $own="nobody", $grp="users") {
write_logging("Check if dir exists\n");
if (is_dir($dirname)) {write_logging("Dir exists\n"); return(false);}
write_logging("Dir does not exist\n");
$parent = $dirname;
write_logging("Getting $parent\n");
while (!is_dir($parent)){
if (!is_dir($parent)) write_logging("Not parent $parent\n"); else write_logging("Parent $parent is\n");
if (!$recursive) return(false);
$pathinfo2 = pathinfo($parent);
$parent = $pathinfo2["dirname"];
}
write_logging("Parent $parent\n");
if (strpos($dirname,'/mnt/user/') === 0) {
write_logging("Getting real disks\n");
$realdisk = trim(shell_exec("getfattr --absolute-names --only-values -n system.LOCATION ".escapeshellarg($parent)." 2>/dev/null"));
if (!empty($realdisk)) {
$dirname = str_replace('/mnt/user/', "/mnt/$realdisk/", $dirname);
$parent = str_replace('/mnt/user/', "/mnt/$realdisk/", $parent);
}
}
$fstype = trim(shell_exec(" stat -f -c '%T' $parent"));
$rtncode = false;
write_logging("fstype:$fstype parent $parent dir name $dirname\n");
switch ($fstype) {
case "zfs":
if (is_dir($parent.'/.zfs')) {
write_logging("ZFS Volume\n");
$zfsdataset = trim(shell_exec("zfs list -H -o name $parent"));
write_logging("Shell $zfsdataset\n");
$zfsdataset .= str_replace($parent,"",$dirname);
write_logging("Dataset $zfsdataset\n");
$zfsoutput = array();
if ($recursive) exec("zfs create -p \"$zfsdataset\"",$zfsoutput,$rtncode);else exec("zfs create \"$zfsdataset\"", $zfsoutput, $rtncode);
write_logging("Output: {$zfsoutput[0]} $rtncode");
if ($rtncode == 0) write_logging( " ZFS Command OK\n"); else write_logging( "ZFS Command Fail\n");
} else {write_logging("Not ZFS dataset\n");$rtncode = 1;}
if ($rtncode > 0) { mkdir($dirname, $permissions, $recursive); write_logging( "created dir:$dirname\n");} else chmod($zfsdataset, $permissions);
break;
case "btrfs":
$btrfsoutput = array();
if ($recursive) exec("btrfs subvolume create --parents \"$dirname\"",$btrfsoutput,$rtncode); else exec("btrfs subvolume create \"$dirname\"", $btrfsoutput, $rtncode);
if ($rtncode > 0) mkdir($dirname, $permissions, $recursive); else chmod($dirname, $permissions);
break;
default:
mkdir($dirname, $permissions, $recursive);
break;
}
chown($dirname, $own);
chgrp($dirname, $grp);
return($rtncode);
}
function my_rmdir($dirname) {
if (!is_dir("$dirname")) {
$return = [
'rtncode' => "false",
'type' => "NoDir",
];
return($return);
}
if (strpos($dirname,'/mnt/user/') === 0) {
$realdisk = trim(shell_exec("getfattr --absolute-names --only-values -n system.LOCATION ".escapeshellarg($dirname)." 2>/dev/null"));
if (!empty($realdisk)) {
$dirname = str_replace('/mnt/user/', "/mnt/$realdisk/", "$dirname");
}
}
$fstype = trim(shell_exec(" stat -f -c '%T' ".escapeshellarg($dirname)));
$rtncode = false;
switch ($fstype) {
case "zfs":
$zfsoutput = array();
$zfsdataset = trim(shell_exec("zfs list -H -o name ".escapeshellarg($dirname))) ;
$cmdstr = "zfs destroy \"$zfsdataset\" 2>&1 ";
$error = exec($cmdstr,$zfsoutput,$rtncode);
$return = [
'rtncode' => $rtncode,
'output' => $zfsoutput,
'dataset' => $zfsdataset,
'type' => $fstype,
'cmd' => $cmdstr,
'error' => $error,
];
break;
case "btrfs":
default:
$rtncode = rmdir($dirname);
$return = [
'rtncode' => $rtncode,
'type' => $fstype,
];
break;
}
return($return);
}
function get_realvolume($path) {
if (strpos($path,"/mnt/user/",0) === 0)
$reallocation = trim(shell_exec("getfattr --absolute-names --only-values -n system.LOCATION ".escapeshellarg($path)." 2>/dev/null"));
else {
$realexplode = explode("/",str_replace("/mnt/","",$path));
$reallocation = $realexplode[0];
}
return $reallocation;
}
function write_logging($value) {
$debug = is_file("/tmp/my_mkdir_debug");
if (!$debug) return;
file_put_contents('/tmp/my_mkdir_output', $value, FILE_APPEND);
}
function device_exists($name) {
global $disks, $devs;
return (array_key_exists($name, $disks) && !str_contains(_var($disks[$name],'status'),'_NP')) || (array_key_exists($name, $devs));
}
# Check for process Core Types.
function parse_cpu_ranges($file) {
if (!is_file($file)) return null;
$ranges = file_get_contents($file);
$ranges = trim($ranges);
if ($ranges === '') return null;
$cores = [];
foreach (explode(',', $ranges) as $range) {
if (strpos($range, '-') !== false) {
list($start, $end) = explode('-', $range);
$cores = array_merge($cores, range((int)$start, (int)$end));
} else {
$cores[] = (int)$range;
}
}
return $cores;
}
function get_intel_core_types() {
$core_types = array();
$cpu_core_file = "/sys/devices/cpu_core/cpus";
$cpu_atom_file = "/sys/devices/cpu_atom/cpus";
$p_cores = parse_cpu_ranges($cpu_core_file);
$e_cores = parse_cpu_ranges($cpu_atom_file);
if ($p_cores) {
foreach ($p_cores as $core) {
$core_types[$core] = _("P-Core");
}
}
if ($e_cores) {
foreach ($e_cores as $core) {
$core_types[$core] = _("E-Core");
}
}
return $core_types;
}
function dmidecode($key, $n, $all=true) {
$entries = array_filter(explode($key, shell_exec("dmidecode -qt$n")??""));
$properties = [];
foreach ($entries as $entry) {
$property = [];
foreach (explode("\n",$entry) as $line) if (strpos($line,': ') !== false) {
[$key, $value] = my_explode(': ',trim($line));
$property[$key] = $value;
}
$properties[] = $property;
}
return $all ? $properties : $properties[0] ?? null;
}
function is_intel_cpu() {
$cpu_vendor_check = exec("grep -Pom1 '^model name\s+:\s*\K.+' /proc/cpuinfo") ?? "";
return stripos($cpu_vendor_check, "intel") !== false;
}
// Load saved PCI data
function loadSavedData($filename) {
if (file_exists($filename)) {
$saveddata = file_get_contents($filename);
} else $saveddata = "";
return json_decode($saveddata, true);
}
// Run lspci -Dmn to get the current devices
function loadCurrentPCIData() {
$output = shell_exec('lspci -Dmn');
$devices = [];
if (file_exists("/boot/config/current.json")) {
$devices = loadSavedData("/boot/config/current.json");
} else {
foreach (explode("\n", trim($output)) as $line) {
$parts = explode(" ", $line);
if (count($parts) < 6) continue; // Skip malformed lines
$description_str = shell_exec(("lspci -s ".$parts[0]));
$description = preg_replace('/^\S+\s+/', '', $description_str);
$device = [
'class' => trim($parts[1], '"'),
'vendor_id' => trim($parts[2], '"'),
'device_id' => trim($parts[3], '"'),
'description' => trim($description,'"'),
];
$devices[$parts[0]] = $device;
}
}
return $devices;
}
// Compare the saved and current data
function comparePCIData() {
$changes = [];
$saved = loadSavedData("/boot/config/savedpcidata.json");
if (!$saved) return [];
$current = loadCurrentPCIData();
// Compare saved devices with current devices
foreach ($saved as $pci_id => $saved_device) {
if (!isset($current[$pci_id])) {
// Device has been removed
$changes[$pci_id] = [
'status' => 'removed',
'device' => $saved_device
];
} else {
// Device exists in both, check for modifications
$current_device = $current[$pci_id];
$differences = [];
// Compare fields
foreach (['vendor_id', 'device_id', 'class'] as $field) {
if (isset($saved_device[$field]) && isset($current_device[$field]) && $saved_device[$field] !== $current_device[$field]) {
$differences[$field] = [
'old' => $saved_device[$field],
'new' => $current_device[$field]
];
}
}
if (!empty($differences)) {
$changes[$pci_id] = [
'status' => 'changed',
'device' => $current_device,
'differences' => $differences
];
}
}
}
// Check for added devices
foreach ($current as $pci_id => $current_device) {
if (!isset($saved[$pci_id])) {
// Device has been added
$changes[$pci_id] = [
'status' => 'added',
'device' => $current_device
];
}
}
return $changes;
}
function clone_list($disk) {
global $pools;
return strpos($disk['status'],'_NP') === false && ($disk['type'] == 'Data' || in_array($disk['name'], $pools));
}
// Deprecated filesystem detection and display functions
// Core function to check a single disk for deprecated filesystems
function check_disk_for_deprecated_fs($disk) {
$deprecated = [];
$fsType = strtolower(_var($disk, 'fsType', ''));
// Check for ReiserFS
if (strpos($fsType, 'reiserfs') !== false) {
$deprecated[] = [
'name' => _var($disk, 'name'),
'fsType' => 'ReiserFS',
'severity' => 'critical',
'message' => 'ReiserFS is deprecated and is no longer supported in Unraid. You will need to downgrade to Unraid 7.2 to take action'
];
}
// Check for XFS v4 (lacks CRC checksums)
if (strpos($fsType, 'xfs') !== false) {
$name = _var($disk, 'name');
$mountPoint = "/mnt/$name";
// Check if disk is mounted
if (is_dir($mountPoint)) {
exec("mountpoint -q " . escapeshellarg($mountPoint) . " 2>/dev/null", $output, $ret);
if ($ret == 0) {
// Get XFS info to check for crc=0 which indicates XFS v4
$xfsInfo = shell_exec("xfs_info " . escapeshellarg($mountPoint) . " 2>/dev/null");
if ($xfsInfo && strpos($xfsInfo, 'crc=0') !== false) {
$deprecated[] = [
'name' => $name,
'fsType' => 'XFS v4',
'severity' => 'notice',
'message' => 'XFS v4 is deprecated and will not be supported in future Unraid releases. You have until 2030 to migrate to XFS v5.'
];
}
}
}
}
return $deprecated;
}
// Generate inline warning HTML for a single disk
function get_inline_fs_warnings($disk) {
$warnings = check_disk_for_deprecated_fs($disk);
$html = '';
foreach ($warnings as $warning) {
if ($warning['severity'] === 'critical') {
// ReiserFS - critical warning
$html .= '<span id="reiserfs" class="warning"><i class="fa fa-exclamation-triangle"></i> ' .
htmlspecialchars(_($warning['message'])) . '</span>';
} else {
// XFS v4 - notice (without .notice class to avoid duplicate icon)
$html .= '<div id="xfsv4" style="color:#0066cc; margin: 5px 0; line-height: 1.5;">' .
'<i class="fa fa-info-circle"></i> ' .
htmlspecialchars(_($warning['message'])) . '</div>';
}
}
return $html;
}
// Check array of disks for deprecated filesystems (used by Main page)
function check_deprecated_filesystems_array($disks, $filter_function) {
$deprecated = [];
foreach ($filter_function($disks) as $disk) {
if (substr($disk['status'],0,7) != 'DISK_NP') {
$disk_warnings = check_disk_for_deprecated_fs($disk);
$deprecated = array_merge($deprecated, $disk_warnings);
}
}
return $deprecated;
}
function display_deprecated_filesystem_warning($deprecated_disks, $type = 'array') {
if (empty($deprecated_disks)) return '';
// Separate warnings by severity
$critical_disks = [];
$notice_disks = [];
foreach ($deprecated_disks as $disk) {
if (_var($disk, 'severity', 'critical') === 'critical') {
$critical_disks[] = $disk;
} else {
$notice_disks[] = $disk;
}
}
$html = '';
// Critical warnings (ReiserFS) - severe styling, reappears on every page load
if (!empty($critical_disks)) {
$id = $type === 'array' ? 'array-critical-warning' : 'pool-critical-warning';
$title = htmlspecialchars($type === 'array' ? 'Critical: Deprecated Filesystem' : 'Critical: Pool Deprecated Filesystem');
$description = htmlspecialchars($type === 'array' ?
'The following array devices are using deprecated filesystems:' :
'The following pool devices are using deprecated filesystems:');
$diskList = '';
foreach ($critical_disks as $disk) {
$name = htmlspecialchars($disk['name']);
$fsType = htmlspecialchars($disk['fsType']);
$message = htmlspecialchars($disk['message']);
$diskList .= "<li><strong>{$name}:</strong> {$fsType} - {$message}</li>\n";
}
$html .= <<<HTML
<div id="{$id}" style="margin: 20px 0;">
<div style="background: #feefb3; border: 1px solid #ff8c2f; border-radius: 4px; padding: 15px; position: relative;">
<button onclick="$('#{$id}').fadeOut();"
style="position: absolute; right: 10px; top: 10px; background: transparent; border: none; color: #ff8c2f; cursor: pointer; font-size: 1.2em;">
<i class="fa fa-times"></i>
</button>
<div style="display: flex; align-items: start;">
<i class="fa fa-exclamation-triangle" style="color: #ff8c2f; margin-right: 10px; font-size: 1.2em;"></i>
<div style="flex: 1; color: #000;">
<div style="font-weight: bold; margin-bottom: 10px; color: #ff8c2f;">
{$title}
</div>
<div style="margin-bottom: 10px;">
{$description}
</div>
<ul style="margin: 10px 0 10px 20px;">
{$diskList}
</ul>
<div style="margin-top: 10px;">
<strong>Action Required:</strong> Migrate to a supported filesystem (XFS v5, BTRFS, or ZFS).
<a href="https://docs.unraid.net/go/convert-reiser-and-xfs"
target="_blank" style="color: #ff8c2f;">View migration guide →</a>
</div>
</div>
</div>
</div>
</div>
HTML;
}
// Notice warnings (XFS v4) - less severe styling, dismissible until reboot via sessionStorage
if (!empty($notice_disks)) {
$id = $type === 'array' ? 'array-notice-warning' : 'pool-notice-warning';
$title = htmlspecialchars($type === 'array' ? 'Notice: Filesystem Update Available' : 'Notice: Pool Filesystem Update Available');
$description = htmlspecialchars($type === 'array' ?
'The following array devices are using older filesystem versions:' :
'The following pool devices are using older filesystem versions:');
$deadline = new DateTime('2030-10-01');
$now = new DateTime('now');
if ($now < $deadline) {
$interval = $now->diff($deadline);
$years = (int)$interval->y;
$months = (int)$interval->m;
$parts = [];
if ($years > 0) $parts[] = $years.' year'.($years === 1 ? '' : 's');
if ($months > 0) $parts[] = $months.' month'.($months === 1 ? '' : 's');
if (empty($parts)) $parts[] = 'less than 1 month';
$timeline = 'before the end of September 2030 ('.implode(' and ', $parts).')';
} else {
$timeline = 'as soon as possible';
}
$diskList = '';
foreach ($notice_disks as $disk) {
$name = htmlspecialchars($disk['name']);
$fsType = htmlspecialchars($disk['fsType']);
$message = htmlspecialchars($disk['message']);
$diskList .= "<li><strong>{$name}:</strong> {$fsType} - {$message}</li>\n";
}
$html .= <<<HTML
<script>
// Check if XFS warning was dismissed this session
if (!sessionStorage.getItem('xfs-{$id}-dismissed')) {
document.write(`
<div id="{$id}" style="margin: 20px 0;">
<div style="background: #e7f3ff; border: 1px solid #0066cc; border-radius: 4px; padding: 15px; position: relative;">
<button onclick="sessionStorage.setItem('xfs-{$id}-dismissed', 'true'); $('#{$id}').fadeOut();"
style="position: absolute; right: 10px; top: 10px; background: transparent; border: none; color: #0066cc; cursor: pointer; font-size: 1.2em;"
title="Dismiss until reboot">
<i class="fa fa-times"></i>
</button>
<div style="display: flex; align-items: start;">
<i class="fa fa-info-circle" style="color: #0066cc; margin-right: 10px; font-size: 1.2em;"></i>
<div style="flex: 1; color: #000;">
<div style="font-weight: bold; margin-bottom: 10px; color: #0066cc;">
{$title}
</div>
<div style="margin-bottom: 10px;">
{$description}
</div>
<ul style="margin: 10px 0 10px 20px;">
{$diskList}
</ul>
<div style="margin-top: 10px;">
<strong>Recommendation:</strong> Plan to migrate to XFS v5, BTRFS, or ZFS {$timeline}.
<a href="https://docs.unraid.net/go/convert-reiser-and-xfs"
target="_blank" style="color: #0066cc;">View migration guide →</a>
</div>
</div>
</div>
</div>
</div>
`);
}
</script>
HTML;
}
return $html;
}
function get_cpu_packages(string $separator = ','): array {
$packages = [];
foreach (glob("/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list") as $path) {
$pkg_id = (int)file_get_contents(dirname($path) . "/physical_package_id");
$siblings = str_replace(",", $separator, trim(file_get_contents($path)));
if (!in_array($siblings, $packages[$pkg_id] ?? [])) {
$packages[$pkg_id][] = $siblings;
}
}
foreach ($packages as &$list) {
$keys = array_map(fn($s) => (int)explode($separator, $s)[0], $list);
array_multisort($keys, SORT_ASC, SORT_NUMERIC, $list);
}
unset($list);
return $packages;
}
function getIpAddressesByPci(string $pciAddress): array
{
$base = "/sys/bus/pci/devices/$pciAddress/net";
if (!is_dir($base)) {
return [];
}
$interfaces = scandir($base);
$result = [];
foreach ($interfaces as $iface) {
if ($iface === '.' || $iface === '..') continue;
//
// Walk upward (eth0 → bond0 → br0 → ...)
//
$chain = [];
$curr = $iface;
while (true) {
$chain[] = $curr;
$masterLink = "/sys/class/net/$curr/master";
if (!is_link($masterLink)) {
break;
}
$curr = basename(readlink($masterLink));
}
//
// Now $chain contains all relevant interfaces
// Example: [eth0, bond0, br0]
//
foreach ($chain as $dev) {
$cmd = sprintf('ip -o addr show dev %s 2>/dev/null', escapeshellarg($dev));
$output = shell_exec($cmd);
if (!$output) continue;
foreach (explode("\n", trim($output)) as $line) {
// IPv4
if (preg_match('/inet\s+(\d+\.\d+\.\d+\.\d+\/\d+)/', $line, $m)) {
$result[$dev][] = $m[1];
}
// IPv6
if (preg_match('/inet6\s+([0-9a-fA-F:]+\/\d+)/', $line, $m)) {
$result[$dev][] = $m[1];
}
}
}
}
//
// Remove duplicates while preserving interface keys
//
foreach ($result as $iface => $ips) {
$result[$iface] = array_values(array_unique($ips));
}
return $result;
}
function getSystemNumaNodeCount() {
$nodes = glob("/sys/devices/system/node/node*") ?: [];
$count = count($nodes);
// Treat “no NUMA directory” as a single-node system
return $count > 0 ? $count : 1;
}
function normalizeNumaNode($node, $numNodes) {
// If system has only 1 node, interpret -1 as node 0
if ($numNodes === 1 && $node === -1) {
return 0;
}
return $node;
}
function getCpuNumaInfo($numNodes) {
$cpus = [];
foreach (glob("/sys/devices/system/cpu/cpu[0-9]*") as $cpuPath) {
$cpu = basename($cpuPath);
$nodes = glob("$cpuPath/node*");
$node = -1;
if (!empty($nodes)) {
$node = intval(str_replace("node", "", basename($nodes[0])));
}
$node = normalizeNumaNode($node, $numNodes);