File: btrfs-search-metadata

package info (click to toggle)
python-btrfs 15-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: python: 4,772; makefile: 195
file content (332 lines) | stat: -rwxr-xr-x 10,146 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/python3
#
# Copyright (C) 2020 Hans van Kranenburg <hans@knorrie.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import argparse
import btrfs
import errno
import os
import sys


class Bork(Exception):
    pass


def get_chunks(fs, **kwargs):
    return fs.chunks()


def get_block_groups(fs, **kwargs):
    return fs.block_groups()


def get_dev_extents(fs, **kwargs):
    return fs.dev_extents()


def get_inode_info(fs, tree, inum, **kwargs):
    # We have tree as arg instead of tree_str (which would have been better)
    # because it's called like that for the command line options, and the code
    # to pass it is fully dynamic.
    tree_str = tree
    del tree

    try:
        tree = btrfs.utils.parse_tree_name(tree_str)
    except ValueError as ve:
        raise Bork(str(ve))
    key = btrfs.ctree.Key(inum, btrfs.ctree.INODE_ITEM_KEY, 0)
    if len(list(fs.search(tree, key, key))) != 1:
        raise Bork("No inode {} found in tree {}".format(inum, tree))
    min_key = btrfs.ctree.Key(inum, 0, 0)
    max_key = btrfs.ctree.Key(inum, -1, -1)
    return fs.search(tree, min_key, max_key)


def args_inode_info(subparsers, command, help_text):
    parser = subparsers.add_parser(
        command,
        help=help_text,
    )
    parser.add_argument(
        '-t',
        '--tree',
        required=True,
        help="ID or name of the metadata tree to use for the search",
    )
    parser.add_argument(
        '--inum',
        required=True,
        type=int,
        help="Inode number to search for, e.g. 257",
    )
    parser.add_argument(
        'path',
        help="Filesystem path pointing inside a mounted Btrfs filesystem",
    )


def get_file_info(fs, **kwargs):
    inum = os.fstat(fs.fd).st_ino
    tree, _ = btrfs.ioctl.ino_lookup(fs.fd, objectid=inum)
    min_key = btrfs.ctree.Key(inum, 0, 0)
    max_key = btrfs.ctree.Key(inum, -1, -1)
    return fs.search(tree, min_key, max_key)


def get_devices(fs, **kwargs):
    return fs.devices()


def get_orphans(fs, **kwargs):
    tree = btrfs.ctree.ROOT_TREE_OBJECTID
    min_key = btrfs.ctree.Key(btrfs.ctree.ORPHAN_OBJECTID, btrfs.ctree.ORPHAN_ITEM_KEY, 0)
    max_key = btrfs.ctree.Key(btrfs.ctree.ORPHAN_OBJECTID, btrfs.ctree.ORPHAN_ITEM_KEY, -1)
    return fs.search(tree, min_key, max_key)


def dump(fs, tree, min_key, max_key, **kwargs):
    # We have tree, min_key, max_key instead of tree_str, min_key_str and
    # max_key_str as args (which would have been better) because they're called
    # like that for the command line options, and the code to pass them is
    # fully dynamic.
    min_key_str = min_key
    del min_key
    max_key_str = max_key
    del max_key
    tree_str = tree
    del tree

    try:
        tree = btrfs.utils.parse_tree_name(tree_str)
    except ValueError as ve:
        raise Bork(str(ve))

    if min_key_str is None:
        min_key = btrfs.ctree.Key(0, 0, 0)
    else:
        min_key = btrfs.utils.parse_key_string(min_key_str)

    if max_key_str is None:
        max_key = btrfs.ctree.Key(0, 0, 0) - 1
    else:
        max_key = btrfs.utils.parse_key_string(max_key_str)

    try:
        yield from fs.search(tree, min_key, max_key)
    except FileNotFoundError:
        raise Bork("Tree {} does not exist".format(tree))


def args_dump(subparsers, command, help_text):
    parser = subparsers.add_parser(
        command,
        help=help_text,
    )
    parser.add_argument(
        '-t',
        '--tree',
        required=True,
        help="ID or name of the metadata tree to use for the search",
    )
    parser.add_argument(
        '--min-key',
        help="Tree key to start at e.g. '(257 DIR_ITEM 0)'",
    )
    parser.add_argument(
        '--max-key',
        help="Tree key to stop at e.g. '(257 DIR_ITEM -1)'",
    )
    parser.add_argument(
        'path',
        help="Filesystem path pointing inside a mounted Btrfs filesystem",
    )


def get_block_group_contents(fs, vaddr, **kwargs):
    block_group = fs.block_group(vaddr)
    min_key = btrfs.ctree.Key(vaddr, 0, 0)
    max_key = btrfs.ctree.Key(vaddr + block_group.length - 1, -1, -1)
    return fs.search(2, min_key, max_key)


def args_block_group_contents(subparsers, command, help_text):
    parser = subparsers.add_parser(
        command,
        help=help_text,
    )
    parser.add_argument(
        '--vaddr',
        required=True,
        type=int,
        help="Virtual address of the start of the block group",
    )
    parser.add_argument(
        'path',
        help="Filesystem path pointing inside a mounted Btrfs filesystem",
    )


def get_block_group_free_space(fs, vaddr, **kwargs):
    try:
        # Use yield from instead of return to be able to catch the exception.
        if vaddr is None:
            yield from fs.free_space_extents()
        else:
            block_group = fs.block_group(vaddr)
            yield from fs.free_space_extents(min_vaddr=vaddr,
                                             max_vaddr=vaddr + block_group.length - 1)
    except FileNotFoundError:
        raise Bork("No Free Space Tree? To show free space you need space_cache=v2.")


def args_block_group_free_space(subparsers, command, help_text):
    parser = subparsers.add_parser(
        command,
        help=help_text,
    )
    parser.add_argument(
        '--vaddr',
        required=False,
        type=int,
        default=None,
        help="Virtual address of the start of the block group",
    )
    parser.add_argument(
        'path',
        help="Filesystem path pointing inside a mounted Btrfs filesystem",
    )


def args_default(subparsers, command, help_text):
    parser = subparsers.add_parser(
        command,
        help=help_text,
    )
    parser.add_argument(
        'path',
        help="Filesystem path pointing inside a mounted Btrfs filesystem",
    )


presets = {
    'chunks': (get_chunks, args_default, "Display chunks"),
    'block_groups': (get_block_groups, args_default, "Display block groups"),
    'dev_extents': (get_dev_extents, args_default, "Display device extents"),
    'file': (get_file_info, args_default, "Display inode information for a file or directory"),
    'inode': (
        get_inode_info,
        args_inode_info,
        "Display inode information for a specific inode number in a tree"
    ),
    'devices': (get_devices, args_default, "Display devices"),
    'orphans': (get_orphans, args_default, "Orphan items from the Root Tree"),
    'dump': (dump, args_dump, "Dump arbitrary ranges of metadata items"),
    'block_group_contents': (
        get_block_group_contents,
        args_block_group_contents,
        "Show block group contents (items from the extent tree)"
    ),
    'block_group_free_space': (
        get_block_group_free_space,
        args_block_group_free_space,
        "Show free space in block groups (from the Free Space Tree)"
    ),
}


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--format',
        choices=('keys', 'short', 'long'),
        default='short',
        help="Print metadata items as keys only, as a short single line per item, "
             "or long output with full contents of all fields."
    )
    subparsers = parser.add_subparsers(
        dest='preset',
    )
    for preset in presets.keys():
        _, args_fn, help_text = presets[preset]
        args_fn(subparsers, preset, help_text)
    return parser.parse_args()


def permission_check(fs):
    """This is a simple canary function that explodes if the user does not have
    enough permissions to use the search ioctl.
    """
    fs.top_level()


def print_key(objs):
    for obj in objs:
        if not isinstance(obj, btrfs.ctree.ItemData):
            continue
        try:
            print(obj.key)
        except TypeError:
            pass


def main():
    args = parse_args()
    output_fn = {
        'keys': print_key,
        'short': btrfs.utils.str_print,
        'long': btrfs.utils.pretty_print,
    }.get(args.format)
    preset = args.preset
    if preset is None:
        raise Bork("Choose a sub command. See help (-h) for a list of them.")
    path = args.path
    try:
        with btrfs.FileSystem(path) as fs:
            permission_check(fs)
            if preset in presets:
                output_fn(presets[preset][0](fs, **vars(args)))
            else:
                raise Bork("Unknown preset search type.")
    except OSError as e:
        if e.errno == errno.EPERM:
            raise Bork("Insufficient permissions to use the btrfs kernel API.\n"
                       "Hint: Try running the script as root user.".format(e))
        elif e.errno == errno.ENOTTY:
            raise Bork("Unable to retrieve data. Hint: Not a mounted btrfs file system?")
        raise


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(130)  # 128 + SIGINT
    except Bork as e:
        print("Error: {}".format(e), file=sys.stderr)
        sys.exit(1)
    except BrokenPipeError:
        pass
    except Exception:
        print("Uncaught error, please report as bug:", file=sys.stderr)
        raise