loop: add setup callback to loop_for_fd

Add a new callback parameter to `LoopControl` that, if specified,
will be invoked after the loop device is opened but before any
other operation is done, like setting the backing file. Can be
used to perform custom setup tasks.
This commit is contained in:
Christian Kellner 2021-12-02 22:20:59 +00:00 committed by Tom Gundersen
parent 568a4ad97a
commit b26d33910a

View file

@ -543,7 +543,11 @@ class LoopControl:
self._check_open()
return fcntl.ioctl(self.fd, self.LOOP_CTL_GET_FREE)
def loop_for_fd(self, fd: int, lock: bool = False, **kwargs):
def loop_for_fd(self,
fd: int,
lock: bool = False,
setup: Optional[Callable[[Loop], None]] = None,
**kwargs):
"""
Get or create an unbound loopback device and bind it to an fd
@ -559,6 +563,10 @@ class LoopControl:
reacting to changes to the device, like processing udev rules.
See https://systemd.io/BLOCK_DEVICE_LOCKING/
A callback can be specified via `setup` that will be invoked
after the loop device is opened but before any other operation
is done, such as setting the backing file.
All given keyword arguments except `lock` are forwarded to the
`Loop.set_status` call.
"""
@ -571,6 +579,14 @@ class LoopControl:
while True:
lo = Loop(self.get_unbound())
# if a setup callback is specified invoke it now
if callable(setup):
try:
setup(lo)
except:
lo.close()
raise
# try to lock the device if requested and use a
# different one if it fails
if lock: