From: Joerg Behrmann Date: Wed, 1 Nov 2023 14:16:14 +0000 (+0100) Subject: qemu: allow checking device availability with actually used flags X-Git-Tag: v19~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0df4bde6dd7ca8573c6bb9534b30ad40304fcc6c;p=thirdparty%2Fmkosi.git qemu: allow checking device availability with actually used flags Also use an actual os.open instead of os.access, because os.access might fail with certain flags in cases where os.open will not. This works around the case where kernel modules after an upgrade cannot be loaded anymore because it is no longer available for the running kernel. --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index cde82ddf0..41ae721a4 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -2579,7 +2579,7 @@ def run_verb(args: MkosiArgs, images: Sequence[MkosiConfig]) -> None: qemu_device_fds = { d: os.open(d.device(), os.O_RDWR|os.O_CLOEXEC|os.O_NONBLOCK) for d in QemuDeviceNode - if d.available(log=True) + if d.available(log=True, flags=os.O_RDWR|os.O_CLOEXEC|os.O_NONBLOCK) } # Get the user UID/GID either on the host or in the user namespace running the build diff --git a/mkosi/qemu.py b/mkosi/qemu.py index 19ac314cd..0d718fd09 100644 --- a/mkosi/qemu.py +++ b/mkosi/qemu.py @@ -46,17 +46,21 @@ class QemuDeviceNode(StrEnum): QemuDeviceNode.vhost_vsock: "a VSock device", }[self] - def available(self, log: bool = False) -> bool: + def available(self, *, log: bool = False, flags: int = (os.R_OK | os.W_OK)) -> bool: if not os.access(self.device(), os.F_OK): if log: logging.warning(f"{self.device()} not found. Not adding {self.description()} to the virtual machine.") return False - if not os.access(self.device(), os.R_OK|os.W_OK): + try: + fd = os.open(self.device(), flags) + os.close(fd) + except OSError: if log: logging.warning( f"Permission denied to access {self.device()}. " - f"Not adding {self.description()} to the virtual machine." + f"Not adding {self.description()} to the virtual machine. " + "(Maybe a kernel module could not be loaded?)" ) return False