From 1a5ac06ec052107f50c4b53fe92cd169da1820fa Mon Sep 17 00:00:00 2001 From: Andrew Hurley Date: Sun, 5 Jul 2026 17:19:46 +0800 Subject: [PATCH] no sudo inside --- lxcd.py | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/lxcd.py b/lxcd.py index 14d28ff..4fb2b9e 100755 --- a/lxcd.py +++ b/lxcd.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 import argparse -import grp import os import re import subprocess @@ -117,19 +116,6 @@ def detect_timezone(): def detect_locale(): return os.environ.get("LANG", "en_US.UTF-8") -def lxc_prefix(): - """Return [] if running inside a Snap, or if the user is root or in the lxd group.""" - if os.environ.get("SNAP"): - return [] - if os.getuid() == 0: - return [] - try: - if grp.getgrnam("lxd").gr_gid in os.getgroups(): - return [] - except KeyError: - pass - return ["sudo"] - def run_cmd(cmd, check=True): """Helper to run shell commands.""" try: @@ -139,15 +125,15 @@ def run_cmd(cmd, check=True): sys.exit(e.returncode) def lxc_run(args, check=True): - """Run an lxc command via run_cmd, auto-adding sudo if needed.""" - cmd = [*lxc_prefix(), "lxc", *args] + """Run an lxc command via run_cmd.""" + cmd = ["lxc", *args] if VERBOSE: print(f"+ {' '.join(cmd)}", file=sys.stderr) return run_cmd(cmd, check=check) def lxc_subprocess(args, **kwargs): - """Run an lxc command via subprocess.run, auto-adding sudo if needed.""" - cmd = [*lxc_prefix(), "lxc", *args] + """Run an lxc command via subprocess.run.""" + cmd = ["lxc", *args] if VERBOSE: print(f"+ {' '.join(cmd)}", file=sys.stderr) return subprocess.run(cmd, **kwargs) @@ -357,7 +343,6 @@ def handle_enter(args): if display: env_prefix += f"DISPLAY={display} " - prefix = lxc_prefix() shell_cmd = "exec /bin/bash --login" exec_cmd = [ "lxc", "exec", args.name, @@ -368,10 +353,7 @@ def handle_enter(args): "sh", "-c", f"env {env_prefix} sh -c '{shell_cmd}'" ] - if prefix: - os.execvp("sudo", ["sudo", *exec_cmd]) - else: - os.execvp("lxc", exec_cmd) + os.execvp("lxc", exec_cmd) def handle_stop(args): print(f"Stopping container '{args.name}'...")