remoteloop: make LoopClient.device a context manager
This commit is contained in:
parent
0dd60b3abf
commit
cb2f383601
2 changed files with 10 additions and 12 deletions
|
|
@ -9,6 +9,7 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
import osbuild.remoteloop as remoteloop
|
import osbuild.remoteloop as remoteloop
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def mount(source, dest, *options):
|
def mount(source, dest, *options):
|
||||||
os.makedirs(dest, 0o755, True)
|
os.makedirs(dest, 0o755, True)
|
||||||
|
|
@ -18,14 +19,6 @@ def mount(source, dest, *options):
|
||||||
finally:
|
finally:
|
||||||
subprocess.run(["umount", "-R", dest], check=True)
|
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):
|
def main(tree, output_dir, options, loop_client):
|
||||||
fmt = options["format"]
|
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)
|
f"{int(partition_size / 1024)}k"], input="y", encoding='utf-8', check=True)
|
||||||
|
|
||||||
# Copy the tree into the target image
|
# 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):
|
mount(loop, mountpoint):
|
||||||
subprocess.run(["cp", "-a", f"{tree}/.", mountpoint], check=True)
|
subprocess.run(["cp", "-a", f"{tree}/.", mountpoint], check=True)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import array
|
import array
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import contextlib
|
||||||
import errno
|
import errno
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
@ -112,7 +113,8 @@ class LoopClient:
|
||||||
def __init__(self, sock):
|
def __init__(self, sock):
|
||||||
self.sock = sock
|
self.sock = sock
|
||||||
|
|
||||||
def create_device(self, filename, offset=None, sizelimit=None):
|
@contextlib.contextmanager
|
||||||
|
def device(self, filename, offset=None, sizelimit=None):
|
||||||
req = {}
|
req = {}
|
||||||
fds = array.array("i")
|
fds = array.array("i")
|
||||||
fd = os.open(filename, os.O_RDWR | os.O_DIRECT)
|
fd = os.open(filename, os.O_RDWR | os.O_DIRECT)
|
||||||
|
|
@ -133,5 +135,8 @@ class LoopClient:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
ret = json.loads(self.sock.recv(1024))
|
ret = json.loads(self.sock.recv(1024))
|
||||||
|
path = os.path.join("/dev", ret["devname"])
|
||||||
return ret["devname"]
|
try:
|
||||||
|
yield path
|
||||||
|
finally:
|
||||||
|
os.unlink(path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue