107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
import subprocess
|
|
import logging
|
|
import os
|
|
from contextlib import contextmanager
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def run_command(cmd, check=True):
|
|
logger.info(f"Running command: {' '.join(cmd)}")
|
|
try:
|
|
result = subprocess.run(cmd, check=check, capture_output=True, text=True)
|
|
return result
|
|
except subprocess.CalledProcessError as e:
|
|
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.
|
|
"""
|
|
logger.info(f"Installing minimal Fedora {releasever} to {mount_root}...")
|
|
|
|
packages = [
|
|
"basesystem",
|
|
"bash",
|
|
"coreutils",
|
|
"kernel",
|
|
"systemd",
|
|
"dnf",
|
|
"grub2-efi-x64",
|
|
"shim-x64",
|
|
"efibootmgr",
|
|
"passwd",
|
|
"rootfiles",
|
|
"vim-minimal"
|
|
]
|
|
|
|
cmd = [
|
|
"dnf", "install", "-y",
|
|
f"--installroot={mount_root}",
|
|
f"--releasever={releasever}",
|
|
"--use-host-config",
|
|
"--setopt=install_weak_deps=False",
|
|
"--nodocs"
|
|
] + packages
|
|
|
|
with mount_pseudo_fs(mount_root):
|
|
run_command(cmd)
|
|
|
|
logger.info("Base system installation complete.")
|
|
|
|
def configure_system(mount_root, partition_info):
|
|
"""
|
|
Basic configuration: fstab and grub.
|
|
"""
|
|
logger.info("Configuring system...")
|
|
|
|
# 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_uuid = get_uuid(partition_info["swap"])
|
|
|
|
fstab_content = f"""
|
|
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
|
|
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)
|
|
|
|
logger.info("System configuration complete.")
|