This commit is contained in:
Andrew Hurley 2026-06-28 17:59:09 +08:00
parent 85b070b132
commit 4c15e16a3a
2 changed files with 10 additions and 28 deletions

View File

@ -26,7 +26,7 @@ _lxcd_completion() {
case "${subcommand}" in
create)
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--nested --sshd --git --certs --aptcache --ssh-client -i --image" -- "$cur"))
COMPREPLY=($(compgen -W "--nested --git --certs --aptcache --ssh-client -i --image -m --minimal" -- "$cur"))
else
COMPREPLY=($(compgen -W "ubuntu: ubuntu/24.04 ubuntu/22.04 debian/12 debian/11" -- "$cur"))
fi
@ -52,7 +52,7 @@ _lxcd_completion() {
if [ $cword -eq 2 ]; then
COMPREPLY=($(compgen -W "${containers}" -- "$cur"))
elif [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--nested --sshd --git --certs --aptcache --ssh-client" -- "$cur"))
COMPREPLY=($(compgen -W "--nested --git --certs --aptcache --ssh-client" -- "$cur"))
fi
;;

34
lxcd.py
View File

@ -129,7 +129,7 @@ def handle_list(args):
active_opts.append("nesting")
# Check labels for other features
for feature in ("certs", "git", "sshd", "aptcache", "ssh-client"):
for feature in ("certs", "git", "aptcache", "ssh-client"):
if get_config(f"user.lxcd.{feature}") == "true":
active_opts.append(feature)
@ -158,6 +158,8 @@ def handle_clone(args):
def handle_create(args):
host_username, uid, gid, host_home = get_real_user_info()
if args.minimal:
args.image = "ubuntu-minimal:"
host_timezone = detect_timezone()
host_locale = detect_locale()
@ -173,12 +175,6 @@ def handle_create(args):
print(f"Creating LXD container '{args.name}' from image '{args.image}'...")
# Determine SSH action based on --sshd flag
ssh_action = "systemctl disable --now ssh.service ssh.socket"
if args.sshd:
ssh_action = "systemctl enable --now ssh.service ssh.socket"
print("Security profiles adjusted: SSH daemon and socket explicitly requested.")
# Cloud-init configuration
apt_proxy_cmd = (
f" - mkdir -p /etc/apt/apt.conf.d\n"
@ -215,7 +211,7 @@ runcmd:
- cp -rpn /etc/skel/. /home/{host_username}/
- mkdir -p {container_coder_path}
{ssh_client_cmd} - chown -R {host_username}:{host_username} /home/{host_username}
- {ssh_action}
- echo "SSH left at default image state"
{apt_proxy_cmd}"""
# Initialize container
@ -257,10 +253,6 @@ runcmd:
lxc_run([ "config", "set", args.name, "security.syscalls.intercept.mknod", "true"])
set_label(args.name, "nested")
# Handle --sshd label (already handled in cloud-init config above)
if args.sshd:
set_label(args.name, "sshd")
# Handle --aptcache option
if args.aptcache:
print(f"APT cache/proxy will be configured to {args.aptcache} via cloud-init...")
@ -396,7 +388,7 @@ def handle_add(args):
print(f"Error: Container '{args.name}' does not exist.", file=sys.stderr)
sys.exit(1)
if not args.nested and not args.sshd and not args.git and not args.certs and not args.aptcache and not args.ssh_client:
if not args.nested and not args.git and not args.certs and not args.aptcache and not args.ssh_client:
print("Error: Specify at least one option to add.\n"
"Example: lxcd add <name> --git --nested", file=sys.stderr)
sys.exit(1)
@ -417,17 +409,8 @@ def handle_add(args):
lxc_run([ "config", "set", args.name, "security.syscalls.intercept.mknod", "true"])
set_label(args.name, "nested")
if args.sshd:
print(f"Activating SSH daemon and socket inside '{args.name}'...")
lxc_run([ "exec", args.name, "--", "systemctl", "enable", "--now", "ssh.service", "ssh.socket"])
set_label(args.name, "sshd")
if args.git:
was_running = not was_stopped
print(f"Installing git inside '{args.name}'...")
lxc_run([ "exec", args.name, "--", "env", "DEBIAN_FRONTEND=noninteractive", "apt-get", "update", "-y"])
lxc_run([ "exec", args.name, "--", "env", "DEBIAN_FRONTEND=noninteractive", "apt-get", "install", "-y", "git"])
host_gitconfig = host_home / ".gitconfig"
if host_gitconfig.exists():
print(f"Mapping host .gitconfig into '{args.name}' as a device mount...")
@ -443,7 +426,7 @@ def handle_add(args):
lxc_run([ "start", args.name])
lxc_run([ "exec", args.name, "--", "cloud-init", "status", "--wait"], check=False)
else:
print(f"Warning: Host ~/.gitconfig not found. Git was installed, but file copy skipped.")
print(f"Warning: Host ~/.gitconfig not found. Skipping git config mapping.")
if args.certs:
host_certs = Path("/etc/ssl/certs/ca-certificates.crt")
@ -515,8 +498,8 @@ def main():
parser_create = subparsers.add_parser("create", help="Create a new LXD container")
parser_create.add_argument("name", help="Name of the container")
parser_create.add_argument("-i", "--image", default="ubuntu:", help="LXD image to use (defaults to 'ubuntu:')")
parser_create.add_argument("-m", "--minimal", action="store_true", help="Use ubuntu-minimal: image instead")
parser_create.add_argument("--nested", action="store_true", help="Enable nesting")
parser_create.add_argument("--sshd", action="store_true", help="Enable SSH daemon")
parser_create.add_argument("--git", action="store_true", help="Map host ~/.gitconfig into the container")
parser_create.add_argument("--certs", action="store_true", help="Mount host CA certificates bundle")
parser_create.add_argument("--aptcache", help="URL for an APT cache/proxy (e.g. http://10.0.0.1:3142)")
@ -560,8 +543,7 @@ def main():
parser_add = subparsers.add_parser("add", help="Add features to an existing container")
parser_add.add_argument("name", help="Name of the container")
parser_add.add_argument("--nested", action="store_true", help="Enable nesting")
parser_add.add_argument("--sshd", action="store_true", help="Enable SSH daemon")
parser_add.add_argument("--git", action="store_true", help="Install git and copy ~/.gitconfig")
parser_add.add_argument("--git", action="store_true", help="Map host ~/.gitconfig into the container")
parser_add.add_argument("--certs", action="store_true", help="Mount host CA certificates bundle")
parser_add.add_argument("--aptcache", help="URL for an APT cache/proxy (e.g. http://10.0.0.1:3142)")
parser_add.add_argument("--ssh-client", action="store_true",