From bec14c13c183cbeb2f920c59f540de41a16ff590 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Tue, 5 Mar 2024 23:01:24 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 3 ++- README.md | 3 ++- alpine-network.sh | 13 +++++++++---- cloud-init.yaml | 2 +- debian.cfg | 4 ++-- redhat.cfg | 4 ++-- reinstall.sh | 49 ++++++++++++++++++++++++++++++++++------------- resize.sh | 1 - trans.sh | 34 ++++++++++++++++++++++++-------- 9 files changed, 80 insertions(+), 33 deletions(-) diff --git a/README.en.md b/README.en.md index d361131..88fb643 100644 --- a/README.en.md +++ b/README.en.md @@ -62,7 +62,8 @@ All features can be used on both Linux and Windows. ### Feature 1: Install Linux -- For machines with static IP, install CentOS, Alma, Rocky, Fedora, Debian, Ubuntu, and add the --ci parameter to force the use of the cloud image. +- Machines with static IP installing CentOS, Alma, Rocky, Fedora, Debian, Ubuntu must include the parameter `--ci`. +- If it is uncertain whether the machine has a static or dynamic IP, you can also add the parameter `--ci` to increase the installation success rate. ```bash bash reinstall.sh centos 7|8|9 (8|9 for the stream version) diff --git a/README.md b/README.md index 4718ba9..0c85a1c 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,8 @@ certutil -urlcache -f -split https://raw.gitmirror.com/bin456789/reinstall/main/ ### 功能 1: 安装 Linux -- 静态 IP 的机器安装 centos、alma、rocky、fedora、debian、ubuntu,需添加参数 --ci 强制使用云镜像 +- 静态 IP 的机器安装 CentOS、Alma、Rocky、Fedora、Debian、Ubuntu,必须添加参数 `--ci` +- 如果不清楚机器是静态 IP 还是动态 IP,也可添加参数 `--ci`,增加安装成功率 ```bash bash reinstall.sh centos 7|8|9 (8|9 为 stream 版本) diff --git a/alpine-network.sh b/alpine-network.sh index f1df98b..74a05e9 100644 --- a/alpine-network.sh +++ b/alpine-network.sh @@ -121,11 +121,11 @@ test_internet() { echo 'Testing Internet Connection...' for i in $(seq 5); do - if is_need_test_ipv4 && nslookup www.qq.com $ipv4_dns1 2>/dev/null; then + if is_need_test_ipv4 && nslookup www.qq.com $ipv4_dns1 >/dev/null 2>&1; then echo "IPv4 has internet." ipv4_has_internet=true fi - if is_need_test_ipv6 && nslookup www.qq.com $ipv6_dns1 2>/dev/null; then + if is_need_test_ipv6 && nslookup www.qq.com $ipv6_dns1 >/dev/null 2>&1; then echo "IPv6 has internet." ipv6_has_internet=true fi @@ -136,14 +136,19 @@ test_internet() { done } +flush_ipv4_config() { + ip -4 addr flush scope global dev "$ethx" + ip -4 route flush dev "$ethx" +} + test_internet # 处理云电脑 dhcp 获取的地址无法上网 if $dhcpv4 && ! $ipv4_has_internet && + [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ] && ! [ "$ipv4_addr" = "$(get_first_ipv4_addr)" ]; then echo "DHCPv4 can't access Internet. And not match static IPv4." - ip -4 addr flush scope global dev "$ethx" - ip -4 route flush dev "$ethx" + flush_ipv4_config add_missing_ipv4_config test_internet if $ipv4_has_internet; then diff --git a/cloud-init.yaml b/cloud-init.yaml index 5462d65..e8b43ce 100644 --- a/cloud-init.yaml +++ b/cloud-init.yaml @@ -18,7 +18,7 @@ chpasswd: runcmd: # opensuse tumbleweed 有 /etc/ssh/sshd_config.d/ 文件夹,没有 /etc/ssh/sshd_config,有/usr/etc/ssh/sshd_config - grep 'Include.*/etc/ssh/sshd_config.d' /etc/ssh/sshd_config && mkdir -p /etc/ssh/sshd_config.d && echo "PermitRootLogin yes" >/etc/ssh/sshd_config.d/01-permitrootlogin.conf || echo "PermitRootLogin yes" >>/etc/ssh/sshd_config - - systemctl restart sshd + - systemctl restart sshd || systemctl restart ssh - touch /etc/cloud/cloud-init.disabled # ubuntu 镜像运行 echo -e '\nDone' ,-e 会被显示出来 - printf '\n%s\n' 'reinstall done' >/dev/tty0 || true diff --git a/debian.cfg b/debian.cfg index 78ff546..9014f0c 100644 --- a/debian.cfg +++ b/debian.cfg @@ -74,10 +74,10 @@ d-i partman/early_command string \ confhome="$(grep -o 'extra\.confhome=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ anna-install fdisk-udeb; \ - xda=$(wget $confhome/get-xda.sh -O- | sh -s); \ + xda=$(wget --tries=5 $confhome/get-xda.sh -O- | sh -s); \ debconf-set partman-auto/disk "/dev/$xda"; \ - ttys=$(wget $confhome/ttys.sh -O- | sh -s console=); \ + ttys=$(wget --tries=5 $confhome/ttys.sh -O- | sh -s console=); \ debconf-set debian-installer/add-kernel-opts "$ttys"; \ kernel="$(grep -o 'extra\.kernel=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ diff --git a/redhat.cfg b/redhat.cfg index 626a676..9a50590 100644 --- a/redhat.cfg +++ b/redhat.cfg @@ -48,13 +48,13 @@ done # 只使用主硬盘 include=/tmp/include-disk-only-use -xda=$(wget --no-check-certificate "$confhome/get-xda.sh" -O- | sh -s) +xda=$(wget --no-check-certificate --tries=5 "$confhome/get-xda.sh" -O- | sh -s) echo "ignoredisk --only-use=$xda" >$include # 设置 tty include=/tmp/include-bootloader # shellcheck disable=SC2154 -console_cmdline=$(wget --no-check-certificate "$confhome/ttys.sh" -O- | sh -s console=) +console_cmdline=$(wget --no-check-certificate --tries=5 "$confhome/ttys.sh" -O- | sh -s console=) echo "bootloader --append=\"$console_cmdline\"" >$include # 有 installer 分区,表示用了两步安装 diff --git a/reinstall.sh b/reinstall.sh index d0ba228..91ae0e4 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -35,7 +35,7 @@ Usage: reinstall.sh centos 7|8|9 windows --iso=http://xxx --image-name='windows xxx' netboot.xyz -Homepage: https://github.com/bin456789/reinstall +Manual: https://github.com/bin456789/reinstall EOF exit 1 } @@ -68,8 +68,20 @@ error_and_exit() { curl() { # 添加 -f, --fail,不然 404 退出码也为0 # 32位 cygwin 已停止更新,证书可能有问题,先添加 --insecure + # centos 7 curl 不支持 --retry-connrefused --retry-all-errors + # 因此手动 retry grep -o 'http[^ ]*' <<<"$@" >&2 - command curl --insecure --connect-timeout 10 --retry 5 --retry-delay 1 -f "$@" + for i in $(seq 5); do + if command curl --insecure --connect-timeout 10 -f "$@"; then + return + else + ret=$? + if [ $ret -eq 22 ]; then + # 404 错误 + return $ret + fi + fi + done } is_in_china() { @@ -243,11 +255,11 @@ assert_not_in_container() { is_in_windows && return if is_have_cmd systemd-detect-virt; then - if systemd-detect-virt -c >/dev/null; then + if systemd-detect-virt -qc; then _error_and_exit fi else - if [ -d /proc/vz ] || grep container=lxc /proc/1/environ; then + if [ -d /proc/vz ] || grep -q container=lxc /proc/1/environ; then _error_and_exit fi fi @@ -447,7 +459,7 @@ setos() { if is_in_china; then ci_mirror=https://mirrors.tuna.tsinghua.edu.cn/gentoo else - ci_mirror=https://distfiles.gentoo.org + ci_mirror=https://mirror.leaseweb.com/gentoo fi if [ "$basearch_alt" = arm64 ]; then @@ -724,7 +736,11 @@ install_pkg() { } install_pkg_real() { - echo "Installing package '$pkg' for command '$cmd'..." + text="$pkg" + if [ "$pkg" != "$cmd" ]; then + text+=" ($cmd)" + fi + echo "Installing package '$text'..." case $pkg_mgr in dnf) dnf install -y --setopt=install_weak_deps=False $pkg ;; yum) yum install -y $pkg ;; @@ -873,7 +889,7 @@ del_cr() { sed 's/\r//g' } -del_blank_lines() { +del_empty_lines() { sed '/^[[:space:]]*$/d' } @@ -983,7 +999,7 @@ collect_netconf() { done # IPv6 - ipv6_type_list=$(cmd /c "chcp 437 & netsh interface ipv6 show address $id normal") + ipv6_type_list=$(netsh interface ipv6 show address $id normal) for ((i = 0; i < ${#ips[@]}; i++)); do ip=${ips[i]} cidr=${subnets[i]} @@ -1550,9 +1566,13 @@ if is_secure_boot_enabled; then error_and_exit "Not Supported with secure boot enabled." fi -# win系统盘 if is_in_windows; then + # win系统盘 c=$(echo $SYSTEMDRIVE | cut -c1) + + # 更改 windows 命令输出语言为英文 + # chcp.com 437 # 会清屏 + mode.com con cp select=437 >/dev/null fi # 必备组件 @@ -1563,9 +1583,12 @@ if ! { [ "$distro" = dd ] || [ "$distro" = windows ] || [ "$distro" = netboot.xy check_ram fi -# alpine --ci 参数无效 -if [ "$distro" = alpine ] && is_use_cloud_image; then - error_and_exit "can't install alpine with cloud image" +# 以下系统忽略 --ci 参数 +if is_use_cloud_image && { + [ "$distro" = dd ] || [ "$distro" = windows ] || [ "$distro" = netboot.xyz ] || [ "$distro" = alpine ] +}; then + echo "ignored --ci" + cloud_image=0 fi # 检查硬件架构 @@ -1809,7 +1832,7 @@ if is_use_grub; then # 生成 grub 配置 # 实测 centos 7 lvm 要手动加载 lvm 模块 echo $target_cfg - cat <&1 | grep 'Not all of the space'; then - printf "fix" | parted /dev/$xda print ---pretend-input-tty - + if fix_partition_table_by_parted 2>&1 | grep -q 'Fixing'; then system_part_num=$(parted /dev/$xda -m print | tail -1 | cut -d: -f1) printf "yes" | parted /dev/$xda resizepart $system_part_num 100% ---pretend-input-tty update_part /dev/$xda @@ -2512,7 +2525,11 @@ clear_previous add_community_repo # 需要在重新分区之前,找到主硬盘 -find_xda +# 重新运行脚本时,可指定 xda +# xda=sda ash trans.start +if [ -z "$xda" ]; then + find_xda +fi if [ "$distro" != "alpine" ]; then setup_nginx_if_enough_ram @@ -2565,6 +2582,7 @@ if is_efi && [ "$distro" != "alpine" ]; then add_fallback_efi_to_nvram fi +sync echo 'done' if [ "$hold" = 2 ]; then exit