install: losetup: add detach_all() (MR 2252)

Add a function that just detaches all loop devices that are backed by a
file inside the pmbootstrap work dir.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2024-06-14 03:54:43 +02:00 committed by Oliver Smith
parent c4b4c41b8d
commit c061ef8123
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB

View file

@ -3,6 +3,7 @@
import json
from pathlib import Path
from typing import List
from pmb.core.context import get_context
from pmb.helpers import logging
import time
@ -85,3 +86,19 @@ def umount(img_path: Path):
return
logging.debug(f"(native) umount {device}")
pmb.chroot.root(["losetup", "-d", device])
def detach_all():
"""
Detach all loop devices used by pmbootstrap
"""
losetup_output = pmb.helpers.run.root(["losetup", "--json", "--list"],
output_return=True)
if not losetup_output:
return
losetup = json.loads(losetup_output)
work = get_context().config.work
for loopdevice in losetup["loopdevices"]:
print(loopdevice["back-file"])
if Path(loopdevice["back-file"]).is_relative_to(work):
pmb.chroot.root(["losetup", "-d", loopdevice["name"]])
return