From f2aff5b80ac04cefd333510a1bcff9698dc6d595 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 4 Apr 2024 20:29:15 +0200 Subject: [PATCH] Check in spawn() whether the command we're trying to run is available Currently, if we try to run a command within a sandbox, we fail with an unclear error if the program is not installed. This is because our FileNotFoundError exception handler is never triggered as the program we run via subprocess is almost always "sh" or "bwrap". Let's make sure we also check for the actual program we're going to run in the sandbox and show a clear error if it's not available. --- mkosi/run.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mkosi/run.py b/mkosi/run.py index 5b579586e..e7a66dd19 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -244,6 +244,14 @@ def spawn( # expect it to pick. assert nfd == SD_LISTEN_FDS_START + i + # First, check if the sandbox works at all before executing the command. + if sandbox and (rc := subprocess.run(sandbox + ["true"]).returncode) != 0: + log_process_failure(sandbox, cmdline, rc) + raise subprocess.CalledProcessError(rc, sandbox + cmdline) + + if subprocess.run(sandbox + ["sh", "-c", f"command -v {cmdline[0]}"], stdout=subprocess.DEVNULL).returncode != 0: + die(f"{cmdline[0]} not found.", hint=f"Is {cmdline[0]} installed on the host system?") + if ( foreground and sandbox and -- 2.47.2