fix: improve alpine init flow

Signed-off-by: Ivan Li <ivanli2048@gmail.com>
This commit is contained in:
2025-12-11 23:06:15 +08:00
parent 10273751d3
commit 12a547eb46

View File

@@ -79,10 +79,22 @@ configure_timezone() {
set_root_password() { set_root_password() {
info "Now setting root password (you will be prompted by passwd)." info "Now setting root password (you will be prompted by passwd)."
passwd root || { while :; do
error "Failed to set root password." if passwd root; then
return 0
fi
warn "Failed to set root password."
printf 'Try again? [y/N]: '
read -r answer || answer=""
case "$answer" in
y|Y)
;;
*)
error "Failed to set root password; aborting."
exit 1 exit 1
} ;;
esac
done
} }
prompt_for_username() { prompt_for_username() {
@@ -115,8 +127,8 @@ create_user_if_needed() {
return 0 return 0
fi fi
info "Creating user '$username' with default shell /bin/zsh..." info "Creating user '$username' with default shell /bin/sh (will switch to zsh later)..."
adduser -D -s /bin/zsh "$username" || { adduser -D -s /bin/sh "$username" || {
error "Failed to create user '$username'." error "Failed to create user '$username'."
exit 1 exit 1
} }
@@ -303,34 +315,40 @@ main() {
require_root require_root
check_alpine check_alpine
install_base_packages
configure_timezone
set_root_password set_root_password
local username local username
username="$(prompt_for_username)" username="$(prompt_for_username)"
create_user_if_needed "$username" create_user_if_needed "$username"
setup_zsh_for_user "$username"
local setup_keys_choice local setup_keys_choice
local want_setup_keys=0
local ssh_disable_password=0 local ssh_disable_password=0
printf 'Do you want to configure SSH key-based login for this user now? [y/N]: ' printf 'Do you want to configure SSH key-based login for this user now? [y/N]: '
read -r setup_keys_choice || setup_keys_choice="" read -r setup_keys_choice || setup_keys_choice=""
case "$setup_keys_choice" in case "$setup_keys_choice" in
y|Y) y|Y)
if setup_authorized_keys "$username" "$SSH_AUTH_KEYS_URL"; then want_setup_keys=1
ssh_disable_password=1
else
ssh_disable_password=0
fi
;; ;;
*) *)
info "Skipping SSH key-based login configuration." info "Skipping SSH key-based login configuration."
;; ;;
esac esac
install_base_packages
configure_timezone
setup_zsh_for_user "$username"
if [ "$want_setup_keys" -eq 1 ]; then
if setup_authorized_keys "$username" "$SSH_AUTH_KEYS_URL"; then
ssh_disable_password=1
else
ssh_disable_password=0
fi
fi
configure_sshd "$ssh_disable_password" configure_sshd "$ssh_disable_password"
enable_sshd_service enable_sshd_service