blob: 6eccfa73253442e5d676ad1961c85106ada1c295 (
plain)
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
|
#!/usr/bin/env bash
# raid.sh - Pure RAID-level logic (testable, no I/O).
# Source after common.sh.
#############################
# Valid-level enumeration
#############################
# Print valid RAID levels for a given disk count, one per line.
# Count <2: nothing printed (single disk = no RAID).
# Count 2: mirror, stripe
# Count 3+: + raidz1
# Count 4+: + raidz2
# Count 5+: + raidz3
raid_valid_levels_for_count() {
local count=$1
[[ $count -lt 2 ]] && return 0
echo mirror
echo stripe
[[ $count -ge 3 ]] && echo raidz1
[[ $count -ge 4 ]] && echo raidz2
[[ $count -ge 5 ]] && echo raidz3
return 0
}
# Return 0 if level is valid for the given disk count, 1 otherwise.
# Empty level with count 1 is valid (no RAID).
raid_is_valid() {
local level=$1 count=$2
if [[ $count -le 1 ]]; then
[[ -z "$level" ]]
return
fi
raid_valid_levels_for_count "$count" | grep -qxF "$level"
}
#############################
# Usable-space computation
#############################
# Print usable bytes for a level given disk count, smallest-disk bytes,
# and total bytes across all disks. Writes nothing and returns 1 for
# unknown levels.
#
# Usage: raid_usable_bytes LEVEL COUNT SMALLEST_BYTES TOTAL_BYTES
raid_usable_bytes() {
local level=$1 count=$2 smallest=$3 total=$4
case "$level" in
mirror) echo "$smallest" ;;
stripe) echo "$total" ;;
raidz1) echo $(( (count - 1) * smallest )) ;;
raidz2) echo $(( (count - 2) * smallest )) ;;
raidz3) echo $(( (count - 3) * smallest )) ;;
*) return 1 ;;
esac
}
# Print fault-tolerance (max number of disks that can fail) for a level
# at the given disk count. Unknown level → return 1.
raid_fault_tolerance() {
local level=$1 count=$2
case "$level" in
mirror) echo $(( count - 1 )) ;;
stripe) echo 0 ;;
raidz1) echo 1 ;;
raidz2) echo 2 ;;
raidz3) echo 3 ;;
*) return 1 ;;
esac
}
#############################
# Preview text
#############################
# Print preview text for a single RAID level. Used by get_raid_level()
# in the fzf preview pane. Calls raid_fault_tolerance and
# raid_usable_bytes for the data lines so the math stays in one place.
# Numeric arguments are unit-agnostic — pass GB if you want GB out.
#
# Usage: raid_preview LEVEL DISK_COUNT TOTAL_GB SMALLEST_GB
# Returns 1 for unknown level (no output).
raid_preview() {
local level=$1 count=$2 total=$3 small=$4
local tol usable
tol=$(raid_fault_tolerance "$level" "$count") || return 1
usable=$(raid_usable_bytes "$level" "$count" "$small" "$total") || return 1
case "$level" in
mirror)
cat <<EOF
MIRROR
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
All disks contain identical copies of data.
Maximum redundancy. Can survive loss of all
disks except one.
Redundancy: Can lose $tol of $count disks
Usable space: ~${usable}GB (smallest disk)
Read speed: Fast (parallel reads)
Write speed: Normal
Best for:
- Boot drives
- Critical data
- Maximum safety
EOF
;;
stripe)
cat <<EOF
STRIPE (RAID0)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
WARNING: NO REDUNDANCY!
Data is striped across all disks.
ANY disk failure = ALL data lost!
Redundancy: NONE
Usable space: ~${usable}GB (all disks)
Read speed: Very fast
Write speed: Very fast
Best for:
- Scratch/temp space
- Replaceable data
- Maximum performance
EOF
;;
raidz1)
cat <<EOF
RAIDZ1 (Single Parity)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
One disk worth of parity distributed
across all disks.
Redundancy: Can lose $tol of $count disks
Usable space: ~${usable}GB ($((count - 1)) of $count disks)
Read speed: Fast
Write speed: Good
Best for:
- General storage
- Good balance of space/safety
EOF
;;
raidz2)
cat <<EOF
RAIDZ2 (Double Parity)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Two disks worth of parity distributed
across all disks.
Redundancy: Can lose $tol of $count disks
Usable space: ~${usable}GB ($((count - 2)) of $count disks)
Read speed: Fast
Write speed: Good
Best for:
- Large arrays (5+ disks)
- Important data
EOF
;;
raidz3)
cat <<EOF
RAIDZ3 (Triple Parity)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Three disks worth of parity distributed
across all disks.
Redundancy: Can lose $tol of $count disks
Usable space: ~${usable}GB ($((count - 3)) of $count disks)
Read speed: Fast
Write speed: Moderate
Best for:
- Very large arrays (8+ disks)
- Archival storage
EOF
;;
*)
return 1
;;
esac
}
|