Debian Codegraph @

bin/btrfs-orphan-cleaner-progress

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2017 Hans van Kranenburg <[email protected]>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 import argparse
25 import btrfs
26 import errno
27 import sys
28 import time
29
30
31 class Bork(Exception):
32 pass
33
34
35 def parse_args():
36 parser = argparse.ArgumentParser()
37 parser.add_argument(
38 'mountpoint',
39 help="Btrfs filesystem mountpoint",
40 )
41 return parser.parse_args()
42
43
44 def report_cleaner_progress(path):
45 report_interval = 120
46 zero_key = btrfs.ctree.Key(0, 0, 0)
47 prev_amount_orphans_left = -1
48
49 with btrfs.FileSystem(path) as fs:
50 while True:
51 current_id = None
52 orphans = fs.orphan_subvol_ids()
53 amount_orphans_left = len(orphans)
54 if prev_amount_orphans_left != amount_orphans_left:
55 print("{} orphans left to clean".format(amount_orphans_left))
56 prev_amount_orphans_left = amount_orphans_left
57 for subvol_id in orphans:
58 subvolumes = list(fs.subvolumes(min_id=subvol_id, max_id=subvol_id))
59 if len(subvolumes) == 0:
60 continue
61 subvol = subvolumes[0]
62 if subvol.drop_progress != zero_key:
63 current_id, since = subvol_id, int(time.time())
64 break
65 if current_id is not None:
66 report_after = 0
67 while True:
68 subvolumes = list(fs.subvolumes(min_id=current_id, max_id=current_id))
69 duration = int(time.time()) - since
70 if len(subvolumes) == 0:
71 if report_after > 0:
72 print("dropping root {} finished after at least {} sec".format(
73 current_id, duration))
74 break
75 if duration >= report_after:
76 subvol = subvolumes[0]
77 print("dropping root {} for at least {} sec drop_progress {}".format(
78 current_id, duration, subvol.drop_progress))
79 report_after += report_interval
80 time.sleep(1)
81 else:
82 time.sleep(1)
83
84
85 def main():
86 args = parse_args()
87 path = args.mountpoint
88 try:
89 report_cleaner_progress(path)
90 except OSError as e:
91 if e.errno == errno.EPERM:
92 raise Bork("Insufficient permissions to use the btrfs kernel API.\n"
93 "Hint: Try running the script as root user.".format(e))
94 elif e.errno == errno.ENOTTY:
95 raise Bork("Unable to retrieve data. Hint: Not a mounted btrfs file system?")
96 raise
97
98
99 if __name__ == '__main__':
100 try:
101 main()
102 except KeyboardInterrupt:
103 print("Exiting...")
104 sys.exit(130) # 128 + SIGINT
105 except Bork as e:
106 print("Error: {0}".format(e), file=sys.stderr)
107 sys.exit(1)
108 except Exception as e:
109 print(e)
110 sys.exit(1)