forked from kdave/btrfsmaintenance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfsmaintenance-functions
68 lines (63 loc) · 1.97 KB
/
btrfsmaintenance-functions
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
#!/bin/bash
#
# this file contains common code for the btrfs maintenance scripts
#
# function: evaluate_auto_mountpoint
# parameter: A variable name
#
# this function checks whether the variable contains the special keyword "auto"
# if yes, all currently mounted btrfs filesystems are evaluated and their mountpoints
# are put into the parameter variable
evaluate_auto_mountpoint() {
MOUNTPOINTSVAR=\$"$1"
if [ "$(eval "expr \"$MOUNTPOINTSVAR\"")" = "auto" ]; then
local BTRFS_DEVICES=""
local DEVICE=""
local MNT=""
local MNTLIST=""
# find all mounted btrfs filesystems, print their device nodes, sort them
# and remove identical entries
BTRFS_DEVICES=$(findmnt --types btrfs --output "SOURCE" --nofsroot --noheading | sort | uniq)
# find one (and only one) corresponding mountpoint for each btrfs device node
for DEVICE in $BTRFS_DEVICES; do
MNT=$(findmnt --types btrfs --first-only --noheadings --output "TARGET" --source "$DEVICE")
if [ -n "$MNTLIST" ]; then
MNTLIST="$MNTLIST:$MNT"
else
MNTLIST="$MNT"
fi
done
echo "evaluate mounted filesystems: $MNTLIST"
eval "$1=$MNTLIST"
fi
}
# function: detect_mixed_bg
# parameter: path to a mounted filesystem
#
# check if the filesystem contains mixed block groups,
detect_mixed_bg() {
# simple test is to read 'btrfs fi df',
# (we could look for /sys/sfs/btrfs/UUID/allocation/mixed if we know
# the UUID)
btrfs filesystem df "$1" | grep -q "Data+Metadata"
}
# function: check_scrub_running
# parameter: path to a mounted filesystem
#
# check if scrub is in progress on a given filesystem, return 0 if it is so
check_scrub_running() {
btrfs scrub status "$1" | grep -q "scrub.*running for"
}
# function: check_balance_running
# parameter: path to a mounted filesystem
#
# check if balance is in progress on a given filesystem, return 0 if it is so
check_balance_running() {
# 0: not in progress
# 1: in progress
# 2: other error (EPERM)
if btrfs balance status "$1" >& /dev/null; then
return 1
fi
return 0
}