mirror of
https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git
synced 2025-07-17 13:25:10 +03:00
Refactor the install code to stop using loop devices and instead create and manipulate a disk image directly. Both ext4 and vfat have mechanisms for formatting and populating partitions at an offset inside an image, other filesystems likely do as well but so far have not been implemented or tested. With this "pmbootstrap install" works for standard EFI disk images (e.g. QEMU, X64 or trailblazer) entirely rootless. Since the creation of the disk images happens in the same user namespace as everything else, the resulting disk images have correct ownership and permissions even though from the host perspective they are all subuids. This gets image building working properly *for the default case*. We can now build disk images! In particular, we can build disk images with a 4k sector size even on a host with a 512 byte sector size (or block size in the filesystem). This is surprisingly hard for some reason since not all libfdisk tools have the right flags. Thankfully sfdisk does. In addition, we now generate UUIDs ourselves, to break the loop between generating fstab and running mkfs (since we also populate the disk image /with/ mkfs, we need to already know the UUID when we run it...). Signed-off-by: Casey Connolly <kcxt@postmarketos.org>
122 lines
3.9 KiB
Python
122 lines
3.9 KiB
Python
# Copyright 2023 Oliver Smith
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
import os
|
|
from pathlib import Path, PurePath
|
|
import pmb.helpers
|
|
from pmb.core import Chroot
|
|
from pmb.types import PathString
|
|
import pmb.helpers.run
|
|
from pmb.init import sandbox
|
|
|
|
|
|
def ismount(folder: Path) -> bool:
|
|
"""Ismount() implementation that works for mount --bind.
|
|
|
|
Workaround for: https://bugs.python.org/issue29707
|
|
"""
|
|
folder = folder.resolve()
|
|
with open("/proc/mounts") 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(
|
|
source: PathString, destination: Path, create_folders: bool = True, umount: bool = False
|
|
) -> None:
|
|
"""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(destination)
|
|
else:
|
|
return
|
|
|
|
# Check/create folders
|
|
for path in [source, destination]:
|
|
if os.path.exists(path):
|
|
continue
|
|
if create_folders:
|
|
pmb.helpers.run.root(["mkdir", "-p", path])
|
|
else:
|
|
raise RuntimeError(f"Mount failed, folder does not exist: {path}")
|
|
|
|
# Actually mount the folder
|
|
sandbox.mount_rbind(str(source), str(destination))
|
|
|
|
# Verify that it has worked
|
|
if not ismount(destination):
|
|
raise RuntimeError(f"Mount failed: {source} -> {destination}")
|
|
|
|
|
|
def bind_file(source: Path, destination: Path, create_folders: bool = False) -> None:
|
|
"""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():
|
|
os.makedirs(dest_dir, exist_ok=True)
|
|
|
|
with sandbox.umask(~0o644):
|
|
os.close(os.open(destination, os.O_CREAT | os.O_CLOEXEC | os.O_EXCL))
|
|
|
|
# Mount
|
|
pmb.logging.info(f"% mount --bind {source} {destination}")
|
|
sandbox.mount_rbind(str(source), str(destination), 0)
|
|
|
|
|
|
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(f"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(folder: Path) -> None:
|
|
"""Umount all folders that are mounted inside a given folder."""
|
|
for mountpoint in umount_all_list(folder):
|
|
if mountpoint.name != "binfmt_misc":
|
|
pmb.logging.info(f"% umount {mountpoint}")
|
|
sandbox.umount2(str(mountpoint), sandbox.MNT_DETACH)
|
|
|
|
|
|
def mount_device_rootfs(chroot_rootfs: Chroot, chroot_base: Chroot = Chroot.native()) -> 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")
|
|
:param chroot_base: the chroot rootfs mounted to
|
|
:returns: the mountpoint (relative to the chroot)
|
|
"""
|
|
mountpoint = PurePath("/mnt", str(chroot_rootfs))
|
|
pmb.helpers.mount.bind(chroot_rootfs.path, chroot_base / mountpoint)
|
|
return mountpoint
|