diff --git a/README.en.md b/README.en.md index 0f00d2e..7b39832 100644 --- a/README.en.md +++ b/README.en.md @@ -11,7 +11,7 @@ Reinstall server with one-click [中文](README.md) ## Highlights -- Support installation of 13 common Linux distributions. +- Support installation of 16 common Linux distributions. - Support for installing Windows using the official original ISO. The script can automatically search for the ISO and drivers. - Support reinstallation in any direction, meaning `Linux to Linux`, `Linux to Win`, `Win to Win`, `Win to Linux`. - Specifically tailored for low-spec machines, addressing insufficient memory that prevents network installation. diff --git a/README.md b/README.md index 706749d..91592e3 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## 亮点 -- 支持安装 13 种常见 Linux 发行版 +- 支持安装 16 种常见 Linux 发行版 - 支持用官方原版 iso 安装 Windows,并且脚本会自动查找 iso 和驱动 - 支持任意方向重装,也就是支持 `Linux to Linux`、`Linux to Win`、`Win to Win`、`Win to Linux` - 专门适配低配小鸡,解决内存过少导致无法进行网络安装 diff --git a/reinstall.sh b/reinstall.sh index 82fe1d3..925c057 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -93,12 +93,12 @@ curl() { return else ret=$? - if [ $ret -eq 22 ]; then - # 403 404 错误 + # 403 404 错误,或者达到重试次数 + if [ $ret -eq 22 ] || [ $i -eq 5 ]; then return $ret fi + sleep 1 fi - sleep 1 done } @@ -1298,34 +1298,30 @@ install_pkg() { esac } - is_need_epel_repo() { - case "$pkg" in - dpkg) [ "$pkg_mgr" = yum ] || [ "$pkg_mgr" = dnf ] ;; - jq) [ "$pkg_mgr" = yum ] ;; # el7/ol7 - *) false ;; - esac - } + # 系统 package名称 repo名称 + # centos/alma/rocky/fedora/anolis epel-release epel + # oracle linux oracle-epel-release ol9_developer_EPEL + # opencloudos epol-release EPOL + check_is_need_epel() { + is_need_epel() { + case "$pkg" in + dpkg) true ;; + jq) is_have_cmd yum && ! is_have_cmd dnf ;; # el7/ol7 的 jq 在 epel 仓库 + *) false ;; + esac + } - add_epel_repo() { - # epel 名称可能是 epel 或 ol9_developer_EPEL - # 如果没启用 - if ! $pkg_mgr repolist | awk '{print $1}' | grep -qi 'epel$'; then - # 删除 epel repo,因为可能安装了但未启用 - rm -rf /etc/yum.repos.d/*epel*.repo - epel_release="$($pkg_mgr list | grep 'epel-release' | awk '{print $1}' | cut -d. -f1 | head -1)" + try_find_epel_name() { + epel=$($pkg_mgr repolist --all | awk '{print $1}' | grep -Ei '(epel|epol)$') + } - # 如果已安装 - if rpm -q $epel_release; then - # 检查是否为最新 - if $pkg_mgr check-update $epel_release; then - $pkg_mgr reinstall -y $epel_release - else - $pkg_mgr update -y $epel_release - fi - else - # 如果未安装 + if is_need_epel; then + if ! try_find_epel_name; then + epel_release="$($pkg_mgr list | grep -E '(epel|epol)-release' | awk '{print $1}' | cut -d. -f1 | head -1)" $pkg_mgr install -y $epel_release + try_find_epel_name fi + enable_epel="--enablerepo=$epel" fi } @@ -1337,8 +1333,14 @@ install_pkg() { echo "Installing package '$text'..." case $pkg_mgr in - dnf) dnf install -y --setopt=install_weak_deps=False $pkg ;; - yum) yum install -y $pkg ;; + dnf) + check_is_need_epel + dnf install $enable_epel -y --setopt=install_weak_deps=False $pkg + ;; + yum) + check_is_need_epel + yum install $enable_epel -y $pkg + ;; emerge) emerge --oneshot $pkg ;; pacman) pacman -Syu --noconfirm --needed $pkg ;; zypper) zypper install -y $pkg ;; @@ -1381,9 +1383,6 @@ install_pkg() { error_and_exit "Can't find compatible package manager. Please manually install $cmd." fi cmd_to_pkg - if is_need_epel_repo; then - add_epel_repo - fi install_pkg_real fi done @@ -2002,7 +2001,7 @@ build_finalos_cmdline() { } build_extra_cmdline() { - for key in confhome hold cloud_image main_disk; do + for key in confhome hold force cloud_image main_disk; do value=${!key} if [ -n "$value" ]; then extra_cmdline+=" extra.$key='$value'" @@ -2695,7 +2694,7 @@ else fi # 整理参数 -if ! opts=$(getopt -n $0 -o "" --long ci,debug,hold:,sleep:,iso:,image-name:,img:,lang:,commit: -- "$@"); then +if ! opts=$(getopt -n $0 -o "" --long ci,debug,hold:,sleep:,iso:,image-name:,img:,lang:,commit:,force: -- "$@"); then usage_and_exit fi @@ -2722,6 +2721,13 @@ while true; do fi shift 2 ;; + --force) + force=$2 + if ! { [ "$force" = bios ] || [ "$force" = efi ]; }; then + error_and_exit "Invalid --force value: $force." + fi + shift 2 + ;; --img) img=$2 shift 2 @@ -3088,10 +3094,11 @@ if is_use_grub; then get_function_content load_grubenv_if_not_loaded >$target_cfg + # 原系统为 openeuler 云镜像,需要添加 --unrestricted,否则要输入密码 del_empty_lines <&2 } error_and_exit() { @@ -73,8 +74,16 @@ apk() { wget() { echo "$@" | grep -o 'http[^ ]*' >&2 for i in $(seq 5); do - command wget "$@" && return - sleep 1 + if command wget "$@"; then + return + else + ret=$? + # 错误,或者达到重试次数 + if [ $i -eq 5 ]; then + return $ret + fi + sleep 1 + fi done } @@ -182,7 +191,11 @@ update_part() { } is_efi() { - [ -d /sys/firmware/efi/ ] + if [ -n "$force" ]; then + [ "$force" = efi ] + else + [ -d /sys/firmware/efi/ ] + fi } is_use_cloud_image() { @@ -595,7 +608,6 @@ grep_efi_index() { add_fallback_efi_to_nvram() { apk add lsblk efibootmgr - EFI_UUID=C12A7328-F81F-11D2-BA4B-00A0C93EC93B efi_row=$(lsblk /dev/$xda -ro NAME,PARTTYPE,PARTUUID | grep -i "$EFI_UUID") efi_part_uuid=$(echo "$efi_row" | awk '{print $3}') efi_part_name=$(echo "$efi_row" | awk '{print $1}') @@ -3396,7 +3408,8 @@ install_windows() { else # 或者用 ms-sys apk add grub-bios - grub-install --boot-directory=/os/boot /dev/$xda + # efi 下,强制安装 mbr 引导,需要添加 --target i386-pc + grub-install --target i386-pc --boot-directory=/os/boot /dev/$xda cat </os/boot/grub/grub.cfg set timeout=5 menuentry "reinstall" {