File: btrfsmaintenance-functions

package info (click to toggle)
btrfsmaintenance 0.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 236 kB
  • sloc: sh: 356; python: 50; makefile: 9
file content (78 lines) | stat: -rw-r--r-- 2,080 bytes parent folder | download
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
#!/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
}