运行更新或更换故障的 RAID1 驱动器时,如何维护 RAID1 上的 EFI 分区?

运行更新或更换故障的 RAID1 驱动器时,如何维护 RAID1 上的 EFI 分区?

我在具有 3 个相同硬盘的系统上安装了 Ubuntu Server 22.03。在磁盘设置过程中,我在每个硬盘上创建了 500M fat32 分区,并将剩余的磁盘空间配置为 RAID1。在 fat32 分区上设置 esp 标志后,我能够将它们全部选为启动设备。安装后,EFI 分区的内容相同(我使用 rsync 检查过)。我想确保系统始终能够从任何驱动器启动。

我想知道的是:

  • 系统更新会使 EFI 分区保持同步吗?
  • 如果是这样,那么在更换故障的 RAID 驱动器时我需要做什么才能确保其 EFI 分区保持同步?

答案1

看起来系统更新不会使 EFI 分区保持最新状态。这里有一个可以解决这个问题的 shell 脚本。

!!!警告!!! 这将覆盖系统上的所有 EFI 分区。如果您有不想同步的 EFI 分区,请不要使用此脚本。

#!/bin/bash

# Define the mount point of the EFI partition
EFI_MOUNT="/boot/efi"
# Temporary mount point base directory
TEMP_MOUNT_DIR="/tmp/efi_sync/"

# Check if anything is mounted at /boot/efi
if ! mountpoint -q $EFI_MOUNT; then
    echo "Error: No partition is mounted on $EFI_MOUNT. Please check your configuration."
    exit 1
fi

# Get the device path of the currently mounted EFI partition
CURRENT_EFI_DEV=$(findmnt -n -o SOURCE --target $EFI_MOUNT)

# Check if the mounted partition is an EFI System Partition
if ! blkid -t TYPE="vfat" $CURRENT_EFI_DEV &>/dev/null; then
    echo "Error: The partition mounted on $EFI_MOUNT is not an EFI System Partition."
    exit 1
fi

# Find the hard drives
DISKS=$(lsblk -d -n -o NAME,TYPE | grep disk | awk '{print $1}')
EFI_PARTITIONS=()

for DISK in $DISKS; do
    # Find all partitions on the drive
    PARTITIONS=$(sudo fdisk -l /dev/$DISK | grep "^/dev")
    while read -r PARTITION; do
        # Determine whether this partition is EFI
        if echo "$PARTITION" | grep -q "EFI System"; then
            EFI_PARTITIONS+=($(awk '{print $1}' <<< "$PARTITION"))
        fi
    done <<< "$PARTITIONS"
done
# Convert array to space-delimited string
EFI_PARTITIONS=${EFI_PARTITIONS[@]}
echo $EFI_PARTITIONS

# Get list of unique drives with EFI partitions
EFI_DRIVES=$(for PART in $EFI_PARTITIONS; do lsblk -no PKNAME $PART; done | sort -u)

for DRIVE in $EFI_DRIVES; do
    echo "installing grub to /dev/$DRIVE"
    grub-install /dev/$drive
done

# Mount EFI partitions and update using rsync
mkdir -p $TEMP_MOUNT_DIR
for PART in $EFI_PARTITIONS; do
    if [ "$PART" != "$CURRENT_EFI_DEV" ]; then
        echo "Syncing $PART with $CURRENT_EFI_DEV"
        mount $PART $TEMP_MOUNT_DIR
        rsync -av --delete $EFI_MOUNT/ $TEMP_MOUNT_DIR/
        umount $TEMP_MOUNT_DIR
    fi
done
rmdir $TEMP_MOUNT_DIR

DRIVES_ARRAY=($EFI_DRIVES)
echo "EFI partitions synchronized and GRUB installed on ${#DISKS_ARRAY[@]} drives."

相关内容