-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergerfs-drivepool-check.py
executable file
·178 lines (154 loc) · 4.84 KB
/
mergerfs-drivepool-check.py
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
#!/usr/bin/env python3
"""
Finds files that are on more than one drive and checks to make sure they
are the same (to detect e.g. silent disk corruption).
Based on "mergerfs.fsck" from mergerfs-tools.
"""
# Original copyright notice from mergerfs.fsck:
#
# Copyright (c) 2016, Antonio SJ Musumeci <[email protected]>
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import argparse
import ctypes
import errno
import io
import os
import subprocess
import sys
checked_count = 0
different_count = 0
different_files = []
_libc = ctypes.CDLL("libc.so.6", use_errno=True)
_lgetxattr = _libc.lgetxattr
_lgetxattr.argtypes = [
ctypes.c_char_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_size_t,
]
def lgetxattr(path, name):
if type(path) == str:
path = path.encode(errors='backslashreplace')
if type(name) == str:
name = name.encode(errors='backslashreplace')
length = 64
while True:
buf = ctypes.create_string_buffer(length)
res = _lgetxattr(path, name, buf, ctypes.c_size_t(length))
if res >= 0:
return buf.raw[0:res]
else:
err = ctypes.get_errno()
if err == errno.ERANGE:
length *= 2
elif err == errno.ENODATA:
return None
else:
raise OSError(err, os.strerror(err), path)
def ismergerfs(path):
try:
lgetxattr(path, "user.mergerfs.fullpath")
return True
except OSError:
return False
def print_stats(Files, Stats):
for i in range(0, len(Files)):
print(" %i: %s" % (i, Files[i].decode(errors='backslashreplace')))
data = (
" - uid: {:5}; gid: {:5}; mode: {:6o}; " "size: {:10}; mtime: {}"
).format(
Stats[i].st_uid,
Stats[i].st_gid,
Stats[i].st_mode,
Stats[i].st_size,
Stats[i].st_mtime,
)
print(data)
def check_consistancy(fullpath, verbose):
paths = lgetxattr(fullpath, "user.mergerfs.allpaths")
if not paths:
return
paths = paths.split(b'\0')
if len(paths) <= 1:
return
global checked_count
checked_count += 1
if verbose:
print("%s" % fullpath)
diff = subprocess.run(['diff', '-q'] + paths)
if diff.returncode != 0:
global different_count
global different_files
different_count += 1
different_files.append(paths)
stats = [os.stat(path) for path in paths]
# print("%s" % fullpath)
if verbose:
print_stats(paths, stats)
def buildargparser():
parser = argparse.ArgumentParser(
description='audit a mergerfs mount for inconsistencies',
)
parser.add_argument(
'dir',
type=str,
help='starting directory',
)
parser.add_argument(
'-v',
'--verbose',
action='store_true',
help='print details of audit item',
)
return parser
def main():
sys.stdout = io.TextIOWrapper(
sys.stdout.buffer,
encoding='utf8',
errors='backslashreplace',
line_buffering=True,
)
sys.stderr = io.TextIOWrapper(
sys.stderr.buffer,
encoding='utf8',
errors='backslashreplace',
line_buffering=True,
)
parser = buildargparser()
args = parser.parse_args()
args.dir = os.path.realpath(args.dir)
if not ismergerfs(args.dir):
print("%s is not a mergerfs directory" % args.dir)
sys.exit(1)
try:
verbose = args.verbose
for (dirname, dirnames, filenames) in os.walk(args.dir):
fulldirpath = os.path.join(args.dir, dirname)
# check_consistancy(fulldirpath,verbose)
for filename in filenames:
fullpath = os.path.join(fulldirpath, filename)
check_consistancy(fullpath, verbose)
except KeyboardInterrupt:
pass
except OSError as e:
if e.errno == errno.EPIPE:
pass
else:
raise
print(f"Checked count: {checked_count}")
print(f"Different count: {different_count}")
if different_files:
for file in different_files:
print(file)
sys.exit(0)
if __name__ == "__main__":
main()