Redo backend installer logic to support BIOS/MBR boot on older devices and switch to GRUB2

This commit is contained in:
2026-02-09 15:02:14 +01:00
parent b37fc8d060
commit e8a2f7aaa9
7 changed files with 187 additions and 202 deletions

View File

@@ -128,123 +128,97 @@ def ensure_unmounted(disk_device):
logger.warning(f"Error during pre-partition unmount: {e}")
def is_uefi():
"""Checks if the system is booted in UEFI mode."""
return os.path.exists("/sys/firmware/efi")
def auto_partition_disk(disk_device):
"""
Automatically partitions the disk:
1. EFI System Partition (2GB standard, 1GB small)
2. Root (Remaining)
3. Swap (RAM + 2GB, or 0 if small)
Layout: P1=EFI, P3=Swap (End), P2=Root (Middle)
Automatically partitions the disk based on boot mode:
UEFI (GPT): ESP (1GB), Root (Remaining), Swap (RAM+2GB)
BIOS (MBR): Root (Remaining, Bootable), Swap (RAM+2GB)
"""
logger.info(f"Starting auto-partitioning on {disk_device}")
log_disk_operation("PARTITION", "start", f"Disk: {disk_device}")
uefi = is_uefi()
logger.info(f"Starting auto-partitioning on {disk_device} (UEFI: {uefi})")
log_disk_operation("PARTITION", "start", f"Disk: {disk_device}, UEFI: {uefi}")
# 0. Ensure unmounted
ensure_unmounted(disk_device)
# Calculate sizes
disk_size = get_disk_size(disk_device)
ram_size = get_total_memory()
# Defaults
efi_mb = 2048
# Calculate sizes
swap_mb = int((ram_size / (1024 * 1024)) + 2048)
min_root_mb = 10240 # 10GB
total_required_mb = efi_mb + swap_mb + min_root_mb
# Cap swap at 16GB for sanity on large RAM systems unless disk is huge
if disk_size < 100 * 1024**3: # < 100GB
swap_mb = min(swap_mb, 4096)
disk_mb = disk_size / (1024 * 1024)
use_swap = disk_mb > (10240 + swap_mb) # Only use swap if we have at least 10GB for root
use_swap = True
if uefi:
# GPT Layout
logger.info("Using GPT layout for UEFI")
run_command(["sgdisk", "-Z", disk_device])
run_command(["sgdisk", "-o", disk_device])
# 1. ESP (1GB)
run_command(["sgdisk", "-n", "1:0:+1024M", "-t", "1:ef00", "-c", "1:EFI System", disk_device])
# 2. Swap (if enabled)
if use_swap:
run_command(["sgdisk", "-n", f"3:-{swap_mb}M:0", "-t", "3:8200", "-c", "3:Swap", disk_device])
# 3. Root
run_command(["sgdisk", "-n", "2:0:0", "-t", "2:8300", "-c", "2:Root", disk_device])
else:
# MBR Layout for BIOS
logger.info("Using MBR layout for BIOS")
# Wipe disk using dd to be sure MBR is cleared
run_command(["dd", "if=/dev/zero", f"of={disk_device}", "bs=512", "count=100"])
# Use parted for MBR
run_command(["parted", "-s", disk_device, "mklabel", "msdos"])
if use_swap:
# Root first, then swap at end
root_end = int(disk_mb - swap_mb)
run_command(["parted", "-s", disk_device, "mkpart", "primary", "ext4", "1MiB", f"{root_end}MiB"])
run_command(["parted", "-s", disk_device, "mkpart", "primary", "linux-swap", f"{root_end}MiB", "100%"])
run_command(["parted", "-s", disk_device, "set", "1", "boot", "on"])
else:
run_command(["parted", "-s", disk_device, "mkpart", "primary", "ext4", "1MiB", "100%"])
run_command(["parted", "-s", disk_device, "set", "1", "boot", "on"])
if disk_mb < total_required_mb:
logger.warning("Disk too small for standard layout. Adjusting...")
efi_mb = 1024
use_swap = False
# Check minimal viability
if disk_mb < (efi_mb + min_root_mb):
raise Exception("Disk too small for installation (Need ~11GB)")
# 1. Zap the disk (destroy all data)
run_command(["sgdisk", "-Z", disk_device])
# 2. Create new GPT table
run_command(["sgdisk", "-o", disk_device])
# 3. Create EFI Partition (Part 1, Start)
run_command(
[
"sgdisk",
"-n",
f"1:0:+{efi_mb}M",
"-t",
"1:ef00",
"-c",
"1:EFI System",
disk_device,
]
)
# 4. Create Swap Partition (Part 3, End) - If enabled
if use_swap:
# sgdisk negative start is from end of disk
# We use partition 3 for Swap
run_command(
[
"sgdisk",
"-n",
f"3:-{swap_mb}M:0",
"-t",
"3:8200",
"-c",
"3:Swap",
disk_device,
]
)
# 5. Create Root Partition (Part 2, Fill Gap)
# This fills the space between P1 and P3 (or end if no swap)
run_command(["sgdisk", "-n", "2:0:0", "-t", "2:8300", "-c", "2:Root", disk_device])
# Inform kernel of changes
run_command(["partprobe", disk_device])
import time
time.sleep(2)
time.sleep(1)
# Identify partitions
if uefi:
efi_part = get_partition_device(disk_device, 1)
root_part = get_partition_device(disk_device, 2)
swap_part = get_partition_device(disk_device, 3) if use_swap else None
else:
efi_part = None # BIOS doesn't use ESP
root_part = get_partition_device(disk_device, 1)
swap_part = get_partition_device(disk_device, 2) if use_swap else None
# 6. Format Partitions
efi_part = get_partition_device(disk_device, 1)
root_part = get_partition_device(disk_device, 2)
swap_part = get_partition_device(disk_device, 3) if use_swap else None
logger.info("Formatting EFI partition...")
run_command(["mkfs.vfat", "-F32", efi_part])
# Format
if uefi:
logger.info(f"Formatting EFI partition {efi_part}...")
run_command(["mkfs.vfat", "-F32", efi_part])
if use_swap:
logger.info("Formatting Swap partition...")
logger.info(f"Formatting Swap partition {swap_part}...")
run_command(["mkswap", swap_part])
logger.info("Formatting Root partition...")
logger.info(f"Formatting Root partition {root_part}...")
run_command(["mkfs.ext4", "-F", root_part])
logger.info("Partitioning and formatting complete.")
log_disk_operation(
"PARTITION",
"complete",
f"EFI: {efi_part}, Root: {root_part}, Swap: {swap_part or 'none'}",
)
result = {"efi": efi_part, "root": root_part}
if use_swap:
result["swap"] = swap_part
else:
# If no swap, we should probably return None or handle it in fstab generation
# backend/os_install.py expects "swap" key for UUID.
# We should probably pass a dummy or None, and update os_install to handle it.
result["swap"] = None
result = {"efi": efi_part, "root": root_part, "swap": swap_part}
log_disk_operation("PARTITION", "complete", str(result))
return result
@@ -253,32 +227,29 @@ def mount_partitions(partition_info, mount_root="/mnt"):
Mounts the partitions into mount_root.
"""
import os
log_disk_operation("MOUNT", "start", f"Mounting to {mount_root}")
# 1. Mount Root
if not os.path.exists(mount_root):
os.makedirs(mount_root)
run_command(["mount", partition_info["root"], mount_root])
# 2. Mount EFI
efi_mount = os.path.join(mount_root, "boot")
if not os.path.exists(efi_mount):
# 2. Mount EFI (if exists)
if partition_info.get("efi"):
efi_mount = os.path.join(mount_root, "boot/efi")
os.makedirs(efi_mount, exist_ok=True)
run_command(["mount", partition_info["efi"], efi_mount])
run_command(["mount", partition_info["efi"], efi_mount])
else:
# For BIOS, /boot is just a directory on root or we could make a separate /boot
# Current logic keeps it on root.
pass
# 3. Enable Swap
if partition_info.get("swap"):
run_command(["swapon", partition_info["swap"]])
logger.info(f"Partitions mounted at {mount_root}")
log_disk_operation(
"MOUNT",
"complete",
f"Root: {partition_info['root']}, EFI: {partition_info['efi']}",
)
log_disk_operation("MOUNT", "complete", f"Root: {partition_info['root']}")
def create_partition(