pmbootstrap-meow/pmb/helpers/mount.py
Caleb Connolly 198f302a36
treewide: add a Chroot type and adopt pathlib.Path (MR 2252)
Introduce a new module: pmb.core to contain explicitly typed pmbootstrap
API. The first component being Suffix and SuffixType. This explicitly
defines what suffixes are possible, future changes should aim to further
constrain this API (e.g. by validating against available device
codenames or architectures for buildroot suffixes).

Additionally, migrate the entire codebase over to using pathlib.Path.
This is a relatively new part of the Python standard library that uses a
more object oriented model for path handling. It also uses strong type
hinting and has other features that make it much cleaner and easier to
work with than pure f-strings. The Chroot class overloads the "/"
operator the same way the Path object does, allowing one to write paths
relative to a given chroot as:

builddir = chroot / "home/pmos/build"

The Chroot class also has a string representation ("native", or
"rootfs_valve-jupiter"), and a .path property for directly accessing the
absolute path (as a Path object).

The general idea here is to encapsulate common patterns into type hinted
code, and gradually reduce the amount of assumptions made around the
codebase so that future changes are easier to implement.

As the chroot suffixes are now part of the Chroot class, we also
implement validation for them, this encodes the rules on suffix naming
and will cause a runtime exception if a suffix doesn't follow the rules.
2024-06-23 12:38:37 +02:00

120 lines
3.9 KiB
Python

# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
import os
from pathlib import Path, PurePath
from typing import List
from pmb.core.types import PmbArgs
import pmb.helpers
from pmb.core import Chroot
import pmb.helpers.run
def ismount(folder: Path):
"""Ismount() implementation that works for mount --bind.
Workaround for: https://bugs.python.org/issue29707
"""
folder = folder.resolve()
with open("/proc/mounts", "r") as handle:
for line in handle:
words = line.split()
if len(words) >= 2 and Path(words[1]) == folder:
return True
if words[0] == folder:
return True
return False
def bind(args: PmbArgs, source: Path, destination: Path, create_folders=True, umount=False):
"""Mount --bind a folder and create necessary directory structure.
:param umount: when destination is already a mount point, umount it first.
"""
# Check/umount destination
if ismount(destination):
if umount:
umount_all(args, destination)
else:
return
# Check/create folders
for path in [source, destination]:
if os.path.exists(path):
continue
if create_folders:
pmb.helpers.run.root(args, ["mkdir", "-p", path])
else:
raise RuntimeError("Mount failed, folder does not exist: " +
path)
# Actually mount the folder
pmb.helpers.run.root(args, ["mount", "--bind", source, destination])
# Verify that it has worked
if not ismount(destination):
raise RuntimeError(f"Mount failed: {source} -> {destination}")
def bind_file(args: PmbArgs, source: Path, destination: Path, create_folders=False):
"""Mount a file with the --bind option, and create the destination file, if necessary."""
# Skip existing mountpoint
if ismount(destination):
return
# Create empty file
if not destination.exists():
if create_folders:
dest_dir: Path = destination.parent
if not dest_dir.is_dir():
pmb.helpers.run.root(args, ["mkdir", "-p", dest_dir])
pmb.helpers.run.root(args, ["touch", destination])
# Mount
pmb.helpers.run.root(args, ["mount", "--bind", source,
destination])
def umount_all_list(prefix: Path, source: Path=Path("/proc/mounts")) -> List[Path]:
"""Parse `/proc/mounts` for all folders beginning with a prefix.
:source: can be changed for testcases
:returns: a list of folders that need to be umounted
"""
ret = []
prefix = prefix.resolve()
with source.open() as handle:
for line in handle:
words = line.split()
if len(words) < 2:
raise RuntimeError("Failed to parse line in " + source + ": " +
line)
mountpoint = Path(words[1].replace(r"\040(deleted)", ""))
if mountpoint.is_relative_to(prefix): # is subpath
ret.append(mountpoint)
ret.sort(reverse=True)
return ret
def umount_all(args: PmbArgs, folder: Path):
"""Umount all folders that are mounted inside a given folder."""
for mountpoint in umount_all_list(folder):
pmb.helpers.run.root(args, ["umount", mountpoint])
if ismount(mountpoint):
raise RuntimeError("Failed to umount: " + mountpoint)
def mount_device_rootfs(args: PmbArgs, chroot_rootfs: Chroot) -> PurePath:
"""
Mount the device rootfs.
:param chroot_rootfs: the chroot where the rootfs that will be
installed on the device has been created (e.g.
"rootfs_qemu-amd64")
:returns: the mountpoint (relative to the native chroot)
"""
mountpoint = PurePath("/mnt", chroot_rootfs.dirname())
pmb.helpers.mount.bind(args, chroot_rootfs.path,
Chroot.native() / mountpoint)
return mountpoint