Files
installer/iridium_installer/backend/os_install.py
2026-02-05 12:48:36 +01:00

216 lines
6.5 KiB
Python

import logging
import os
import subprocess
import sys
from contextlib import contextmanager
logger = logging.getLogger(__name__)
# Import network logging for critical operations
from .network_logging import log_to_discord
def log_os_install(operation, status="start", details=""):
log_to_discord(
"INFO", f"OSINSTALL_{operation}_{status}: {details}", module="os_install"
)
class CommandResult:
def __init__(self, stdout, stderr, returncode):
self.stdout = stdout
self.stderr = stderr
self.returncode = returncode
def run_command(cmd, check=True):
logger.info(f"Running command: {' '.join(cmd)}")
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # Line buffered
)
stdout_lines = []
stderr_lines = []
# Helper to read stream
def read_stream(stream, line_list, log_level):
for line in stream:
line_clean = line.strip()
if line_clean:
log_level(line_clean)
line_list.append(line)
import threading
t1 = threading.Thread(
target=read_stream, args=(process.stdout, stdout_lines, logger.info)
)
t2 = threading.Thread(
target=read_stream, args=(process.stderr, stderr_lines, logger.error)
)
t1.start()
t2.start()
t1.join()
t2.join()
returncode = process.wait()
stdout_str = "".join(stdout_lines)
stderr_str = "".join(stderr_lines)
if check and returncode != 0:
raise subprocess.CalledProcessError(
returncode, cmd, output=stdout_str, stderr=stderr_str
)
return CommandResult(stdout_str, stderr_str, returncode)
@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="43"):
"""
Installs minimal Fedora packages to mount_root.
"""
logger.info(f"Installing minimal Fedora {releasever} to {mount_root}...")
log_os_install("INSTALL", "start", f"Target: {mount_root}, Release: {releasever}")
packages = [
"basesystem",
"bash",
"coreutils",
"kernel",
"systemd",
"dnf",
"efibootmgr",
"passwd",
"rootfiles",
"vim-minimal",
]
# Offline installation logic
iso_repo = "/run/install/repo"
dnf_offline_args = []
if os.path.exists(iso_repo):
logger.info(f"Found ISO repository at {iso_repo}. Using offline mode.")
dnf_offline_args = [
"--disablerepo=*",
f"--repofrompath=iridium-iso,{iso_repo}",
"--enablerepo=iridium-iso"
]
else:
logger.warning(f"ISO repository not found at {iso_repo}. Falling back to host configuration.")
cmd = [
"dnf",
"install",
"-y",
f"--installroot={mount_root}",
f"--releasever={releasever}",
"--use-host-config",
"--setopt=install_weak_deps=False",
"--nodocs",
] + dnf_offline_args + packages
with mount_pseudo_fs(mount_root):
run_command(cmd)
logger.info("Base system installation complete.")
log_os_install("INSTALL", "complete", f"Installed to {mount_root}")
def configure_system(mount_root, partition_info):
"""
Basic configuration: fstab and systemd-boot.
"""
logger.info("Configuring system...")
log_os_install("CONFIGURE", "start", f"Configuring system in {mount_root}")
# 1. Generate fstab
def get_uuid(dev):
res = run_command(["blkid", "-s", "UUID", "-o", "value", dev])
return res.stdout.strip()
root_uuid = get_uuid(partition_info["root"])
efi_uuid = get_uuid(partition_info["efi"])
swap_entry = ""
if partition_info.get("swap"):
swap_uuid = get_uuid(partition_info["swap"])
swap_entry = f"UUID={swap_uuid} none swap defaults 0 0\n"
fstab_content = f"""
UUID={root_uuid} / ext4 defaults 1 1
UUID={efi_uuid} /boot vfat defaults 0 2
{swap_entry}
"""
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 systemd-boot
with mount_pseudo_fs(mount_root):
# Ensure machine-id exists for kernel-install
if not os.path.exists(os.path.join(mount_root, "etc/machine-id")):
run_command(["chroot", mount_root, "systemd-machine-id-setup"])
# Set kernel command line
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")
# Install systemd-boot to the ESP (mounted at /boot)
run_command(["chroot", mount_root, "bootctl", "install"])
# 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}...")
kernel_image = f"/lib/modules/{kver}/vmlinuz"
# In Fedora, the initrd is usually created in /boot/initramfs-<version>.img
# During installation, dnf should have triggered dracut.
initrd_path = f"/boot/initramfs-{kver}.img"
cmd = ["chroot", mount_root, "kernel-install", "add", kver, kernel_image]
if os.path.exists(os.path.join(mount_root, initrd_path.lstrip("/"))):
cmd.append(initrd_path)
run_command(cmd)
logger.info("System configuration complete.")
log_os_install("CONFIGURE", "complete", "systemd-boot configured successfully")