From e27bb822529ca994abf76af62c3ad26a3e77d6eb Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Thu, 3 Jun 2021 23:36:37 +0100 Subject: [PATCH] allow detecting when in chroot (MR 2063) Touch the file /in-pmbootstrap in chroots so that we can avoid performing automated actions that should only happen on a real device (like flashing the kernel). --- pmb/chroot/init.py | 11 +++++++++++ pmb/install/_install.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/pmb/chroot/init.py b/pmb/chroot/init.py index 34f8b62f..ce22aa50 100644 --- a/pmb/chroot/init.py +++ b/pmb/chroot/init.py @@ -30,6 +30,16 @@ def copy_resolv_conf(args, suffix="native"): pmb.helpers.run.root(args, ["touch", chroot]) +def mark_in_chroot(args, suffix="native"): + """ + Touch a flag so we can know when we're running in chroot (and + don't accidentally flash partitions on our host) + """ + in_chroot_file = f"{args.work}/chroot_{suffix}/in-pmbootstrap" + if not os.path.exists(in_chroot_file): + pmb.helpers.run.root(args, ["touch", in_chroot_file]) + + def setup_qemu_emulation(args, suffix): arch = pmb.parse.arch.from_chroot_suffix(args, suffix) if not pmb.parse.arch.cpu_emulation_required(args, arch): @@ -53,6 +63,7 @@ def init(args, suffix="native"): pmb.chroot.mount(args, suffix) setup_qemu_emulation(args, suffix) + mark_in_chroot(args, suffix) if os.path.islink(f"{chroot}/bin/sh"): pmb.config.workdir.chroot_check_channel(args, suffix) copy_resolv_conf(args, suffix) diff --git a/pmb/install/_install.py b/pmb/install/_install.py index f8e8f600..143ddfe5 100644 --- a/pmb/install/_install.py +++ b/pmb/install/_install.py @@ -710,6 +710,10 @@ def install_on_device_installer(args, step, steps): boot_label, "pmOS_install", args.split, args.sdcard) +def cleanup(args, suffix): + pmb.chroot.root(args, ["rm", "/in-pmbootstrap"], suffix) + + def create_device_rootfs(args, step, steps): # List all packages to be installed (including the ones specified by --add) # and upgrade the installed packages/apkindexes @@ -776,6 +780,7 @@ def create_device_rootfs(args, step, steps): setup_hostname(args) disable_sshd(args) + cleanup(args, suffix) def install(args):