# -*- coding: utf-8 -*- """ SPDX-FileCopyrightText: 2023-2025 PeppermintOS Team (peppermintosteam@proton.me) SPDX-License-Identifier: GPL-3.0-or-later This module provides a function to perform essential system reconfigurations within the chroot environment of a Void Linux system. It is based on the `mklive.sh` script (specifically the `install_packages()` function) and ensures that the base system is properly configured for the ISO. 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: - PeppermintOS Team (peppermintosteam@proton.me) - Development and maintenance of the project. License: This code is distributed under the GNU General Public License version 3 or later (GPL-3.0-or-later). For more details, please refer to the LICENSE file included in the project or visit: https://www.gnu.org/licenses/gpl-3.0.html """ import os import sys import subprocess import logging BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, BASE_DIR) try: from builder.core.xbps_commands import xbps_commands from builder.core.command_runner import run_command from builder.configs import logger_config logger = logger_config.setup_logger('system_reconfigure') except ImportError as e: print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.") sys.exit(1) def reconfigure_system_in_chroot(rootfs_path, architecture, repositories_list, base_cache_dir, locale="en_US.UTF-8"): """ Performs essential reconfigurations... (Docstring updated) Args: rootfs_path (str): Path to the rootfs directory of the chroot. architecture (str): Target architecture (e.g., 'x86_64'). repositories_list (list): List of repository dictionaries. base_cache_dir (str): Base path to the XBPS cache directory. locale (str, optional): UTF-8 locale to be configured (default: 'en_US.UTF-8'). """ logger.info("=> Starting Void Linux system reconfiguration in chroot...") xbps_repository_args = [] for repo_dict in repositories_list: repo_url = repo_dict['uri'] 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 = [] 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'] command_str = " ".join(reconfigure_base_files_cmd) logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}") result = run_command(reconfigure_base_files_cmd) if result.returncode != 0: logger.warning(f"Warning: Reconfiguration of base-files returned an error, but proceeding. Error: {result.stderr.decode()}") else: logger.info("=> (Step 4) Reconfiguration of base-files COMPLETE.") libc_locales_file = os.path.join(rootfs_path, 'etc', 'default', 'libc-locales') if os.path.exists(libc_locales_file): logger.info(f"=> (Step 5) Configuring UTF-8 locale in: {libc_locales_file}") try: with open(libc_locales_file, 'r') as f: lines = f.readlines() with open(libc_locales_file, 'w') as f: for line in lines: if line.strip().startswith(f"#{locale}"): f.write(line.replace("#", "", 1)) else: f.write(line) logger.info(f"=> UTF-8 locale '{locale}' configured in {libc_locales_file}.") except Exception as e: logger.error(f"Error configuring UTF-8 locale in {libc_locales_file}: {e}") logger.warning("Warning: Failed to configure UTF-8 locale, but proceeding.") else: 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...") # *** ADD LOGGING TO LIST DKMS PACKAGE FILES *** 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"] command_str = " ".join(reconfigure_dkms_cmd) logger.info(f"=> Executing command (ENV -i): {command_str}") result_dkms_reconfig = run_command(reconfigure_dkms_cmd) if result_dkms_reconfig.returncode != 0: logger.warning(f"Warning: DKMS reconfiguration returned an error, but proceeding. Error: {result_dkms_reconfig.stderr.decode()}") 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_all_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"]] + ["-a"] command_str = " ".join(reconfigure_all_cmd) logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}") result_reconfigure_all = run_command(reconfigure_all_cmd) 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()}") else: 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"] cmd_env = os.environ.copy() cmd_env['XBPS_ARCH'] = architecture command_str = " ".join(check_dash_cmd) logger.info(f"=> Executing command (XBPS_ARCH={architecture}): {command_str}") result_dash_check = run_command(check_dash_cmd, env=cmd_env) if result_dash_check.returncode == 0: logger.info("=> Dash found. Setting dash as /bin/sh...") set_dash_sh_cmd = [xbps_commands["XBPS_ALTERNATIVES_CMD"]] + ["-s", "dash"] command_str = " ".join(set_dash_sh_cmd) logger.info(f"=> Executing command [CHROOT: {rootfs_path}]: {command_str}") result_dash_set = run_command(set_dash_sh_cmd) 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: logger.info("=> Setting dash as /bin/sh COMPLETE.") else: logger.info("=> Dash not found in the chroot. Skipping configuration of dash as /bin/sh.") logger.info("=> Void Linux system reconfiguration in chroot COMPLETE.") logger.info("--------------------------------------------------------------------\n")