Repository navigation
Expand file tree
/
Copy pathnmdctl
More file actions
4875 lines (4242 loc) · 178 KB
/
Copy pathnmdctl
File metadata and controls
4875 lines (4242 loc) · 178 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
# shellcheck disable=SC2155,SC2207
#
# nmdctl - NonRAID array management utility
#
# Check for bash version 4 or higher (needed for associative arrays)
if ((BASH_VERSINFO[0] < 4)); then
echo "Error: This script requires bash version 4 or higher"
exit 1
fi
# Our version
VERSION=1.23.0
# Colors for pretty output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Monitor mode status bars
HEADER_BG='\033[44;37m' # Blue background, white text
FOOTER_BG='\033[100;97m' # Dark gray background, bright white text
# Default superblock path
DEFAULT_SUPERBLOCK="/nonraid.dat"
SUPERBLOCK_PATH=""
# LUKS keyfile path
LUKS_KEYFILE="/etc/nonraid/luks-keyfile"
# verbose, unattended
VERBOSE=0
UNATTENDED=0
# status modes
NO_FS=0
MONITOR_MODE=0
MONITOR_INTERVAL=2
# Global associative array for nmdstat values
declare -g -A NMDSTAT_VALUES
# Global associative arrays for collected status data
declare -g -A ARRAY_STATUS_DATA
declare -g -A DISK_STATUS_DATA
declare -g -A RESYNC_STATUS_DATA
# Driver stats interface, override for mock tests
PROC_NMDSTAT="${PROC_NMDSTAT:-/proc/nmdstat}"
# Display usage information
usage() {
local exit_code="${1:-1}"
cat <<EOF
nmdctl - NonRAID array management utility (v${VERSION})
Usage:
nmdctl [GLOBAL OPTIONS] COMMAND [COMMAND OPTIONS]
Status and monitoring command:
status [OPTIONS] Show current array status
-v, --verbose Show detailed status information
--no-fs Don't show filesystem information
-o, --output FORMAT Output format: default, prometheus, json, terse
-m, --monitor [INTERVAL] Monitor mode: refresh status every INTERVAL seconds (default: $MONITOR_INTERVAL)
Array Management Commands:
create Create a new array with interactive disk assignment
create [-f] [SLOT:DEV[:ID] ..] Create a new array with layout directly as arguments
Example: nmdctl create P:/dev/sdb1 1:/dev/sdc1 2:/dev/sdd1 ...
Options: -f, --force: Skip device availability checks, allow any block device
start Start the array
stop Stop the array
import Import all disks to the array without starting it
Disk Management Commands:
add [SLOT:DEV[:ID]] Add a new disk to the array
replace SLOT[:DEV[:ID]] Replace a disk in the specified slot
unassign SLOT Unassign a disk from the specified slot
Maintenance Commands:
reload Reload nonraid module with specified superblock
check [OPTION] Start or stop parity check
Options: CORRECT (default), NOCORRECT, PAUSE, RESUME, CANCEL
set [SETTING] [VALUE] Set array settings
Use 'nmdctl set' for list of available settings
Filesystem Commands:
mount [MOUNTPREFIX] Mount all active data disks
Default prefix: /mnt/disk
unmount Unmount all active data disks
Global Options:
-s, --super PATH Superblock file path (default: ${DEFAULT_SUPERBLOCK})
-k, --keyfile PATH LUKS keyfile path (default: ${LUKS_KEYFILE})
-u, --unattended Enable unattended mode
--no-color Disable colored output
-v, --verbose Enable verbose output
-V, --version Display version information
-h, --help Display this help message
EOF
exit "$exit_code"
}
# Check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}Error: This command must be run as root${NC}"
exit 1
fi
}
# Run a command via nmdcmd
run_nmd_command() {
check_root
if ! echo -n "$1" > /proc/nmdcmd; then
echo -e "${RED}Error: Failed to run command '$1'${NC}"
return 1
fi
return 0
}
# Check if the nonraid module is loaded, and load it if superblock is provided
check_module_loaded() {
local superblock="${SUPERBLOCK_PATH:-$DEFAULT_SUPERBLOCK}"
local is_create_command=0
# Check if we're being called as part of the create command
local call_stack="${FUNCNAME[*]}"
if [[ "$call_stack" == *"create_array"* ]]; then
is_create_command=1
fi
# Check if module is already loaded
if lsmod | grep -q nonraid; then
# For create_array, verify the module is using the correct superblock
if [ "$is_create_command" -eq 1 ] && [ -f "$PROC_NMDSTAT" ]; then
# Get all nmdstat values if we haven't already
if [ ${#NMDSTAT_VALUES[@]} -eq 0 ]; then
get_all_nmdstat_values NMDSTAT_VALUES
fi
local current_superblock="${NMDSTAT_VALUES[sbName]}"
# If the current superblock doesn't match what we want to use
if [ "$current_superblock" != "$superblock" ] && [ -n "$current_superblock" ] && [ "$current_superblock" != "(null)" ]; then
echo -e "${RED}Error: NonRAID module is loaded with a different superblock${NC}"
echo -e "Currently loaded with: ${YELLOW}$current_superblock${NC}"
echo -e "Attempting to create array with: ${YELLOW}$superblock${NC}"
echo -e ""
echo -e "This could accidentally modify the wrong superblock file."
echo -e "Explicitly reload the module with the new superblock with: ${YELLOW}nmdctl --super $superblock reload${NC}"
return 1
fi
fi
return 0
fi
# Module not loaded, attempt to load it
# For create command, we can accept non-existent superblock files
if [ -f "$superblock" ] || [ "$is_create_command" -eq 1 ]; then
echo -e "${YELLOW}NonRAID module not loaded, attempting to load with superblock: $superblock${NC}"
# For create command with new superblock, provide info
if [ ! -f "$superblock" ] && [ "$is_create_command" -eq 1 ]; then
echo -e "${YELLOW}Note: Superblock file '$superblock' doesn't exist yet${NC}"
echo -e "${YELLOW}A new superblock will be created when the array is started${NC}"
fi
if ! modprobe nonraid super="$superblock"; then
echo -e "${RED}Error: Failed to load nonraid module${NC}"
return 1
fi
echo -e "${GREEN}Successfully loaded nonraid module${NC}"
return 0
else
echo -e "${RED}Error: nonraid module is not loaded${NC}"
echo -e "Superblock file not found: $superblock"
echo -e "To create an array with this new superblock, run: ${YELLOW}nmdctl --super $superblock create${NC}"
# exit early, but cleanly
exit 0
fi
}
# Check if /proc/nmdstat exists
check_nmdstat_exists() {
if [ ! -f "$PROC_NMDSTAT" ]; then
# Clear the NMDSTAT_VALUES array since /proc/nmdstat doesn't exist
for key in "${!NMDSTAT_VALUES[@]}"; do
unset 'NMDSTAT_VALUES["$key"]'
done
# Try to load the module first if it's not loaded
if ! lsmod | grep -q nonraid; then
check_module_loaded
fi
# Check again after attempted load
if [ ! -f "$PROC_NMDSTAT" ]; then
echo -e "${RED}Error: /proc/nmdstat not found${NC}"
echo -e "Please ensure the nonraid module is properly loaded"
return 1
fi
fi
return 0
}
# Get all values from nmdstat and store them in an associative array
# Usage: get_all_nmdstat_values nmdstat_array
# Afterward, access values with ${nmdstat_array["key"]}
get_all_nmdstat_values() {
local -n array_ref=$1
# Clear the array first
for key in "${!array_ref[@]}"; do
unset 'array_ref["$key"]'
done
# Read all values from /proc/nmdstat
while IFS='=' read -r key value; do
array_ref["$key"]="$value"
done < <(cat "$PROC_NMDSTAT" 2>/dev/null)
# Store timestamp when data was read
array_ref["timestamp"]=$(date +%s)
# Return success only if we read something
[ ${#array_ref[@]} -gt 1 ]
}
# Get value from nmdstat (for backward compatibility)
get_nmdstat_value() {
grep -E "^$1=" "$PROC_NMDSTAT" | cut -d= -f2
}
# Format disk status
format_disk_status() {
local status="$1"
case "$status" in
"DISK_OK")
echo -e "${GREEN}OK${NC}"
;;
"DISK_INVALID")
echo -e "${RED}INVALID${NC}"
;;
"DISK_NP_MISSING")
echo -e "${YELLOW}MISSING${NC}"
;;
"DISK_WRONG")
echo -e "${RED}WRONG${NC}"
;;
"DISK_DSBL"|"DISK_NP_DSBL")
echo -e "${YELLOW}DISABLED${NC}"
;;
"DISK_NEW"|"DISK_DSBL_NEW")
echo -e "${BLUE}NEW${NC}"
;;
*)
echo -e "${YELLOW}$status${NC}"
;;
esac
}
# Format array state with color
format_array_state() {
local state="$1"
case "$state" in
"STARTED")
echo -e "${GREEN}$state${NC}"
;;
"STOPPED")
echo -e "${YELLOW}$state${NC}"
;;
"NEW_ARRAY")
echo -e "${BLUE}$state${NC}"
;;
"RECON_DISK"|"DISABLE_DISK"|"SWAP_DSBL")
echo -e "${YELLOW}$state${NC}"
;;
"ERROR:"*)
echo -e "${RED}$state${NC}"
;;
*)
echo "$state"
;;
esac
}
format_available_devices() {
local available_devices=("$@")
echo -e "Available devices:"
echo ""
echo -e " # Device Size(GB) ID"
echo -e " -- ---------- -------- ---------------------------------"
local device_index=1
for device_info in "${available_devices[@]}"; do
IFS='|' read -r dev partition size_kb size_gb disk_id <<< "$device_info"
# Show shortened disk ID for clarity
local short_id="${disk_id}"
if [ ${#short_id} -gt 33 ]; then
short_id="${short_id:0:30}..."
fi
if [ -z "$short_id" ]; then
short_id=$(echo -e "${RED}No disk ID found - cannot use in array${NC}")
fi
printf " %-2d %-10s %-8s %s\n" \
"$device_index" \
"$(basename "$dev")" \
"$size_gb" \
"$short_id"
device_index=$((device_index + 1))
done
echo ""
}
# Format health status with appropriate colors
format_health_status() {
local health_status="$1"
local health_formatted=""
case "$health_status" in
"ERROR") health_formatted="${RED}ERROR${NC}" ;;
"NEW") health_formatted="${BLUE}NEW${NC}" ;;
"NEW_DISK") health_formatted="${BLUE}NEW_DISK${NC}" ;;
"OFFLINE") health_formatted="${RED}OFFLINE${NC}" ;;
"PARTIAL") health_formatted="${YELLOW}PARTIAL${NC}" ;;
"DEGRADED") health_formatted="${YELLOW}DEGRADED${NC}" ;;
"WARNING") health_formatted="${YELLOW}WARNING${NC}" ;;
"READY") health_formatted="${YELLOW}READY${NC}" ;;
"HEALTHY") health_formatted="${GREEN}HEALTHY${NC}" ;;
*) health_formatted="$health_status" ;;
esac
echo -e "$health_formatted"
}
# Check if the nonraid driver is in an inconsistent state
# Returns 0 if inconsistent, 1 if consistent
check_driver_inconsistent_state() {
# If NMDSTAT_VALUES is empty, populate it first
if [ ${#NMDSTAT_VALUES[@]} -eq 0 ]; then
get_all_nmdstat_values NMDSTAT_VALUES
fi
# We check for driver states only if the array is started
if [ "${NMDSTAT_VALUES[mdState]}" != "STARTED" ]; then
return 1
fi
# Get the mdnum* counter values
local mdnummissing="${NMDSTAT_VALUES[mdNumMissing]:-0}"
local mdnuminvalid="${NMDSTAT_VALUES[mdNumInvalid]:-0}"
local mdnumwrong="${NMDSTAT_VALUES[mdNumWrong]:-0}"
local mdnumdisabled="${NMDSTAT_VALUES[mdNumDisabled]:-0}"
local mdnumreplaced="${NMDSTAT_VALUES[mdNumReplaced]:-0}"
local mdnumnew="${NMDSTAT_VALUES[mdNumNew]:-0}"
# Check if any counter has a value > 0
local has_counter_activity=0
if [ "$mdnummissing" -gt 0 ] || [ "$mdnuminvalid" -gt 0 ] ||
[ "$mdnumwrong" -gt 0 ] || [ "$mdnumdisabled" -gt 0 ] ||
[ "$mdnumreplaced" -gt 0 ] || [ "$mdnumnew" -gt 0 ]; then
has_counter_activity=1
fi
# If counters are clean, there's at least no known driver state inconsistency
if [ "$has_counter_activity" -eq 0 ]; then
return 1
fi
# If array is empty, we should be OK for now
if [ "$(get_defined_slots_count)" -eq 0 ]; then
return 1
fi
# Check if all defined disks are in DISK_OK state
local all_disks_ok=1
for slot in $(get_defined_slots); do
local rdevstatus="${NMDSTAT_VALUES[rdevStatus.$slot]}"
if [ "$rdevstatus" != "DISK_OK" ]; then
all_disks_ok=0
break
fi
done
# Inconsistent state: counters show activity but all disks are OK
if [ "$all_disks_ok" -eq 1 ]; then
return 0
fi
# No known inconsistencies found
return 1
}
# Get count of defined disk slots in the array
# Returns the count of all slots with a defined disk
get_defined_slots_count() {
# If NMDSTAT_VALUES is empty, populate it first
if [ ${#NMDSTAT_VALUES[@]} -eq 0 ]; then
get_all_nmdstat_values NMDSTAT_VALUES
fi
local count=0
# Count all keys matching diskSize.X with non-zero values
for key in "${!NMDSTAT_VALUES[@]}"; do
if [[ $key =~ ^diskSize\.([0-9]+)$ ]] && [ "${NMDSTAT_VALUES[$key]}" -gt 0 ]; then
count=$((count + 1))
fi
done
echo "$count"
}
# Get array of defined slot numbers
# Returns space-separated list of slot numbers that have disks defined
get_defined_slots() {
# If NMDSTAT_VALUES is empty, populate it first
if [ ${#NMDSTAT_VALUES[@]} -eq 0 ]; then
get_all_nmdstat_values NMDSTAT_VALUES
fi
local slots=()
# Extract slot numbers for keys matching diskSize.X with non-zero values
for key in "${!NMDSTAT_VALUES[@]}"; do
if [[ $key =~ ^diskSize\.([0-9]+)$ ]] && [ "${NMDSTAT_VALUES[$key]}" -gt 0 ]; then
slots+=("${BASH_REMATCH[1]}")
fi
done
# Sort the slots numerically and output as space-separated list
printf "%s\n" "${slots[@]}" | sort -n | tr '\n' ' '
}
# Format time duration in seconds to a human-readable format (HH:MM:SS)
format_time_duration() {
local seconds=$1
local days=$(( seconds / 86400 ))
local hours=$(( (seconds % 86400) / 3600 ))
local minutes=$(( (seconds % 3600) / 60 ))
local secs=$(( seconds % 60 ))
if [ "$days" -gt 0 ]; then
printf "%d days, %d hours" "$days" "$hours"
elif [ "$hours" -gt 0 ]; then
printf "%d hours, %02d minutes" "$hours" "$minutes"
elif [ "$minutes" -gt 0 ]; then
printf "%d minutes, %02d seconds" "$minutes" "$secs"
else
printf "%d sec" "$secs"
fi
}
# Collect array summary data
collect_array_summary() {
ARRAY_STATUS_DATA["mdstate"]="${NMDSTAT_VALUES[mdState]}"
ARRAY_STATUS_DATA["sblabel"]="${NMDSTAT_VALUES[sbLabel]}"
ARRAY_STATUS_DATA["sbname"]="${NMDSTAT_VALUES[sbName]}"
ARRAY_STATUS_DATA["mdnumdisks"]="${NMDSTAT_VALUES[mdNumDisks]}"
ARRAY_STATUS_DATA["total_slots"]=$(get_defined_slots_count)
}
# Collect array health data
collect_array_health() {
local mdstate="${NMDSTAT_VALUES[mdState]}"
local mdnummissing="${NMDSTAT_VALUES[mdNumMissing]}"
local mdnuminvalid="${NMDSTAT_VALUES[mdNumInvalid]}"
local mdnumwrong="${NMDSTAT_VALUES[mdNumWrong]}"
local mdnumdisabled="${NMDSTAT_VALUES[mdNumDisabled]}"
local mdnumreplaced="${NMDSTAT_VALUES[mdNumReplaced]}"
local mdnumnew="${NMDSTAT_VALUES[mdNumNew]}"
local sbsynced="${NMDSTAT_VALUES[sbSynced]}"
local sbsynced2="${NMDSTAT_VALUES[sbSynced2]}"
local sbsyncerrs="${NMDSTAT_VALUES[sbSyncErrs]}"
local sbsyncexit="${NMDSTAT_VALUES[sbSyncExit]}"
# sbSyncErrs counts the errors the last check found, but a correcting check
# that finished cleanly has already fixed them
local unresolved_sync_errs="$sbsyncerrs"
if [ "${NMDSTAT_VALUES[mdResyncCorr]}" = "1" ] && [ "$sbsyncexit" = "0" ]; then
unresolved_sync_errs=0
fi
# Count the total number of disk slots that are defined in the array
local total_slots=$(get_defined_slots_count)
# Count how many disks have been imported and how many read/write errors there are
# and how many unassigned disks there are
# XXX: there's still the terminology issue that a disk slot can be unassigned and unimported,
# we currently only count imported unassigned slots
local imported_disks=0
local unassigned_disks=0
local disks_num_errors=0
for slot in $(get_defined_slots); do
local rdevname="${NMDSTAT_VALUES[rdevName.$slot]}"
local rdevstatus="${NMDSTAT_VALUES[rdevStatus.$slot]}"
if [ -n "$rdevname" ] && [ "$rdevname" != "none" ]; then
imported_disks=$((imported_disks + 1))
elif [ "$rdevstatus" = "DISK_NP_DSBL" ] || [ "$rdevstatus" = "DISK_NP_MISSING" ]; then
unassigned_disks=$((unassigned_disks + 1))
fi
disks_num_errors=$((disks_num_errors + "${NMDSTAT_VALUES[rdevNumErrors.$slot]}"))
done
# Store all health-related data
ARRAY_STATUS_DATA["imported_disks"]="$imported_disks"
ARRAY_STATUS_DATA["unassigned_disks"]="$unassigned_disks"
ARRAY_STATUS_DATA["disks_num_errors"]="$disks_num_errors"
ARRAY_STATUS_DATA["mdnummissing"]="$mdnummissing"
ARRAY_STATUS_DATA["mdnuminvalid"]="$mdnuminvalid"
ARRAY_STATUS_DATA["mdnumwrong"]="$mdnumwrong"
ARRAY_STATUS_DATA["mdnumdisabled"]="$mdnumdisabled"
ARRAY_STATUS_DATA["mdnumreplaced"]="$mdnumreplaced"
ARRAY_STATUS_DATA["mdnumnew"]="$mdnumnew"
ARRAY_STATUS_DATA["sbsyncerrs"]="$sbsyncerrs"
ARRAY_STATUS_DATA["sbsyncexit"]="$sbsyncexit"
# Calculate last sync information
local last_sync_human="${YELLOW}never${NC}"
local last_sync_timestamp=0
local last_sync_ago=0
local last_sync_elapsed=0
local last_sync_status=""
if [ "$sbsynced2" != "0" ]; then
last_sync_timestamp=$sbsynced2
last_sync_ago=$(($(printf "%(%s)T")-sbsynced2))
last_sync_elapsed=$((sbsynced2 - sbsynced))
last_sync_human="$(format_time_duration "$last_sync_ago") ago"
if [ "$last_sync_ago" -gt 8640000 ]; then
last_sync_human="${YELLOW}$last_sync_human${NC} (more than 100 days)"
fi
if [ "$VERBOSE" -gt 0 ]; then
last_sync_human="$last_sync_human - completed in $(format_time_duration "$last_sync_elapsed")"
fi
if [ "$sbsyncexit" -gt 0 ]; then
last_sync_human="$last_sync_human ${RED}(errors encountered)${NC}"
last_sync_status="errors"
else
last_sync_status="completed"
fi
elif [ "$sbsynced" != "0" ]; then
last_sync_human="${BLUE}in progress${NC}"
last_sync_status="in_progress"
else
last_sync_status="never"
fi
ARRAY_STATUS_DATA["last_sync_human"]="$last_sync_human"
ARRAY_STATUS_DATA["last_sync_timestamp"]="$last_sync_timestamp"
ARRAY_STATUS_DATA["last_sync_ago"]="$last_sync_ago"
ARRAY_STATUS_DATA["last_sync_status"]="$last_sync_status"
ARRAY_STATUS_DATA["last_sync_elapsed"]="$last_sync_elapsed"
# Determine health status
local health_status=""
local health_details=""
local health_code=1 # 0=healthy, 1=warning/degraded, 2=offline/error
# Check for critical error conditions first
if [[ "$mdstate" = "ERROR:"* ]]; then
health_status="ERROR"
health_details="Array is in ERROR state"
health_code=2
elif [ "$mdstate" = "NEW_ARRAY" ]; then
health_status="NEW"
health_details="New array, parity needs to be built"
health_code=1
elif [ -n "$mdnumnew" ] && [ "$mdnumnew" -gt 0 ]; then
health_status="NEW_DISK"
health_details="New disk added to the array"
health_code=1
elif [ "$imported_disks" -eq 0 ] && [ "$total_slots" -gt 0 ]; then
health_status="OFFLINE"
health_details="No disks imported"
if [ "$unassigned_disks" -gt 0 ]; then
health_details="$health_details, $unassigned_disks unassigned"
fi
health_code=2
elif [ "$imported_disks" -eq 0 ] && [ "$total_slots" -eq 0 ]; then
health_status="OFFLINE"
health_details="Array is unconfigured"
health_code=2
elif [ "$imported_disks" -lt "$total_slots" ] && [ "$mdstate" != "STARTED" ]; then
health_status="PARTIAL"
health_details="$imported_disks/$total_slots disks imported"
if [ "$unassigned_disks" -gt 0 ]; then
health_details="$health_details, $unassigned_disks unassigned"
fi
health_code=1
elif [ "$mdnummissing" -gt 0 ] || [ "$mdnuminvalid" -gt 0 ] ||
[ "$mdnumwrong" -gt 0 ] || [ "$mdnumdisabled" -gt 0 ] ||
[ "$mdnumreplaced" -gt 0 ] || [ "$unresolved_sync_errs" -gt 0 ] ||
[ "$sbsyncexit" -gt 0 ]; then
health_status="DEGRADED"
local issues=""
[ "$mdnummissing" -gt 0 ] && issues="${issues}Missing: $mdnummissing, "
[ "$mdnuminvalid" -gt 0 ] && issues="${issues}Invalid: $mdnuminvalid, "
[ "$mdnumwrong" -gt 0 ] && issues="${issues}Wrong: $mdnumwrong, "
[ "$mdnumdisabled" -gt 0 ] && issues="${issues}Disabled: $mdnumdisabled, "
[ "$mdnumreplaced" -gt 0 ] && issues="${issues}Replaced: $mdnumreplaced, "
[ "$unresolved_sync_errs" -gt 0 ] && issues="${issues}Sync Errors: $sbsyncerrs, "
[ "$disks_num_errors" -gt 0 ] && issues="${issues}I/O Errors: $disks_num_errors, "
health_details=${issues%, }
health_code=1
elif [ "$disks_num_errors" -gt 0 ]; then
health_status="WARNING"
health_details="All disks present, but some have I/O errors ($disks_num_errors total)"
health_code=1
elif [ "$mdstate" = "STOPPED" ]; then
health_status="READY"
health_details="All disks imported but array is not started"
health_code=1
else
health_status="HEALTHY"
health_details="All disks present and functioning"
health_code=0
fi
ARRAY_STATUS_DATA["health_status"]="$health_status"
ARRAY_STATUS_DATA["health_details"]="$health_details"
ARRAY_STATUS_DATA["health_code"]="$health_code"
}
# Collect array size and parity data
collect_array_size_and_parity() {
local has_parity=false
local has_second_parity=false
local data_size_gb=0
local parity_size_gb=0
local second_parity_size_gb=0
# Check slots 0 and 29 for parity disks
local parity_disk_size="${NMDSTAT_VALUES[diskSize.0]}"
if [ -n "$parity_disk_size" ] && [ "$parity_disk_size" -gt 0 ]; then
has_parity=true
parity_size_gb=$(format_kbytes "$parity_disk_size" 0 0 "gb")
fi
# Check P state
local parity_disk_status="${NMDSTAT_VALUES[rdevStatus.0]}"
if [[ "$parity_disk_status" == DISK_NP* ]] || [ "$parity_disk_status" = "DISK_DSBL_NEW" ] || [ "$parity_disk_status" = "DISK_INVALID" ]; then
has_parity=false
fi
local second_parity_disk_size="${NMDSTAT_VALUES[diskSize.29]}"
if [ -n "$second_parity_disk_size" ] && [ "$second_parity_disk_size" -gt 0 ]; then
has_second_parity=true
second_parity_size_gb=$(format_kbytes "$second_parity_disk_size" 0 0 "gb")
fi
# Check Q state
local second_parity_disk_status="${NMDSTAT_VALUES[rdevStatus.29]}"
if [[ "$second_parity_disk_status" == DISK_NP* ]] || [ "$second_parity_disk_status" = "DISK_DSBL_NEW" ] || [ "$second_parity_disk_status" = "DISK_INVALID" ]; then
has_second_parity=false
fi
# Count data disks and sum their sizes
local data_disk_count=0
local data_size_kb=0
for slot in $(seq 1 28); do
local disk_size="${NMDSTAT_VALUES[diskSize.$slot]}"
if [ -n "$disk_size" ] && [ "$disk_size" -gt 0 ]; then
data_disk_count=$((data_disk_count + 1))
data_size_kb=$((data_size_kb + disk_size))
fi
done
data_size_gb=$(format_kbytes "$data_size_kb" 0 0 "gb")
ARRAY_STATUS_DATA["has_parity"]="$has_parity"
ARRAY_STATUS_DATA["has_second_parity"]="$has_second_parity"
ARRAY_STATUS_DATA["data_size_kb"]="$data_size_kb"
ARRAY_STATUS_DATA["data_size_gb"]="$data_size_gb"
ARRAY_STATUS_DATA["data_disk_count"]="$data_disk_count"
ARRAY_STATUS_DATA["parity_size_kb"]="$parity_disk_size"
ARRAY_STATUS_DATA["parity_size_gb"]="$parity_size_gb"
ARRAY_STATUS_DATA["second_parity_size_kb"]="$second_parity_disk_size"
ARRAY_STATUS_DATA["second_parity_size_gb"]="$second_parity_size_gb"
}
# Collect resync operation status data
collect_resync_status() {
local mdresync="${NMDSTAT_VALUES[mdResync]}"
local mdresyncaction="${NMDSTAT_VALUES[mdResyncAction]}"
local mdresynccorr="${NMDSTAT_VALUES[mdResyncCorr]}"
local mdresyncpos="${NMDSTAT_VALUES[mdResyncPos]}"
local mdresyncsize="${NMDSTAT_VALUES[mdResyncSize]}"
local mdresyncdt="${NMDSTAT_VALUES[mdResyncDt]}"
local mdresyncdb="${NMDSTAT_VALUES[mdResyncDb]}"
RESYNC_STATUS_DATA["active"]="false"
RESYNC_STATUS_DATA["paused"]="false"
RESYNC_STATUS_DATA["pending"]="false"
RESYNC_STATUS_DATA["action"]="$mdresyncaction"
RESYNC_STATUS_DATA["progress_percent"]="0"
RESYNC_STATUS_DATA["position_kb"]="0"
RESYNC_STATUS_DATA["position_gb"]="0"
RESYNC_STATUS_DATA["size_kb"]="0"
RESYNC_STATUS_DATA["size_gb"]="0"
RESYNC_STATUS_DATA["rate_kb_s"]="0"
RESYNC_STATUS_DATA["elapsed_seconds"]="0"
RESYNC_STATUS_DATA["eta_seconds"]="0"
RESYNC_STATUS_DATA["friendly_action"]=$(format_resync_action "$mdresyncaction" "$mdresynccorr")
local array_start_maybe="Start the array and use"
if [ "${NMDSTAT_VALUES[mdState]}" = "STARTED" ]; then
array_start_maybe="Use"
fi
RESYNC_STATUS_DATA["array_start_hint"]="$array_start_maybe"
# Check for paused operation
if [ "$mdresync" = "0" ] && [ "$mdresyncpos" != "0" ] && [ -n "$mdresyncpos" ]; then
# A paused operation is still active, it just isn't making progress
RESYNC_STATUS_DATA["active"]="true"
RESYNC_STATUS_DATA["paused"]="true"
local progress=0
if [ "$mdresyncsize" -gt 0 ]; then
progress=$(( mdresyncpos * 100 / mdresyncsize ))
fi
RESYNC_STATUS_DATA["progress_percent"]="$progress"
RESYNC_STATUS_DATA["position_kb"]="$mdresyncpos"
RESYNC_STATUS_DATA["position_gb"]=$(format_kbytes "$mdresyncpos" 0 1 "gb")
RESYNC_STATUS_DATA["size_kb"]="$mdresyncsize"
RESYNC_STATUS_DATA["size_gb"]=$(format_kbytes "$mdresyncsize" 0 0 "gb")
# Check for pending operations
elif [ "$mdresync" = "0" ] && { [ "$mdresyncaction" = "clear" ] || [[ "$mdresyncaction" == recon* ]]; }; then
RESYNC_STATUS_DATA["pending"]="true"
RESYNC_STATUS_DATA["size_kb"]="$mdresyncsize"
RESYNC_STATUS_DATA["size_gb"]=$(format_kbytes "$mdresyncsize" 0 0 "gb")
# Check for active operation
elif [ "$mdresync" != "0" ] && [ -n "$mdresync" ]; then
RESYNC_STATUS_DATA["active"]="true"
# Calculate progress percentage
local progress=0
if [ "$mdresyncsize" -gt 0 ]; then
progress=$(( mdresyncpos * 100 / mdresyncsize ))
fi
# Calculate rate (blocks per second to KB/s)
local rate_kb_s=0
if [ "$mdresyncdt" -gt 0 ]; then
rate_kb_s=$(( mdresyncdb / mdresyncdt ))
fi
# Calculate estimated time remaining
local eta_seconds=0
if [ "$rate_kb_s" -gt 0 ] && [ "$mdresyncsize" -gt "$mdresyncpos" ]; then
local remaining_blocks=$(( mdresyncsize - mdresyncpos ))
eta_seconds=$(( remaining_blocks / rate_kb_s ))
fi
# Calculate actual elapsed time from start of resync operation
local elapsed_seconds=0
local sbsynced="${NMDSTAT_VALUES[sbSynced]}"
if [ "$sbsynced" != "0" ] && [ -n "$sbsynced" ]; then
elapsed_seconds=$(($(printf "%(%s)T")-sbsynced))
fi
RESYNC_STATUS_DATA["progress_percent"]="$progress"
RESYNC_STATUS_DATA["position_kb"]="$mdresyncpos"
RESYNC_STATUS_DATA["position_gb"]=$(format_kbytes "$mdresyncpos" 0 1 "gb")
RESYNC_STATUS_DATA["size_kb"]="$mdresyncsize"
RESYNC_STATUS_DATA["size_gb"]=$(format_kbytes "$mdresyncsize" 0 0 "gb")
RESYNC_STATUS_DATA["rate_kb_s"]="$rate_kb_s"
RESYNC_STATUS_DATA["elapsed_seconds"]="$elapsed_seconds"
RESYNC_STATUS_DATA["eta_seconds"]="$eta_seconds"
fi
}
# Collect disk status data
collect_disk_status() {
local mdstate="${NMDSTAT_VALUES[mdState]}"
# Store general disk status information
DISK_STATUS_DATA["array_started"]="false"
[ "$mdstate" = "STARTED" ] && DISK_STATUS_DATA["array_started"]="true"
for idx in $(seq 0 29); do
local diskname="${NMDSTAT_VALUES[diskName.$idx]}"
local diskid="${NMDSTAT_VALUES[diskId.$idx]}"
local rdevstatus="${NMDSTAT_VALUES[rdevStatus.$idx]}"
# Determine if this slot should be processed
local should_process=false
local slottype=""
if [ "$idx" -eq 0 ]; then
# Always display P (parity)
slottype="P"
should_process=true
elif [ "$idx" -eq 29 ]; then
# Display Q if it has an ID OR it's a newly assigned disk
slottype="Q"
[ -n "$diskid" ] || [[ "$rdevstatus" == DISK_*_NEW ]] && should_process=true
else
# Data disks (slots 1-28)
slottype="data"
[ -n "$diskname" ] || [ "$rdevstatus" = "DISK_NEW" ] && should_process=true
fi
if [ "$should_process" = true ]; then
local disksize="${NMDSTAT_VALUES[diskSize.$idx]}"
local rdevname="${NMDSTAT_VALUES[rdevName.$idx]}"
local rdevnumerrors="${NMDSTAT_VALUES[rdevNumErrors.$idx]}"
local rdevreads="${NMDSTAT_VALUES[rdevReads.$idx]}"
local rdevwrites="${NMDSTAT_VALUES[rdevWrites.$idx]}"
# For DISK_NEW, get size from rdevSize instead of diskSize
if [[ ("$rdevstatus" = "DISK_NEW" || "$rdevstatus" = "DISK_DSBL_NEW") ]] && { [ -z "$disksize" ] || [ "$disksize" = "0" ]; }; then
local rdevsize="${NMDSTAT_VALUES[rdevSize.$idx]}"
if [ -n "$rdevsize" ] && [ "$rdevsize" -gt 0 ]; then
disksize="$rdevsize"
fi
if [ -z "$diskid" ]; then
diskid="${NMDSTAT_VALUES[rdevId.$idx]}"
fi
fi
# Store common disk data
if [ -n "$rdevstatus" ]; then
DISK_STATUS_DATA["slot_${idx}_present"]="true"
DISK_STATUS_DATA["slot_${idx}_type"]="$slottype"
DISK_STATUS_DATA["slot_${idx}_size_kb"]="$disksize"
DISK_STATUS_DATA["slot_${idx}_size_gb"]=$(format_kbytes "$disksize" 0 0 "gb")
DISK_STATUS_DATA["slot_${idx}_device"]="${rdevname:-none}"
DISK_STATUS_DATA["slot_${idx}_status"]="$rdevstatus"
DISK_STATUS_DATA["slot_${idx}_errors"]="${rdevnumerrors:-0}"
DISK_STATUS_DATA["slot_${idx}_disk_id"]="$diskid"
DISK_STATUS_DATA["slot_${idx}_disk_name"]="${diskname:-none}"
DISK_STATUS_DATA["slot_${idx}_reads"]="${rdevreads:-0}"
DISK_STATUS_DATA["slot_${idx}_writes"]="${rdevwrites:-0}"
# Get filesystem information if array is started
if [ "$slottype" = "data" ] && [ "$mdstate" = "STARTED" ] && [ -n "$diskname" ] && [ "$diskname" != "none" ] && [ "$NO_FS" -eq 0 ]; then
if [ -b "/dev/$diskname" ]; then
local fs_type mountpoint
# In MONITOR_MODE, we trust that fs_type and mountpoint dont really change
if [ "$MONITOR_MODE" -eq 1 ] && [ -n "${DISK_STATUS_DATA[slot_${idx}_fs_type]}" ] && [ -n "${DISK_STATUS_DATA[slot_${idx}_mountpoint]}" ]; then
fs_type="${DISK_STATUS_DATA[slot_${idx}_fs_type]}"
mountpoint="${DISK_STATUS_DATA[slot_${idx}_mountpoint]}"
else
fs_type=$(get_fs_type "/dev/$diskname")
mountpoint=$(get_mountpoint "$diskname" "$fs_type")
DISK_STATUS_DATA["slot_${idx}_fs_type"]="${fs_type:-unknown}"
DISK_STATUS_DATA["slot_${idx}_mountpoint"]="${mountpoint:-unmounted}"
fi
local usage="-"
if [ "$mountpoint" != "unmounted" ] && [ "$mountpoint" != "-" ]; then
usage=$(get_fs_usage "$mountpoint" "$fs_type")
fi
DISK_STATUS_DATA["slot_${idx}_usage"]="$usage"
fi
fi
fi
fi
done
}
# Output formatting functions
format_human_output() {
# Array summary
local mdstate="${ARRAY_STATUS_DATA[mdstate]}"
local sblabel="${ARRAY_STATUS_DATA[sblabel]}"
local sbname="${ARRAY_STATUS_DATA[sbname]}"
local mdnumdisks="${ARRAY_STATUS_DATA[mdnumdisks]}"
echo -e "Array State : $(format_array_state "$mdstate")"
[ -n "$sblabel" ] && echo -e "Array Label : $sblabel"
# Check if superblock is valid
if [ "$sbname" = "(null)" ]; then
echo -e "Superblock : ${RED}INVALID${NC}"
echo -e "${YELLOW}Warning: Module loaded without specifying superblock.${NC}"
echo -e "${YELLOW}Use '${GREEN}nmdctl --super /path/to/superblock.dat reload${YELLOW}' to reload with valid superblock.${NC}"
else
echo -e "Superblock : $sbname"
fi
echo -e "Disks Present : $mdnumdisks"
# Check for driver inconsistent state and show warning
if check_driver_inconsistent_state; then
echo ""
echo -e "${YELLOW}WARNING: Driver internal state is inconsistent!${NC}"
echo -e "${YELLOW}The array mdNum* counters show non-zero values, but all individual disks are DISK_OK status.${NC}"
echo -e "${YELLOW}This can happen after initial array creation, but it may cause unexpected behavior.${NC}"
echo -e "${YELLOW}Recommend reloading the driver and starting the array again: ${GREEN}nmdctl reload && nmdctl start${NC}"
echo ""
fi
# Array health
local health_status="${ARRAY_STATUS_DATA[health_status]}"
local health_details="${ARRAY_STATUS_DATA[health_details]}"
local last_sync_human="${ARRAY_STATUS_DATA[last_sync_human]}"
local health_formatted="$(format_health_status "$health_status")"
echo -n -e "Array Health : $health_formatted"
if [ -n "$health_details" ]; then
echo -e " ($health_details)"
else
echo ""
fi
echo -e "Array Checked : $last_sync_human"
# Array size and parity
local data_size_kb="${ARRAY_STATUS_DATA[data_size_kb]}"
local data_disk_count="${ARRAY_STATUS_DATA[data_disk_count]}"
local has_parity="${ARRAY_STATUS_DATA[has_parity]}"
local has_second_parity="${ARRAY_STATUS_DATA[has_second_parity]}"
local parity_size_kb="${ARRAY_STATUS_DATA[parity_size_kb]}"
local second_parity_size_kb="${ARRAY_STATUS_DATA[second_parity_size_kb]}"
echo -e "Array Size : $(format_kbytes "$data_size_kb") (${data_disk_count} data disk(s))"
echo -n "Parity : "
if [ "$has_parity" = "true" ] && [ "$has_second_parity" = "true" ]; then
echo -e "${GREEN}Dual Parity${NC} (P: $(format_kbytes "$parity_size_kb") , Q: $(format_kbytes "$second_parity_size_kb"))"
elif [ "$has_parity" = "true" ]; then
echo -e "${GREEN}Single Parity${NC} ($(format_kbytes "$parity_size_kb"))"
else
echo -e "${YELLOW}No Parity${NC}"
fi
# Resync status
format_human_resync_status
# Disk status
format_human_disk_status
}
format_human_resync_status() {
local active="${RESYNC_STATUS_DATA[active]}"
local paused="${RESYNC_STATUS_DATA[paused]}"
local pending="${RESYNC_STATUS_DATA[pending]}"
local friendly_action="${RESYNC_STATUS_DATA[friendly_action]}"
local array_start_hint="${RESYNC_STATUS_DATA[array_start_hint]}"
if [ "$paused" = "true" ]; then
local progress="${RESYNC_STATUS_DATA[progress_percent]}"
local pos_kb="${RESYNC_STATUS_DATA[position_kb]}"
local size_kb="${RESYNC_STATUS_DATA[size_kb]}"
local pos_kb_formatted=$(format_kbytes "$pos_kb" 1 2)
local size_kb_formatted=$(format_kbytes "$size_kb")
echo ""
echo -e "Operation : ${BLUE}$friendly_action${NC} ${YELLOW}(PAUSED)${NC}"
echo -e "Progress : $progress% ($pos_kb_formatted / $size_kb_formatted)"
echo -e "${YELLOW}Resume with : '${GREEN}nmdctl check resume${YELLOW}' command${NC}"
elif [ "$pending" = "true" ]; then
local action="${RESYNC_STATUS_DATA[action]}"
echo ""
if [ "$action" = "clear" ]; then
echo -e "${YELLOW}Disk clearing pending: New disk needs to be cleared${NC}"
else
echo -e "${YELLOW}Disk reconstruction pending: ${BLUE}$friendly_action${NC}"
fi
echo -e "${YELLOW}${array_start_hint} '${GREEN}nmdctl check${YELLOW}' command to start the operation${NC}"
elif [ "$active" = "true" ]; then
local progress="${RESYNC_STATUS_DATA[progress_percent]}"
local pos_kb="${RESYNC_STATUS_DATA[position_kb]}"
local size_kb="${RESYNC_STATUS_DATA[size_kb]}"
local rate_kb_s="${RESYNC_STATUS_DATA[rate_kb_s]}"
local elapsed_seconds="${RESYNC_STATUS_DATA[elapsed_seconds]}"
local eta_seconds="${RESYNC_STATUS_DATA[eta_seconds]}"
local elapsed_formatted=$(format_time_duration "$elapsed_seconds")
local eta_formatted=$(format_time_duration "$eta_seconds")
local pos_kb_formatted=$(format_kbytes "$pos_kb" 1 2)
local size_kb_formatted=$(format_kbytes "$size_kb")
local rate_formatted=$(format_io_rate "$rate_kb_s" 0)
echo ""
echo -e "Operation : ${BLUE}$friendly_action${NC}"
echo -e "Progress : $progress% ($pos_kb_formatted / $size_kb_formatted)"
echo -e "Speed : $rate_formatted"
echo -e "Elapsed Time : $elapsed_formatted"
echo -e "ETA : $eta_formatted"
fi
}
format_human_disk_status() {
local array_started="${DISK_STATUS_DATA[array_started]}"
echo ""