From d0376ee0fb6bef4072e3dc1f5270c92c326c81d3 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Mon, 20 Jan 2020 13:38:50 +0100 Subject: [PATCH] pmb.chroot.init: mount --bind qemu-user binary (!1861) Avoid copying the qemu-user binary to each chroot by using a bind mount. This ensures that we remove it when we shutdown the chroot. --- pmb/chroot/init.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/pmb/chroot/init.py b/pmb/chroot/init.py index db28471f..9fc4a80c 100644 --- a/pmb/chroot/init.py +++ b/pmb/chroot/init.py @@ -45,16 +45,29 @@ def copy_resolv_conf(args, suffix="native"): pmb.helpers.run.root(args, ["touch", chroot]) +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): + return + + chroot = args.work + "/chroot_" + suffix + arch_qemu = pmb.parse.arch.alpine_to_qemu(arch) + + # mount --bind the qemu-user binary + pmb.chroot.binfmt.register(args, arch) + pmb.helpers.mount.bind_file(args, args.work + "/chroot_native/usr/bin/qemu-" + arch_qemu, + chroot + "/usr/bin/qemu-" + arch_qemu + "-static", + create_folders=True) + + def init(args, suffix="native"): # When already initialized: just prepare the chroot chroot = args.work + "/chroot_" + suffix arch = pmb.parse.arch.from_chroot_suffix(args, suffix) - emulate = pmb.parse.arch.cpu_emulation_required(args, arch) pmb.chroot.mount(args, suffix) + setup_qemu_emulation(args, suffix) if os.path.islink(chroot + "/bin/sh"): - if emulate: - pmb.chroot.binfmt.register(args, arch) copy_resolv_conf(args, suffix) pmb.chroot.apk.update_repository_list(args, suffix) return @@ -62,10 +75,6 @@ def init(args, suffix="native"): # Require apk-tools-static pmb.chroot.apk_static.init(args) - # Non-native chroot: set up QEMU with binfmt_misc - if emulate: - pmb.chroot.binfmt.register(args, arch) - logging.info("(" + suffix + ") install alpine-base") # Initialize cache @@ -80,14 +89,6 @@ def init(args, suffix="native"): copy_resolv_conf(args, suffix) pmb.chroot.apk.update_repository_list(args, suffix) - # Non-native chroot: install qemu-user-binary - if emulate: - arch_qemu = pmb.parse.arch.alpine_to_qemu(arch) - pmb.helpers.run.root(args, ["mkdir", "-p", chroot + "/usr/bin"]) - pmb.helpers.run.root(args, ["cp", args.work + - "/chroot_native/usr/bin/qemu-" + arch_qemu, - chroot + "/usr/bin/qemu-" + arch_qemu + "-static"]) - # Install alpine-base pmb.helpers.repo.update(args, arch) pmb.chroot.apk_static.run(args, ["--no-progress", "--root", chroot,