search for orphan disks

This commit is contained in:
David Schroeder
2026-09-22 22:01:53 -05:00
parent 6d461b16f6
commit 158d3ffe76
3 changed files with 21 additions and 4 deletions
+15 -1
View File
@@ -30,6 +30,8 @@ def enabled(value):
def references(root, nodes, guests):
"""Read pmxcfs on this host, including offline guests and all config sections."""
refs, errors, seen, contents = {}, [], set(), {}
guest_names = {(guest['type'], str(guest['vmid'])): guest.get('name')
for guest in guests}
for node in nodes:
for kind, directory in [('qemu', 'qemu-server'), ('lxc', 'lxc')]:
folder = root / node['node'] / directory
@@ -48,6 +50,18 @@ def references(root, nodes, guests):
continue
contents[str(path)] = content
seen.add((node['node'], kind, path.stem))
name = guest_names.get((kind, path.stem))
name_key = 'name' if kind == 'qemu' else 'hostname'
for line in content.splitlines():
line = line.strip()
if line.startswith('['):
break
key, sep, value = line.partition(':')
if sep and key == name_key and value.strip():
name = value.strip()
break
guest_id = f"{'VM' if kind == 'qemu' else 'LXC'} {path.stem}"
guest_label = f'{name} ({guest_id})' if name else f'{guest_id} (name unavailable)'
section = 'current'
for line in content.splitlines():
line = line.strip()
@@ -61,7 +75,7 @@ def references(root, nodes, guests):
continue
for volume in VOLUME.findall(value):
refs.setdefault(volume, []).append({
'node': node['node'], 'guest': f'{kind} {path.stem}',
'node': node['node'], 'guest': guest_label,
'unused': bool(re.fullmatch(r'unused\d+', key)) and section == 'current',
'section': section,
})