builder_peppermint_void/builder/core/mount_helper.py
2025-04-25 12:38:42 +00:00

81 lines
3.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 helper functions for mounting and unmounting essential virtual filesystems
within the chroot environment during the PeppermintOS build process. These functions are crucial
for allowing the builder to interact with the root filesystem as if it were the target system.
The module includes functions to mount `proc`, `sys`, and `dev` from the host system into the chroot
and to unmount them when they are no longer needed, ensuring a clean and isolated build 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 logging
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_DIR)
try:
from builder.core.bootstrap.paths import paths
from builder.core.command_runner import run_command
from builder.configs import logger_config # Import the logger_config module
logger = logger_config.setup_logger('mount_helper') # Pass the 'mount_helper' argument
except ImportError as e:
print(f"Error importing necessary modules: {e}. Ensure your environment is set up correctly.")
sys.exit(1)
def mount_essential_filesystems_in_chroot(paths, logger):
"""Mounts the essential virtual filesystems inside the chroot environment."""
rootfs_path = paths['ROOTFS']
logger.info("=> Mounting essential filesystems in the chroot...")
mount_points = ["proc", "sys", "dev"]
for point in mount_points:
source_path = f"/{point}"
target_path = os.path.join(rootfs_path, point)
mount_command = ["mount", "--bind", source_path, target_path]
logger.info(f"=> Mounting {point} to: {target_path}")
run_command(mount_command)
logger.info("=> Essential filesystems mounted in the chroot successfully.")
def unmount_essential_filesystems_in_chroot(paths, logger):
"""Unmounts the essential virtual filesystems from within the chroot environment, recursively and forcefully if necessary."""
rootfs_path = paths['ROOTFS']
logger.info("=> Unmounting essential filesystems from the chroot...")
mount_points = ["sys", "dev", "proc"] # Keeping the order to match the original script
for point in mount_points:
target_path = os.path.join(rootfs_path, point)
if os.path.isdir(target_path):
umount_command = ["umount", "-R", "-f", target_path]
logger.info(f"=> Recursively and forcefully unmounting {point} from: {target_path}")
result = run_command(umount_command)
if result.returncode != 0:
logger.error(f"=> Failed to unmount {target_path}: {result.stderr.strip()}")
else:
logger.info(f"=> {target_path} unmounted successfully.")
else:
logger.warning(f"=> Directory {target_path} not found, no need to unmount.")
logger.info("=> Essential filesystems unmounted from the chroot successfully.")