From 306252349eea4e5ed6b041049f781778b89f2429 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Wed, 17 May 2023 22:54:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=87=8D=E8=A3=85=E5=88=B0?= =?UTF-8?q?=20debian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 54 ++++++++++++++++----- ks.cfg | 2 +- preseed.cfg | 69 +++++++++++++++++++++++++++ reinstall.sh | 132 ++++++++++++++++++++++++++++++++------------------- 4 files changed, 194 insertions(+), 63 deletions(-) create mode 100644 preseed.cfg diff --git a/README.md b/README.md index 9720543..2e2ee83 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,42 @@ -# reinstall -Usage: -``` -curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh -bash reinstall.sh centos-7/8/9 alma-8/9 rocky-8/9 fedora-36/37/38 ubuntu-20.04/22.04 alpine-3.16/3.17/3.18 -reboot -``` -Username: `root` - - -Password: `123@@@` - +# reinstall +一个一键重装脚本 + +#### 亮点: +``` +使用官方安装方式,非第三方 DD 镜像,更安全 +支持 BIOS/EFI 机器,支持 ARM 机器 +可能是第一个支持在 1g 内存上安装 红帽 7/8/9 系列的脚本 +可能是第一个支持重装到 ubuntu 22.04 的脚本 +可能是第一个支持重装到 alpine 的脚本 +``` +#### 使用: +``` +curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh +bash reinstall.sh centos-7 或其他系统 +reboot +``` +#### 支持重装到: +``` +centos-7/8/9 # centos 8/9 为 stream 版本 +alma-8/9 +rocky-8/9 +fedora-36/37/38 +ubuntu-20.04/22.04 +alpine-3.16/3.17/3.18 +debian-10/11 +``` +#### 内存要求: +``` +debian 384m +centos/alma/rocky/fedora 1g +alpine ? +ubuntu ? +``` +#### 网络要求: +``` +要求有 IPv4、DHCPv4 +``` +#### 默认用户名 / 密码: + ``` + root 123@@@ + ```` \ No newline at end of file diff --git a/ks.cfg b/ks.cfg index 030e252..d0420df 100644 --- a/ks.cfg +++ b/ks.cfg @@ -76,7 +76,7 @@ fi # 排除虚拟机用不上的组件 include=/tmp/exclude-packages-for-vm touch $include -if hostnamectl | grep 'Virtualization:'; then +if systemd-detect-virt -v; then cat <$include # 不删除usb相关的包 因为甲骨文云有usb设备 作用未知 # -usb_modeswitch diff --git a/preseed.cfg b/preseed.cfg new file mode 100644 index 0000000..b7b42cb --- /dev/null +++ b/preseed.cfg @@ -0,0 +1,69 @@ +# B.4.1. 本地化 +d-i debian-installer/locale string en_US +d-i keyboard-configuration/xkb-keymap select us + +# B.4.2. 网络设置 +d-i netcfg/get_hostname string unassigned-hostname +d-i netcfg/get_domain string unassigned-domain +d-i netcfg/hostname string localhost + +# B.4.3. 网络控制台 + +# B.4.4. 镜像设置 +d-i mirror/country string manual +d-i mirror/http/hostname string deb.debian.org +d-i mirror/http/directory string /debian/ + +# B.4.5. 帐号设置 +d-i passwd/make-user boolean false +d-i passwd/root-password password 123@@@ +d-i passwd/root-password-again password 123@@@ + +# B.4.6. 时钟与时区设置 +d-i clock-setup/utc boolean true +d-i time/zone string Asia/Shanghai +d-i clock-setup/ntp boolean true + +# B.4.7. 分区 +d-i partman-auto/method string regular +d-i partman-auto/choose_recipe select atomic +d-i partman-partitioning/confirm_write_new_label boolean true +d-i partman/choose_partition select finish +d-i partman/confirm boolean true +d-i partman/confirm_nooverwrite boolean true +d-i partman-basicfilesystems/no_swap boolean true + +# B.4.8. 基本系统安装 + +# B.4.9. 设置 apt +d-i apt-setup/non-free boolean true +d-i apt-setup/contrib boolean true +d-i apt-setup/enable-source-repositories boolean false +d-i apt-setup/services-select multiselect security, updates, backports +d-i apt-setup/security_host string security.debian.org + +# B.4.10. 选择软件包 +tasksel tasksel/first multiselect ssh-server +d-i pkgsel/upgrade select none + +# B.4.11. 安装 bootloader +d-i grub-installer/only_debian boolean true +d-i grub-installer/with_other_os boolean false +d-i grub2/force_efi_extra_removable boolean true + +# B.4.12. 完成安装 +d-i finish-install/reboot_in_progress note + +# B.4.13. 预置其他的软件包 + +# B.5.1. 安装过程中运行用户命令 +d-i preseed/early_command string \ + wget https://geoip.fedoraproject.org/city -O - | grep -w CHN && hostname=ftp.cn.debian.org || hostname=deb.debian.org; \ + debconf-set mirror/http/hostname $hostname + +d-i partman/early_command string \ + debconf-set partman-auto/disk "$(list-devices disk | head -n1)" + +d-i preseed/late_command string \ + echo "PermitRootLogin yes" >/target/etc/ssh/sshd_config.d/01-permitrootlogin.conf || \ + echo "PermitRootLogin yes" >/target/etc/ssh/sshd_config diff --git a/reinstall.sh b/reinstall.sh index 2746927..60719b4 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -3,7 +3,7 @@ confhome=https://raw.githubusercontent.com/bin456789/reinstall/main localtest_confhome=http://192.168.253.1 usage_and_exit() { - echo "Usage: reinstall.sh centos-7/8/9 alma-8/9 rocky-8/9 fedora-36/37/38 ubuntu-20.04/22.04 alpine-3.16/3.17/3.18" + echo "Usage: reinstall.sh centos-7/8/9 alma-8/9 rocky-8/9 fedora-36/37/38 ubuntu-20.04/22.04 alpine-3.16/3.17/3.18 debian-10/11" exit 1 } @@ -20,11 +20,8 @@ setos() { local step=$1 local distro=$2 local releasever=$3 - local ks=$4 setos_alpine() { - eval ${step}_distro=$distro - if [ "$localtest" = 1 ]; then mirror=$confhome/alpine-netboot-3.17.3-x86_64/boot eval ${step}_vmlinuz=$mirror/vmlinuz-lts @@ -45,10 +42,32 @@ setos() { fi } + setos_debian() { + if [ "$localtest" = 1 ]; then + mirror=$confhome/debian/install.amd + eval ${step}_vmlinuz=$mirror/vmlinuz + eval ${step}_initrd=$mirror/initrd.gz + else + case "$releasever" in + 12) codename=bookworm ;; + 11) codename=bullseye ;; + 10) codename=buster ;; + esac + if is_in_china; then + hostname=ftp.cn.debian.org + else + hostname=deb.debian.org + fi + mirror=http://$hostname/debian/dists/$codename/main/installer-$basearch_alt/current/images/netboot/debian-installer/$basearch_alt + eval ${step}_vmlinuz=$mirror/linux + eval ${step}_initrd=$mirror/initrd.gz + fi + eval ${step}_ks=$confhome/preseed.cfg + } + setos_ubuntu() { if [ "$localtest" = 1 ]; then mirror=$confhome/ - eval ${step}_ks=$confhome/$ks else if is_in_china; then case "$basearch" in @@ -61,23 +80,16 @@ setos() { "aarch64") mirror=https://cdimage.ubuntu.com/releases/$releasever/release/ ;; esac fi - eval ${step}_ks=$confhome/user-data fi - case "$basearch" in - "x86_64") arch=amd64 ;; - "aarch64") arch=arm64 ;; - esac - - filename=$(curl $mirror | grep -oP "ubuntu-$releasever.*?-live-server-$arch.iso" | head -1) + filename=$(curl $mirror | grep -oP "ubuntu-$releasever.*?-live-server-$basearch_alt.iso" | head -1) eval ${step}_iso=$mirror$filename - eval ${step}_distro=ubuntu + eval ${step}_ks=$confhome/user-data } setos_redhat() { if [ "$localtest" = 1 ]; then mirror=$confhome/$releasever/ - eval ${step}_ks=$confhome/$ks else case $distro in "centos") @@ -95,8 +107,7 @@ setos() { mirror=$(curl -L $mirrorlist | sed "/^#/d" | sed "/anigil/d" | head -1 | sed "s,\$basearch,$basearch,") eval "${step}_mirrorlist='${mirrorlist}'" fi - eval ${step}_ks=$confhome/$ks - eval ${step}_distro=${distro} + eval ${step}_ks=$confhome/ks.cfg eval ${step}_vmlinuz=${mirror}images/pxeboot/vmlinuz eval ${step}_initrd=${mirror}images/pxeboot/initrd.img eval ${step}_squashfs=${mirror}images/install.img @@ -106,21 +117,26 @@ setos() { fi } + eval ${step}_distro=$distro case "$distro" in ubuntu) setos_ubuntu ;; alpine) setos_alpine ;; + debian) setos_debian ;; *) setos_redhat ;; esac } # 检查是否为正确的系统名 verify_os_string() { - for os in 'centos-7|8|9' 'alma|rocky-8|9' 'fedora-36|37|38' 'ubuntu-20.04|22.04' 'alpine-3.16|3.17|3.18'; do + for os in 'centos-7|8|9' 'alma|rocky-8|9' 'fedora-36|37|38' 'ubuntu-20.04|22.04' 'alpine-3.16|3.17|3.18' 'debian-10|11|12'; do ds=$(echo $os | cut -d- -f1) vers=$(echo $os | cut -d- -f2 | sed 's \. \\\. g') finalos=$(echo "$@" | tr '[:upper:]' '[:lower:]' | sed -n -E "s,^($ds)[ :-]?($vers)$,\1:\2,p") if [ -n "$finalos" ]; then distro=$(echo $finalos | cut -d: -f1) + if [ "$distro" = centos ] || [ "$distro" = alma ] || [ "$distro" = rocky ]; then + distro_like=redhat + fi releasever=$(echo $finalos | cut -d: -f2) return fi @@ -138,6 +154,8 @@ apt_install() { install_pkg() { pkgs=$* for pkg in $pkgs; do + # util-linux 用 lsmem 命令测试 + [ "$pkg" = util-linux ] && pkg=lsmem if ! command -v $pkg; then { apt_install $pkgs || @@ -152,6 +170,28 @@ install_pkg() { done } +check_ram() { + # lsmem最准确但centos7 arm 和alpine不能用 + # arm 24g dmidecode 显示少了128m + install_pkg util-linux + ram_size=$(lsmem -b 2>/dev/null | grep 'Total online memory:' | awk '{ print $NF/1024/1024 }') + if [ -z $ram_size ]; then + install_pkg dmidecode + ram_size=$(dmidecode -t 17 | grep "Size.*[GM]B" | awk '{if ($3=="GB") s+=$2*1024; else s+=$2} END {print s}') + fi + + case "$distro" in + alpine) ram_requirement=0 ;; # 未测试 + debian) ram_requirement=384 ;; + *) ram_requirement=1024 ;; + esac + + if [ $ram_size -lt $ram_requirement ]; then + echo "Could not install $distro: RAM < $ram_requirement MB." + exit 1 + fi +} + # 脚本入口 if [ "$EUID" -ne 0 ]; then echo "Please run as root." @@ -190,38 +230,27 @@ if [ -f /etc/alpine-release ]; then apk add grep fi -# 获取内存大小,lsmem最准确但centos7 arm 和alpine不能用 -# arm 24g dmidecode 显示少了128m -if [ $distro != "alpine" ]; then - install_pkg util-linux - ram_size=$(lsmem -b 2>/dev/null | grep 'Total online memory:' | awk '{ print $NF/1024/1024 }') - if [ -z $ram_size ]; then - install_pkg dmidecode - ram_size=$(dmidecode -t 17 | grep "Size.*[GM]B" | awk '{if ($3=="GB") s+=$2*1024; else s+=$2} END {print s}') - fi - - if [ $ram_size -lt 1024 ]; then - echo 'RAM < 1G. Unsupported.' - exit 1 - fi -fi - +check_ram basearch=$(uname -m) +case "$basearch" in +"x86_64") basearch_alt=amd64 ;; +"aarch64") basearch_alt=arm64 ;; +esac + # 以下目标系统需要进入alpine环境安装 # ubuntu/alpine # el8/9/fedora 任何架构 <2g # el7 aarch64 <1.5g -if [ $distro = "ubuntu" ] || - [ $distro = "alpine" ] || - { [ $releasever -ge 8 ] && [ $ram_size -lt 2048 ]; } || - { [ $releasever -eq 7 ] && [ $ram_size -lt 1536 ] && [ $basearch = "aarch64" ]; }; then - [ $distro = "ubuntu" ] && ks=user-data || ks=ks.cfg +if [ "$distro" = "ubuntu" ] || + [ "$distro" = "alpine" ] || + { [ "$distro_like" = "redhat" ] && [ $releasever -ge 8 ] && [ $ram_size -lt 2048 ]; } || + { [ "$distro_like" = "redhat" ] && [ $releasever -eq 7 ] && [ $ram_size -lt 1536 ] && [ $basearch = "aarch64" ]; }; then # 安装alpine时,使用指定的版本。 alpine作为中间系统时,使用 3.18 - [ $distro = "alpine" ] && alpine_releasever=$releasever || alpine_releasever=3.18 - setos finalos $distro $releasever $ks + [ "$distro" = "alpine" ] && alpine_releasever=$releasever || alpine_releasever=3.18 + setos finalos $distro $releasever setos nextos alpine $alpine_releasever else - setos nextos $distro $releasever ks.cfg + setos nextos $distro $releasever fi # 下载启动内核 @@ -229,11 +258,10 @@ fi { cd / echo $nextos_vmlinuz - curl -Lo vmlinuz $nextos_vmlinuz + curl -Lo reinstall-vmlinuz $nextos_vmlinuz echo $nextos_initrd - curl -Lo initrd $nextos_initrd - touch reinstall.mark + curl -Lo reinstall-initrd $nextos_initrd } # 转换 finalos_a=1 为 finalos.a=1 ,排除 finalos_mirrorlist @@ -289,7 +317,7 @@ if [ -n "$finalos_cmdline" ]; then rm -rf $tmp_dir mkdir -p $tmp_dir cd $tmp_dir - zcat /initrd | cpio -idm + zcat /reinstall-initrd | cpio -idm # hack # exec /bin/busybox switch_root $switch_root_opts $sysroot $chart_init "$KOPT_init" $KOPT_init_args # 3.17 @@ -309,7 +337,7 @@ EOF # -c Identical to "-H newc", use the new (SVR4) # portable format.If you wish the old portable # (ASCII) archive format, use "-H odc" instead. - find . | cpio -o -H newc | gzip -1 >/initrd + find . | cpio -o -H newc | gzip -1 >/reinstall-initrd # 删除临时文件 cd / @@ -320,7 +348,11 @@ EOF # apkovl=sda2:ext4:/apkovl.tar.gz 官方有写但不生效 cmdline="alpine_repo=$nextos_repo modloop=$nextos_modloop $extra_cmdline $finalos_cmdline " else - cmdline="root=live:$nextos_squashfs inst.ks=$nextos_ks $extra_cmdline" + if [ $distro = debian ]; then + cmdline="lowmem=+1 lowmem/low=1 auto=true priority=critical url=$nextos_ks" + else + cmdline="root=live:$nextos_squashfs inst.ks=$nextos_ks $extra_cmdline" + fi fi custom_cfg=$grub_cfg_dir/custom.cfg @@ -329,9 +361,9 @@ cat <