core: 支持 linux 设置普通账号

This commit is contained in:
bin456789
2026-05-29 07:25:16 +08:00
parent 74824ef7b4
commit bd48e70776
4 changed files with 683 additions and 165 deletions
+160 -57
View File
@@ -102,6 +102,7 @@ Usage: $reinstall_____ anolis 7|8|23
reset
Options: For Linux/Windows:
[--username USERNAME]
[--password PASSWORD]
[--ssh-key KEY]
[--ssh-port PORT]
@@ -1942,15 +1943,29 @@ verify_os_name() {
}
verify_os_args() {
# 必备参数
case "$distro" in
dd) [ -n "$img" ] || error_and_exit "dd need --img" ;;
redhat) [ -n "$img" ] || error_and_exit "redhat need --img" ;;
dd) [ -n "$img" ] || error_and_exit "dd need --img." ;;
redhat) [ -n "$img" ] || error_and_exit "redhat need --img." ;;
windows) [ -n "$image_name" ] || error_and_exit "Install Windows need --image-name." ;;
esac
# 用户名/密码/证书相关
case "$distro" in
netboot.xyz | windows) [ -z "$ssh_keys" ] || error_and_exit "not support ssh key for $distro" ;;
netboot.xyz)
[ -z "$username" ] || error_and_exit "not support set username for $distro."
[ -z "$password" ] || error_and_exit "not support set password for $distro."
[ -z "$ssh_keys" ] || error_and_exit "not support set ssh key for $distro."
;;
windows)
[ -z "$ssh_keys" ] || error_and_exit "not support set ssh key for $distro."
;;
esac
# 不能同时使用证书和密码
if [ -n "$password" ] && [ -n "$ssh_keys" ]; then
error_and_exit "Cannot set both password and ssh key."
fi
}
get_cmd_path() {
@@ -2341,30 +2356,28 @@ trim() {
}
assert_username_valid() {
if ! msg=$(is_username_valid); then
error_and_exit "$msg"
fi
}
is_username_valid() {
# https://learn.microsoft.com/windows-hardware/customize/desktop/unattend/microsoft-windows-shell-setup-useraccounts-localaccounts-localaccount-name
# 不能为 none [ ] / \ : | < > + = ; , ? * % @
# 账号为空,则使用 Administrator
# 账号为空
if [ -z "$username" ]; then
echo "Username: Will use the built-in Administrator account in ISO language."
return 0
error_and_exit "Username: Can not be empty."
fi
# 账号为 none
if [ "$(to_lower <<<"$username")" = none ]; then
echo "Username: Do not use the name \"NONE\", this is a restricted username."
return 1
error_and_exit "Username: Can not be 'none'."
fi
# 账号包含非法字符
if grep -q '[][/\:|<>+=;,?*%@]' <<<"$username"; then
echo "Username: Do not use any of the following characters: / \ [ ] : | < > + = ; , ? * % @"
return 1
error_and_exit "Username: Do not use any of the following characters: / \ [ ] : | < > + = ; , ? * % @"
fi
}
# trans.sh 有同名方法
is_administrator_username() {
username_in_lower=$(to_lower <<<"$1")
# 如果输入以下用户名则忽略,并使用系统内置的 Administrator 账号
# 防止系统有两个不同语言的 Administrator 账号而造成困扰
@@ -2376,27 +2389,38 @@ is_username_valid() {
администратор \
järjestelmänvalvoja \
rendszergazda; do
if [ "$(to_lower <<<"$username")" = "$builtin_username" ]; then
echo "Username: Will use the built-in Administrator account in ISO language."
unset username
if [ "$username_in_lower" = "$builtin_username" ]; then
return 0
fi
done
return 1
}
prompt_username() {
info "prompt username"
warn false "Leave blank to use Administrator"
warn false "不填写则使用 Administrator"
if [ "$distro" = windows ]; then
default_username=administrator
else
default_username=root
fi
warn false "Set username, leave blank to use $default_username"
warn false "设置用户名,不填写则使用 $default_username"
IFS= read -r -p "Username: " username
username="$(printf "%s" "$username" | trim)"
if [ -z "$username" ]; then
username=$default_username
fi
assert_username_valid
}
prompt_password() {
info "prompt password"
warn false "Leave blank to use a random password."
warn false "不填写则使用随机密码"
warn false "Set password, leave blank to use a random password."
warn false "设置密码,不填写则使用随机密码"
while true; do
IFS= read -r -p "Password: " password
if [ -n "$password" ]; then
@@ -3626,7 +3650,7 @@ EOF
# 2. 删除 debian busybox 无法识别的语法
# 3. 删除 apk 语句
# 4. debian 11/12 initrd 无法识别 > >
# 5. debian 11/12 initrd 无法识别 < <
# 5. debian 11/12 initrd 无法识别 < < ,注意可能分两行写
# 6. debian 11 initrd 无法识别 set -E
# 7. debian 11 initrd 无法识别 trap ERR
# 8. debian 9 initrd 无法识别 ${string//find/replace}
@@ -3637,11 +3661,16 @@ EOF
-e "s/> >/$replace/" \
-e "s/< </$replace/" \
-e "s/\. <\(/$replace/" \
-e "s/< \\\\/$replace/" \
-e "s/ <\(/$replace/" \
-e "s/^[[:space:]]*apk[[:space:]]/$replace/" \
-e "s/^[[:space:]]*trap[[:space:]]/$replace/" \
-e "s/\\$\{.*\/\/.*\/.*\}/$replace/" \
-e "/^[[:space:]]*set[[:space:]]/s/E//" \
$initrd_dir/trans.sh
# 测试魔改后的 trans.sh 有没有语法问题
bash -n $initrd_dir/trans.sh
}
get_disk_drivers() {
@@ -4499,9 +4528,7 @@ while true; do
shift 2
;;
--user | --username)
if ! [ "$distro" = windows ]; then
error_and_exit "$1 is only supported for installing Windows."
fi
[ -n "$2" ] || error_and_exit "Need value for $1"
username="$(printf "%s" "$2" | trim)"
assert_username_valid
shift 2
@@ -4682,7 +4709,7 @@ done
verify_os_args
# 用户名
if [ "$distro" = windows ] && [ -z "$username" ]; then
if ! is_netboot_xyz && [ -z "$username" ]; then
prompt_username
fi
@@ -4964,54 +4991,130 @@ fi
info 'info'
echo "$distro $releasever"
case "$distro" in
windows) username=${username:-administrator} ;;
netboot.xyz) username= ;;
dd | *) username=root ;;
esac
ssh_port=${ssh_port:-22}
rdp_port=${rdp_port:-3389}
web_port=${web_port:-80}
if [ -n "$username" ]; then
if [ "$distro" = netboot.xyz ]; then
:
elif [ "$distro" = alpine ] && [ "$hold" = 1 ]; then
info "Alpine Live OS"
echo "Username: $username"
if [ -n "$ssh_keys" ]; then
echo "Public Key: $ssh_keys"
else
echo "Password: $password"
fi
if [ -n "$ssh_port" ]; then
echo "SSH Port: $ssh_port"
fi
fi
echo "SSH Port: $ssh_port"
if is_netboot_xyz; then
echo 'Reboot to start netboot.xyz.'
elif is_alpine_live; then
echo 'Reboot to start Alpine Live OS.'
elif is_use_dd; then
elif [ "$distro" = fnos ]; then
info "While Install (View Logs)"
echo "Username: $username"
if [ -n "$ssh_keys" ]; then
echo "Public Key: $ssh_keys"
else
echo "Password: $password"
fi
echo "SSH Port: $ssh_port"
echo "WEB Port: $web_port"
info "After Install"
echo "安装后不会开启 SSH 服务。"
echo "你需要尽快到 http://IP:5666 配置账号密码。"
echo
echo "SSH Service is disabled after installation."
echo "You need to config the username and password on http://IP:5666 as soon as possible."
elif [ "$distro" = windows ]; then
info "While Install (View Logs)"
echo "Username: $username"
echo "Password: $password"
echo "SSH Port: $ssh_port"
echo "WEB Port: $web_port"
info "After Install"
if is_administrator_username "$username"; then
echo "Username: $username (Depends on Windows iso's language)"
else
echo "Username: $username"
fi
echo "Password: $password"
echo "RDP Port: $rdp_port"
elif [ "$distro" = dd ]; then
info "While Install (View Logs)"
echo "Username: $username"
if [ -n "$ssh_keys" ]; then
echo "Public Key: $ssh_keys"
else
echo "Password: $password"
fi
echo "SSH Port: $ssh_port"
echo "WEB Port: $web_port"
info "After Install"
if [ -n "$cloud_data" ]; then
echo "Cloud Data: $cloud_data"
echo "Cloud Data Files: $cloud_data_files"
else
echo "Username: [Depends on image]"
echo "Public Key: [Depends on image]"
echo "Password: [Depends on image]"
echo "SSH Port: [Depends on image]"
fi
show_dd_password_tips
echo 'Reboot to start DD.'
elif [ "$distro" = fnos ]; then
echo "Special note for FNOS:"
echo "Reboot to start the installation."
echo "SSH login is disabled when installation completed."
echo "You need to config the account and password on http://SERVER_IP:5666 as soon as possible."
echo
echo "飞牛 OS 注意事项:"
echo "重启后开始安装。"
echo "安装完成后不支持 SSH 登录。"
echo "你需要尽快在 http://SERVER_IP:5666 配置账号密码。"
else
echo "Reboot to start the installation."
# 普通 linux
info "While Install (View Logs)"
echo "Username: $username"
if [ -n "$ssh_keys" ]; then
echo "Public Key: $ssh_keys"
else
echo "Password: $password"
fi
echo "SSH Port: $ssh_port"
echo "WEB Port: $web_port"
info "After Install"
echo "Username: $username"
if [ -n "$ssh_keys" ]; then
echo "Public Key: $ssh_keys"
else
echo "Password: $password"
fi
echo "SSH Port: $ssh_port"
fi
if is_in_windows; then
echo
echo 'You can run this command to reboot:'
echo 'shutdown /r /t 0'
fi
echo
echo "If you want to revert all changes made by this script, run \"$reinstall_____ reset\""
if [ "$distro" = netboot.xyz ]; then
echo '重启后进入 netboot.xyz。'
echo "或者现在运行 \"$reinstall_____ reset\" 以清除该引导项。"
echo
echo 'Reboot to start netboot.xyz.'
echo "Or run \"$reinstall_____ reset\" now to clear this boot entry."
echo
elif [ "$distro" = alpine ] && [ "$hold" = 1 ]; then
echo '重启后进入 Alpine Live OS。'
echo "或者现在运行 \"$reinstall_____ reset\" 以清除该引导项。"
echo
echo 'Reboot to start Alpine Live OS.'
echo "Or run \"$reinstall_____ reset\" now to clear this boot entry."
echo
else
warn false '警告:重装会清除主硬盘的所有数据,包括所有分区!'
echo '重启后开始重装。'
echo "或者现在运行 \"$reinstall_____ reset\" 以取消重装。"
echo
warn false 'Warning: Reinstalling will erase all data on the main disk, including all partitions!'
echo 'Reboot to start the reinstallation.'
echo "Or run \"$reinstall_____ reset\" now to cancel the reinstallation."
fi
echo