Debian Codegraph @

btrfsmaintenance-functions

raw symbols SCIP
1 #!/bin/bash
2 #
3 # this file contains common code for the btrfs maintenance scripts
4 #
5
6 # function: expand_auto_mountpoint
7 # parameter: path list from config variable or 'auto'
8 #
9 # if the parameter is 'auto', this function prints path list of all btrfs
10 # mountpoints, otherwise prints the parameter unchanged
11 expand_auto_mountpoint() {
12 local MNTLIST="$1"
13
14 if [ "$MNTLIST" = "auto" ]; then
15 local BTRFS_DEVICES
16 local DEVICE
17 local MNT
18
19 # find all mounted btrfs filesystems, print their device nodes, sort them
20 # and remove identical entries
21 BTRFS_DEVICES=$(findmnt --types btrfs --output "SOURCE" --nofsroot --noheading | sort | uniq)
22 # find one (and only one) corresponding mountpoint for each btrfs device node
23 MNTLIST=""
24 for DEVICE in $BTRFS_DEVICES; do
25 MNT=$(findmnt --types btrfs --first-only --noheadings --output "TARGET" --source "$DEVICE")
26 if [ -n "$MNTLIST" ]; then
27 MNTLIST="$MNTLIST:$MNT"
28 else
29 MNTLIST="$MNT"
30 fi
31 done
32 fi
33 echo -n "$MNTLIST"
34 }
35
36 # function: detect_mixed_bg
37 # parameter: path to a mounted filesystem
38 #
39 # check if the filesystem contains mixed block groups,
40 detect_mixed_bg() {
41 # simple test is to read 'btrfs fi df',
42 # (we could look for /sys/sfs/btrfs/UUID/allocation/mixed if we know
43 # the UUID)
44
45 btrfs filesystem df "$1" | grep -q "Data+Metadata"
46 }
47
48 # function: check_scrub_running
49 # parameter: path to a mounted filesystem
50 #
51 # check if scrub is in progress on a given filesystem, return 0 if it is so
52 check_scrub_running() {
53 btrfs scrub status "$1" | grep -q "scrub.*running for"
54 }
55
56 # function: check_balance_running
57 # parameter: path to a mounted filesystem
58 #
59 # check if balance is in progress on a given filesystem, return 0 if it is so
60 check_balance_running() {
61 # 0: not in progress
62 # 1: in progress
63 # 2: other error (EPERM)
64 if btrfs balance status "$1" >& /dev/null; then
65 return 1
66 fi
67 return 0
68 }
69
70 # function: is_btrfs
71 # parameter: path to a mounted filesystem
72 #
73 # check if filesystem is a btrfs
74 is_btrfs() {
75 local FS=$(stat -f --format=%T "$1")
76
77 [ "$FS" = "btrfs" ] && return 0
78 return 1
79 }
80
81 # function: is_raid56
82 # parameter: path to a mounted filesystem
83 #
84 # check if filesystem is on a RAID-5 or RAID-6
85 is_raid56() {
86 btrfs filesystem usage "$1" | grep Data,RAID5 && return 0
87 btrfs filesystem usage "$1" | grep Data,RAID6 && return 0
88 return 1
89 }
90
91 # function: btrfs_fsid
92 # parameter: path to a mounted filesystem
93 #
94 # return filesystem UUID on a given path
95 btrfs_fsid() {
96 btrfs filesystem show "$1" | sed -n -e '/uuid:/ {s/^.*uuid: //;p }'
97 }
98
99 # function: run_task
100 # parameter: command to run, expecting the mountpoint to be the last argument
101 #
102 # run the given command with concurrency protection unless allowed by the
103 # config, use for tasks that should not run at the same time due to heavy IO
104 run_task() {
105 local MNT="${@:$#}"
106 local UUID=$(btrfs_fsid "$MNT")
107 local verbose
108
109 if test "$BTRFS_ALLOW_CONCURRENCY" = "true"; then
110 "$@"
111 else
112 # Flock older than 2.27 does not support --verbose option, check
113 # if it's available as we'd like to log the information
114 if /usr/bin/flock --help 2>&1 | grep -q -- --verbose; then
115 verbose="--verbose"
116 fi
117
118 /usr/bin/flock $verbose /run/btrfs-maintenance-running."$UUID" "$@"
119 fi
120 }