fix: mount pseudo-fs during install, simplify partitioning, and add mount point config

This commit is contained in:
2026-02-03 20:49:41 +01:00
parent dc417d15d3
commit 848b2e7e74
5 changed files with 70 additions and 140 deletions

View File

@@ -1,6 +1,7 @@
import subprocess
import logging
import os
from contextlib import contextmanager
logger = logging.getLogger(__name__)
@@ -13,6 +14,30 @@ def run_command(cmd, check=True):
logger.error(f"Command failed: {e.stderr}")
raise
@contextmanager
def mount_pseudo_fs(mount_root):
"""
Context manager to bind mount /dev, /proc, and /sys into mount_root.
"""
logger.info(f"Mounting pseudo-filesystems to {mount_root}...")
mounts = ["dev", "proc", "sys"]
mounted_paths = []
try:
for fs in mounts:
target = os.path.join(mount_root, fs)
os.makedirs(target, exist_ok=True)
run_command(["mount", "--bind", f"/{fs}", target])
mounted_paths.append(target)
yield
finally:
logger.info(f"Unmounting pseudo-filesystems from {mount_root}...")
for path in reversed(mounted_paths):
try:
run_command(["umount", "-l", path])
except Exception as e:
logger.warning(f"Failed to unmount {path}: {e}")
def install_minimal_os(mount_root, releasever="41"):
"""
Installs minimal Fedora packages to mount_root.
@@ -34,9 +59,6 @@ def install_minimal_os(mount_root, releasever="41"):
"vim-minimal"
]
# Fedora 41 (or whatever is current)
# We use --releasever to ensure dnf knows which version to fetch
# We use --use-host-config because the installroot is initially empty and has no repo config
cmd = [
"dnf", "install", "-y",
f"--installroot={mount_root}",
@@ -46,7 +68,9 @@ def install_minimal_os(mount_root, releasever="41"):
"--nodocs"
] + packages
run_command(cmd)
with mount_pseudo_fs(mount_root):
run_command(cmd)
logger.info("Base system installation complete.")
def configure_system(mount_root, partition_info):
@@ -56,7 +80,6 @@ def configure_system(mount_root, partition_info):
logger.info("Configuring system...")
# 1. Generate fstab
# We can get UUIDs using blkid
def get_uuid(dev):
res = run_command(["blkid", "-s", "UUID", "-o", "value", dev])
return res.stdout.strip()
@@ -70,30 +93,14 @@ UUID={root_uuid} / ext4 defaults 1 1
UUID={efi_uuid} /boot/efi vfat defaults 0 2
UUID={swap_uuid} none swap defaults 0 0
"""
os.makedirs(os.path.join(mount_root, "etc"), exist_ok=True)
with open(os.path.join(mount_root, "etc/fstab"), "w") as f:
f.write(fstab_content)
# 2. Configure GRUB
# This is tricky because we need to chroot or use --boot-directory
# Simplest for "barely bootable shell" is to try running grub2-mkconfig inside chroot
# Need to bind mount dev, proc, sys for chroot
for dev in ["dev", "proc", "sys"]:
target = os.path.join(mount_root, dev)
run_command(["mount", "--bind", f"/{dev}", target])
try:
# grub2-mkconfig -o /boot/grub2/grub.cfg (or wherever Fedora puts it)
# On UEFI Fedora: /boot/efi/EFI/fedora/grub.cfg usually just redirects to /boot/grub2/grub.cfg
# Let's just do both or standard one.
with mount_pseudo_fs(mount_root):
# grub2-mkconfig -o /boot/grub2/grub.cfg
chroot_cmd = ["chroot", mount_root, "grub2-mkconfig", "-o", "/boot/grub2/grub.cfg"]
run_command(chroot_cmd)
# Also need to make sure EFI boot entry is there, but shim/grub2 packages usually handle it in %post?
# Maybe not in --installroot.
finally:
# Unmount binds
for dev in ["sys", "proc", "dev"]:
run_command(["umount", os.path.join(mount_root, dev)])
logger.info("System configuration complete.")