![运行更新或更换故障的 RAID1 驱动器时,如何维护 RAID1 上的 EFI 分区?](https://linux22.com/image/1199446/%E8%BF%90%E8%A1%8C%E6%9B%B4%E6%96%B0%E6%88%96%E6%9B%B4%E6%8D%A2%E6%95%85%E9%9A%9C%E7%9A%84%20RAID1%20%E9%A9%B1%E5%8A%A8%E5%99%A8%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E7%BB%B4%E6%8A%A4%20RAID1%20%E4%B8%8A%E7%9A%84%20EFI%20%E5%88%86%E5%8C%BA%EF%BC%9F.png)
我在具有 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."