From: Daan De Meyer Date: Tue, 12 Mar 2024 20:05:50 +0000 (+0000) Subject: Check for TERM=unknown and set TERM=dumb if not on tty X-Git-Tag: v22~12^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dd0ab3d780d196cbffd53d657c20a7d33097f32c;p=thirdparty%2Fmkosi.git Check for TERM=unknown and set TERM=dumb if not on tty 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 --- diff --git a/mkosi/config.py b/mkosi/config.py index 4c3743ec1..ad01d44a5 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -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: diff --git a/mkosi/run.py b/mkosi/run.py index 813046084..61a17bcb1 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -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, } diff --git a/tests/__init__.py b/tests/__init__.py index 25dcb5ded..471151cc9 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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",