diff --git a/assemblers/org.osbuild.qemu b/assemblers/org.osbuild.qemu index c754bb25..7fbe8451 100755 --- a/assemblers/org.osbuild.qemu +++ b/assemblers/org.osbuild.qemu @@ -9,6 +9,7 @@ import subprocess import sys import osbuild.remoteloop as remoteloop + @contextlib.contextmanager def mount(source, dest, *options): os.makedirs(dest, 0o755, True) @@ -18,14 +19,6 @@ def mount(source, dest, *options): finally: subprocess.run(["umount", "-R", dest], check=True) -@contextlib.contextmanager -def loop_device(loop_client, image, size, offset=0): - devname = loop_client.create_device(image, offset=offset, sizelimit=size) - path = f"/dev/{devname}" - try: - yield path - finally: - os.unlink(path) def main(tree, output_dir, options, loop_client): fmt = options["format"] @@ -101,7 +94,7 @@ def main(tree, output_dir, options, loop_client): f"{int(partition_size / 1024)}k"], input="y", encoding='utf-8', check=True) # Copy the tree into the target image - with loop_device(loop_client, image, partition_size, partition_offset) as loop, \ + with loop_client.device(image, partition_offset, partition_size) as loop, \ mount(loop, mountpoint): subprocess.run(["cp", "-a", f"{tree}/.", mountpoint], check=True) diff --git a/osbuild/remoteloop.py b/osbuild/remoteloop.py index b57396ce..c1a43665 100644 --- a/osbuild/remoteloop.py +++ b/osbuild/remoteloop.py @@ -1,5 +1,6 @@ import array import asyncio +import contextlib import errno import json import os @@ -112,7 +113,8 @@ class LoopClient: def __init__(self, sock): self.sock = sock - def create_device(self, filename, offset=None, sizelimit=None): + @contextlib.contextmanager + def device(self, filename, offset=None, sizelimit=None): req = {} fds = array.array("i") fd = os.open(filename, os.O_RDWR | os.O_DIRECT) @@ -133,5 +135,8 @@ class LoopClient: os.close(fd) ret = json.loads(self.sock.recv(1024)) - - return ret["devname"] + path = os.path.join("/dev", ret["devname"]) + try: + yield path + finally: + os.unlink(path)