| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import argparse |
| 25 |
import btrfs |
| 26 |
import errno |
| 27 |
import os |
| 28 |
import sys |
| 29 |
|
| 30 |
|
| 31 |
class Bork(Exception): |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
def get_chunks(fs, **kwargs): |
| 36 |
return fs.chunks() |
| 37 |
|
| 38 |
|
| 39 |
def get_block_groups(fs, **kwargs): |
| 40 |
return fs.block_groups() |
| 41 |
|
| 42 |
|
| 43 |
def get_dev_extents(fs, **kwargs): |
| 44 |
return fs.dev_extents() |
| 45 |
|
| 46 |
|
| 47 |
def get_inode_info(fs, tree, inum, **kwargs): |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
tree_str = tree |
| 52 |
del tree |
| 53 |
|
| 54 |
try: |
| 55 |
tree = btrfs.utils.parse_tree_name(tree_str) |
| 56 |
except ValueError as ve: |
| 57 |
raise Bork(str(ve)) |
| 58 |
key = btrfs.ctree.Key(inum, btrfs.ctree.INODE_ITEM_KEY, 0) |
| 59 |
if len(list(fs.search(tree, key, key))) != 1: |
| 60 |
raise Bork("No inode {} found in tree {}".format(inum, tree)) |
| 61 |
min_key = btrfs.ctree.Key(inum, 0, 0) |
| 62 |
max_key = btrfs.ctree.Key(inum, -1, -1) |
| 63 |
return fs.search(tree, min_key, max_key) |
| 64 |
|
| 65 |
|
| 66 |
def args_inode_info(subparsers, command, help_text): |
| 67 |
parser = subparsers.add_parser( |
| 68 |
command, |
| 69 |
help=help_text, |
| 70 |
) |
| 71 |
parser.add_argument( |
| 72 |
'-t', |
| 73 |
'--tree', |
| 74 |
required=True, |
| 75 |
help="ID or name of the metadata tree to use for the search", |
| 76 |
) |
| 77 |
parser.add_argument( |
| 78 |
'--inum', |
| 79 |
required=True, |
| 80 |
type=int, |
| 81 |
help="Inode number to search for, e.g. 257", |
| 82 |
) |
| 83 |
parser.add_argument( |
| 84 |
'path', |
| 85 |
help="Filesystem path pointing inside a mounted Btrfs filesystem", |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
def get_file_info(fs, **kwargs): |
| 90 |
inum = os.fstat(fs.fd).st_ino |
| 91 |
tree, _ = btrfs.ioctl.ino_lookup(fs.fd, objectid=inum) |
| 92 |
min_key = btrfs.ctree.Key(inum, 0, 0) |
| 93 |
max_key = btrfs.ctree.Key(inum, -1, -1) |
| 94 |
return fs.search(tree, min_key, max_key) |
| 95 |
|
| 96 |
|
| 97 |
def get_devices(fs, **kwargs): |
| 98 |
return fs.devices() |
| 99 |
|
| 100 |
|
| 101 |
def get_orphans(fs, **kwargs): |
| 102 |
tree = btrfs.ctree.ROOT_TREE_OBJECTID |
| 103 |
min_key = btrfs.ctree.Key(btrfs.ctree.ORPHAN_OBJECTID, btrfs.ctree.ORPHAN_ITEM_KEY, 0) |
| 104 |
max_key = btrfs.ctree.Key(btrfs.ctree.ORPHAN_OBJECTID, btrfs.ctree.ORPHAN_ITEM_KEY, -1) |
| 105 |
return fs.search(tree, min_key, max_key) |
| 106 |
|
| 107 |
|
| 108 |
def dump(fs, tree, min_key, max_key, **kwargs): |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
min_key_str = min_key |
| 114 |
del min_key |
| 115 |
max_key_str = max_key |
| 116 |
del max_key |
| 117 |
tree_str = tree |
| 118 |
del tree |
| 119 |
|
| 120 |
try: |
| 121 |
tree = btrfs.utils.parse_tree_name(tree_str) |
| 122 |
except ValueError as ve: |
| 123 |
raise Bork(str(ve)) |
| 124 |
|
| 125 |
if min_key_str is None: |
| 126 |
min_key = btrfs.ctree.Key(0, 0, 0) |
| 127 |
else: |
| 128 |
min_key = btrfs.utils.parse_key_string(min_key_str) |
| 129 |
|
| 130 |
if max_key_str is None: |
| 131 |
max_key = btrfs.ctree.Key(0, 0, 0) - 1 |
| 132 |
else: |
| 133 |
max_key = btrfs.utils.parse_key_string(max_key_str) |
| 134 |
|
| 135 |
try: |
| 136 |
yield from fs.search(tree, min_key, max_key) |
| 137 |
except FileNotFoundError: |
| 138 |
raise Bork("Tree {} does not exist".format(tree)) |
| 139 |
|
| 140 |
|
| 141 |
def args_dump(subparsers, command, help_text): |
| 142 |
parser = subparsers.add_parser( |
| 143 |
command, |
| 144 |
help=help_text, |
| 145 |
) |
| 146 |
parser.add_argument( |
| 147 |
'-t', |
| 148 |
'--tree', |
| 149 |
required=True, |
| 150 |
help="ID or name of the metadata tree to use for the search", |
| 151 |
) |
| 152 |
parser.add_argument( |
| 153 |
'--min-key', |
| 154 |
help="Tree key to start at e.g. '(257 DIR_ITEM 0)'", |
| 155 |
) |
| 156 |
parser.add_argument( |
| 157 |
'--max-key', |
| 158 |
help="Tree key to stop at e.g. '(257 DIR_ITEM -1)'", |
| 159 |
) |
| 160 |
parser.add_argument( |
| 161 |
'path', |
| 162 |
help="Filesystem path pointing inside a mounted Btrfs filesystem", |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def get_block_group_contents(fs, vaddr, **kwargs): |
| 167 |
block_group = fs.block_group(vaddr) |
| 168 |
min_key = btrfs.ctree.Key(vaddr, 0, 0) |
| 169 |
max_key = btrfs.ctree.Key(vaddr + block_group.length - 1, -1, -1) |
| 170 |
return fs.search(2, min_key, max_key) |
| 171 |
|
| 172 |
|
| 173 |
def args_block_group_contents(subparsers, command, help_text): |
| 174 |
parser = subparsers.add_parser( |
| 175 |
command, |
| 176 |
help=help_text, |
| 177 |
) |
| 178 |
parser.add_argument( |
| 179 |
'--vaddr', |
| 180 |
required=True, |
| 181 |
type=int, |
| 182 |
help="Virtual address of the start of the block group", |
| 183 |
) |
| 184 |
parser.add_argument( |
| 185 |
'path', |
| 186 |
help="Filesystem path pointing inside a mounted Btrfs filesystem", |
| 187 |
) |
| 188 |
|
| 189 |
|
| 190 |
def get_block_group_free_space(fs, vaddr, **kwargs): |
| 191 |
try: |
| 192 |
|
| 193 |
if vaddr is None: |
| 194 |
yield from fs.free_space_extents() |
| 195 |
else: |
| 196 |
block_group = fs.block_group(vaddr) |
| 197 |
yield from fs.free_space_extents(min_vaddr=vaddr, |
| 198 |
max_vaddr=vaddr + block_group.length - 1) |
| 199 |
except FileNotFoundError: |
| 200 |
raise Bork("No Free Space Tree? To show free space you need space_cache=v2.") |
| 201 |
|
| 202 |
|
| 203 |
def args_block_group_free_space(subparsers, command, help_text): |
| 204 |
parser = subparsers.add_parser( |
| 205 |
command, |
| 206 |
help=help_text, |
| 207 |
) |
| 208 |
parser.add_argument( |
| 209 |
'--vaddr', |
| 210 |
required=False, |
| 211 |
type=int, |
| 212 |
default=None, |
| 213 |
help="Virtual address of the start of the block group", |
| 214 |
) |
| 215 |
parser.add_argument( |
| 216 |
'path', |
| 217 |
help="Filesystem path pointing inside a mounted Btrfs filesystem", |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def args_default(subparsers, command, help_text): |
| 222 |
parser = subparsers.add_parser( |
| 223 |
command, |
| 224 |
help=help_text, |
| 225 |
) |
| 226 |
parser.add_argument( |
| 227 |
'path', |
| 228 |
help="Filesystem path pointing inside a mounted Btrfs filesystem", |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
presets = { |
| 233 |
'chunks': (get_chunks, args_default, "Display chunks"), |
| 234 |
'block_groups': (get_block_groups, args_default, "Display block groups"), |
| 235 |
'dev_extents': (get_dev_extents, args_default, "Display device extents"), |
| 236 |
'file': (get_file_info, args_default, "Display inode information for a file or directory"), |
| 237 |
'inode': ( |
| 238 |
get_inode_info, |
| 239 |
args_inode_info, |
| 240 |
"Display inode information for a specific inode number in a tree" |
| 241 |
), |
| 242 |
'devices': (get_devices, args_default, "Display devices"), |
| 243 |
'orphans': (get_orphans, args_default, "Orphan items from the Root Tree"), |
| 244 |
'dump': (dump, args_dump, "Dump arbitrary ranges of metadata items"), |
| 245 |
'block_group_contents': ( |
| 246 |
get_block_group_contents, |
| 247 |
args_block_group_contents, |
| 248 |
"Show block group contents (items from the extent tree)" |
| 249 |
), |
| 250 |
'block_group_free_space': ( |
| 251 |
get_block_group_free_space, |
| 252 |
args_block_group_free_space, |
| 253 |
"Show free space in block groups (from the Free Space Tree)" |
| 254 |
), |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
def parse_args(): |
| 259 |
parser = argparse.ArgumentParser() |
| 260 |
parser.add_argument( |
| 261 |
'--format', |
| 262 |
choices=('keys', 'short', 'long'), |
| 263 |
default='short', |
| 264 |
help="Print metadata items as keys only, as a short single line per item, " |
| 265 |
"or long output with full contents of all fields." |
| 266 |
) |
| 267 |
subparsers = parser.add_subparsers( |
| 268 |
dest='preset', |
| 269 |
) |
| 270 |
for preset in presets.keys(): |
| 271 |
_, args_fn, help_text = presets[preset] |
| 272 |
args_fn(subparsers, preset, help_text) |
| 273 |
return parser.parse_args() |
| 274 |
|
| 275 |
|
| 276 |
def permission_check(fs): |
| 277 |
"""This is a simple canary function that explodes if the user does not have |
| 278 |
enough permissions to use the search ioctl. |
| 279 |
""" |
| 280 |
fs.top_level() |
| 281 |
|
| 282 |
|
| 283 |
def print_key(objs): |
| 284 |
for obj in objs: |
| 285 |
if not isinstance(obj, btrfs.ctree.ItemData): |
| 286 |
continue |
| 287 |
try: |
| 288 |
print(obj.key) |
| 289 |
except TypeError: |
| 290 |
pass |
| 291 |
|
| 292 |
|
| 293 |
def main(): |
| 294 |
args = parse_args() |
| 295 |
output_fn = { |
| 296 |
'keys': print_key, |
| 297 |
'short': btrfs.utils.str_print, |
| 298 |
'long': btrfs.utils.pretty_print, |
| 299 |
}.get(args.format) |
| 300 |
preset = args.preset |
| 301 |
if preset is None: |
| 302 |
raise Bork("Choose a sub command. See help (-h) for a list of them.") |
| 303 |
path = args.path |
| 304 |
try: |
| 305 |
with btrfs.FileSystem(path) as fs: |
| 306 |
permission_check(fs) |
| 307 |
if preset in presets: |
| 308 |
output_fn(presets[preset][0](fs, **vars(args))) |
| 309 |
else: |
| 310 |
raise Bork("Unknown preset search type.") |
| 311 |
except OSError as e: |
| 312 |
if e.errno == errno.EPERM: |
| 313 |
raise Bork("Insufficient permissions to use the btrfs kernel API.\n" |
| 314 |
"Hint: Try running the script as root user.".format(e)) |
| 315 |
elif e.errno == errno.ENOTTY: |
| 316 |
raise Bork("Unable to retrieve data. Hint: Not a mounted btrfs file system?") |
| 317 |
raise |
| 318 |
|
| 319 |
|
| 320 |
if __name__ == '__main__': |
| 321 |
try: |
| 322 |
main() |
| 323 |
except KeyboardInterrupt: |
| 324 |
sys.exit(130) |
| 325 |
except Bork as e: |
| 326 |
print("Error: {}".format(e), file=sys.stderr) |
| 327 |
sys.exit(1) |
| 328 |
except BrokenPipeError: |
| 329 |
pass |
| 330 |
except Exception: |
| 331 |
print("Uncaught error, please report as bug:", file=sys.stderr) |
| 332 |
raise |