-
Notifications
You must be signed in to change notification settings - Fork 80
/
btrfsmaintenance-functions
120 lines (106 loc) · 3.14 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
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
#!/bin/bash
#
# this file contains common code for the btrfs maintenance scripts
#
# function: expand_auto_mountpoint
# parameter: path list from config variable or 'auto'
#
# if the parameter is 'auto', this function prints path list of all btrfs
# mountpoints, otherwise prints the parameter unchanged
expand_auto_mountpoint() {
local MNTLIST="$1"
if [ "$MNTLIST" = "auto" ]; then
local BTRFS_DEVICES
local DEVICE
local MNT
# 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
MNTLIST=""
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
fi
echo -n "$MNTLIST"
}
# 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
}
# function: is_btrfs
# parameter: path to a mounted filesystem
#
# check if filesystem is a btrfs
is_btrfs() {
local FS=$(stat -f --format=%T "$1")
[ "$FS" = "btrfs" ] && return 0
return 1
}
# function: is_raid56
# parameter: path to a mounted filesystem
#
# check if filesystem is on a RAID-5 or RAID-6
is_raid56() {
btrfs filesystem usage "$1" | grep Data,RAID5 && return 0
btrfs filesystem usage "$1" | grep Data,RAID6 && return 0
return 1
}
# function: btrfs_fsid
# parameter: path to a mounted filesystem
#
# return filesystem UUID on a given path
btrfs_fsid() {
btrfs filesystem show "$1" | sed -n -e '/uuid:/ {s/^.*uuid: //;p }'
}
# function: run_task
# parameter: command to run, expecting the mountpoint to be the last argument
#
# run the given command with concurrency protection unless allowed by the
# config, use for tasks that should not run at the same time due to heavy IO
run_task() {
local MNT="${@:$#}"
local UUID=$(btrfs_fsid "$MNT")
local verbose
if test "$BTRFS_ALLOW_CONCURRENCY" = "true"; then
"$@"
else
# Flock older than 2.27 does not support --verbose option, check
# if it's available as we'd like to log the information
if /usr/bin/flock --help 2>&1 | grep -q -- --verbose; then
verbose="--verbose"
fi
/usr/bin/flock $verbose /run/btrfs-maintenance-running."$UUID" "$@"
fi
}