-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·391 lines (328 loc) · 9.88 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·391 lines (328 loc) · 9.88 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
#!/usr/bin/env bash
mkdir -p bin
mkdir -p page/src
mkdir -p .github/workflows
cat > VERSION << 'EOF'
1.0.0
EOF
cat > settings.json << 'EOF'
{
"dry_run": true,
"threads": 4,
"tolerance": 5,
"pool": "/mnt/mosarray"
}
EOF
cat > functions << 'EOF'
#!/usr/bin/env bash
PLUGIN="mergerfs_rebalance"
PLUGIN_DIR="/usr/bin/plugins"
SCRIPT="$PLUGIN_DIR/mergerfs-rebalance.sh"
LOG_FILE="/var/log/mos/plugins/$PLUGIN/rebalance.log"
install() {
mkdir -p $PLUGIN_DIR
cp bin/mergerfs-rebalance.sh $SCRIPT
chmod +x $SCRIPT
}
uninstall() {
rm -f $SCRIPT
}
rebalance() {
$SCRIPT "$@"
}
logs() {
tail -n 200 "$LOG_FILE"
}
status() {
grep PROGRESS "$LOG_FILE" | tail -1
}
EOF
chmod +x functions
cat > bin/mergerfs-rebalance.sh << 'EOF'
#!/usr/bin/env bash
POOL="${1:-/mnt/mosarray}"
DRY_RUN="${2:-true}"
TOLERANCE="${3:-5}"
# Argument 4 (THREADS) is reserved for future parallel support and is not used.
PLUGIN="mergerfs_rebalance"
LOG_DIR="/var/log/mos/plugins/$PLUGIN"
LOG_FILE="$LOG_DIR/rebalance.log"
mkdir -p "$LOG_DIR"
: > "$LOG_FILE"
log() {
echo "$(date '+%F %T') $*" | tee -a "$LOG_FILE"
}
die() {
log "ERROR: $*"
exit 1
}
# ---- Detect mergerfs layout ----
detect_mergerfs_branches() {
local pool="$1"
# Find the mergerfs mount entry for this pool path
local mount_line
mount_line=$(awk -v pool="$pool" '$2 == pool && $3 == "fuse.mergerfs"' /proc/mounts | head -1)
if [[ -z "$mount_line" ]]; then
die "No mergerfs mount found for pool: $pool"
fi
log "Detected mergerfs mount: $mount_line"
# The source field (field 1) contains colon-separated branch paths, e.g.:
# /mnt/disk1:/mnt/disk2:/mnt/disk3
local source
source=$(echo "$mount_line" | awk '{print $1}')
# Strip any per-branch policy suffixes (e.g. /mnt/disk1=RW -> /mnt/disk1)
echo "$source" | tr ':' '\n' | sed 's/=.*//'
}
# ---- Detect snapraid data drives ----
detect_snapraid_data_drives() {
local conf="${1:-/etc/snapraid.conf}"
if [[ ! -f "$conf" ]]; then
log "WARNING: $conf not found -- skipping snapraid drive cross-check"
return
fi
grep -E '^data[[:space:]]' "$conf" | awk '{print $3}'
}
# ---- Disk space helpers ----
get_avail_kb() {
df --output=avail "$1" 2>/dev/null | tail -1 | tr -d ' '
}
# Returns how many percent below the average a given available-KB value is.
# A positive result means the branch is overfull; negative means it has surplus.
pct_below_avg() {
local avail="$1"
local avg="$2"
if (( avg > 0 )); then
echo $(( (avg - avail) * 100 / avg ))
else
echo 0
fi
}
# Global error counter updated by rebalance_pool; read in main to gate snapraid sync.
REBALANCE_ERRORS=0
# ---- Rebalance ----
rebalance_pool() {
local pool="$1"
local dry_run="$2"
local tolerance="$3"
log "Detecting mergerfs layout for pool: $pool"
local branches
mapfile -t branches < <(detect_mergerfs_branches "$pool")
if [[ ${#branches[@]} -eq 0 ]]; then
die "No mergerfs branches found for pool: $pool"
fi
log "Found ${#branches[@]} branch(es): ${branches[*]}"
# Cross-check with snapraid data drives
local snapraid_drives
mapfile -t snapraid_drives < <(detect_snapraid_data_drives)
if [[ ${#snapraid_drives[@]} -gt 0 ]]; then
log "Snapraid data drives: ${snapraid_drives[*]}"
# Warn about any branch not covered by snapraid
for branch in "${branches[@]}"; do
local found=false
for sd in "${snapraid_drives[@]}"; do
[[ "$branch" == "$sd" || "$branch" == "${sd%/}" ]] && found=true && break
done
$found || log "WARNING: branch $branch is not listed as a snapraid data drive"
done
fi
# Collect available space per branch; skip branches where df fails or returns non-numeric data.
declare -A avail_map
local total_avail=0
local n=0
local valid_branches=()
for branch in "${branches[@]}"; do
local avail
avail=$(get_avail_kb "$branch")
if ! [[ "$avail" =~ ^[0-9]+$ ]]; then
log "WARNING: could not read available space for $branch -- skipping"
continue
fi
avail_map["$branch"]=$avail
total_avail=$(( total_avail + avail ))
n=$(( n + 1 ))
valid_branches+=("$branch")
log " $branch: ${avail} KB available"
done
if (( n == 0 )); then
die "No branches with readable free space found in pool $pool. Check branch mount points and disk health."
fi
local avg_avail=$(( total_avail / n ))
log "Average available across branches: ${avg_avail} KB (tolerance: ${tolerance}%)"
local moved=0
for src_branch in "${valid_branches[@]}"; do
local src_avail=${avail_map[$src_branch]}
# Skip if src is not significantly below average (i.e. not overfull)
local diff_pct
diff_pct=$(pct_below_avg "$src_avail" "$avg_avail")
if (( diff_pct <= tolerance )); then
continue
fi
log "Branch $src_branch is overfull (${src_avail} KB avail, ${diff_pct}% below average)"
while IFS= read -r -d '' file; do
# Refresh src available after each move
src_avail=${avail_map[$src_branch]}
diff_pct=$(pct_below_avg "$src_avail" "$avg_avail")
(( diff_pct <= tolerance )) && break
local file_size
file_size=$(du -sk "$file" 2>/dev/null | awk '{print $1}')
[[ -z "$file_size" || "$file_size" -eq 0 ]] && continue
# Find the branch with the most free space (that isn't the source).
# rsync is used instead of mv because mergerfs branches can span different
# physical filesystems, making atomic rename (mv) unreliable across them.
local best_target=""
local best_avail=0
for tgt_branch in "${valid_branches[@]}"; do
[[ "$tgt_branch" == "$src_branch" ]] && continue
local tgt_avail=${avail_map[$tgt_branch]}
if (( tgt_avail > best_avail && tgt_avail > file_size )); then
best_avail=$tgt_avail
best_target=$tgt_branch
fi
done
[[ -z "$best_target" ]] && continue
local rel_path="${file#${src_branch}/}"
local dest="${best_target}/${rel_path}"
local dest_dir
dest_dir=$(dirname "$dest")
if [[ "$dry_run" == "true" ]]; then
log "DRY-RUN: would move $file -> $dest"
else
# Skip if destination already exists to prevent silent data loss.
if [[ -e "$dest" ]]; then
log "SKIP: destination already exists -- skipping to avoid overwrite: $dest"
continue
fi
if mkdir -p "$dest_dir" && rsync -a --remove-source-files "$file" "$dest"; then
# File removed from src: src gains free space; file added to target: target loses space
avail_map[$src_branch]=$(( src_avail + file_size ))
avail_map[$best_target]=$(( best_avail - file_size ))
moved=$(( moved + 1 ))
log "Moved: $file -> $dest"
else
REBALANCE_ERRORS=$(( REBALANCE_ERRORS + 1 ))
log "ERROR: failed to move $file -> $dest"
fi
fi
done < <(find "$src_branch" -type f -print0 2>/dev/null)
done
log "Rebalance complete. Moved: $moved file(s), Errors: $REBALANCE_ERRORS"
log "PROGRESS:90"
}
# ---- Main ----
log "Starting rebalance: pool=$POOL dry_run=$DRY_RUN tolerance=$TOLERANCE"
rebalance_pool "$POOL" "$DRY_RUN" "$TOLERANCE"
# ---- Run snapraid sync ----
# Skip sync if any move errors occurred to avoid snapshotting an inconsistent state.
if (( REBALANCE_ERRORS > 0 )); then
log "ERROR: $REBALANCE_ERRORS move error(s) occurred — skipping snapraid sync"
log "PROGRESS:100"
log "Completed with errors"
exit 1
fi
if command -v snapraid &>/dev/null; then
if [[ "$DRY_RUN" == "true" ]]; then
log "DRY-RUN: would run: snapraid sync"
else
log "Running snapraid sync..."
if snapraid sync 2>&1 | tee -a "$LOG_FILE"; then
log "Snapraid sync complete"
else
log "ERROR: snapraid sync failed"
fi
fi
else
log "WARNING: snapraid binary not found -- skipping sync"
fi
log "PROGRESS:100"
log "Completed"
EOF
chmod +x bin/mergerfs-rebalance.sh
cat > page/plugin.config.js << 'EOF'
export default {
name: "mergerfs_rebalance",
displayName: "MergerFS Rebalance",
description: "Balance files across a mergerfs pool",
icon: "mdi-harddisk",
author: "FugginOld",
version: "1.0.0"
};
EOF
cat > page/index.html << 'EOF'
MergerFS Rebalance Plugin
EOF
cat > page/src/Plugin.vue << 'EOF'
<template>
<div>
<h2>MergerFS Rebalance</h2>
<button @click="run">Run Rebalance</button>
<pre>{{ logs }}</pre>
</div>
</template>
<script>
export default {
data() {
return { logs: "" };
},
methods: {
async run() {
await fetch("/mos/plugins/executefunction", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
plugin: "mergerfs_rebalance",
function: "rebalance"
})
});
setInterval(this.updateLogs, 2000);
},
async updateLogs() {
const res = await fetch("/mos/plugins/executefunction", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
plugin: "mergerfs_rebalance",
function: "logs"
})
});
this.logs = await res.text();
}
}
};
</script>
EOF
cat > .github/workflows/build-plugin.yml << 'EOF'
name: Build MOS Plugin
on:
push:
branches: [main]
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Prepare
run: |
chmod +x functions
chmod +x bin/mergerfs-rebalance.sh
mkdir -p build/usr/bin/plugins
cp bin/mergerfs-rebalance.sh build/usr/bin/plugins/
cp functions build/
cp -r page build/
cp settings.json build/
cp VERSION build/
- name: Package
run: |
cd build
tar -czf ../mos-plugin.tar.gz *
EOF
cat > README.md << 'EOF'
# MOS MergerFS Rebalance Plugin
Plugin for MOS to rebalance files across mergerfs disks.
Features:
- Auto disk detection
- Multi-threaded rebalance
- Live logs
- Progress tracking
EOF