systemd installs: merge /usr on chroot init (MR 2273)

This commit is contained in:
Oliver Smith 2024-02-23 01:20:27 +01:00
parent e96ca36376
commit 6f3ed45d49
No known key found for this signature in database
GPG Key ID: 5AE7F5513E0885CB
5 changed files with 87 additions and 4 deletions

View File

@ -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

View File

@ -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)

View File

@ -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
#

11
pmb/config/other.py Normal file
View File

@ -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")

38
pmb/data/merge-usr.sh Normal file
View File

@ -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