create-lxc: prompt for root password, enable sshd when set

Accepts ROOT_PASSWORD from env or reads from /dev/tty (works under
curl|bash). When set, pipes it through `chpasswd` via stdin (secret
stays out of argv) and installs+enables openssh with PermitRootLogin
yes. Skipping leaves the prior no-password behavior intact so pct
enter remains the fallback.
This commit is contained in:
d4nn13 committed 2026-04-19 19:11:01 +02:00
1 parent 4e7e0f34ba
commit eaca8305ae
2 files changed
+53 -1

No files matched your search

+42
View File
@@ -40,6 +40,32 @@ fi
log() { printf '[create-lxc] %s\n' "$*"; }
# --- root password ----------------------------------------------------------
# Accept ROOT_PASSWORD from env; otherwise prompt on the controlling TTY
# (works even when invoked via `curl … | bash` because we read from /dev/tty).
# If neither is available, fall back to the no-password default — `pct enter`
# from the Proxmox host is still possible.
if [[ -z "${ROOT_PASSWORD:-}" ]]; then
if [[ -r /dev/tty && -w /dev/tty ]]; then
exec 3</dev/tty
printf 'root password for container (empty to skip): ' >/dev/tty
IFS= read -rs ROOT_PASSWORD <&3
printf '\n' >/dev/tty
if [[ -n "$ROOT_PASSWORD" ]]; then
printf 'confirm: ' >/dev/tty
IFS= read -rs ROOT_PASSWORD_CONFIRM <&3
printf '\n' >/dev/tty
if [[ "$ROOT_PASSWORD" != "$ROOT_PASSWORD_CONFIRM" ]]; then
echo "passwords do not match" >&2
exit 1
fi
fi
exec 3<&-
else
log "no ROOT_PASSWORD set and no TTY — root will have no password (pct enter only)"
fi
fi
# --- pick VMID --------------------------------------------------------------
VMID=$(pvesh get /cluster/nextid)
log "VMID=$VMID hostname=$HOSTNAME cores=$CORES mem=${MEMORY}MiB disk=${DISK}GiB"
@@ -114,6 +140,22 @@ pct exec "$VMID" -- sh -c 'apk add --no-cache bash curl ca-certificates >/dev/nu
log "running bootstrap from $BOOTSTRAP_URL"
pct exec "$VMID" -- sh -c "curl -fsSL '$BOOTSTRAP_URL' | WIRECON_REPO='$WIRECON_REPO' WIRECON_REF='$WIRECON_REF' bash"
# --- optional: set root password + enable sshd -----------------------------
if [[ -n "${ROOT_PASSWORD:-}" ]]; then
log "setting root password"
# Pipe via stdin so the secret never appears in argv / ps.
printf 'root:%s\n' "$ROOT_PASSWORD" | pct exec "$VMID" -- chpasswd
log "installing + enabling sshd (PermitRootLogin yes)"
pct exec "$VMID" -- sh -c '
apk add --no-cache openssh >/dev/null
sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/" /etc/ssh/sshd_config
grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config || echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
rc-update add sshd default
rc-service sshd restart
'
fi
cat <<EOF
[create-lxc] container $VMID ($HOSTNAME) provisioned.