]>
Commit | Line | Data |
---|---|---|
9d50331a | 1 | #!/usr/bin/python3 |
f1dca11c | 2 | |
959ef981 | 3 | # SPDX-License-Identifier: GPL-2.0+ |
f1dca11c DW |
4 | # Copyright (C) 2018 Oracle. All rights reserved. |
5 | # | |
6 | # Author: Darrick J. Wong <darrick.wong@oracle.com> | |
959ef981 DC |
7 | |
8 | # Run online scrubbers in parallel, but avoid thrashing. | |
f1dca11c DW |
9 | |
10 | import subprocess | |
11 | import json | |
12 | import threading | |
13 | import time | |
14 | import sys | |
824b5807 | 15 | import os |
3dd91472 | 16 | import argparse |
f1dca11c DW |
17 | |
18 | retcode = 0 | |
19 | terminate = False | |
20 | ||
824b5807 DW |
21 | def DEVNULL(): |
22 | '''Return /dev/null in subprocess writable format.''' | |
23 | try: | |
24 | from subprocess import DEVNULL | |
25 | return DEVNULL | |
26 | except ImportError: | |
27 | return open(os.devnull, 'wb') | |
28 | ||
f1dca11c DW |
29 | def find_mounts(): |
30 | '''Map mountpoints to physical disks.''' | |
31 | ||
32 | fs = {} | |
33 | cmd=['lsblk', '-o', 'KNAME,TYPE,FSTYPE,MOUNTPOINT', '-J'] | |
34 | result = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
35 | result.wait() | |
36 | if result.returncode != 0: | |
37 | return fs | |
74aed9c8 | 38 | sarray = [x.decode(sys.stdout.encoding) for x in result.stdout.readlines()] |
f1dca11c DW |
39 | output = ' '.join(sarray) |
40 | bdevdata = json.loads(output) | |
41 | # The lsblk output had better be in disks-then-partitions order | |
42 | for bdev in bdevdata['blockdevices']: | |
43 | if bdev['type'] in ('disk', 'loop'): | |
44 | lastdisk = bdev['kname'] | |
45 | if bdev['fstype'] == 'xfs': | |
46 | mnt = bdev['mountpoint'] | |
47 | if mnt is None: | |
48 | continue | |
49 | if mnt in fs: | |
50 | fs[mnt].add(lastdisk) | |
51 | else: | |
52 | fs[mnt] = set([lastdisk]) | |
53 | return fs | |
54 | ||
824b5807 DW |
55 | def kill_systemd(unit, proc): |
56 | '''Kill systemd unit.''' | |
57 | proc.terminate() | |
58 | cmd=['systemctl', 'stop', unit] | |
59 | x = subprocess.Popen(cmd) | |
60 | x.wait() | |
61 | ||
f1dca11c DW |
62 | def run_killable(cmd, stdout, killfuncs, kill_fn): |
63 | '''Run a killable program. Returns program retcode or -1 if we can't start it.''' | |
64 | try: | |
65 | proc = subprocess.Popen(cmd, stdout = stdout) | |
66 | real_kill_fn = lambda: kill_fn(proc) | |
67 | killfuncs.add(real_kill_fn) | |
68 | proc.wait() | |
69 | try: | |
70 | killfuncs.remove(real_kill_fn) | |
71 | except: | |
72 | pass | |
73 | return proc.returncode | |
74 | except: | |
75 | return -1 | |
76 | ||
07c6fd59 DW |
77 | # systemd doesn't like unit instance names with slashes in them, so it |
78 | # replaces them with dashes when it invokes the service. However, it's not | |
79 | # smart enough to convert the dashes to something else, so when it unescapes | |
80 | # the instance name to feed to xfs_scrub, it turns all dashes into slashes. | |
81 | # "/moo-cow" becomes "-moo-cow" becomes "/moo/cow", which is wrong. systemd | |
82 | # actually /can/ escape the dashes correctly if it is told that this is a path | |
83 | # (and not a unit name), but it didn't do this prior to January 2017, so fix | |
84 | # this for them. | |
85 | def systemd_escape(path): | |
86 | '''Escape a path to avoid mangled systemd mangling.''' | |
87 | ||
88 | if '-' not in path: | |
89 | return path | |
90 | cmd = ['systemd-escape', '--path', path] | |
91 | try: | |
92 | proc = subprocess.Popen(cmd, stdout = subprocess.PIPE) | |
93 | proc.wait() | |
94 | for line in proc.stdout: | |
95 | return '-' + line.decode(sys.stdout.encoding).strip() | |
96 | except: | |
97 | return path | |
98 | ||
f1dca11c DW |
99 | def run_scrub(mnt, cond, running_devs, mntdevs, killfuncs): |
100 | '''Run a scrub process.''' | |
101 | global retcode, terminate | |
102 | ||
103 | print("Scrubbing %s..." % mnt) | |
104 | sys.stdout.flush() | |
105 | ||
106 | try: | |
107 | if terminate: | |
108 | return | |
109 | ||
824b5807 | 110 | # Try it the systemd way |
07c6fd59 | 111 | cmd=['systemctl', 'start', 'xfs_scrub@%s' % systemd_escape(mnt)] |
824b5807 DW |
112 | ret = run_killable(cmd, DEVNULL(), killfuncs, \ |
113 | lambda proc: kill_systemd('xfs_scrub@%s' % mnt, proc)) | |
114 | if ret == 0 or ret == 1: | |
115 | print("Scrubbing %s done, (err=%d)" % (mnt, ret)) | |
116 | sys.stdout.flush() | |
117 | retcode |= ret | |
118 | return | |
119 | ||
120 | if terminate: | |
121 | return | |
122 | ||
f1dca11c DW |
123 | # Invoke xfs_scrub manually |
124 | cmd=['@sbindir@/xfs_scrub', '@scrub_args@', mnt] | |
125 | ret = run_killable(cmd, None, killfuncs, \ | |
126 | lambda proc: proc.terminate()) | |
127 | if ret >= 0: | |
128 | print("Scrubbing %s done, (err=%d)" % (mnt, ret)) | |
129 | sys.stdout.flush() | |
130 | retcode |= ret | |
131 | return | |
132 | ||
133 | if terminate: | |
134 | return | |
135 | ||
136 | print("Unable to start scrub tool.") | |
137 | sys.stdout.flush() | |
138 | finally: | |
139 | running_devs -= mntdevs | |
140 | cond.acquire() | |
141 | cond.notify() | |
142 | cond.release() | |
143 | ||
144 | def main(): | |
145 | '''Find mounts, schedule scrub runs.''' | |
146 | def thr(mnt, devs): | |
147 | a = (mnt, cond, running_devs, devs, killfuncs) | |
148 | thr = threading.Thread(target = run_scrub, args = a) | |
149 | thr.start() | |
150 | global retcode, terminate | |
151 | ||
3dd91472 DW |
152 | parser = argparse.ArgumentParser( \ |
153 | description = "Scrub all mounted XFS filesystems.") | |
154 | parser.add_argument("-V", help = "Report version and exit.", \ | |
155 | action = "store_true") | |
156 | args = parser.parse_args() | |
157 | ||
158 | if args.V: | |
159 | print("xfs_scrub_all version @pkg_version@") | |
160 | sys.exit(0) | |
161 | ||
f1dca11c DW |
162 | fs = find_mounts() |
163 | ||
824b5807 DW |
164 | # Tail the journal if we ourselves aren't a service... |
165 | journalthread = None | |
166 | if 'SERVICE_MODE' not in os.environ: | |
167 | try: | |
168 | cmd=['journalctl', '--no-pager', '-q', '-S', 'now', \ | |
169 | '-f', '-u', 'xfs_scrub@*', '-o', \ | |
170 | 'cat'] | |
171 | journalthread = subprocess.Popen(cmd) | |
172 | except: | |
173 | pass | |
174 | ||
f1dca11c DW |
175 | # Schedule scrub jobs... |
176 | running_devs = set() | |
177 | killfuncs = set() | |
178 | cond = threading.Condition() | |
179 | while len(fs) > 0: | |
180 | if len(running_devs) == 0: | |
181 | mnt, devs = fs.popitem() | |
182 | running_devs.update(devs) | |
183 | thr(mnt, devs) | |
184 | poppers = set() | |
185 | for mnt in fs: | |
186 | devs = fs[mnt] | |
187 | can_run = True | |
188 | for dev in devs: | |
189 | if dev in running_devs: | |
190 | can_run = False | |
191 | break | |
192 | if can_run: | |
193 | running_devs.update(devs) | |
194 | poppers.add(mnt) | |
195 | thr(mnt, devs) | |
196 | for p in poppers: | |
197 | fs.pop(p) | |
198 | cond.acquire() | |
199 | try: | |
200 | cond.wait() | |
201 | except KeyboardInterrupt: | |
202 | terminate = True | |
203 | print("Terminating...") | |
204 | sys.stdout.flush() | |
205 | while len(killfuncs) > 0: | |
206 | fn = killfuncs.pop() | |
207 | fn() | |
208 | fs = [] | |
209 | cond.release() | |
210 | ||
824b5807 DW |
211 | if journalthread is not None: |
212 | journalthread.terminate() | |
213 | ||
214 | # See the service mode comments in xfs_scrub.c for why we do this. | |
215 | if 'SERVICE_MODE' in os.environ: | |
216 | time.sleep(2) | |
217 | if retcode != 0: | |
218 | retcode = 1 | |
219 | ||
f1dca11c DW |
220 | sys.exit(retcode) |
221 | ||
222 | if __name__ == '__main__': | |
223 | main() |