From 5d3227fe64f8f8f146311a535da39ebdfdbe7001 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Fri, 2 Feb 2024 00:31:21 +0800 Subject: [PATCH] =?UTF-8?q?core:=20=E4=BD=BF=E7=94=A8=20get-xda.sh=20?= =?UTF-8?q?=E5=AF=BB=E6=89=BE=E4=B8=BB=E7=A1=AC=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian.cfg | 7 +++++-- get-xda.sh | 37 +++++++++++++++++++++++++++++++++++++ redhat.cfg | 24 ++++-------------------- ubuntu-storage-early.sh | 23 +---------------------- ubuntu.yaml | 5 +++-- 5 files changed, 50 insertions(+), 46 deletions(-) create mode 100644 get-xda.sh diff --git a/debian.cfg b/debian.cfg index 68bdb54..78ff546 100644 --- a/debian.cfg +++ b/debian.cfg @@ -71,9 +71,12 @@ d-i preseed/early_command string \ debconf-set mirror/http/hostname "$hostname" d-i partman/early_command string \ - debconf-set partman-auto/disk "$(list-devices disk | head -n1)"; \ - confhome="$(grep -o 'extra\.confhome=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ + + anna-install fdisk-udeb; \ + xda=$(wget $confhome/get-xda.sh -O- | sh -s); \ + debconf-set partman-auto/disk "/dev/$xda"; \ + ttys=$(wget $confhome/ttys.sh -O- | sh -s console=); \ debconf-set debian-installer/add-kernel-opts "$ttys"; \ diff --git a/get-xda.sh b/get-xda.sh new file mode 100644 index 0000000..5419428 --- /dev/null +++ b/get-xda.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# debian ubuntu redhat 安装模式共用此脚本 +# alpine 未用到此脚本 + +get_all_disks() { + # busybox blkid 不接受任何参数 + disks=$(blkid | cut -d: -f1 | cut -d/ -f3 | sed -E 's/p?[0-9]+$//' | sort -u) + # blkid 会显示 sr0,经过上面的命令输出为 sr + # 因此要检测是否有效 + for disk in $disks; do + if [ -b "/dev/$disk" ]; then + echo "$disk" + fi + done +} + +get_xda() { + main_disk="$(grep -o 'extra\.main_disk=[^ ]*' /proc/cmdline | cut -d= -f2)" + + # 防止 $main_disk 为空 + if [ -z "$main_disk" ]; then + return 1 + fi + + for disk in $(get_all_disks); do + if fdisk -l "/dev/$disk" | grep -iq "$main_disk"; then + echo "$disk" + return + fi + done + + # 如果没找到,返回假的值,防止意外地格式化全部盘 + echo 'FAKE_DISK' + return 1 +} + +get_xda diff --git a/redhat.cfg b/redhat.cfg index e3fc7db..626a676 100644 --- a/redhat.cfg +++ b/redhat.cfg @@ -44,33 +44,17 @@ for var in $(grep -o "\b$prefix\.[^ ]*" /proc/cmdline | xargs); do eval "$(echo "$var" | sed -E "s/$prefix\.([^=]*)=(.*)/\1='\2'/")" done -get_all_disks() { - lsblk -rn --nodeps -o NAME,TYPE | grep 'disk$' | awk '{print $1}' -} - -get_xda() { - # 防止 $main_disk 为空 - if [ -z "$main_disk" ]; then - return - fi - - for disk in $(get_all_disks); do - # shellcheck disable=SC2154 - if fdisk -l "/dev/$disk" | grep -iq "$main_disk"; then - echo "$disk" - break - fi - done -} +# centos7 证书链未更新,需要 --no-check-certificate # 只使用主硬盘 include=/tmp/include-disk-only-use -echo "ignoredisk --only-use=$(get_xda)" >$include +xda=$(wget --no-check-certificate "$confhome/get-xda.sh" -O- | sh -s) +echo "ignoredisk --only-use=$xda" >$include # 设置 tty include=/tmp/include-bootloader # shellcheck disable=SC2154 -console_cmdline=$(wget "$confhome/ttys.sh" -O- | sh -s console=) +console_cmdline=$(wget --no-check-certificate "$confhome/ttys.sh" -O- | sh -s console=) echo "bootloader --append=\"$console_cmdline\"" >$include # 有 installer 分区,表示用了两步安装 diff --git a/ubuntu-storage-early.sh b/ubuntu-storage-early.sh index 95ff165..eca5957 100644 --- a/ubuntu-storage-early.sh +++ b/ubuntu-storage-early.sh @@ -1,24 +1,4 @@ #!/bin/bash - -get_all_disks() { - lsblk -rn --nodeps -o NAME,TYPE | grep 'disk$' | awk '{print $1}' -} - -get_xda() { - # 防止 $main_disk 为空 - if [ -z "$main_disk" ]; then - return - fi - - for disk in $(get_all_disks); do - # shellcheck disable=SC2154 - if fdisk -l "/dev/$disk" | grep -iq "$main_disk"; then - echo "$disk" - break - fi - done -} - sed -i -E '/^\.{3}$/d' /autoinstall.yaml echo 'storage:' >>/autoinstall.yaml @@ -28,12 +8,11 @@ cat <>/autoinstall.yaml size: 0 EOF -xda="$(get_xda)" - # 是用 size 寻找分区,number 没什么用 # https://curtin.readthedocs.io/en/latest/topics/storage.html size_os=$(lsblk -bn -o SIZE /dev/disk/by-label/os) +# shellcheck disable=SC2154 if parted "/dev/$xda" print | grep '^Partition Table' | grep gpt; then # efi if [ -e /dev/disk/by-label/efi ]; then diff --git a/ubuntu.yaml b/ubuntu.yaml index e9e306d..0f13565 100644 --- a/ubuntu.yaml +++ b/ubuntu.yaml @@ -27,8 +27,9 @@ autoinstall: done # 生成分区信息 - export main_disk - curl -L $confhome/ubuntu-storage-early.sh | bash -s + xda=$(curl -L "$confhome/get-xda.sh" | sh -s) + export xda + curl -L "$confhome/ubuntu-storage-early.sh" | sh -s # 内核风味 # https://bugs.launchpad.net/subiquity/+bug/1989353