Debian Codegraph @

bin/btrfs-space-calculator

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 sys
27
28
29 class WastedSpaceError(Exception):
30 pass
31
32
33 class FakeDevItem(object):
34 def __init__(self, devid, total_bytes):
35 self.devid = devid
36 self.total_bytes = total_bytes
37 self.bytes_used = 0
38
39
40 class FakeFileSystem(object):
41 def __init__(self, device_sizes, mixed_groups, metadata_profile, data_profile):
42 self._mixed_groups = mixed_groups
43 self._devices = [FakeDevItem(devid+1, size)
44 for devid, size in enumerate(device_sizes)]
45
46 def mixed_groups(self):
47 return self._mixed_groups
48
49 def space_info(self):
50 return []
51
52 def devices(self):
53 return self._devices
54
55 def chunks(self):
56 return []
57
58
59 def parse_args():
60 parser = argparse.ArgumentParser(description="Calculate usable and unallocatable disk space")
61 parser.add_argument(
62 '-m', '--metadata',
63 required=True,
64 action='store',
65 help="metadata profile, values like for data profile",
66 )
67 parser.add_argument(
68 '-d', '--data',
69 required=True,
70 action='store',
71 help="data profile, raid0, raid1, raid5, raid6, raid10, dup or single",
72 )
73 parser.add_argument(
74 '-M', '--mixed',
75 action='store_true',
76 help="use mixed block groups (data and metadata profile must match)",
77 )
78 parser.add_argument(
79 '-r', '--ratio',
80 action='store',
81 type=int,
82 help="data to metadata ratio for non-mixed filesystem, e.g. 200 (the default), "
83 "which means allocate 0.5%% for metadata",
84 )
85 parser.add_argument(
86 'size',
87 nargs='+',
88 help="disk sizes, e.g. 2TB 500G 1TiB",
89 )
90 return parser.parse_args()
91
92
93 _str_to_profile_map = {
94 'raid0': btrfs.BLOCK_GROUP_RAID0,
95 'raid1': btrfs.BLOCK_GROUP_RAID1,
96 'raid1c3': btrfs.BLOCK_GROUP_RAID1C3,
97 'raid1c4': btrfs.BLOCK_GROUP_RAID1C4,
98 'raid5': btrfs.BLOCK_GROUP_RAID5,
99 'raid6': btrfs.BLOCK_GROUP_RAID6,
100 'raid10': btrfs.BLOCK_GROUP_RAID10,
101 'single': 0,
102 'dup': btrfs.BLOCK_GROUP_DUP,
103 }
104
105
106 def main():
107 args = parse_args()
108
109 device_sizes = []
110 for size in args.size:
111 try:
112 device_sizes.append(btrfs.utils.parse_pretty_size(size))
113 except ValueError as e:
114 raise WastedSpaceError("Invalid device size {}".format(size))
115 mixed_groups = args.mixed
116 try:
117 metadata_profile = _str_to_profile_map[args.metadata]
118 except KeyError as e:
119 raise WastedSpaceError("Unknown profile {}".format(args.metadata))
120 try:
121 data_profile = _str_to_profile_map[args.data]
122 except KeyError as e:
123 raise WastedSpaceError("Unknown profile {}".format(args.data))
124 if mixed_groups and metadata_profile != data_profile:
125 raise WastedSpaceError(
126 "When using mixed groups, metadata and data profile need to be identical.")
127 fs = FakeFileSystem(device_sizes, mixed_groups, metadata_profile, data_profile)
128 if not mixed_groups:
129 usage = btrfs.fs_usage.FsUsage(
130 fs,
131 data_metadata_ratio=args.ratio,
132 target_profile_metadata=metadata_profile,
133 target_profile_data=data_profile,
134 )
135 else:
136 usage = btrfs.fs_usage.FsUsage(
137 fs,
138 target_profile_mixed=metadata_profile,
139 )
140 print("Target metadata profile: {}".format(
141 btrfs.utils.space_profile_description(metadata_profile)))
142 print("Target data profile: {}".format(
143 btrfs.utils.space_profile_description(data_profile)))
144 print("Mixed block groups: {}".format(mixed_groups))
145 print("Total raw filesystem size: {}".format(btrfs.utils.pretty_size(usage.total)))
146 print("Device sizes:")
147 for devid, size in enumerate(device_sizes):
148 print(" Device {}: {}".format(devid+1, btrfs.utils.pretty_size(size)))
149 if not mixed_groups:
150 print("Metadata to data ratio: 1:{}".format(usage.default_data_metadata_ratio))
151 print("Estimated virtual space to use for metadata: {}".format(
152 btrfs.utils.pretty_size(usage.estimated_full_allocatable_virtual_metadata)))
153 print("Estimated virtual space to use for data: {}".format(
154 btrfs.utils.pretty_size(usage.estimated_full_allocatable_virtual_data)))
155 else:
156 if args.ratio is not None:
157 print("Notice: Metadata to data ratio is not used in mixed mode.")
158 print("Estimated virtual space to use for metadata and data: {}".format(
159 btrfs.utils.pretty_size(usage.estimated_full_allocatable_virtual_mixed)))
160 print("Total unallocatable raw amount: {}".format(
161 btrfs.utils.pretty_size(usage.unallocatable_hard)))
162 print("Unallocatable raw bytes per device:")
163 for key in sorted(usage.dev_usage.keys()):
164 print(" Device {}: {}".format(
165 key, btrfs.utils.pretty_size(usage.dev_usage[key].unallocatable_hard)))
166
167
168 if __name__ == '__main__':
169 try:
170 main()
171 except WastedSpaceError as e:
172 print("Error: {0}".format(e), file=sys.stderr)
173 sys.exit(1)