1
0
Fork 1
mirror of https://gitlab.postmarketos.org/postmarketOS/pmbootstrap.git synced 2025-07-24 21:15:10 +03:00
pmbootstrap/pmb/chroot/shutdown.py
Casey Connolly 9dc294dbbf pmbootstrap: unshare PID namespaec
Put ourselves in a new PID namespace so that daemons we might start in
the chroot like adbd will be killed on exit.

This simplifies "shutdown" since we no longer need to kill these
processes.

Signed-off-by: Casey Connolly <kcxt@postmarketos.org>
2025-06-13 21:47:42 +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.localdir.glob("chroot_*/in-pmbootstrap"):
pmb.helpers.run.root(["rm", marker])
logging.debug("Shutdown complete")