| 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 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) |
| 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) |