From 9a157a68f77552383b697fde2e55fff89cdd2afc Mon Sep 17 00:00:00 2001 From: Nathan Chancellor Date: Mon, 23 Dec 2024 23:34:19 -0700 Subject: [PATCH] env: Add detection for environment using systemd-nspawn Signed-off-by: Nathan Chancellor --- fish/functions/using_nspawn.fish | 7 +++++++ python/lib/utils.py | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 fish/functions/using_nspawn.fish diff --git a/fish/functions/using_nspawn.fish b/fish/functions/using_nspawn.fish new file mode 100644 index 00000000..0e1c5f87 --- /dev/null +++ b/fish/functions/using_nspawn.fish @@ -0,0 +1,7 @@ +#!/usr/bin/env fish +# SPDX-License-Identifier: MIT +# Copyright (C) 2022-2023 Nathan Chancellor + +function using_nspawn -d "Checks if host is using systemd-nspawn for development container" + PYTHONPATH=$PYTHON_FOLDER python3 -c 'import lib.utils, sys; sys.exit(0 if lib.utils.using_nspawn() else 1)' +end diff --git a/python/lib/utils.py b/python/lib/utils.py index b9f8e2fa..e9fa88ae 100755 --- a/python/lib/utils.py +++ b/python/lib/utils.py @@ -101,6 +101,12 @@ def in_container(): '/.dockerenv').is_file() +def in_nspawn(): + # An nspawn container has to have systemd-detect-virt but this may not + # always run where systemd-detect-virt exists. + return shutil.which('systemd-detect-virt') and detect_virt('-c') == 'systemd-nspawn' + + def path_and_text(*args): if (path := Path(*args)).exists(): return path, path.read_text(encoding='utf-8') @@ -184,6 +190,11 @@ def run_check_rc_zero(*args, **kwargs): return chronic(*args, **kwargs, check=False).returncode == 0 +def using_nspawn(): + etc_nspawn = Path('/etc/systemd/nspawn') + return etc_nspawn.exists() and list(etc_nspawn.iterdir()) + + def print_or_run_cmd(cmd, dryrun, end='\n\n'): if dryrun: print_cmd(cmd, end=end)