From 6f3ed45d49f5ff53cf9e7a742a4ec2fd3426fdd2 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Fri, 23 Feb 2024 01:20:27 +0100 Subject: [PATCH] systemd installs: merge /usr on chroot init (MR 2273) --- pmb/chroot/__init__.py | 2 +- pmb/chroot/init.py | 39 ++++++++++++++++++++++++++++++++++++--- pmb/config/__init__.py | 1 + pmb/config/other.py | 11 +++++++++++ pmb/data/merge-usr.sh | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 pmb/config/other.py create mode 100644 pmb/data/merge-usr.sh diff --git a/pmb/chroot/__init__.py b/pmb/chroot/__init__.py index 01b50a01..8d675d54 100644 --- a/pmb/chroot/__init__.py +++ b/pmb/chroot/__init__.py @@ -1,6 +1,6 @@ # Copyright 2023 Oliver Smith # SPDX-License-Identifier: GPL-3.0-or-later -from pmb.chroot.init import init, init_keys +from pmb.chroot.init import init, init_keys, UsrMerge from pmb.chroot.mount import mount, mount_native_into_foreign, remove_mnt_pmbootstrap from pmb.chroot.root import root from pmb.chroot.user import user diff --git a/pmb/chroot/init.py b/pmb/chroot/init.py index e2622ffc..104bf210 100644 --- a/pmb/chroot/init.py +++ b/pmb/chroot/init.py @@ -1,9 +1,10 @@ # Copyright 2023 Oliver Smith # SPDX-License-Identifier: GPL-3.0-or-later +import enum +import filecmp +import glob import logging import os -import glob -import filecmp import pmb.chroot import pmb.chroot.apk_static @@ -14,6 +15,16 @@ import pmb.helpers.run import pmb.parse.arch +class UsrMerge(enum.Enum): + """ + Merge /usr while initializing chroot. + https://systemd.io/THE_CASE_FOR_THE_USR_MERGE/ + """ + AUTO = 0 + ON = 1 + OFF = 2 + + def copy_resolv_conf(args, suffix="native"): """ Use pythons super fast file compare function (due to caching) @@ -74,7 +85,23 @@ def init_keys(args): pmb.helpers.run.root(args, ["cp", key, target]) -def init(args, suffix="native"): +def init_usr_merge(args, suffix): + logging.info(f"({suffix}) merge /usr") + script = f"{pmb.config.pmb_src}/pmb/data/merge-usr.sh" + pmb.helpers.run.root(args, ["sh", "-e", script, "CALLED_FROM_PMB", + f"{args.work}/chroot_{suffix}"]) + + +def init(args, suffix="native", usr_merge=UsrMerge.AUTO): + """ + Initialize a chroot by copying the resolv.conf and updating + /etc/apk/repositories. If /bin/sh is missing, create the chroot from + scratch. + + :param usr_merge: set to ON to force having a merged /usr. With AUTO it is + only done if the user chose to install systemd in + pmbootstrap init. + """ # When already initialized: just prepare the chroot chroot = f"{args.work}/chroot_{suffix}" arch = pmb.parse.arch.from_chroot_suffix(args, suffix) @@ -127,3 +154,9 @@ def init(args, suffix="native"): pmb.chroot.root(args, ["mkdir", "-p", target], suffix) pmb.chroot.user(args, ["ln", "-s", target, link_name], suffix) pmb.chroot.root(args, ["chown", "pmos:pmos", target], suffix) + + # Merge /usr + if usr_merge is UsrMerge.AUTO and pmb.config.is_systemd_selected(args): + usr_merge = UsrMerge.ON + if usr_merge is UsrMerge.ON: + init_usr_merge(args, suffix) diff --git a/pmb/config/__init__.py b/pmb/config/__init__.py index 587b9d5a..28b5485c 100644 --- a/pmb/config/__init__.py +++ b/pmb/config/__init__.py @@ -13,6 +13,7 @@ from pmb.config.load import load from pmb.config.save import save from pmb.config.merge_with_args import merge_with_args from pmb.config.sudo import which_sudo +from pmb.config.other import is_systemd_selected # diff --git a/pmb/config/other.py b/pmb/config/other.py new file mode 100644 index 00000000..7a01e69d --- /dev/null +++ b/pmb/config/other.py @@ -0,0 +1,11 @@ +# Copyright 2024 Oliver Smith +# SPDX-License-Identifier: GPL-3.0-or-later +import pmb.helpers.ui + + +def is_systemd_selected(args): + if args.systemd == "always": + return True + if args.systemd == "never": + return False + return pmb.helpers.ui.check_option(args, args.ui, "pmb:systemd") diff --git a/pmb/data/merge-usr.sh b/pmb/data/merge-usr.sh new file mode 100644 index 00000000..8866e6a1 --- /dev/null +++ b/pmb/data/merge-usr.sh @@ -0,0 +1,38 @@ +#!/bin/sh -e +# Copyright 2024 Oliver Smith +# SPDX-License-Identifier: GPL-3.0-or-later + +if [ "$1" != "CALLED_FROM_PMB" ]; then + echo "ERROR: this script is only meant to be called by pmbootstrap" + exit 1 +fi + +CHROOT="$2" + +test -n "$CHROOT" +test -f "$CHROOT"/in-pmbootstrap + +if [ -L "$CHROOT"/bin ]; then + echo "ERROR: chroot has merged usr already: $CHROOT" + exit 1 +fi + +# /bin -> /usr/bin +mv "$CHROOT"/bin/* "$CHROOT"/usr/bin/ +rmdir "$CHROOT"/bin +ln -s usr/bin "$CHROOT"/bin + +# /sbin -> /usr/bin +mv "$CHROOT"/sbin/* "$CHROOT"/usr/bin/ +rmdir "$CHROOT"/sbin +ln -s usr/bin "$CHROOT"/sbin + +# /lib -> /usr/lib +mv "$CHROOT"/lib/* "$CHROOT"/usr/lib/ +rmdir "$CHROOT"/lib +ln -s usr/lib "$CHROOT"/lib + +# /usr/sbin -> /usr/bin +mv "$CHROOT"/usr/sbin/* "$CHROOT"/usr/bin/ +rmdir "$CHROOT"/usr/sbin +ln -s bin "$CHROOT"/usr/sbin