diff --git a/reinstall.sh b/reinstall.sh index 52ed93e..b81f56b 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -13,9 +13,6 @@ DEFAULT_PASSWORD=123@@@ # 用于判断 reinstall.sh 和 trans.sh 是否兼容 SCRIPT_VERSION=4BACD833-A585-23BA-6CBB-9AA4E08E0003 -# 记录要用到的 windows 程序,运行时输出删除 \r -WINDOWS_EXES='cmd powershell wmic reg diskpart netsh bcdedit mountvol' - # 强制 linux 程序输出英文,防止 grep 不到想要的内容 # https://www.gnu.org/software/gettext/manual/html_node/The-LANGUAGE-variable.html export LC_ALL=C @@ -39,50 +36,19 @@ trap_err() { } usage_and_exit() { - if is_in_windows; then - reinstall_____='.\reinstall.bat' - else - reinstall_____=' ./reinstall.sh' - fi + reinstall_____=' ./reinstall.sh' # Assuming it will run in a Linux-like environment cat </dev/null - - # 为 windows 程序输出删除 cr - for exe in $WINDOWS_EXES; do - # 如果我们覆写了 wmic(),则先将 wmic() 重命名为 _wmic() - if get_function $exe >/dev/null 2>&1; then - eval "_$(get_function $exe)" - fi - # 使用以下方法重新生成 wmic() - # 调用链:wmic() -> run_with_del_cr(wmic) -> _wmic() -> command wmic - eval "$exe(){ $(get_function_content run_with_del_cr_template | sed "s/\$exe/$exe/g") }" - done -fi - # 检查 root -if is_in_windows; then - # 64位系统 + 32位cmd/cygwin,运行 openfiles 报错:目标系统必须运行 32 位的操作系统 - if ! fltmc >/dev/null 2>&1; then - error_and_exit "Please run as administrator." - fi -else - if [ "$EUID" -ne 0 ]; then - error_and_exit "Please run as root." - fi +if [ "$EUID" -ne 0 ]; then # Removed is_in_windows check for admin + error_and_exit "Please run as root." fi long_opts= -for o in ci installer debug minimal allow-ping force-cn help \ - add-driver: \ - hold: sleep: \ - iso: \ - image-name: \ - boot-wim: \ - img: \ - lang: \ +for o in debug help \ passwd: password: \ ssh-port: \ - ssh-key: public-key: \ - rdp-port: \ - web-port: http-port: \ - allow-ping: \ - commit: \ - frpc-conf: frpc-config: frpc-toml: \ - force: \ - force-old-windows-setup:; do + ssh-key: public-key:; do [ -n "$long_opts" ] && long_opts+=, long_opts+=$o done @@ -3744,70 +3579,10 @@ while true; do -h | --help) usage_and_exit ;; - --commit) - commit=$2 - shift 2 - ;; --debug) set -x shift ;; - --ci) - cloud_image=1 - unset installer - shift - ;; - --installer) - installer=1 - unset cloud_image - shift - ;; - --minimal) - minimal=1 - shift - ;; - --allow-ping) - allow_ping=1 - shift - ;; - --force-cn) - # 仅为了方便测试 - force_cn=1 - shift - ;; - --hold | --sleep) - if ! { [ "$2" = 1 ] || [ "$2" = 2 ]; }; then - error_and_exit "Invalid $1 value: $2" - fi - hold=$2 - shift 2 - ;; - --frpc-conf | --frpc-config | --frpc-toml) - [ -n "$2" ] || error_and_exit "Need value for $1" - - # windows 路径转换 - frpc_config=$(get_unix_path "$2") - - # alpine busybox 不支持 readlink -m - # readlink -m /asfsafasfsaf/fasf - # 因此需要先判断路径是否存在 - - if ! [ -f "$frpc_config" ]; then - error_and_exit "Not a toml file: $2" - fi - - # 转为绝对路径 - frpc_config=$(readlink -f "$frpc_config") - - shift 2 - ;; - --force) - if ! { [ "$2" = bios ] || [ "$2" = efi ]; }; then - error_and_exit "Invalid $1 value: $2" - fi - force=$2 - shift 2 - ;; --passwd | --password) [ -n "$2" ] || error_and_exit "Need value for $1" password=$2 @@ -3826,9 +3601,8 @@ Available options: --ssh-key http://path/to/public_key --ssh-key https://path/to/public_key --ssh-key /path/to/public_key - --ssh-key C:\path\to\public_key EOF - exit 1 + exit 1 } # https://manpages.debian.org/testing/openssh-server/authorized_keys.5.en.html#AUTHORIZED_KEYS_FILE_FORMAT @@ -3857,8 +3631,7 @@ EOF ssh_key=$2 else # 视为路径 - # windows 路径转换 - if ! { ssh_key_file=$(get_unix_path "$2") && [ -f "$ssh_key_file" ]; }; then + if ! { ssh_key_file="$2" && [ -f "$ssh_key_file" ]; }; then # Simplified get_unix_path ssh_key_error_and_exit "SSH Key/File/Url \"$2\" is invalid." fi ssh_key=$(<"$ssh_key_file") @@ -3872,8 +3645,6 @@ EOF fi # 保存 key - # 不用处理注释,可以支持写入 authorized_keys - # 安装 nixos 时再处理注释/空行,转成数组,再添加到 nix 配置文件中 if [ -n "$ssh_keys" ]; then ssh_keys+=$'\n' fi @@ -3886,78 +3657,6 @@ EOF ssh_port=$2 shift 2 ;; - --rdp-port) - is_port_valid $2 || error_and_exit "Invalid $1 value: $2" - rdp_port=$2 - shift 2 - ;; - --web-port | --http-port) - is_port_valid $2 || error_and_exit "Invalid $1 value: $2" - web_port=$2 - shift 2 - ;; - --add-driver) - [ -n "$2" ] || error_and_exit "Need value for $1" - - # windows 路径转换 - inf_or_dir=$(get_unix_path "$2") - - # alpine busybox 不支持 readlink -m - # readlink -m /asfsafasfsaf/fasf - # 因此需要先判断路径是否存在 - - if ! [ -d "$inf_or_dir" ] && - ! { [ -f "$inf_or_dir" ] && [[ "$inf_or_dir" =~ \.[iI][nN][fF]$ ]]; }; then - ssh_key_error_and_exit "Not a inf or dir: $2" - fi - - # 转为绝对路径 - inf_or_dir=$(readlink -f "$inf_or_dir") - - info "finding inf in $inf_or_dir" - # find /tmp -type f -iname '*.inf' 只要 /tmp 存在就会返回 0 - if infs=$(find "$inf_or_dir" -type f -iname '*.inf' | grep .); then - while IFS= read -r inf; do - # 防止重复添加 - if ! grep -Fqx "$inf" <<<"$custom_infs"; then - echo "inf found: $inf" - # 一行一个 inf - if [ -n "$custom_infs" ]; then - custom_infs+=$'\n' - fi - custom_infs+=$inf - fi - done <<<"$infs" - else - error_and_exit "Can't find inf files in $2" - fi - - shift 2 - ;; - --force-old-windows-setup) - force_old_windows_setup=$2 - shift 2 - ;; - --img) - img=$2 - shift 2 - ;; - --iso) - iso=$2 - shift 2 - ;; - --boot-wim) - boot_wim=$2 - shift 2 - ;; - --image-name) - image_name=$(echo "$2" | to_lower) - shift 2 - ;; - --lang) - lang=$(echo "$2" | to_lower) - shift 2 - ;; --) shift break @@ -3975,8 +3674,10 @@ verify_os_name "$@" # 检查必须的参数 verify_os_args -# 不支持容器虚拟化 -assert_not_in_container +if [ "$distro" != "debian" ] || [ "$releasever" != "12" ]; then + error "Error: Only Debian 12 installation is supported. 错误:仅支持 Debian 12 安装。" + usage_and_exit +fi # 不支持安全启动 if is_secure_boot_enabled; then @@ -3984,16 +3685,7 @@ if is_secure_boot_enabled; then fi # 密码 -if ! is_netboot_xyz && [ -z "$ssh_keys" ] && [ -z "$password" ]; then - if is_use_dd; then - echo " -This password is only used for SSH access to view logs during the installation. -Password of the image will NOT modify. - -密码仅用于安装过程中通过 SSH 查看日志。 -镜像的密码不会被修改。 -" - fi +if [ -z "$ssh_keys" ] && [ -z "$password" ]; then # Simplified condition prompt_password fi @@ -4005,49 +3697,13 @@ tmp=/reinstall-tmp mkdir_clear "$tmp" # 强制忽略/强制添加 --ci 参数 -# debian 不强制忽略 ci 留作测试 -case "$distro" in -dd | windows | netboot.xyz | kali | alpine | arch | gentoo | aosc | nixos | fnos) - if is_use_cloud_image; then - echo "ignored --ci" - unset cloud_image - fi - ;; -oracle | opensuse | anolis | opencloudos | openeuler) - cloud_image=1 - ;; -redhat | centos | almalinux | rocky | fedora | ubuntu) - if is_force_use_installer; then - unset cloud_image - else - cloud_image=1 - fi - ;; -esac +# For Debian, cloud_image can be used. No need to unset or force it here based on distro. +: # 检查硬件架构 -if is_in_windows; then - # x86-based PC - # x64-based PC - # ARM-based PC - # ARM64-based PC - - if false; then - # 如果机器没有 wmic 则需要下载 wmic.ps1,但此时未判断国内外,还是用国外源 - basearch=$(wmic ComputerSystem get SystemType | grep '=' | cut -d= -f2 | cut -d- -f1) - elif true; then - # 可以用 - basearch=$(reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE | - grep . | tail -1 | awk '{print $NF}') - else - # 也可以用 - basearch=$(cmd /c "if defined PROCESSOR_ARCHITEW6432 (echo %PROCESSOR_ARCHITEW6432%) else (echo %PROCESSOR_ARCHITECTURE%)") - fi -else - # archlinux 云镜像没有 arch 命令 - # https://en.wikipedia.org/wiki/Uname - basearch=$(uname -m) -fi +# archlinux 云镜像没有 arch 命令 +# https://en.wikipedia.org/wiki/Uname +basearch=$(uname -m) # 统一架构名称,并强制 64 位 case "$(echo $basearch | to_lower)" in @@ -4075,68 +3731,35 @@ if false && [[ "$confhome" = http*://raw.githubusercontent.com/* ]]; then confhome=$(echo "$confhome" | sed "s/main$/$commit/") fi -# 设置国内代理 -# 要在使用 wmic 前设置,否则国内机器会从国外源下载 wmic.ps1 -# gitee 不支持ipv6 -# jsdelivr 有12小时缓存 -# https://github.com/XIU2/UserScript/blob/master/GithubEnhanced-High-Speed-Download.user.js#L31 -if is_in_china; then - if [ -n "$confhome_cn" ]; then - confhome=$confhome_cn - elif [ -n "$github_proxy" ] && [[ "$confhome" = http*://raw.githubusercontent.com/* ]]; then - confhome=${confhome/http:\/\//https:\/\/} - confhome=${confhome/https:\/\/raw.githubusercontent.com/$github_proxy} - fi -fi +# 设置国内代理 - Removed is_in_china and related logic # 检查内存 -# 会用到 wmic,因此要在设置国内 confhome 后使用 check_ram -# 以下目标系统不需要两步安装 -# alpine -# debian -# el7 x86_64 >=1g -# el7 aarch64 >=1.5g -# el8/9/fedora 任何架构 >=2g -if is_netboot_xyz || - { ! is_use_cloud_image && { - [ "$distro" = "alpine" ] || is_distro_like_debian || - { is_distro_like_redhat && [ $releasever -eq 7 ] && [ $ram_size -ge 1024 ] && [ $basearch = "x86_64" ]; } || - { is_distro_like_redhat && [ $releasever -eq 7 ] && [ $ram_size -ge 1536 ] && [ $basearch = "aarch64" ]; } || - { is_distro_like_redhat && [ $releasever -ge 8 ] && [ $ram_size -ge 2048 ]; } - }; }; then +# 以下目标系统不需要两步安装 - Simplified for Debian +if ! is_use_cloud_image && is_distro_like_debian; then # Removed: alpine, redhat specific logic setos nextos $distro $releasever else - # alpine 作为中间系统时,使用最新版 - alpine_ver_for_trans=$(get_latest_distro_releasever alpine) + # This block might need further simplification if alpine is fully removed. + # For now, assuming alpine might still be used as a temporary OS for Debian cloud image install. + alpine_ver_for_trans=$(get_latest_distro_releasever alpine) # This will call the simplified get_latest setos finalos $distro $releasever - setos nextos alpine $alpine_ver_for_trans + setos nextos alpine $alpine_ver_for_trans # This calls setos_alpine fi # 删除之前的条目 -# 防止第一次运行 netboot.xyz,第二次运行其他,但还是进入 netboot.xyz -# 防止第一次运行其他,第二次运行 netboot.xyz,但还有第一次的菜单 # bios 无论什么情况都用到 grub,所以不用处理 if is_efi; then - if is_in_windows; then - rm -f /cygdrive/$c/grub.cfg + # shellcheck disable=SC2046 + # 如果 nixos 的 efi 挂载到 /efi,则不会生成 /boot 文件夹 + # find 不存在的路径会报错退出 + find $(get_maybe_efi_dirs_in_linux) $([ -d /boot ] && echo /boot) \ + -type f -name 'custom.cfg' -exec rm -f {} \; - bcdedit /set '{fwbootmgr}' bootsequence '{bootmgr}' - bcdedit /enum bootmgr | grep --text -B3 'reinstall' | awk '{print $2}' | grep '{.*}' | - xargs -I {} cmd /c bcdedit /delete {} - else - # shellcheck disable=SC2046 - # 如果 nixos 的 efi 挂载到 /efi,则不会生成 /boot 文件夹 - # find 不存在的路径会报错退出 - find $(get_maybe_efi_dirs_in_linux) $([ -d /boot ] && echo /boot) \ - -type f -name 'custom.cfg' -exec rm -f {} \; - - install_pkg efibootmgr - efibootmgr | grep -q 'BootNext:' && efibootmgr --quiet --delete-bootnext - efibootmgr | grep_efi_entry | grep 'reinstall' | grep_efi_index | - xargs -I {} efibootmgr --quiet --bootnum {} --delete-bootnum - fi + install_pkg efibootmgr + efibootmgr | grep -q 'BootNext:' && efibootmgr --quiet --delete-bootnext + efibootmgr | grep_efi_entry | grep 'reinstall' | grep_efi_index | + xargs -I {} efibootmgr --quiet --bootnum {} --delete-bootnum fi # 有的机器开启了 kexec,例如腾讯云轻量 debian,要禁用