1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-13 03:19:47 +03:00
pmbootstrap/pmb/chroot/shutdown.py
Casey Connolly 9f8edf539d treewide: split chroots from workdir
Introduce a new "cache" subdirectory in the pmbootstrap workdir, all the
cache and config bits go in here, anything that needs to be accessible
from inside a chroot. The whole dir is then bind-mounted into the chroot
as /cache with appropriate symlinks.

This dir is in the config as config.cache.

In addition, all the cache_* and other config dirs are renamed to
be closer to the names of the equivalent dirs in the chroot (e.g.
abuild-config) and to avoid redundant naming since they are now under a
"cache" dir.

Signed-off-by: Casey Connolly <kcxt@postmarketos.org>
2025-07-11 19:36:23 +02:00

50 lines
2 KiB
Python

# Copyright 2023 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
from pmb.helpers import logging
import pmb.chroot
import pmb.helpers.mount
from pmb.core import Chroot
from pmb.core.context import get_context
def shutdown_cryptsetup_device(name: str) -> None:
"""
:param name: cryptsetup device name, usually "pm_crypt" in pmbootstrap
"""
if not (Chroot.native() / "dev/mapper" / name).exists():
return
pmb.chroot.apk.install(["cryptsetup"], Chroot.native())
status = pmb.chroot.root(["cryptsetup", "status", name], output_return=True, check=False)
if not status:
logging.warning(
"WARNING: Failed to run cryptsetup to get the status"
" for " + name + ", assuming it is not mounted"
" (shutdown fails later if it is)!"
)
return
if status.startswith("/dev/mapper/" + name + " is active."):
pmb.chroot.root(["cryptsetup", "luksClose", name])
elif status.startswith("/dev/mapper/" + name + " is inactive."):
# When "cryptsetup status" fails, the device is not mounted and we
# have a left over file (#83)
pmb.chroot.root(["rm", "/dev/mapper/" + name])
else:
raise RuntimeError("Failed to parse 'cryptsetup status' output!")
def shutdown(only_install_related: bool = False) -> None:
# Umount installation-related paths (order is important!)
# pmb.helpers.mount.umount_all(chroot / "mnt/install")
shutdown_cryptsetup_device("pm_crypt")
# Remove "in-pmbootstrap" marker from all chroots. This marker indicates
# that pmbootstrap has set up all mount points etc. to run programs inside
# the chroots, but we want it gone afterwards (e.g. when the chroot
# contents get copied to a rootfs / installer image, or if creating an
# android recovery zip from its contents).
for marker in get_context().config.work.glob("chroot_*/in-pmbootstrap"):
pmb.helpers.run.root(["rm", marker])
logging.debug("Shutdown complete")