diff --git a/openrelik_worker_common/mount_utils.py b/openrelik_worker_common/mount_utils.py index b86dede..20ac659 100644 --- a/openrelik_worker_common/mount_utils.py +++ b/openrelik_worker_common/mount_utils.py @@ -102,6 +102,9 @@ def setup(self): # Check if required tools are available self._required_tools_available() + # Check the required kernel modules are available + self._required_modules_loaded() + # Setup the block device ext = image_path.suffix.strip(".") if ext.lower() in self.supported_qcowtypes: @@ -256,6 +259,27 @@ def _nbdsetup(self): return self.blkdevice + def _required_modules_loaded(self) -> None: + """Checks if a required kernel module is loaded. + + The following modules are checked: + * nbd (For mounting qcow disk images) + + Raises: + RuntimeError: as soon as we find a module that isn't loaded. + """ + + for module in ["nbd"]: + try: + subprocess.check_call( + ["/usr/bin/grep", "-E", f"^{module}\\s", "/proc/modules"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + except subprocess.CalledProcessError: + raise RuntimeError( + f"Required kernel module {module} is not loaded. " + f"Load it with '/sbin/modprobe {module}' on the Host.") + def _required_tools_available(self) -> bool: """Check if required cli tools are available.