Skip to content

Commit

Permalink
WIP - repair tmp mount point when polling explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Aug 28, 2024
1 parent 22852a6 commit 8b2e738
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions pkg/storaged/btrfs/btrfs-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ def list_filesystems():
if b['fstype'] == "btrfs":
uuid = b['uuid']
mps = list(filter(lambda x: x is not None and not x.startswith(TMP_MP_DIR), b['mountpoints']))
has_tmp_mp = len(list(filter(lambda x: x is not None and x.startswith(TMP_MP_DIR), b['mountpoints']))) > 0
if uuid not in filesystems:
filesystems[uuid] = { 'uuid': uuid, 'devices': [ b['name'] ], 'mountpoints': mps }
filesystems[uuid] = { 'uuid': uuid, 'devices': [ b['name'] ], 'mountpoints': mps, 'has_tmp_mountpoint': has_tmp_mp }
else:
filesystems[uuid]['devices'] += [ b['name'] ]
filesystems[uuid]['mountpoints'] += mps
filesystems[uuid]['has_tmp_mountpoint'] = filesystems[uuid]['has_tmp_mountpoint'] or has_tmp_mp
return filesystems

tmp_mountpoints = set()

def add_tmp_mountpoint(uuid, dev):
def add_tmp_mountpoint(fs, dev, opt_repair):
global tmp_mountpoints
uuid = fs['uuid']
if uuid not in tmp_mountpoints:
# sys.stderr.write(f"ADDING {uuid}\n")
tmp_mountpoints.add(uuid)
Expand All @@ -72,6 +75,7 @@ def add_tmp_mountpoint(uuid, dev):
db[uuid] += 1
else:
db[uuid] = 1
if not fs['has_tmp_mountpoint'] and (db[uuid] == 1 or opt_repair):
dir = TMP_MP_DIR + "/" + uuid
# sys.stderr.write(f"MOUNTING {dir}\n")
os.makedirs(dir, exist_ok=True)
Expand Down Expand Up @@ -101,16 +105,16 @@ def remove_all_tmp_mountpoints():
for mp in set(tmp_mountpoints):
remove_tmp_mountpoint(mp)

def force_mount_point(fs):
add_tmp_mountpoint(fs['uuid'], fs['devices'][0])
def force_mount_point(fs, opt_repair):
add_tmp_mountpoint(fs, fs['devices'][0], opt_repair)
return TMP_MP_DIR + "/" + fs['uuid']

def get_mount_point(fs, opt_mount):
def get_mount_point(fs, opt_mount, opt_repair):
if len(fs['mountpoints']) > 0:
remove_tmp_mountpoint(fs['uuid'])
return fs['mountpoints'][0]
elif opt_mount:
return force_mount_point(fs)
return force_mount_point(fs, opt_repair)
else:
return None

Expand Down Expand Up @@ -148,12 +152,12 @@ def get_usages(uuid):
usages[match[2].decode()] = int(match[1]);
return usages;

def poll(opt_mount):
def poll(opt_mount, opt_repair):
# sys.stderr.write("POLL\n")
filesystems = list_filesystems()
info = { }
for fs in filesystems.values():
mp = get_mount_point(fs, opt_mount)
mp = get_mount_point(fs, opt_mount, opt_repair)
if mp:
try:
info[fs['uuid']] = {
Expand All @@ -167,27 +171,27 @@ def poll(opt_mount):
return info

def cmd_monitor(opt_mount):
old_infos = poll(opt_mount)
old_infos = poll(opt_mount, False)
sys.stdout.write(json.dumps(old_infos) + "\n")
sys.stdout.flush()
while True:
time.sleep(5.0)
new_infos = poll(opt_mount)
new_infos = poll(opt_mount, False)
if new_infos != old_infos:
sys.stdout.write(json.dumps(new_infos) + "\n")
sys.stdout.flush()
old_infos = new_infos

def cmd_poll(opt_mount):
infos = poll(opt_mount)
infos = poll(opt_mount, True)
sys.stdout.write(json.dumps(infos) + "\n")
sys.stdout.flush()

def cmd_do(uuid, cmd):
filesystems = list_filesystems()
for fs in filesystems.values():
if fs['uuid'] == uuid:
mp = force_mount_point(fs)
mp = force_mount_point(fs, True)
with contextlib.chdir(mp):
subprocess.check_call(cmd)

Expand Down

0 comments on commit 8b2e738

Please sign in to comment.