Update os_install.py

This commit is contained in:
2026-02-03 21:32:51 +01:00
parent b70ff549a7
commit e611f174be

View File

@@ -1,28 +1,30 @@
import subprocess
import logging import logging
import os import os
import subprocess
import sys import sys
from contextlib import contextmanager from contextlib import contextmanager
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class CommandResult: class CommandResult:
def __init__(self, stdout, stderr, returncode): def __init__(self, stdout, stderr, returncode):
self.stdout = stdout self.stdout = stdout
self.stderr = stderr self.stderr = stderr
self.returncode = returncode self.returncode = returncode
def run_command(cmd, check=True): def run_command(cmd, check=True):
logger.info(f"Running command: {' '.join(cmd)}") logger.info(f"Running command: {' '.join(cmd)}")
process = subprocess.Popen( process = subprocess.Popen(
cmd, cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
text=True, text=True,
bufsize=1 # Line buffered bufsize=1, # Line buffered
) )
stdout_lines = [] stdout_lines = []
stderr_lines = [] stderr_lines = []
@@ -35,25 +37,33 @@ def run_command(cmd, check=True):
line_list.append(line) line_list.append(line)
import threading 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 = 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() t1.start()
t2.start() t2.start()
t1.join() t1.join()
t2.join() t2.join()
returncode = process.wait() returncode = process.wait()
stdout_str = "".join(stdout_lines) stdout_str = "".join(stdout_lines)
stderr_str = "".join(stderr_lines) stderr_str = "".join(stderr_lines)
if check and returncode != 0: if check and returncode != 0:
raise subprocess.CalledProcessError(returncode, cmd, output=stdout_str, stderr=stderr_str) raise subprocess.CalledProcessError(
returncode, cmd, output=stdout_str, stderr=stderr_str
)
return CommandResult(stdout_str, stderr_str, returncode) return CommandResult(stdout_str, stderr_str, returncode)
@contextmanager @contextmanager
def mount_pseudo_fs(mount_root): def mount_pseudo_fs(mount_root):
""" """
@@ -62,7 +72,7 @@ def mount_pseudo_fs(mount_root):
logger.info(f"Mounting pseudo-filesystems to {mount_root}...") logger.info(f"Mounting pseudo-filesystems to {mount_root}...")
mounts = ["dev", "proc", "sys"] mounts = ["dev", "proc", "sys"]
mounted_paths = [] mounted_paths = []
try: try:
for fs in mounts: for fs in mounts:
target = os.path.join(mount_root, fs) target = os.path.join(mount_root, fs)
@@ -78,12 +88,13 @@ def mount_pseudo_fs(mount_root):
except Exception as e: except Exception as e:
logger.warning(f"Failed to unmount {path}: {e}") logger.warning(f"Failed to unmount {path}: {e}")
def install_minimal_os(mount_root, releasever="41"):
def install_minimal_os(mount_root, releasever="43"):
""" """
Installs minimal Fedora packages to mount_root. Installs minimal Fedora packages to mount_root.
""" """
logger.info(f"Installing minimal Fedora {releasever} to {mount_root}...") logger.info(f"Installing minimal Fedora {releasever} to {mount_root}...")
packages = [ packages = [
"basesystem", "basesystem",
"bash", "bash",
@@ -96,29 +107,32 @@ def install_minimal_os(mount_root, releasever="41"):
"efibootmgr", "efibootmgr",
"passwd", "passwd",
"rootfiles", "rootfiles",
"vim-minimal" "vim-minimal",
] ]
cmd = [ cmd = [
"dnf", "install", "-y", "dnf",
"install",
"-y",
f"--installroot={mount_root}", f"--installroot={mount_root}",
f"--releasever={releasever}", f"--releasever={releasever}",
"--use-host-config", "--use-host-config",
"--setopt=install_weak_deps=False", "--setopt=install_weak_deps=False",
"--nodocs" "--nodocs",
] + packages ] + packages
with mount_pseudo_fs(mount_root): with mount_pseudo_fs(mount_root):
run_command(cmd) run_command(cmd)
logger.info("Base system installation complete.") logger.info("Base system installation complete.")
def configure_system(mount_root, partition_info): def configure_system(mount_root, partition_info):
""" """
Basic configuration: fstab and grub. Basic configuration: fstab and grub.
""" """
logger.info("Configuring system...") logger.info("Configuring system...")
# 1. Generate fstab # 1. Generate fstab
def get_uuid(dev): def get_uuid(dev):
res = run_command(["blkid", "-s", "UUID", "-o", "value", dev]) res = run_command(["blkid", "-s", "UUID", "-o", "value", dev])
@@ -126,7 +140,7 @@ def configure_system(mount_root, partition_info):
root_uuid = get_uuid(partition_info["root"]) root_uuid = get_uuid(partition_info["root"])
efi_uuid = get_uuid(partition_info["efi"]) efi_uuid = get_uuid(partition_info["efi"])
swap_entry = "" swap_entry = ""
if partition_info.get("swap"): if partition_info.get("swap"):
swap_uuid = get_uuid(partition_info["swap"]) swap_uuid = get_uuid(partition_info["swap"])
@@ -140,11 +154,17 @@ UUID={efi_uuid} /boot/efi vfat defaults 0 2
os.makedirs(os.path.join(mount_root, "etc"), exist_ok=True) os.makedirs(os.path.join(mount_root, "etc"), exist_ok=True)
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(fstab_content) f.write(fstab_content)
# 2. Configure GRUB # 2. Configure GRUB
with mount_pseudo_fs(mount_root): with mount_pseudo_fs(mount_root):
# grub2-mkconfig -o /boot/grub2/grub.cfg # grub2-mkconfig -o /boot/grub2/grub.cfg
chroot_cmd = ["chroot", 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) run_command(chroot_cmd)
logger.info("System configuration complete.") logger.info("System configuration complete.")