mirror of
https://github.com/bin456789/reinstall.git
synced 2025-01-18 20:39:14 +08:00
core: 优化代码
This commit is contained in:
parent
d59668b663
commit
bec14c13c1
@ -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)
|
||||
|
@ -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 版本)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)"; \
|
||||
|
@ -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 分区,表示用了两步安装
|
||||
|
49
reinstall.sh
49
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 <<EOF | del_blank_lines | tee $target_cfg
|
||||
cat <<EOF | del_empty_lines | tee $target_cfg
|
||||
set timeout=5
|
||||
menuentry "$(get_entry_name)" {
|
||||
$(! is_in_windows && echo 'insmod lvm')
|
||||
|
@ -19,7 +19,6 @@ update_part() {
|
||||
# ubuntu grownpart
|
||||
|
||||
# 找出主硬盘
|
||||
# shellcheck disable=SC2010
|
||||
root_drive=$(mount | awk '$3=="/" {print $1}')
|
||||
xda=$(lsblk -r --inverse "$root_drive" | grep -w disk | awk '{print $1}')
|
||||
|
||||
|
26
trans.sh
26
trans.sh
@ -53,10 +53,20 @@ add_community_repo() {
|
||||
fi
|
||||
}
|
||||
|
||||
# 有时网络问题下载失败,导致脚本中断
|
||||
# 因此需要重试
|
||||
apk() {
|
||||
for i in $(seq 5); do
|
||||
command apk "$@" && return
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
# busybox 的 wget 没有重试功能
|
||||
wget() {
|
||||
for i in $(seq 5); do
|
||||
command wget "$@" && return
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
@ -116,6 +126,7 @@ download() {
|
||||
--user-agent=Wget/1.21.1 \
|
||||
--max-tries 1 \
|
||||
$save $url && return
|
||||
sleep 1
|
||||
done
|
||||
}
|
||||
|
||||
@ -1347,7 +1358,7 @@ EOF
|
||||
# el 全系也用 NetworkManager,但他们的配置文件是 sysconfig,因此不受影响
|
||||
# https://github.com/canonical/cloud-init/commit/5d440856cb6d2b4c908015fe4eb7227615c17c8b
|
||||
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
|
||||
echo '"static6": "manual",' | insert_into_file $network_manager_py after '"static": "manual",'
|
||||
fi
|
||||
@ -1854,15 +1865,17 @@ dd_qcow() {
|
||||
|
||||
}
|
||||
|
||||
fix_partition_table_by_parted() {
|
||||
parted /dev/$xda -f -s print
|
||||
}
|
||||
|
||||
resize_after_install_cloud_image() {
|
||||
# 提前扩容
|
||||
# 1 修复 vultr 512m debian 10/11 generic/genericcloud 首次启动 kernel panic
|
||||
# 2 修复 gentoo websync 时空间不足
|
||||
if [ "$distro" = debian ] || [ "$distro" = gentoo ]; then
|
||||
apk add parted
|
||||
if parted /dev/$xda -s print 2>&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
|
||||
|
||||
# 需要在重新分区之前,找到主硬盘
|
||||
# 重新运行脚本时,可指定 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user