From f037751cf51b506b0088c2219908bafe2c2dae36 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Wed, 3 Apr 2024 21:53:52 +0800 Subject: [PATCH] =?UTF-8?q?debian:=20=E5=B0=9D=E8=AF=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20256M=20=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian.cfg | 10 +- reinstall.bat | 2 +- reinstall.sh | 265 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 261 insertions(+), 16 deletions(-) diff --git a/debian.cfg b/debian.cfg index 5dcd4e2..c4e5c94 100644 --- a/debian.cfg +++ b/debian.cfg @@ -3,6 +3,9 @@ # https://www.debian.org/releases/stable/example-preseed.txt # https://preseed.debian.net/debian-preseed/bookworm/amd64-main-full.txt + # 下面这行语句无效,因为本行后面有反斜杠,前面有空格(安装器认为不算注释)\ +d-i debian-installer/locale string en_US + # B.4.1. 本地化 d-i debian-installer/locale string en_US d-i keyboard-configuration/xkb-keymap select us @@ -104,7 +107,6 @@ d-i partman/early_command string \ confhome="$(grep -o 'extra\.confhome=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ swapfile=/target/swapfile; \ postinst=/var/lib/dpkg/info/bootstrap-base.postinst; \ - anna-install fdisk-udeb; \ cp $postinst $postinst.orig; \ echo "fallocate -l 384M $swapfile; mkswap $swapfile; swapon $swapfile; $postinst.orig" >$postinst; \ @@ -114,6 +116,7 @@ d-i partman/early_command string \ xda=$(wget --tries=5 $confhome/get-xda.sh -O- | sh -s); \ debconf-set partman-auto/disk "/dev/$xda"; \ + rm -rf /usr/sbin/fdisk /usr/sbin/sfdisk; \ ttys=$(wget --tries=5 $confhome/ttys.sh -O- | sh -s console=); \ debconf-set debian-installer/add-kernel-opts "$ttys"; \ @@ -122,8 +125,11 @@ d-i partman/early_command string \ debconf-set base-installer/kernel/image "$kernel"; \ [ -d /sys/firmware/efi ] && debconf-set partman-auto/expert_recipe "$(debconf-get partman-auto/expert_recipe_efi)"; \ - [ -d /sys/firmware/efi ] || debconf-set partman-auto/expert_recipe "$(debconf-get partman-auto/expert_recipe_bios)" + [ -d /sys/firmware/efi ] || debconf-set partman-auto/expert_recipe "$(debconf-get partman-auto/expert_recipe_bios)"; \ + true >/bin/os-prober + +# os-prober 卡太久,因此跳过 # debian 10 没有 /target/etc/ssh/sshd_config.d/ 文件夹 d-i preseed/late_command string \ diff --git a/reinstall.bat b/reinstall.bat index 7fe60f6..f18cf26 100644 --- a/reinstall.bat +++ b/reinstall.bat @@ -52,7 +52,7 @@ if not errorlevel 1 ( ) rem pkgs 改动了才重新运行 Cygwin 安装程序 -set pkgs="curl,cpio,p7zip,bind-utils,ipcalc,dos2unix" +set pkgs="curl,cpio,p7zip,bind-utils,ipcalc,dos2unix,binutils" set tags=%tmp%\cygwin-installed-!pkgs! if not exist !tags! ( rem win10 arm 支持运行 x86 软件 diff --git a/reinstall.sh b/reinstall.sh index 8dc4967..5fba44c 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -41,6 +41,7 @@ Usage: $reinstall____ centos 7|8|9 netboot.xyz Manual: https://github.com/bin456789/reinstall + EOF exit 1 } @@ -737,6 +738,17 @@ install_pkg() { cmd_to_pkg() { unset USE case $cmd in + ar) + case "$pkg_mgr" in + *) pkg="binutils" ;; + esac + ;; + xz) + case "$pkg_mgr" in + apt) pkg="xz-utils" ;; + *) pkg="xz" ;; + esac + ;; lsblk | findmnt) case "$pkg_mgr" in apk) pkg="$cmd" ;; @@ -775,12 +787,40 @@ install_pkg() { esac } + is_need_epel_repo() { + [ "$pkg" = dpkg ] && { [ "$pkg_mgr" = yum ] || [ "$pkg_mgr" = dnf ]; } + } + + 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)" + + # 如果已安装 + if rpm -qa | grep -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 + # 如果未安装 + $pkg_mgr install -y $epel_release + fi + fi + } + install_pkg_real() { 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 ;; @@ -826,6 +866,9 @@ 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 @@ -859,7 +902,7 @@ check_ram() { if is_in_windows; then ram_size=$(wmic memorychip get capacity | tail +2 | awk '{sum+=$1} END {print sum/1024/1024}') else - # lsmem最准确但centos7 arm 和alpine不能用 + # lsmem最准确但 centos7 arm 和 alpine 不能用,debian 9 util-linux 没有 lsmem # arm 24g dmidecode 显示少了128m # arm 24g lshw 显示23BiB # ec2 t4g arm alpine 用 lsmem 和 dmidecode 都无效,要用 lshw,但结果和free -m一致,其他平台则没问题 @@ -951,6 +994,10 @@ del_empty_lines() { # 记录主硬盘 find_main_disk() { + if [ -n "$main_disk" ]; then + return + fi + if is_in_windows; then # TODO: # 已测试 vista @@ -1108,6 +1155,7 @@ collect_netconf() { echo "IPv4 Gateway: $ipv4_gateway" echo "IPv6 Address: $ipv6_addr" echo "IPv6 Gateway: $ipv6_gateway" + echo } add_efi_entry_in_windows() { @@ -1480,6 +1528,7 @@ id-assoc na 0 { EOF dhcp6c -c /var/lib/netcfg/dhcp6c.conf "$iface" sleep 10 + # kill-all-dhcp kill -9 "$(cat /var/run/dhcp6c.pid)" db_progress STEP 1 @@ -1491,7 +1540,7 @@ EOF # 运行trans.sh,保存配置 db_progress INFO base-installer/progress/netcfg - . /trans.sh + sh /trans.sh db_progress STEP 1 } @@ -1503,7 +1552,183 @@ EOF sed "s|@mac_addr|$mac_addr|" | sed "s|@netconf|$netconf|" >var/lib/dpkg/info/netcfg.postinst + # shellcheck disable=SC2317 + expand_packages() { + expand_packages_real "$@" | while read -r k_ v; do + # shellcheck disable=SC2001 + case $(echo "$k_" | sed 's/://') in + Package) + package="$v" + ;; + Priority) + # shellcheck disable=SC2154 + if [ "$v" = standard ] && echo "$disabled_list" | grep -qx "$package"; then + v=optional + fi + ;; + esac + + if [ -z "$k_" ]; then + echo + else + echo "$k_ $v" + fi + done + } + + # shellcheck disable=SC2012 + kver=$(ls -d lib/modules/* | awk -F/ '{print $NF}') + + net_retriever=usr/lib/debian-installer/retriever/net-retriever + sed -i 's/^expand_packages()/expand_packages_real()/' $net_retriever + insert_into_file $net_retriever after '#!/bin/sh' <$udeb_list + fi + + # 下载 udeb + curl -Lo $tmp/tmp.udeb http://$deb_hostname/debian/"$(grep /$package $udeb_list)" + + if false; then + # 使用 dpkg + # cygwin 没有 dpkg + install_pkg dpkg + dpkg -x $tmp/tmp.udeb $extract_dir + else + # 使用 ar tar xz + # cygwin 需安装 binutils + # centos7 ar 不支持 --output + install_pkg ar tar xz + (cd $tmp && ar x $tmp/tmp.udeb) + tar xf $tmp/data.tar.xz -C $extract_dir + fi + } + + # 不用在 windows 判断是哪种硬盘控制器,因为 256M 运行 windows 只可能是 xp,而脚本本来就不支持 xp + get_disk_controller() { + ( + cd "$(readlink -f /sys/block/$xda)" + while ! [ "$(pwd)" = / ]; do + if [ -d driver ]; then + basename "$(readlink -f driver)" + fi + cd .. + done + ) + } + + # 提前下载 fdisk + # 因为 fdisk-udeb 包含 fdisk 和 sfdisk,提前下载可减少占用 + mkdir_clear $tmp/fdisk + download_and_extract_udeb fdisk-udeb $tmp/fdisk + cp -f $tmp/fdisk/usr/sbin/fdisk usr/sbin/ + + if [ $ram_size -gt 256 ]; then + sed -i '/^pata-modules/d' $net_retriever + sed -i '/^sata-modules/d' $net_retriever + sed -i '/^scsi-modules/d' $net_retriever + else + # <=256M 极限优化 + find_main_disk + extra_drivers= + for driver in $(get_disk_controller); do + echo "using driver: $driver" + case $driver in + nvme | virtio_blk | virtio_scsi | hv_storvsc) extra_drivers+=" $driver" ;; + pata_legacy) sed -i '/^pata-modules/d' $net_retriever ;; + pata_* | sata_* | ahci) error_and_exit "Debain cloud kernel does not support this driver: $driver" ;; + esac + done + + # extra drivers + # 先不管 xen vmware + if [ -n "$extra_drivers" ]; then + mkdir_clear $tmp/scsi + download_and_extract_udeb scsi-modules-$kver-di $tmp/scsi + ( + cd lib/modules/*/kernel/drivers/ + for driver in $extra_drivers; do + echo "adding driver: $driver" + case $driver in + nvme) + mkdir -p nvme/host + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/nvme/host/nvme.ko nvme/host/ + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/nvme/host/nvme-core.ko nvme/host/ + ;; + virtio_blk) + mkdir -p block + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/block/virtio_blk.ko block/ + ;; + virtio_scsi) + mkdir -p scsi + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/scsi/virtio_scsi.ko scsi/ + ;; + hv_storvsc) + mkdir -p scsi + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/scsi/hv_storvsc.ko scsi/ + cp -f $tmp/scsi/lib/modules/*/kernel/drivers/scsi/scsi_transport_fc.ko scsi/ + ;; + esac + done + ) + fi + fi + # 将 use_level 2 9 修改为 use_level 1 + # x86 use_level 2 会出现 No root file system is defined. + # arm 即使 use_level 1 也会出现 No root file system is defined. sed -i 's/use_level=[29]/use_level=1/' lib/debian-installer-startup.d/S15lowmem # hack 3 @@ -1671,6 +1896,7 @@ mod_initrd() { rm -rf etc/brltty rm -rf sbin/wpa_supplicant rm -rf usr/lib/libasound.so.* + rm -rf usr/share/alsa ( cd lib/modules/*/kernel/drivers/net/ethernet/ for item in *; do @@ -1683,17 +1909,29 @@ mod_initrd() { ( cd lib/modules/*/kernel for item in \ - drivers/mmc \ - drivers/net/bonding \ - drivers/net/usb \ - drivers/net/wireless \ - drivers/pcmcia \ - drivers/staging \ - drivers/usb/serial \ - drivers/usb/storage \ - net/bluetooth \ net/mac80211 \ - net/wireless; do + net/wireless \ + net/bluetooth \ + drivers/hid \ + drivers/mmc \ + drivers/mtd \ + drivers/usb \ + drivers/ssb \ + drivers/mfd \ + drivers/bcma \ + drivers/pcmcia \ + drivers/parport \ + drivers/platform \ + drivers/staging \ + drivers/net/usb \ + drivers/net/bonding \ + drivers/net/wireless \ + drivers/input/rmi4 \ + drivers/input/keyboard \ + drivers/input/touchscreen \ + drivers/bus/mhi \ + drivers/char/pcmcia \ + drivers/misc/cardreader; do rm -rf $item done ) @@ -1707,6 +1945,7 @@ mod_initrd() { # (ASCII) archive format, use "-H odc" instead. find . | cpio --quiet -o -H newc | gzip -1 >/reinstall-initrd cd - >/dev/null + ls -lh /reinstall-initrd } # 脚本入口 @@ -2059,7 +2298,7 @@ if is_use_grub; then # 生成 grub 配置 # 实测 centos 7 lvm 要手动加载 lvm 模块 echo $target_cfg - cat <