new commit

This commit is contained in:
Manuel 2025-04-28 12:24:18 +00:00
parent eee4d3f035
commit ef20c72cb8

View File

@ -5,13 +5,9 @@ SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team
SPDX-License-Identifier: GPL-3.0-or-later SPDX-License-Identifier: GPL-3.0-or-later
This module provides a function to perform essential system reconfigurations within the chroot This module provides a function to perform essential system reconfigurations within the
environment of a Void Linux system. It is based on the `mklive.sh` script (specifically the target root filesystem of a Void Linux system, executed from the host. It uses a chroot
`install_packages()` function) and ensures that the base system is properly configured for the ISO. helper for commands that require execution within the target environment.
The module handles tasks such as reconfiguring base files, setting the default locale,
reconfiguring DKMS (if installed), reconfiguring all packages, and optionally setting `dash`
as the system shell (`/bin/sh`).
Credits: Credits:
- PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project. - PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project.
@ -27,65 +23,58 @@ import sys
import subprocess import subprocess
import logging import logging
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, BASE_DIR) sys.path.insert(0, BASE_DIR)
try: try:
from builder.core.xbps_commands import xbps_commands from builder.core.xbps_commands import xbps_commands
from builder.core.command_runner import run_command from builder.core.command_runner import run_command_in_target
from builder.configs import logger_config from builder.configs import logger_config
logger = logger_config.setup_logger('system_reconfigure') logger = logger_config.setup_logger('system_reconfigure')
except ImportError as e: except ImportError as e:
print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.") try:
basic_logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)
basic_logger.error(f"Error importing necessary modules for system_reconfigure: {e}. Ensure your environment is set up correctly.")
except Exception:
print(f"Error importing necessary modules for system_reconfigure: {e}. Ensure your environment is set up correctly.")
sys.exit(1) sys.exit(1)
def reconfigure_system_in_chroot(rootfs_path, architecture, repositories_list, base_cache_dir, locale="en_US.UTF-8"): def reconfigure_system_in_chroot(rootfs_path, target_architecture, repositories_data, paths, locale="en_US.UTF-8"):
""" """
Performs essential reconfigurations... (Docstring updated) Performs essential reconfigurations in the target root filesystem, executed from the host.
Uses run_command_in_target for commands requiring execution within the target environment.
Args: Args:
rootfs_path (str): Path to the rootfs directory of the chroot. rootfs_path (str): Path to the target root filesystem directory.
architecture (str): Target architecture (e.g., 'x86_64'). target_architecture (str): Target architecture (e.g., 'aarch64').
repositories_list (list): List of repository dictionaries. repositories_data (list): List of repository dictionaries. (Note: Not directly used by all commands in target).
base_cache_dir (str): Base path to the XBPS cache directory. paths (dict): Dictionary of build paths. (Needed for consistency or future use, though rootfs_path is explicit).
locale (str, optional): UTF-8 locale to be configured (default: 'en_US.UTF-8'). locale (str, optional): UTF-8 locale to be configured (default: 'en_US.UTF-8').
Raises:
subprocess.CalledProcessError: If a command executed in target fails.
FileNotFoundError: If the target rootfs or command is not found.
ValueError: If arguments are incorrect.
""" """
logger.info("=> Starting Void Linux system reconfiguration in chroot...") logger.info("=> Starting Void Linux system reconfiguration...")
xbps_repository_args = [] if not rootfs_path or not os.path.isdir(rootfs_path):
for repo_dict in repositories_list: logger.error(f"Invalid target root filesystem path: {rootfs_path}")
repo_url = repo_dict['uri'] raise ValueError(f"Invalid target root filesystem path: {rootfs_path}")
xbps_repository_args.extend(["-R", repo_url])
architecture_cache_dir = os.path.join(base_cache_dir, architecture)
xbps_cachedir_args = ["-c", architecture_cache_dir]
package_list = [] logger.info("=> (Step 4) Forcing reconfiguration of base-files IN TARGET...")
initramfs_pkgs = []
logger.info("=> (Step 2) Assuming pseudo-filesystems are already mounted in the chroot.")
if package_list or initramfs_pkgs:
logger.info("=> (Step 3) Reconfiguring/Updating packages...")
reinstall_step_cmd = ["LANG=C"] + ["XBPS_ARCH=" + architecture] + [xbps_commands.XBPS_INSTALL_CMD] + ["-U"] + xbps_repository_args + xbps_cachedir_args + ["-y"] + package_list + initramfs_pkgs
command_str = " ".join(reinstall_step_cmd)
logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}")
else:
logger.info("=> (Step 3) Package list for reconfiguration/update is empty. Skipping step.")
logger.info("=> (Step 4) Forcing reconfiguration of base-files...")
reconfigure_base_files_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], '-f', 'base-files'] reconfigure_base_files_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], '-f', 'base-files']
command_str = " ".join(reconfigure_base_files_cmd) result = run_command_in_target(rootfs_path, reconfigure_base_files_cmd, target_architecture)
logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}")
result = run_command(reconfigure_base_files_cmd)
if result.returncode != 0: if result.returncode != 0:
logger.warning(f"Warning: Reconfiguration of base-files returned an error, but proceeding. Error: {result.stderr.decode()}") logger.warning(f"Warning: Reconfiguration of base-files in target returned an error ({result.returncode}), but proceeding.")
else: else:
logger.info("=> (Step 4) Reconfiguration of base-files COMPLETE.") logger.info("=> (Step 4) Reconfiguration of base-files COMPLETE.")
libc_locales_file = os.path.join(rootfs_path, 'etc', 'default', 'libc-locales') libc_locales_file = os.path.join(rootfs_path, 'etc', 'default', 'libc-locales')
if os.path.exists(libc_locales_file): if os.path.exists(libc_locales_file):
logger.info(f"=> (Step 5) Configuring UTF-8 locale in: {libc_locales_file}") logger.info(f"=> (Step 5) Configuring UTF-8 locale in: {libc_locales_file}")
@ -105,60 +94,52 @@ def reconfigure_system_in_chroot(rootfs_path, architecture, repositories_list, b
else: else:
logger.info(f"=> (Step 5) Locale file {libc_locales_file} not found. Skipping locale configuration.") logger.info(f"=> (Step 5) Locale file {libc_locales_file} not found. Skipping locale configuration.")
logger.info("=> (Step 6) Checking and reconfiguring DKMS (if installed)...")
check_dkms_cmd = [xbps_commands["XBPS_QUERY_CMD"], '-r', rootfs_path, 'dkms']
result_dkms_check = run_command(check_dkms_cmd)
if result_dkms_check.returncode == 0:
logger.info("=> DKMS found. Forcing DKMS reconfiguration...")
list_dkms_files_cmd = [xbps_commands["XBPS_QUERY_CMD"], '-r', rootfs_path, '-f', 'dkms']
logger.info(f"=> Listing files installed by the dkms package inside the chroot...")
result_list_dkms_files = run_command(list_dkms_files_cmd)
if result_list_dkms_files.returncode == 0:
logger.debug(f"=> List of files from the dkms package:\n{result_list_dkms_files.stdout.strip()}")
else:
logger.warning(f"=> Failed to list files of the dkms package, but proceeding.")
reconfigure_dkms_cmd = ["env", "-i", xbps_commands["XBPS_RECONFIGURE_CMD"], '-r', rootfs_path, "dkms"] logger.info("=> (Step 6) Checking and reconfiguring DKMS (if installed) IN TARGET...")
command_str = " ".join(reconfigure_dkms_cmd) check_dkms_cmd = [xbps_commands["XBPS_QUERY_CMD"], 'dkms']
logger.info(f"=> Executing command (ENV -i): {command_str}") try:
result_dkms_reconfig = run_command(reconfigure_dkms_cmd) result_dkms_check = run_command_in_target(rootfs_path, check_dkms_cmd, target_architecture, check=False)
if result_dkms_reconfig.returncode != 0: if result_dkms_check.returncode == 0:
logger.warning(f"Warning: DKMS reconfiguration returned an error, but proceeding. Error: {result_dkms_reconfig.stderr.decode()}") logger.info("=> DKMS found in target. Forcing DKMS reconfiguration IN TARGET...")
else:
logger.info("=> DKMS reconfiguration COMPLETE.")
else:
logger.info("=> DKMS not found in the chroot. Skipping DKMS reconfiguration.")
logger.info("=> (Step 7) Reconfiguring all packages (-a)...") reconfigure_dkms_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], "dkms"]
reconfigure_all_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"]] + ["-a"] result_dkms_reconfig = run_command_in_target(rootfs_path, reconfigure_dkms_cmd, target_architecture, check=False)
command_str = " ".join(reconfigure_all_cmd) if result_dkms_reconfig.returncode != 0:
logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}") logger.warning(f"Warning: DKMS reconfiguration in target returned an error ({result_dkms_reconfig.returncode}), but proceeding.")
result_reconfigure_all = run_command(reconfigure_all_cmd) else:
logger.info("=> DKMS reconfiguration COMPLETE.")
else:
logger.info("=> DKMS not found in the target. Skipping DKMS reconfiguration.")
except FileNotFoundError:
logger.warning("Warning: xbps-query command not found in target chroot. Skipping DKMS check.")
logger.info("=> (Step 7) Reconfiguring all packages (-a) IN TARGET...")
reconfigure_all_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], "-a"]
result_reconfigure_all = run_command_in_target(rootfs_path, reconfigure_all_cmd, target_architecture, check=False)
if result_reconfigure_all.returncode != 0: if result_reconfigure_all.returncode != 0:
logger.warning(f"Warning: Reconfiguration of all packages (-a) returned an error, but proceeding. Error: {result_reconfigure_all.stderr.decode()}") logger.warning(f"Warning: Reconfiguration of all packages (-a) in target returned an error ({result_reconfigure_all.returncode}), but proceeding.")
else: else:
logger.info("=> Reconfiguration of all packages (-a) COMPLETE.") logger.info("=> Reconfiguration of all packages (-a) COMPLETE.")
logger.info("=> (Step 8) Checking and configuring dash as /bin/sh (if dash installed)...")
check_dash_cmd = [xbps_commands["XBPS_QUERY_CMD"], '-r', rootfs_path, "dash"] logger.info("=> (Step 8) Checking and configuring dash as /bin/sh (if dash installed) IN TARGET...")
cmd_env = os.environ.copy() check_dash_cmd = [xbps_commands["XBPS_QUERY_CMD"], "dash"]
cmd_env['XBPS_ARCH'] = architecture try:
command_str = " ".join(check_dash_cmd) result_dash_check = run_command_in_target(rootfs_path, check_dash_cmd, target_architecture, check=False)
logger.info(f"=> Executing command (XBPS_ARCH={architecture}): {command_str}") if result_dash_check.returncode == 0:
result_dash_check = run_command(check_dash_cmd, env=cmd_env) logger.info("=> Dash found in target. Setting dash as /bin/sh IN TARGET...")
if result_dash_check.returncode == 0: set_dash_sh_cmd = [xbps_commands["XBPS_ALTERNATIVES_CMD"], "-s", "dash"]
logger.info("=> Dash found. Setting dash as /bin/sh...") result_dash_set = run_command_in_target(rootfs_path, set_dash_sh_cmd, target_architecture, check=False)
set_dash_sh_cmd = [xbps_commands["XBPS_ALTERNATIVES_CMD"]] + ["-s", "dash"] if result_dash_set.returncode != 0:
command_str = " ".join(set_dash_sh_cmd) logger.warning(f"Warning: Failed to set dash as /bin/sh in target ({result_dash_set.returncode}), but proceeding.")
logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}") else:
result_dash_set = run_command(set_dash_sh_cmd) logger.info("=> Setting dash as /bin/sh COMPLETE.")
if result_dash_set.returncode != 0:
logger.warning(f"Warning: Failed to set dash as /bin/sh, but proceeding. Error: {result_dash_set.stderr.decode()}")
else: else:
logger.info("=> Setting dash as /bin/sh COMPLETE.") logger.info("=> Dash not found in the target. Skipping configuration of dash as /bin/sh.")
else: except FileNotFoundError:
logger.info("=> Dash not found in the chroot. Skipping configuration of dash as /bin/sh.") logger.warning("Warning: xbps-query or xbps-alternatives command not found in target chroot. Skipping dash configuration.")
logger.info("=> Void Linux system reconfiguration in chroot COMPLETE.") logger.info("=> Void Linux system reconfiguration COMPLETE.")
logger.info("--------------------------------------------------------------------\n") logger.info("--------------------------------------------------------------------\n")