Reduce the number of bootenv vars

This commit is contained in:
2026-04-06 02:20:41 +08:00
parent f8db036a5f
commit 50d9440e0a
14 changed files with 455 additions and 93 deletions

View File

@@ -2,10 +2,9 @@
set -eu
### User-configurable vars
IMG="./out/monok8s-dev.img.gz"
DEFAULT_DISK="/dev/disk7" # whole disk, not a partition
IMG_DIR="./out"
CONFIG_PART_SUFFIX="s1" # config partition after flashing
ENV_SEARCH_DIR="./out" # optional; leave empty to use image dir
ENV_SEARCH_DIR="./out" # optional; leave empty to use selected image dir
MNT="/tmp/monok8s-usb-config.$$"
@@ -59,6 +58,131 @@ wait_for_mountable_partition() {
return 1
}
select_image() {
dir="$1"
[ -d "$dir" ] || die "Image directory not found: $dir"
OLD_IFS="${IFS}"
IFS='
'
set -- $(find "$dir" -maxdepth 1 -type f -name '*.img.gz' | sort)
IFS="${OLD_IFS}"
[ "$#" -gt 0 ] || die "No .img.gz images found in $dir"
echo "Available images:"
n=1
for img in "$@"; do
size="$(stat -f%z "$img" 2>/dev/null || echo "?")"
echo " [$n] $(basename "$img") (${size} bytes)"
n=$((n + 1))
done
echo
printf "Select image number: "
read -r choice
case "$choice" in
''|*[!0-9]*)
die "Invalid selection: $choice"
;;
esac
n=1
for img in "$@"; do
if [ "$n" -eq "$choice" ]; then
SELECTED_IMG="$img"
return 0
fi
n=$((n + 1))
done
die "Selection out of range: $choice"
}
list_candidate_disks() {
found=0
for dev in /dev/disk*; do
base="$(basename "$dev")"
case "$base" in
disk[0-9])
;;
*)
continue
;;
esac
info="$(diskutil list "$dev" 2>/dev/null || true)"
[ -n "$info" ] || continue
# Skip Apple disks
echo "$info" | grep -q "APFS" && continue
info="$(diskutil info "$dev" 2>/dev/null || true)"
found=1
media_name="$(echo "$info" | sed -n 's/^ *Device \/ Media Name: *//p' | head -n1)"
protocol="$(echo "$info" | sed -n 's/^ *Protocol: *//p' | head -n1)"
size="$(echo "$info" | sed -n 's/^ *Disk Size: *//p' | head -n1)"
echo "$dev|$media_name|$protocol|$size"
done
[ "$found" -eq 1 ] || return 1
return 0
}
select_disk() {
candidates="$(list_candidate_disks || true)"
if [ -z "$candidates" ]; then
warn "No obvious external candidate disks found."
printf "Enter target disk manually (example: /dev/disk7): "
read -r TARGET_DISK
[ -n "$TARGET_DISK" ] || die "No disk entered"
return 0
fi
echo "Candidate target disks:"
n=1
echo "$candidates" | while IFS='|' read -r dev media protocol size; do
echo " [$n] $dev - $media - $protocol - $size"
n=$((n + 1))
done
echo
printf "Select disk number (or type a full /dev/diskN path): "
read -r choice
case "$choice" in
/dev/disk[0-9]*)
TARGET_DISK="$choice"
return 0
;;
''|*[!0-9]*)
die "Invalid selection: $choice"
;;
esac
n=1
OLD_IFS="${IFS}"
IFS='
'
for line in $candidates; do
if [ "$n" -eq "$choice" ]; then
TARGET_DISK="$(echo "$line" | cut -d'|' -f1)"
IFS="${OLD_IFS}"
return 0
fi
n=$((n + 1))
done
IFS="${OLD_IFS}"
die "Selection out of range: $choice"
}
### Sanity checks
require_cmd diskutil
require_cmd gunzip
@@ -70,24 +194,26 @@ require_cmd cp
require_cmd sed
require_cmd stat
require_cmd pv
require_cmd cut
require_cmd grep
require_cmd head
printf "disk ($DEFAULT_DISK): "
read -r TARGET_DISK
select_image "$IMG_DIR"
IMG="$SELECTED_IMG"
if [ -z "$TARGET_DISK" ]; then
TARGET_DISK="$DEFAULT_DISK"
fi
select_disk
TARGET_RAW_DISK="/dev/r$(basename "$TARGET_DISK")"
TARGET_BASENAME="$(basename "$TARGET_DISK")"
### Derived vars
TARGET_CONFIG_PART="/dev/${TARGET_BASENAME}${CONFIG_PART_SUFFIX}"
IMG_DIR="$(cd "$(dirname "$IMG")" && pwd)"
SEARCH_DIR="${ENV_SEARCH_DIR:-$IMG_DIR}"
IMG_SIZE_BYTES="$(stat -f%z "$IMG")"
IMG_DIR_ABS="$(cd "$(dirname "$IMG")" && pwd)"
SEARCH_DIR="${ENV_SEARCH_DIR:-$IMG_DIR_ABS}"
[ -f "$IMG" ] || die "Image not found: $IMG"
IMG_SIZE_BYTES="$(stat -f%z "$IMG")"
[ -b "$TARGET_DISK" ] || die "Target disk not found: $TARGET_DISK"
[ "$(id -u)" -eq 0 ] || die "Run this script with sudo"
@@ -122,7 +248,7 @@ diskutil unmountDisk force "$TARGET_DISK" || die "Failed to unmount $TARGET_DISK
log "Flashing image with progress..."
pv -s "$IMG_SIZE_BYTES" "$IMG" \
| gunzip \
| dd of="$TARGET_RAW_DISK" bs=1m
| dd of="$TARGET_RAW_DISK" bs=4m
log "Syncing writes..."
sync