core: 优化代码

This commit is contained in:
bin456789 2024-03-05 23:01:24 +08:00
parent d59668b663
commit bec14c13c1
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B
9 changed files with 80 additions and 33 deletions

View File

@ -62,7 +62,8 @@ All features can be used on both Linux and Windows.
### Feature 1: Install Linux ### 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
bash reinstall.sh centos 7|8|9 (8|9 for the stream version) bash reinstall.sh centos 7|8|9 (8|9 for the stream version)

View File

@ -62,7 +62,8 @@ certutil -urlcache -f -split https://raw.gitmirror.com/bin456789/reinstall/main/
### 功能 1: 安装 Linux ### 功能 1: 安装 Linux
- 静态 IP 的机器安装 centos、alma、rocky、fedora、debian、ubuntu需添加参数 --ci 强制使用云镜像 - 静态 IP 的机器安装 CentOS、Alma、Rocky、Fedora、Debian、Ubuntu必须添加参数 `--ci`
- 如果不清楚机器是静态 IP 还是动态 IP也可添加参数 `--ci`,增加安装成功率
```bash ```bash
bash reinstall.sh centos 7|8|9 (8|9 为 stream 版本) bash reinstall.sh centos 7|8|9 (8|9 为 stream 版本)

View File

@ -121,11 +121,11 @@ test_internet() {
echo 'Testing Internet Connection...' echo 'Testing Internet Connection...'
for i in $(seq 5); do 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." echo "IPv4 has internet."
ipv4_has_internet=true ipv4_has_internet=true
fi 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." echo "IPv6 has internet."
ipv6_has_internet=true ipv6_has_internet=true
fi fi
@ -136,14 +136,19 @@ test_internet() {
done done
} }
flush_ipv4_config() {
ip -4 addr flush scope global dev "$ethx"
ip -4 route flush dev "$ethx"
}
test_internet test_internet
# 处理云电脑 dhcp 获取的地址无法上网 # 处理云电脑 dhcp 获取的地址无法上网
if $dhcpv4 && ! $ipv4_has_internet && if $dhcpv4 && ! $ipv4_has_internet &&
[ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ] &&
! [ "$ipv4_addr" = "$(get_first_ipv4_addr)" ]; then ! [ "$ipv4_addr" = "$(get_first_ipv4_addr)" ]; then
echo "DHCPv4 can't access Internet. And not match static IPv4." echo "DHCPv4 can't access Internet. And not match static IPv4."
ip -4 addr flush scope global dev "$ethx" flush_ipv4_config
ip -4 route flush dev "$ethx"
add_missing_ipv4_config add_missing_ipv4_config
test_internet test_internet
if $ipv4_has_internet; then if $ipv4_has_internet; then

View File

@ -18,7 +18,7 @@ chpasswd:
runcmd: runcmd:
# opensuse tumbleweed 有 /etc/ssh/sshd_config.d/ 文件夹,没有 /etc/ssh/sshd_config有/usr/etc/ssh/sshd_config # 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 - 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 - touch /etc/cloud/cloud-init.disabled
# ubuntu 镜像运行 echo -e '\nDone' -e 会被显示出来 # ubuntu 镜像运行 echo -e '\nDone' -e 会被显示出来
- printf '\n%s\n' 'reinstall done' >/dev/tty0 || true - printf '\n%s\n' 'reinstall done' >/dev/tty0 || true

View File

@ -74,10 +74,10 @@ d-i partman/early_command string \
confhome="$(grep -o 'extra\.confhome=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ confhome="$(grep -o 'extra\.confhome=[^ ]*' /proc/cmdline | cut -d= -f2)"; \
anna-install fdisk-udeb; \ 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"; \ 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"; \ debconf-set debian-installer/add-kernel-opts "$ttys"; \
kernel="$(grep -o 'extra\.kernel=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ kernel="$(grep -o 'extra\.kernel=[^ ]*' /proc/cmdline | cut -d= -f2)"; \

View File

@ -48,13 +48,13 @@ done
# 只使用主硬盘 # 只使用主硬盘
include=/tmp/include-disk-only-use 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 echo "ignoredisk --only-use=$xda" >$include
# 设置 tty # 设置 tty
include=/tmp/include-bootloader include=/tmp/include-bootloader
# shellcheck disable=SC2154 # 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 echo "bootloader --append=\"$console_cmdline\"" >$include
# 有 installer 分区,表示用了两步安装 # 有 installer 分区,表示用了两步安装

View File

@ -35,7 +35,7 @@ Usage: reinstall.sh centos 7|8|9
windows --iso=http://xxx --image-name='windows xxx' windows --iso=http://xxx --image-name='windows xxx'
netboot.xyz netboot.xyz
Homepage: https://github.com/bin456789/reinstall Manual: https://github.com/bin456789/reinstall
EOF EOF
exit 1 exit 1
} }
@ -68,8 +68,20 @@ error_and_exit() {
curl() { curl() {
# 添加 -f, --fail不然 404 退出码也为0 # 添加 -f, --fail不然 404 退出码也为0
# 32位 cygwin 已停止更新,证书可能有问题,先添加 --insecure # 32位 cygwin 已停止更新,证书可能有问题,先添加 --insecure
# centos 7 curl 不支持 --retry-connrefused --retry-all-errors
# 因此手动 retry
grep -o 'http[^ ]*' <<<"$@" >&2 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() { is_in_china() {
@ -243,11 +255,11 @@ assert_not_in_container() {
is_in_windows && return is_in_windows && return
if is_have_cmd systemd-detect-virt; then 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 _error_and_exit
fi fi
else 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 _error_and_exit
fi fi
fi fi
@ -447,7 +459,7 @@ setos() {
if is_in_china; then if is_in_china; then
ci_mirror=https://mirrors.tuna.tsinghua.edu.cn/gentoo ci_mirror=https://mirrors.tuna.tsinghua.edu.cn/gentoo
else else
ci_mirror=https://distfiles.gentoo.org ci_mirror=https://mirror.leaseweb.com/gentoo
fi fi
if [ "$basearch_alt" = arm64 ]; then if [ "$basearch_alt" = arm64 ]; then
@ -724,7 +736,11 @@ install_pkg() {
} }
install_pkg_real() { 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 case $pkg_mgr in
dnf) dnf install -y --setopt=install_weak_deps=False $pkg ;; dnf) dnf install -y --setopt=install_weak_deps=False $pkg ;;
yum) yum install -y $pkg ;; yum) yum install -y $pkg ;;
@ -873,7 +889,7 @@ del_cr() {
sed 's/\r//g' sed 's/\r//g'
} }
del_blank_lines() { del_empty_lines() {
sed '/^[[:space:]]*$/d' sed '/^[[:space:]]*$/d'
} }
@ -983,7 +999,7 @@ collect_netconf() {
done done
# IPv6 # 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 for ((i = 0; i < ${#ips[@]}; i++)); do
ip=${ips[i]} ip=${ips[i]}
cidr=${subnets[i]} cidr=${subnets[i]}
@ -1550,9 +1566,13 @@ if is_secure_boot_enabled; then
error_and_exit "Not Supported with secure boot enabled." error_and_exit "Not Supported with secure boot enabled."
fi fi
# win系统盘
if is_in_windows; then if is_in_windows; then
# win系统盘
c=$(echo $SYSTEMDRIVE | cut -c1) c=$(echo $SYSTEMDRIVE | cut -c1)
# 更改 windows 命令输出语言为英文
# chcp.com 437 # 会清屏
mode.com con cp select=437 >/dev/null
fi fi
# 必备组件 # 必备组件
@ -1563,9 +1583,12 @@ if ! { [ "$distro" = dd ] || [ "$distro" = windows ] || [ "$distro" = netboot.xy
check_ram check_ram
fi fi
# alpine --ci 参数无效 # 以下系统忽略 --ci 参数
if [ "$distro" = alpine ] && is_use_cloud_image; then if is_use_cloud_image && {
error_and_exit "can't install alpine with cloud image" [ "$distro" = dd ] || [ "$distro" = windows ] || [ "$distro" = netboot.xyz ] || [ "$distro" = alpine ]
}; then
echo "ignored --ci"
cloud_image=0
fi fi
# 检查硬件架构 # 检查硬件架构
@ -1809,7 +1832,7 @@ if is_use_grub; then
# 生成 grub 配置 # 生成 grub 配置
# 实测 centos 7 lvm 要手动加载 lvm 模块 # 实测 centos 7 lvm 要手动加载 lvm 模块
echo $target_cfg echo $target_cfg
cat <<EOF | del_blank_lines | tee $target_cfg cat <<EOF | del_empty_lines | tee $target_cfg
set timeout=5 set timeout=5
menuentry "$(get_entry_name)" { menuentry "$(get_entry_name)" {
$(! is_in_windows && echo 'insmod lvm') $(! is_in_windows && echo 'insmod lvm')

View File

@ -19,7 +19,6 @@ update_part() {
# ubuntu grownpart # ubuntu grownpart
# 找出主硬盘 # 找出主硬盘
# shellcheck disable=SC2010
root_drive=$(mount | awk '$3=="/" {print $1}') root_drive=$(mount | awk '$3=="/" {print $1}')
xda=$(lsblk -r --inverse "$root_drive" | grep -w disk | awk '{print $1}') xda=$(lsblk -r --inverse "$root_drive" | grep -w disk | awk '{print $1}')

View File

@ -53,10 +53,20 @@ add_community_repo() {
fi fi
} }
# 有时网络问题下载失败,导致脚本中断
# 因此需要重试
apk() {
for i in $(seq 5); do
command apk "$@" && return
sleep 1
done
}
# busybox 的 wget 没有重试功能 # busybox 的 wget 没有重试功能
wget() { wget() {
for i in $(seq 5); do for i in $(seq 5); do
command wget "$@" && return command wget "$@" && return
sleep 1
done done
} }
@ -116,6 +126,7 @@ download() {
--user-agent=Wget/1.21.1 \ --user-agent=Wget/1.21.1 \
--max-tries 1 \ --max-tries 1 \
$save $url && return $save $url && return
sleep 1
done done
} }
@ -1347,7 +1358,7 @@ EOF
# el 全系也用 NetworkManager但他们的配置文件是 sysconfig因此不受影响 # el 全系也用 NetworkManager但他们的配置文件是 sysconfig因此不受影响
# https://github.com/canonical/cloud-init/commit/5d440856cb6d2b4c908015fe4eb7227615c17c8b # https://github.com/canonical/cloud-init/commit/5d440856cb6d2b4c908015fe4eb7227615c17c8b
if grep -E 'fedora:38' $os_dir/etc/os-release; then if grep -E 'fedora:38' $os_dir/etc/os-release; then
network_manager_py=$os_dir/usr/lib/python3.11/site-packages/cloudinit/net/network_manager.py && network_manager_py=$os_dir/usr/lib/python3.11/site-packages/cloudinit/net/network_manager.py
if ! grep '"static6": "manual",' $network_manager_py; then if ! grep '"static6": "manual",' $network_manager_py; then
echo '"static6": "manual",' | insert_into_file $network_manager_py after '"static": "manual",' echo '"static6": "manual",' | insert_into_file $network_manager_py after '"static": "manual",'
fi fi
@ -1854,15 +1865,17 @@ dd_qcow() {
} }
fix_partition_table_by_parted() {
parted /dev/$xda -f -s print
}
resize_after_install_cloud_image() { resize_after_install_cloud_image() {
# 提前扩容 # 提前扩容
# 1 修复 vultr 512m debian 10/11 generic/genericcloud 首次启动 kernel panic # 1 修复 vultr 512m debian 10/11 generic/genericcloud 首次启动 kernel panic
# 2 修复 gentoo websync 时空间不足 # 2 修复 gentoo websync 时空间不足
if [ "$distro" = debian ] || [ "$distro" = gentoo ]; then if [ "$distro" = debian ] || [ "$distro" = gentoo ]; then
apk add parted apk add parted
if parted /dev/$xda -s print 2>&1 | grep 'Not all of the space'; then if fix_partition_table_by_parted 2>&1 | grep -q 'Fixing'; then
printf "fix" | parted /dev/$xda print ---pretend-input-tty
system_part_num=$(parted /dev/$xda -m print | tail -1 | cut -d: -f1) 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 printf "yes" | parted /dev/$xda resizepart $system_part_num 100% ---pretend-input-tty
update_part /dev/$xda update_part /dev/$xda
@ -2512,7 +2525,11 @@ clear_previous
add_community_repo add_community_repo
# 需要在重新分区之前,找到主硬盘 # 需要在重新分区之前,找到主硬盘
find_xda # 重新运行脚本时,可指定 xda
# xda=sda ash trans.start
if [ -z "$xda" ]; then
find_xda
fi
if [ "$distro" != "alpine" ]; then if [ "$distro" != "alpine" ]; then
setup_nginx_if_enough_ram setup_nginx_if_enough_ram
@ -2565,6 +2582,7 @@ if is_efi && [ "$distro" != "alpine" ]; then
add_fallback_efi_to_nvram add_fallback_efi_to_nvram
fi fi
sync
echo 'done' echo 'done'
if [ "$hold" = 2 ]; then if [ "$hold" = 2 ]; then
exit exit