Keep netboot entries in efiboot
when defining Zero-OS as first boot, we can not get the node back into rescue mode as that is the way Hetzner functions: tne netboot get an ipxe that doe a local reload or, in case of rescue starts a full netboot
This commit is contained in:
1 parent
1d63b5e723
commit
d0fd7fcc7b
1 file changed
+66
-28
+66
-28
@@ -44,7 +44,7 @@ find_blockdev() {
|
|||||||
DEV_NAME=$(basename "$DEV_PATH")
|
DEV_NAME=$(basename "$DEV_PATH")
|
||||||
# Exclude unwanted devices
|
# Exclude unwanted devices
|
||||||
case "$DEV_NAME" in
|
case "$DEV_NAME" in
|
||||||
loop*|ram*|sr*|fd*|md*|dm-*)
|
loop* | ram* | sr* | fd* | md* | dm-*)
|
||||||
continue
|
continue
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
@@ -70,22 +70,45 @@ manage_boot_entries() {
|
|||||||
log "Managing UEFI boot entries..."
|
log "Managing UEFI boot entries..."
|
||||||
|
|
||||||
# Check if efibootmgr is available
|
# Check if efibootmgr is available
|
||||||
if ! command -v efibootmgr &> /dev/null; then
|
if ! command -v efibootmgr &>/dev/null; then
|
||||||
log "Error: 'efibootmgr' is not installed. Please install it and rerun the script."
|
log "Error: 'efibootmgr' is not installed. Please install it and rerun the script."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get the list of current boot entries
|
# Get the list of current boot entries
|
||||||
CURRENT_BOOT_ENTRIES=$(efibootmgr | grep "BootOrder")
|
log "Retrieving current boot entries..."
|
||||||
log "Current BootOrder: $CURRENT_BOOT_ENTRIES"
|
BOOT_ENTRIES_INFO=$(efibootmgr -v | grep -E "^Boot[0-9A-F]{4}")
|
||||||
|
|
||||||
# Delete all existing boot entries except the EFI Shell (if present)
|
# Arrays to hold boot entry numbers
|
||||||
log "Deleting existing boot entries..."
|
NETBOOT_ENTRIES=()
|
||||||
BOOT_ENTRIES=$(efibootmgr | grep -E "^Boot[0-9A-F]{4}" | awk '{print $1}' | sed 's/\*//')
|
ENTRIES_TO_DELETE=()
|
||||||
for ENTRY in $BOOT_ENTRIES; do
|
|
||||||
efibootmgr -b ${ENTRY#Boot} -B
|
# Identify netboot entries and entries to delete
|
||||||
log "Deleted boot entry $ENTRY"
|
while read -r LINE; do
|
||||||
|
ENTRY_NUM=$(echo "$LINE" | awk '{print $1}' | sed 's/Boot//;s/\*//')
|
||||||
|
ENTRY_DESC=$(echo "$LINE" | cut -d' ' -f2-)
|
||||||
|
ENTRY_PATH=$(echo "$LINE" | awk -F'File' '{print $2}')
|
||||||
|
|
||||||
|
# Check if the entry is a netboot entry (e.g., contains "PXE", "Network", or "IPv4"/"IPv6")
|
||||||
|
if echo "$ENTRY_DESC" | grep -qiE "(PXE|Network|IPV4|IPV6)"; then
|
||||||
|
NETBOOT_ENTRIES+=("$ENTRY_NUM")
|
||||||
|
log "Identified netboot entry: Boot$ENTRY_NUM - $ENTRY_DESC"
|
||||||
|
else
|
||||||
|
ENTRIES_TO_DELETE+=("$ENTRY_NUM")
|
||||||
|
log "Marking entry for deletion: Boot$ENTRY_NUM - $ENTRY_DESC"
|
||||||
|
fi
|
||||||
|
done <<<"$BOOT_ENTRIES_INFO"
|
||||||
|
|
||||||
|
# Delete non-netboot entries
|
||||||
|
if [ ${#ENTRIES_TO_DELETE[@]} -gt 0 ]; then
|
||||||
|
log "Deleting non-netboot boot entries..."
|
||||||
|
for ENTRY_NUM in "${ENTRIES_TO_DELETE[@]}"; do
|
||||||
|
efibootmgr -b "$ENTRY_NUM" -B
|
||||||
|
log "Deleted boot entry Boot$ENTRY_NUM"
|
||||||
done
|
done
|
||||||
|
else
|
||||||
|
log "No non-netboot entries to delete."
|
||||||
|
fi
|
||||||
|
|
||||||
# Create a new boot entry pointing to BOOTX64.EFI on the first partition
|
# Create a new boot entry pointing to BOOTX64.EFI on the first partition
|
||||||
log "Creating new boot entry for /dev/${PARTPREFIX}1..."
|
log "Creating new boot entry for /dev/${PARTPREFIX}1..."
|
||||||
@@ -96,19 +119,27 @@ manage_boot_entries() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Set the boot order to include only the new boot entry
|
# Retrieve the new boot entry number
|
||||||
NEW_BOOT_NUM=$(efibootmgr | grep "Zero-OS" | awk '{print $1}' | sed 's/\*//;s/Boot//')
|
NEW_BOOT_NUM=$(efibootmgr | grep "Zero-OS" | awk '{print $1}' | sed 's/\*//;s/Boot//')
|
||||||
if [ -z "$NEW_BOOT_NUM" ]; then
|
if [ -z "$NEW_BOOT_NUM" ]; then
|
||||||
log "Error: Failed to retrieve new boot entry number."
|
log "Error: Failed to retrieve new boot entry number."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
log "Setting BootOrder to only include Boot$NEW_BOOT_NUM..."
|
|
||||||
efibootmgr -o $NEW_BOOT_NUM || {
|
# Set the new boot order: netboot entries first, followed by the new boot entry
|
||||||
|
log "Setting new BootOrder with netboot entries first..."
|
||||||
|
BOOT_ORDER=""
|
||||||
|
for ENTRY_NUM in "${NETBOOT_ENTRIES[@]}"; do
|
||||||
|
BOOT_ORDER+="$ENTRY_NUM,"
|
||||||
|
done
|
||||||
|
BOOT_ORDER+="$NEW_BOOT_NUM"
|
||||||
|
|
||||||
|
efibootmgr -o "$BOOT_ORDER" || {
|
||||||
log "Error: Failed to set new BootOrder."
|
log "Error: Failed to set new BootOrder."
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
log "New BootOrder set successfully."
|
log "New BootOrder set successfully: $BOOT_ORDER"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main script execution
|
# Main script execution
|
||||||
@@ -121,7 +152,7 @@ main() {
|
|||||||
# Parse command-line arguments
|
# Parse command-line arguments
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-d|--device)
|
-d | --device)
|
||||||
shift
|
shift
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
log "Error: '--device' requires a non-empty option argument."
|
log "Error: '--device' requires a non-empty option argument."
|
||||||
@@ -130,7 +161,7 @@ main() {
|
|||||||
BLOCKDEV_SPECIFIED="$1"
|
BLOCKDEV_SPECIFIED="$1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-f|--farm)
|
-f | --farm)
|
||||||
shift
|
shift
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
log "Error: '--farm' requires a non-empty option argument."
|
log "Error: '--farm' requires a non-empty option argument."
|
||||||
@@ -139,7 +170,7 @@ main() {
|
|||||||
FARM="$1"
|
FARM="$1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-e|--environment)
|
-e | --environment)
|
||||||
shift
|
shift
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
log "Error: '--environment' requires a non-empty option argument."
|
log "Error: '--environment' requires a non-empty option argument."
|
||||||
@@ -148,7 +179,7 @@ main() {
|
|||||||
ENVIRONMENT="$1"
|
ENVIRONMENT="$1"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-h|--help)
|
-h | --help)
|
||||||
usage
|
usage
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -160,7 +191,7 @@ main() {
|
|||||||
|
|
||||||
# Validate environment
|
# Validate environment
|
||||||
case "$ENVIRONMENT" in
|
case "$ENVIRONMENT" in
|
||||||
dev|qa|test|prod)
|
dev | qa | test | prod)
|
||||||
log "Using environment: $ENVIRONMENT"
|
log "Using environment: $ENVIRONMENT"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -205,28 +236,34 @@ main() {
|
|||||||
|
|
||||||
# Wipe filesystem signatures
|
# Wipe filesystem signatures
|
||||||
log "Wiping filesystem signatures on /dev/${BLOCKDEV}..."
|
log "Wiping filesystem signatures on /dev/${BLOCKDEV}..."
|
||||||
wipefs -a -f /dev/${BLOCKDEV} || { log "Failed to wipe filesystem signatures."; exit 1; }
|
wipefs -a -f /dev/${BLOCKDEV} || {
|
||||||
|
log "Failed to wipe filesystem signatures."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# Create GPT partition table
|
# Create GPT partition table
|
||||||
log "Creating GPT partition table on /dev/${BLOCKDEV}..."
|
log "Creating GPT partition table on /dev/${BLOCKDEV}..."
|
||||||
parted -s /dev/${BLOCKDEV} mklabel gpt || { log "Failed to create GPT partition table."; exit 1; }
|
parted -s /dev/${BLOCKDEV} mklabel gpt || {
|
||||||
|
log "Failed to create GPT partition table."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# Create partitions
|
# Create partitions
|
||||||
log "Creating partitions on /dev/${BLOCKDEV}..."
|
log "Creating partitions on /dev/${BLOCKDEV}..."
|
||||||
if ! parted -s /dev/${BLOCKDEV} "mkpart zosboot fat32 1MiB 300MiB set 1 esp on mkpart zoscache btrfs 300MiB 100%"; then
|
if ! parted -s /dev/${BLOCKDEV} "mkpart zosboot fat16 1MiB 100MiB set 1 esp on mkpart zoscache btrfs 100MiB 100%"; then
|
||||||
log "Failed to create partitions."
|
log "Failed to create partitions."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Format partitions
|
# Format partitions
|
||||||
log "Formatting /dev/${PARTPREFIX}1 as FAT32..."
|
log "Formatting /dev/${PARTPREFIX}1 as FAT32..."
|
||||||
if ! mkfs.vfat -F32 /dev/${PARTPREFIX}1; then
|
if ! mkfs.vfat -F32 -nZOSPXE /dev/${PARTPREFIX}1; then
|
||||||
log "Failed to format /dev/${PARTPREFIX}1 as FAT32."
|
log "Failed to format /dev/${PARTPREFIX}1 as FAT32."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log "Formatting /dev/${PARTPREFIX}2 as BTRFS..."
|
log "Formatting /dev/${PARTPREFIX}2 as BTRFS..."
|
||||||
if ! mkfs.btrfs -f /dev/${PARTPREFIX}2; then
|
if ! mkfs.btrfs -f /dev/${PARTPREFIX}2 -LZOSCACHE; then
|
||||||
log "Failed to format /dev/${PARTPREFIX}2 as BTRFS."
|
log "Failed to format /dev/${PARTPREFIX}2 as BTRFS."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
@@ -244,9 +281,7 @@ main() {
|
|||||||
mkdir -p "${MOUNT_POINT}/efi/boot"
|
mkdir -p "${MOUNT_POINT}/efi/boot"
|
||||||
|
|
||||||
# Download the required BOOTX64.EFI file
|
# Download the required BOOTX64.EFI file
|
||||||
# NOTE:: EDIT THIS WHEN APPROPRIATE AND IN PROD
|
EFI_FILE_URL="https://bootstrap.grid.tf/uefi/${ENVIRONMENT}/${FARM}"
|
||||||
EFI_FILE_URL="https://bootstrap.grid.tf/uefi/${ENVIRONMENT}/${FARM}/debug/zero-os-development-zos-v4-debug-7d2de62033.efi?version=v4"
|
|
||||||
# EFI_FILE_URL="https://bootstrap.grid.tf/uefi/${ENVIRONMENT}/${FARM}"
|
|
||||||
log "Downloading BOOTX64.EFI from ${EFI_FILE_URL}..."
|
log "Downloading BOOTX64.EFI from ${EFI_FILE_URL}..."
|
||||||
if ! wget -q "${EFI_FILE_URL}" -O "${MOUNT_POINT}/efi/boot/BOOTX64.EFI"; then
|
if ! wget -q "${EFI_FILE_URL}" -O "${MOUNT_POINT}/efi/boot/BOOTX64.EFI"; then
|
||||||
log "Failed to download BOOTX64.EFI."
|
log "Failed to download BOOTX64.EFI."
|
||||||
@@ -257,7 +292,10 @@ main() {
|
|||||||
|
|
||||||
# Unmount the partition
|
# Unmount the partition
|
||||||
log "Unmounting ${MOUNT_POINT}..."
|
log "Unmounting ${MOUNT_POINT}..."
|
||||||
umount -r "${MOUNT_POINT}" || { log "Failed to unmount ${MOUNT_POINT}."; exit 1; }
|
umount -r "${MOUNT_POINT}" || {
|
||||||
|
log "Failed to unmount ${MOUNT_POINT}."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# Manage UEFI boot entries
|
# Manage UEFI boot entries
|
||||||
manage_boot_entries
|
manage_boot_entries
|
||||||
|
|||||||
Reference in new issue
Block a user