Switch to systemd-boot for UEFI while keeping GRUB2 for BIOS/MBR

This commit is contained in:
2026-02-10 13:29:02 +01:00
parent 2e8e99f481
commit ada17eefd7
2 changed files with 42 additions and 35 deletions

View File

@@ -236,7 +236,8 @@ def mount_partitions(partition_info, mount_root="/mnt"):
# 2. Mount EFI (if exists) # 2. Mount EFI (if exists)
if partition_info.get("efi"): if partition_info.get("efi"):
efi_mount = os.path.join(mount_root, "boot/efi") # For systemd-boot, we prefer mounting ESP to /boot
efi_mount = os.path.join(mount_root, "boot")
os.makedirs(efi_mount, exist_ok=True) os.makedirs(efi_mount, exist_ok=True)
run_command(["mount", partition_info["efi"], efi_mount]) run_command(["mount", partition_info["efi"], efi_mount])
else: else:

View File

@@ -145,14 +145,12 @@ def install_minimal_os(mount_root, releasever="43"):
"passwd", "passwd",
"rootfiles", "rootfiles",
"vim-minimal", "vim-minimal",
"grub2-tools",
"grubby",
] ]
if uefi: if uefi:
packages += ["grub2-efi-x64", "grub2-efi-x64-modules", "shim-x64", "efibootmgr"] packages += ["systemd-boot-unsigned", "efibootmgr"]
else: else:
packages += ["grub2-pc"] packages += ["grub2-pc", "grub2-tools", "grubby"]
# Offline installation logic # Offline installation logic
possible_repos = [ possible_repos = [
@@ -223,7 +221,7 @@ def install_minimal_os(mount_root, releasever="43"):
def configure_system(mount_root, partition_info, user_info=None, disk_device=None): def configure_system(mount_root, partition_info, user_info=None, disk_device=None):
""" """
Basic configuration: fstab, grub2, and user creation. Basic configuration: fstab, bootloader, and user creation.
""" """
logger.info("Configuring system...") logger.info("Configuring system...")
log_os_install("CONFIGURE", "start", f"Configuring system in {mount_root}") log_os_install("CONFIGURE", "start", f"Configuring system in {mount_root}")
@@ -241,7 +239,8 @@ def configure_system(mount_root, partition_info, user_info=None, disk_device=Non
if uefi and partition_info.get("efi"): if uefi and partition_info.get("efi"):
efi_uuid = get_uuid(partition_info["efi"]) efi_uuid = get_uuid(partition_info["efi"])
fstab_lines.append(f"UUID={efi_uuid} /boot/efi vfat defaults 0 2") # For systemd-boot, we mount ESP to /boot
fstab_lines.append(f"UUID={efi_uuid} /boot vfat defaults 0 2")
if partition_info.get("swap"): if partition_info.get("swap"):
swap_uuid = get_uuid(partition_info["swap"]) swap_uuid = get_uuid(partition_info["swap"])
@@ -251,9 +250,9 @@ def configure_system(mount_root, partition_info, user_info=None, disk_device=Non
with open(os.path.join(mount_root, "etc/fstab"), "w") as f: with open(os.path.join(mount_root, "etc/fstab"), "w") as f:
f.write("\n".join(fstab_lines) + "\n") f.write("\n".join(fstab_lines) + "\n")
# Ensure EFI is mounted for GRUB installation # Ensure EFI is mounted for bootloader installation if UEFI
if uefi and partition_info.get("efi"): if uefi and partition_info.get("efi"):
efi_target = os.path.join(mount_root, "boot/efi") efi_target = os.path.join(mount_root, "boot")
os.makedirs(efi_target, exist_ok=True) os.makedirs(efi_target, exist_ok=True)
# Check if already mounted # Check if already mounted
res = subprocess.run(["mount"], capture_output=True, text=True) res = subprocess.run(["mount"], capture_output=True, text=True)
@@ -284,35 +283,42 @@ def configure_system(mount_root, partition_info, user_info=None, disk_device=Non
except Exception as e: except Exception as e:
logger.error(f"Failed to set passwords: {e}") logger.error(f"Failed to set passwords: {e}")
# 3. Configure GRUB2 # 3. Configure Bootloader
logger.info("Configuring GRUB2...") if uefi:
logger.info("Configuring systemd-boot...")
if not os.path.exists(os.path.join(mount_root, "etc/machine-id")):
run_command(["chroot", mount_root, "systemd-machine-id-setup"])
os.makedirs(os.path.join(mount_root, "etc/kernel"), exist_ok=True)
with open(os.path.join(mount_root, "etc/kernel/cmdline"), "w") as f:
f.write(f"root=UUID={root_uuid} rw quiet\n")
with open(os.path.join(mount_root, "etc/kernel/layout"), "w") as f:
f.write("bls\n")
run_command(["chroot", mount_root, "bootctl", "install", "--path=/boot"])
# Add kernel entries
modules_dir = os.path.join(mount_root, "lib/modules")
if os.path.exists(modules_dir):
kvers = [d for d in os.listdir(modules_dir) if os.path.isdir(os.path.join(modules_dir, d))]
for kver in kvers:
logger.info(f"Adding kernel entry for {kver}...")
initrd_path = f"/boot/initramfs-{kver}.img"
if not os.path.exists(os.path.join(mount_root, initrd_path.lstrip("/"))):
run_command(["chroot", mount_root, "dracut", "--force", initrd_path, kver])
kernel_image = f"/lib/modules/{kver}/vmlinuz"
run_command(["chroot", mount_root, "kernel-install", "add", kver, kernel_image, initrd_path])
else:
logger.info("Configuring GRUB2 (BIOS)...")
# Ensure /etc/default/grub exists # Ensure /etc/default/grub exists
grub_default = os.path.join(mount_root, "etc/default/grub") grub_default = os.path.join(mount_root, "etc/default/grub")
if not os.path.exists(grub_default): if not os.path.exists(grub_default):
with open(grub_default, "w") as f: with open(grub_default, "w") as f:
f.write('GRUB_TIMEOUT=5\nGRUB_DISTRIBUTOR="$(sed \'s, release .*$,,g\' /etc/system-release)"\nGRUB_DEFAULT=saved\nGRUB_DISABLE_SUBMENU=true\nGRUB_TERMINAL_OUTPUT="console"\nGRUB_CMDLINE_LINUX="rhgb quiet"\nGRUB_DISABLE_RECOVERY="true"\nGRUB_ENABLE_BLSCFG=true\n') f.write('GRUB_TIMEOUT=5\nGRUB_DISTRIBUTOR="$(sed \'s, release .*$,,g\' /etc/system-release)"\nGRUB_DEFAULT=saved\nGRUB_DISABLE_SUBMENU=true\nGRUB_TERMINAL_OUTPUT="console"\nGRUB_CMDLINE_LINUX="rhgb quiet"\nGRUB_DISABLE_RECOVERY="true"\nGRUB_ENABLE_BLSCFG=true\n')
if uefi:
efi_dir = "/boot/efi"
logger.info("Installing GRUB2 for UEFI...")
# Fedora's grub2-install requires --force for EFI to acknowledge it won't be Secure Boot signed
run_command(["chroot", mount_root, "grub2-install", "--target=x86_64-efi", f"--efi-directory={efi_dir}", "--bootloader-id=iridium", "--recheck", "--force"])
config_dir = os.path.join(mount_root, "boot/efi/EFI/iridium")
os.makedirs(config_dir, exist_ok=True)
config_path = "/boot/efi/EFI/iridium/grub.cfg"
run_command(["chroot", mount_root, "grub2-mkconfig", "-o", config_path])
# Fedora compatibility link
fedora_dir = os.path.join(mount_root, "boot/efi/EFI/fedora")
os.makedirs(fedora_dir, exist_ok=True)
with open(os.path.join(fedora_dir, "grub.cfg"), "w") as f:
f.write(f"configfile /EFI/iridium/grub.cfg\n")
else:
if not disk_device: if not disk_device:
# Try to guess disk device from root partition
disk_device = partition_info["root"].rstrip("0123456789") disk_device = partition_info["root"].rstrip("0123456789")
if disk_device.endswith("p"): disk_device = disk_device[:-1] if disk_device.endswith("p"): disk_device = disk_device[:-1]
@@ -323,4 +329,4 @@ def configure_system(mount_root, partition_info, user_info=None, disk_device=Non
run_command(["sync"]) run_command(["sync"])
logger.info("System configuration complete.") logger.info("System configuration complete.")
log_os_install("CONFIGURE", "complete", "GRUB2 and user configured successfully") log_os_install("CONFIGURE", "complete", "Bootloader and user configured successfully")