1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-19 02:25:08 +03:00

Revert "chroot: allow mounting the device rootfs (MR 2252)"

This reverts commit f331b95824.
This commit is contained in:
Casey Connolly 2025-05-10 16:08:34 +02:00
parent d8886b086a
commit 9a6e32fb6d
No known key found for this signature in database
GPG key ID: 0583312B195F64B6
7 changed files with 26 additions and 64 deletions

View file

@ -1,6 +1,5 @@
# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
from pmb.core.chroot import ChrootType
from pmb.core.pkgrepo import pkgrepo_default_path
from pmb.helpers import logging
import os
@ -8,40 +7,28 @@ from pathlib import Path
import pmb.chroot.binfmt
import pmb.config
import pmb.helpers.run
import pmb.install.losetup
import pmb.helpers.mount
from pmb.core import Chroot
from pmb.core.context import get_context
from pmb.init import sandbox
def mount_chroot_image(chroot: Chroot) -> None:
"""Mount an IMAGE type chroot, to modify an existing rootfs image. This
doesn't support split images yet!"""
# Make sure everything is nicely unmounted just to be super safe
# this is definitely overkill
pmb.chroot.shutdown()
pmb.install.losetup.detach_all()
chroot_native = Chroot.native()
pmb.chroot.init(chroot_native)
loopdev = pmb.install.losetup.mount(
Path("/") / Path(chroot.name).relative_to(chroot_native.path)
)
pmb.helpers.mount.bind_file(loopdev, chroot_native / "dev/install")
# Set up device mapper bits
pmb.helpers.run.root(["kpartx", "-u", loopdev])
chroot.path.mkdir(exist_ok=True)
loopdev_basename = os.path.basename(loopdev)
# # The name of the IMAGE chroot is the path to the rootfs image
pmb.helpers.run.root(["mount", f"/dev/mapper/{loopdev_basename}p2", chroot.path])
pmb.helpers.run.root(["mount", f"/dev/mapper/{loopdev_basename}p1", chroot.path / "boot"])
pmb.config.workdir.chroot_save_init(chroot)
logging.info(f"({chroot}) mounted {chroot.name}")
def create_device_nodes(chroot: Chroot):
"""
Create device nodes for null, zero, full, random, urandom in the chroot.
"""
try:
# Create all device nodes as specified in the config
for dev in pmb.config.chroot_device_nodes:
path = chroot / "dev" / str(dev[4])
if not path.exists():
pmb.helpers.run.root(["mknod",
"-m", str(dev[0]), # permissions
path, # name
str(dev[1]), # type
str(dev[2]), # major
str(dev[3]), # minor
])
def mount_dev_tmpfs(chroot: Chroot = Chroot.native()) -> None:
"""
@ -60,12 +47,7 @@ def mount_dev_tmpfs(chroot: Chroot = Chroot.native()) -> None:
devop.execute("/", str(chroot.path))
def mount(chroot: Chroot) -> None:
if chroot.type == ChrootType.IMAGE and not pmb.mount.ismount(chroot.path):
mount_chroot_image(chroot)
if not chroot.path.exists():
os.mkdir(str(chroot.path))
def mount(chroot: Chroot):
# Mount tmpfs as the chroot's /dev
mount_dev_tmpfs(chroot)

View file

@ -15,7 +15,6 @@ class ChrootType(enum.Enum):
BUILDROOT = "buildroot"
INSTALLER = "installer"
NATIVE = "native"
IMAGE = "image"
def __str__(self) -> str:
return self.name
@ -58,15 +57,12 @@ class Chroot:
if self.__type == ChrootType.NATIVE and self.__name != "":
raise ValueError(f"The native suffix can't have a name but got: '{self.__name}'")
if self.__type == ChrootType.IMAGE and not Path(self.__name).exists():
raise ValueError(f"Image file '{self.__name}' does not exist")
# rootfs suffixes must have a valid device name
if self.__type == ChrootType.ROOTFS and (len(self.__name) < 3 or "-" not in self.__name):
raise ValueError(f"Invalid device name: '{self.__name}'")
def __str__(self) -> str:
if len(self.__name) > 0 and self.type != ChrootType.IMAGE:
if len(self.__name) > 0:
return f"{self.__type.value}_{self.__name}"
else:
return self.__type.value

View file

@ -66,14 +66,11 @@ def _parse_flavor(device: str, autoinstall: bool = True) -> str:
def _parse_suffix(args: PmbArgs) -> Chroot:
deviceinfo = pmb.parse.deviceinfo()
if getattr(args, "image", None):
rootfs = Chroot.native() / f"home/pmos/rootfs/{deviceinfo.codename}.img"
return Chroot(ChrootType.IMAGE, str(rootfs))
if getattr(args, "rootfs", None):
return Chroot(ChrootType.ROOTFS, get_context().config.device)
elif args.buildroot:
if args.buildroot == "device":
return Chroot.buildroot(deviceinfo.arch)
return Chroot.buildroot(pmb.parse.deviceinfo().arch)
else:
return Chroot.buildroot(Arch.from_str(args.buildroot))
elif args.suffix:
@ -150,18 +147,11 @@ def chroot(args: PmbArgs) -> None:
# Suffix
chroot = _parse_suffix(args)
user = args.user
if (
user
and chroot != Chroot.native()
and chroot.type not in [ChrootType.BUILDROOT, ChrootType.IMAGE]
):
if user and chroot != Chroot.native() and chroot.type != ChrootType.BUILDROOT:
raise RuntimeError("--user is only supported for native or buildroot_* chroots.")
if args.xauth and chroot != Chroot.native():
raise RuntimeError("--xauth is only supported for native chroot.")
if chroot.type == ChrootType.IMAGE:
pmb.chroot.mount(chroot)
# apk: check minimum version, install packages
pmb.chroot.apk.check_min_version(chroot)
if args.add:

View file

@ -48,6 +48,11 @@ def mount(img_path: Path, _sector_size: int | None = None) -> Path:
losetup_cmd += ["-b", str(int(sector_size))]
pmb.chroot.root(losetup_cmd, check=False)
try:
device_by_back_file(img_path)
return
except RuntimeError:
pass
try:
return device_by_back_file(img_path)
@ -100,6 +105,5 @@ def detach_all() -> None:
work = get_context().config.work
for loopdevice in losetup["loopdevices"]:
if Path(loopdevice["back-file"]).is_relative_to(work):
pmb.helpers.run.root(["kpartx", "-d", loopdevice["name"]], check=False)
pmb.helpers.run.root(["losetup", "-d", loopdevice["name"]])
pmb.chroot.root(["losetup", "-d", loopdevice["name"]])
return

View file

@ -1050,11 +1050,6 @@ def get_parser() -> argparse.ArgumentParser:
" 'background'. Details: pmb/helpers/run_core.py",
default="tui",
)
chroot.add_argument(
"--image",
help="Mount the rootfs image and treat it like a normal chroot.",
action="store_true",
)
chroot.add_argument(
"--usb",
help="Make USB devices accessible inside the chroot.",

View file

@ -419,10 +419,6 @@ def run(args: PmbArgs) -> None:
)
arch = pmb.parse.deviceinfo().arch
# Make sure the rootfs image isn't mounted
pmb.mount.umount_all(Chroot(ChrootType.IMAGE, "").path)
pmb.install.losetup.detach_all()
img_path = system_image(device)
img_path_2nd = None
if args.second_storage:

View file

@ -159,7 +159,6 @@ class PmbArgs(Namespace):
http: bool
ignore_depends: bool
image_size: str
image: bool
install_base: bool
install_blockdev: bool
install_cgpt: bool