Debian Codegraph @

bin/btrfs-usage-report

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2018 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
29
30 class Bork(Exception):
31 pass
32
33
34 def parse_args():
35 parser = argparse.ArgumentParser()
36 parser.add_argument(
37 'mountpoint',
38 help="Btrfs filesystem mountpoint",
39 )
40 return parser.parse_args()
41
42
43 def report_usage(path):
44 with btrfs.FileSystem(path) as fs:
45 mixed_groups = fs.mixed_groups()
46 usage = fs.usage()
47
48 print("Btrfs usage report for {}".format(path))
49 print("Filesystem ID: {}".format(fs.fsid))
50 print("Mixed groups: {}".format(mixed_groups))
51 print()
52
53 print("Total physical space usage:")
54 print("| ")
55 print("| Total filesystem size: {}".format(usage.total_str))
56 print("| Allocated bytes: {}".format(usage.allocated_str))
57 print("| Allocatable bytes remaining: {}".format(usage.allocatable_left_str))
58 print()
59
60 print("Target profiles:")
61 print("|")
62 target_profiles = [
63 ('type', 'profile'),
64 ('----', '-------'),
65 ('System', usage.target_profile_system_str),
66 ]
67 if not mixed_groups:
68 target_profiles.extend([
69 ('Metadata', usage.target_profile_metadata_str),
70 ('Data', usage.target_profile_data_str),
71 ])
72 else:
73 target_profiles.extend([
74 ('Data+Metadata', usage.target_profile_data_metadata_str),
75 ])
76 for target_profile in target_profiles:
77 print("| {: <12} {: <12}".format(*target_profile))
78 print()
79
80 print("Estimated virtual space left for use:")
81 print("|")
82 virtual_estims = [
83 ('type', 'free'),
84 ('----', '----'),
85 ]
86 if not mixed_groups:
87 virtual_estims.extend([
88 ('Data', usage.free_data_str),
89 ('MetaData', usage.free_metadata_str),
90 ])
91 else:
92 virtual_estims.extend([
93 ('Data+Metadata', usage.free_str),
94 ])
95 for virtual_estim in virtual_estims:
96 print("| {: <12} {: <12}".format(*virtual_estim))
97 print()
98
99 print("Virtual space usage by block group type:")
100 print("|")
101 virtual_block_group_type_usage_table_values = [
102 ('type', 'total', 'used'),
103 ('----', '-----', '----'),
104 ]
105 for _, virtual_block_group_type_usage in usage.virtual_block_group_type_usage.items():
106 virtual_block_group_type_usage_table_values.append((
107 btrfs.utils.space_type_description(virtual_block_group_type_usage.type),
108 virtual_block_group_type_usage.total_str,
109 virtual_block_group_type_usage.used_str,
110 ))
111 for virtual_block_group_type_usage_table_value in \
112 virtual_block_group_type_usage_table_values:
113 print("| {: <12} {: >12} {: >12}".format(*virtual_block_group_type_usage_table_value))
114 print()
115
116 print("Allocated raw disk bytes by chunk type.")
117 print("|")
118 raw_space_usage_table_values = [
119 ('flags', 'allocated', 'used', 'parity *)'),
120 ('-----', '---------', '----', '---------'),
121 ]
122 for _, raw_space_usage in usage.raw_space_usage.items():
123 raw_space_usage_table_values.append((
124 btrfs.utils.block_group_flags_str(raw_space_usage.flags),
125 raw_space_usage.allocated_str,
126 raw_space_usage.used_str,
127 raw_space_usage.parity_str,
128 ))
129 for raw_space_usage_table_value in raw_space_usage_table_values:
130 print("| {: <16} {: >12} {: >12} {: >12}".format(*raw_space_usage_table_value))
131 print("|")
132 print("| *) Parity is a reserved part of the allocated bytes, limiting the")
133 print("| amount that can be used for data or metadata.")
134 print()
135
136 print("Allocated bytes per device:")
137 print("|")
138 dev_usage_table_values = [
139 ('devid', 'total size', 'allocated', 'path'),
140 ('-----', '----------', '---------', '----'),
141 ]
142 for devid in sorted(usage.dev_usage.keys()):
143 dev_usage = usage.dev_usage[devid]
144 dev_usage_table_values.append((
145 dev_usage.devid,
146 dev_usage.total_str,
147 dev_usage.allocated_str,
148 btrfs.ioctl.dev_info(fs.fd, devid).path,
149 ))
150 for dev_usage_table_value in dev_usage_table_values:
151 print("| {: <8} {: >12} {: >12} {}".format(*dev_usage_table_value))
152 print()
153
154 print("Allocated bytes per device, split up by chunk type.")
155 for devid in sorted(usage.dev_usage.keys()):
156 print("|")
157 print("| Device ID: {}".format(devid))
158 dev_usage = usage.dev_usage[devid]
159 dev_space_usage_table_values = [
160 ('flags', 'allocated', 'parity *)'),
161 ('-----', '---------', '---------'),
162 ]
163 for _, dev_space_usage in dev_usage.dev_space_usage.items():
164 dev_space_usage_table_values.append((
165 btrfs.utils.block_group_flags_str(dev_space_usage.flags),
166 dev_space_usage.allocated_str,
167 dev_space_usage.parity_str,
168 ))
169 for dev_space_usage_table_value in dev_space_usage_table_values:
170 print("| | {: <16} {: >12} {: >12}".format(*dev_space_usage_table_value))
171 print("|")
172 print("| *) Parity is a reserved part of the allocated bytes, limiting the")
173 print("| amount that can be used for data or metadata.")
174 print()
175
176 print("Unallocatable raw disk space:")
177 print("|")
178 print("| Reclaimable (by using balance): {}".format(usage.unallocatable_reclaimable_str))
179 print("| Not reclaimable (because of different disk sizes): {}".format(
180 usage.unallocatable_hard_str))
181 print()
182
183 print("Unallocatable bytes per device, given current target profiles:")
184 print("|")
185 wasted_sizes_table_values = [
186 ('devid', 'soft *)', 'hard **)', 'reclaimable ***)'),
187 ('-----', '-------', '--------', '----------------'),
188 ]
189 for devid in sorted(usage.dev_usage.keys()):
190 dev_usage = usage.dev_usage[devid]
191 wasted_sizes_table_values.append((
192 devid,
193 dev_usage.unallocatable_soft_str,
194 dev_usage.unallocatable_hard_str,
195 dev_usage.unallocatable_reclaimable_str,
196 ))
197 for wasted_sizes_table_value in wasted_sizes_table_values:
198 print("| {: <8} {: >12} {: >12} {: >16}".format(*wasted_sizes_table_value))
199 print("|")
200 print("| *) Because allocations in the filesystem are unbalanced.")
201 print("| **) Because of having different sizes of devices attached.")
202 print("| ***) Amount of 'soft' unallocatable space that can be reclaimed,")
203 print("| before hitting the 'hard' limit.")
204
205
206 def main():
207 args = parse_args()
208 path = args.mountpoint
209 try:
210 report_usage(path)
211 except OSError as e:
212 if e.errno == errno.EPERM:
213 raise Bork("Insufficient permissions to use the btrfs kernel API.\n"
214 "Hint: Try running the script as root user.".format(e))
215 elif e.errno == errno.ENOTTY:
216 raise Bork("Unable to retrieve data. Hint: Not a mounted btrfs file system?")
217 raise
218
219
220 if __name__ == '__main__':
221 try:
222 main()
223 except KeyboardInterrupt:
224 print("Exiting...")
225 sys.exit(130) # 128 + SIGINT
226 except Bork as e:
227 print("Error: {0}".format(e), file=sys.stderr)
228 sys.exit(1)
229 except Exception as e:
230 print(e)
231 sys.exit(1)