builder_peppermint_void/builder/core/system_reconfigure.py
2025-04-28 12:24:18 +00:00

145 lines
7.3 KiB
Python

# -*- 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
target root filesystem of a Void Linux system, executed from the host. It uses a chroot
helper for commands that require execution within the target environment.
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.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_in_target
from builder.configs import logger_config
logger = logger_config.setup_logger('system_reconfigure')
except ImportError as e:
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)
def reconfigure_system_in_chroot(rootfs_path, target_architecture, repositories_data, paths, locale="en_US.UTF-8"):
"""
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:
rootfs_path (str): Path to the target root filesystem directory.
target_architecture (str): Target architecture (e.g., 'aarch64').
repositories_data (list): List of repository dictionaries. (Note: Not directly used by all commands in target).
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').
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...")
if not rootfs_path or not os.path.isdir(rootfs_path):
logger.error(f"Invalid target root filesystem path: {rootfs_path}")
raise ValueError(f"Invalid target root filesystem path: {rootfs_path}")
logger.info("=> (Step 4) Forcing reconfiguration of base-files IN TARGET...")
reconfigure_base_files_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], '-f', 'base-files']
result = run_command_in_target(rootfs_path, reconfigure_base_files_cmd, target_architecture)
if result.returncode != 0:
logger.warning(f"Warning: Reconfiguration of base-files in target returned an error ({result.returncode}), but proceeding.")
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) IN TARGET...")
check_dkms_cmd = [xbps_commands["XBPS_QUERY_CMD"], 'dkms']
try:
result_dkms_check = run_command_in_target(rootfs_path, check_dkms_cmd, target_architecture, check=False)
if result_dkms_check.returncode == 0:
logger.info("=> DKMS found in target. Forcing DKMS reconfiguration IN TARGET...")
reconfigure_dkms_cmd = [xbps_commands["XBPS_RECONFIGURE_CMD"], "dkms"]
result_dkms_reconfig = run_command_in_target(rootfs_path, reconfigure_dkms_cmd, target_architecture, check=False)
if result_dkms_reconfig.returncode != 0:
logger.warning(f"Warning: DKMS reconfiguration in target returned an error ({result_dkms_reconfig.returncode}), but proceeding.")
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:
logger.warning(f"Warning: Reconfiguration of all packages (-a) in target returned an error ({result_reconfigure_all.returncode}), but proceeding.")
else:
logger.info("=> Reconfiguration of all packages (-a) COMPLETE.")
logger.info("=> (Step 8) Checking and configuring dash as /bin/sh (if dash installed) IN TARGET...")
check_dash_cmd = [xbps_commands["XBPS_QUERY_CMD"], "dash"]
try:
result_dash_check = run_command_in_target(rootfs_path, check_dash_cmd, target_architecture, check=False)
if result_dash_check.returncode == 0:
logger.info("=> Dash found in target. Setting dash as /bin/sh IN TARGET...")
set_dash_sh_cmd = [xbps_commands["XBPS_ALTERNATIVES_CMD"], "-s", "dash"]
result_dash_set = run_command_in_target(rootfs_path, set_dash_sh_cmd, target_architecture, check=False)
if result_dash_set.returncode != 0:
logger.warning(f"Warning: Failed to set dash as /bin/sh in target ({result_dash_set.returncode}), but proceeding.")
else:
logger.info("=> Setting dash as /bin/sh COMPLETE.")
else:
logger.info("=> Dash not found in the target. Skipping configuration of dash as /bin/sh.")
except FileNotFoundError:
logger.warning("Warning: xbps-query or xbps-alternatives command not found in target chroot. Skipping dash configuration.")
logger.info("=> Void Linux system reconfiguration COMPLETE.")
logger.info("--------------------------------------------------------------------\n")