# -*- 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 enable a list of services within a chroot environment by creating symbolic links in the appropriate runit service directory. 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 logging import sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, BASE_DIR) try: from builder.configs import logger_config logger = logger_config.setup_logger('enable_services') except ImportError as e: print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.") sys.exit(1) def enable_services_in_chroot(rootfs_path, service_list): """ Enables a list of services within the chroot environment by creating symbolic links. MODIFIED to use source paths relative to the rootfs. Args: rootfs_path (str): Path to the rootfs directory of the chroot. service_list (list): A list of service names to enable. """ logger.info("=> Starting service activation in chroot (with relative source paths)...") logger.info(f"=> List of services to enable: {service_list}") for service_name in service_list: # Source path within the rootfs (for the symbolic link) service_source_path_within_rootfs = os.path.join('/', 'etc', 'sv', service_name) # Source path on the host file system (for existence check) service_source_path_on_host = os.path.join(rootfs_path, 'etc', 'sv', service_name) service_target_path_chroot = os.path.join(rootfs_path, 'etc', 'runit', 'runsvdir', 'default', service_name) logger.debug(f"=> Checking if the service exists IN CHROOT: {service_source_path_on_host}") if not os.path.exists(service_source_path_on_host): error_msg = f"Service '{service_name}' not found IN CHROOT at: {service_source_path_on_host}" logger.error(error_msg) raise FileNotFoundError(error_msg) else: logger.debug(f"=> Service found IN CHROOT: {service_source_path_on_host}") logger.info(f"=> Creating symbolic link for service '{service_name}' in chroot: {service_target_path_chroot} -> {service_source_path_within_rootfs}") try: if os.path.lexists(service_target_path_chroot): logger.debug(f"=> Symbolic link already exists at: {service_target_path_chroot}. Removing before creating again.") os.unlink(service_target_path_chroot) os.symlink(service_source_path_within_rootfs, service_target_path_chroot) logger.info(f"=> Symbolic link for service '{service_name}' created successfully.") except OSError as e: logger.error(f"Error creating symbolic link for service '{service_name}' at: {service_target_path_chroot}: {e}") raise logger.info("=> Service activation in chroot completed.")