]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Check for TERM=unknown and set TERM=dumb if not on tty
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 12 Mar 2024 20:05:50 +0000 (20:05 +0000)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 13 Mar 2024 10:11:29 +0000 (11:11 +0100)
in CI, TERM is set to "unknown" so let's check for that and translate
it to "dumb" if we're not on a tty which systemd checks for when it
decides whether to enable logging or not. Also set TERM itself on
the kernel command line which is another thing parsed by systemd to check
whether to log colors or not. Finally, make sure we set "TERM" correctly
in our own environment that is passed to scripts

mkosi/config.py
mkosi/run.py
tests/__init__.py

index 4c3743ec1bc32b2479d96d10bae6895cacb2a9a3..ad01d44a5bcd7406503b20ec415f740d125eb3d7 100644 (file)
@@ -21,6 +21,7 @@ import shlex
 import shutil
 import string
 import subprocess
+import sys
 import tempfile
 import textwrap
 import uuid
@@ -3381,15 +3382,24 @@ def load_credentials(args: argparse.Namespace) -> dict[str, str]:
     return creds
 
 
+def finalize_term() -> str:
+    if not (term := os.getenv("TERM")) or term == "unknown":
+        term = "vt220" if sys.stderr.isatty() else "dumb"
+
+    return term
+
+
 def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]:
     tty = args.architecture.default_serial_tty()
     columns, lines = shutil.get_terminal_size()
+    term = finalize_term()
+
     cmdline = [
         # Make sure we set up networking in the VM/container.
         "systemd.wants=network.target",
         # Make sure we don't load vmw_vmci which messes with virtio vsock.
         "module_blacklist=vmw_vmci",
-        f"systemd.tty.term.{tty}={os.getenv('TERM', 'vt220')}",
+        f"systemd.tty.term.{tty}={term}",
         f"systemd.tty.columns.{tty}={columns}",
         f"systemd.tty.rows.{tty}={lines}",
     ]
@@ -3408,12 +3418,12 @@ def load_kernel_command_line_extra(args: argparse.Namespace) -> list[str]:
         cmdline += ["systemd.volatile=yes"]
 
     if not args.qemu_gui:
-        columns, lines = shutil.get_terminal_size()
         cmdline += [
-            f"systemd.tty.term.console={os.getenv('TERM', 'vt220')}",
+            f"systemd.tty.term.console={term}",
             f"systemd.tty.columns.console={columns}",
             f"systemd.tty.rows.console={lines}",
             f"console={tty}",
+            f"TERM={term}",
         ]
 
     for s in args.kernel_command_line_extra:
@@ -3430,6 +3440,7 @@ def load_environment(args: argparse.Namespace) -> dict[str, str]:
         "SYSTEMD_TMPFILES_FORCE_SUBVOL": "0",
         "KERNEL_INSTALL_BYPASS": "1",
         "SYSTEMD_HWDB_UPDATE_BYPASS": "1",
+        "TERM": finalize_term(),
     }
 
     if args.image_id is not None:
index 8130460846e928d640a412e0fbfae569d3f5db5a..61a17bcb1634d9dd9cba16dbf4f8b99c679d4ef6 100644 (file)
@@ -156,7 +156,7 @@ def run(
 
     env = {
         "PATH": os.environ["PATH"],
-        "TERM": os.getenv("TERM", "vt220"),
+        "TERM": env.get("TERM") or os.getenv("TERM", "vt220"),
         "LANG": "C.UTF-8",
         **env,
     }
@@ -256,7 +256,7 @@ def spawn(
 
     env = {
         "PATH": os.environ["PATH"],
-        "TERM": os.getenv("TERM", "vt220"),
+        "TERM": env.get("TERM") or os.getenv("TERM", "vt220"),
         "LANG": "C.UTF-8",
         **env,
     }
index 25dcb5dedc53863a762553f4fa7adee09ed1fa62..471151cc98dd6786c3f029fa3fc4cd9c43590745 100644 (file)
@@ -10,7 +10,7 @@ from typing import Any, NamedTuple, Optional
 
 import pytest
 
-from mkosi.config import Architecture
+from mkosi.config import Architecture, finalize_term
 from mkosi.distributions import Distribution
 from mkosi.run import run
 from mkosi.types import _FILE, CompletedProcess, PathString
@@ -54,6 +54,7 @@ class Image:
     ) -> CompletedProcess:
         kcl = [
             f"console={Architecture.native().default_serial_tty()}",
+            f"TERM={finalize_term()}",
             "loglevel=6",
             "systemd.crash_shell",
             "systemd.log_level=debug",