V8.00.000.2025.06.17
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m18s
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m18s
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
@@ -58,102 +58,96 @@ prepare_mounts() {
|
||||
# shellcheck disable=SC2034
|
||||
VAR_CHROOT_SYS_MASK_HELPER=$(cat <<'EOF'
|
||||
#-------------------------------------------------------------------------------
|
||||
# Helpers: mount detection and conditional umount (idempotent).
|
||||
# Quick sys mask for initramfs build (minimalistic, idempotent)
|
||||
# - No strict mountpoint probing; we just try and remember what worked.
|
||||
# - Safe with 'set -e' because we branch on command success/failure.
|
||||
|
||||
# Detect if PATH is an exact mountpoint (not just covered by a parent mount).
|
||||
cdi_is_mountpoint() {
|
||||
declare path="${1:?target path required}"
|
||||
|
||||
if command -v mountpoint >/dev/null 2>&1; then
|
||||
|
||||
mountpoint -q -- "${path}"
|
||||
|
||||
return $?
|
||||
|
||||
fi
|
||||
|
||||
if command -v findmnt >/dev/null 2>&1; then
|
||||
|
||||
# Exact mountpoint check (not -T which matches enclosing mounts).
|
||||
findmnt -rn --mountpoint "${path}" >/dev/null 2>&1 && return 0 || return 1
|
||||
|
||||
fi
|
||||
|
||||
# Fallback: parse /proc/self/mountinfo (field 5 = mountpoint, no spaces here).
|
||||
awk -v p="${path}" '$5==p {found=1; exit} END{exit (found?0:1)}' /proc/self/mountinfo 2>/dev/null
|
||||
}
|
||||
|
||||
cdi_umount_if_mountpoint() {
|
||||
declare path="${1:?target path required}"
|
||||
|
||||
if cdi_is_mountpoint "${path}"; then
|
||||
|
||||
umount -l -- "${path}" || { printf 'ERROR: cannot umount %s\n' "${path}" >&2; return 128; }
|
||||
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
cdi_sys_mask_enter() {
|
||||
cdi_sys_quick_enter() {
|
||||
declare state="/run/.ciss_sysmask"
|
||||
declare did_unmount_cg="0"
|
||||
declare did_mask_sys="0"
|
||||
|
||||
echo "Inside chroot: cdi_sys_quick_enter() @ Start."
|
||||
|
||||
mkdir -p /run
|
||||
|
||||
# Record only true mountpoints.
|
||||
declare had_sys="0"
|
||||
declare had_cg="0"
|
||||
|
||||
cdi_is_mountpoint /sys && had_sys="1"
|
||||
cdi_is_mountpoint /sys/fs/cgroup && had_cg="1"
|
||||
|
||||
printf 'HAD_SYS=%s\nHAD_CG=%s\n' "${had_sys}" "${had_cg}" >| "${state}"
|
||||
|
||||
# Unmount only if exact mountpoints exist.
|
||||
cdi_umount_if_mountpoint /sys/fs/cgroup || return 129
|
||||
cdi_umount_if_mountpoint /sys || return 130
|
||||
|
||||
mkdir -p /sys
|
||||
mount -t tmpfs -o ro,nosuid,nodev,noexec,mode=0555,size=1M tmpfs /sys \
|
||||
|| { printf 'ERROR: cannot mount tmpfs on /sys\n' >&2; return 131; }
|
||||
|
||||
# 1) Try to unmount 'cgroup2' (ok if it wasn't mounted).
|
||||
if umount -l /sys/fs/cgroup 2>/dev/null; then
|
||||
|
||||
did_unmount_cg="1"
|
||||
|
||||
else
|
||||
|
||||
did_unmount_cg="0"
|
||||
|
||||
fi
|
||||
|
||||
# 2) Try to overlay '/sys' with a tiny read-only 'tmpfs' (mask host sysfs view).
|
||||
if mount -t tmpfs -o ro,nosuid,nodev,noexec,mode=0555,size=1M tmpfs /sys 2>/dev/null; then
|
||||
|
||||
did_mask_sys="1"
|
||||
|
||||
else
|
||||
|
||||
did_mask_sys="0"
|
||||
|
||||
fi
|
||||
|
||||
printf 'MASK_SYS=%s\nUNMOUNTED_CG=%s\n' "${did_mask_sys}" "${did_unmount_cg}" >| "${state}"
|
||||
|
||||
echo "${state}"
|
||||
|
||||
echo "Inside chroot: cdi_sys_quick_enter() @ End."
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
cdi_sys_mask_leave() {
|
||||
cdi_sys_quick_leave() {
|
||||
declare state="/run/.ciss_sysmask"
|
||||
declare had_sys="0"
|
||||
declare had_cg="0"
|
||||
declare did_mask_sys="0"
|
||||
declare did_unmount_cg="0"
|
||||
|
||||
echo "Inside chroot: cdi_sys_quick_leave() @ Start."
|
||||
|
||||
if [[ -f "${state}" ]]; then
|
||||
|
||||
# shellcheck disable=SC2155
|
||||
declare had_sys="$(grep -Eo '^HAD_SYS=[01]' "${state}" 2>/dev/null | cut -d= -f2 || printf '0')"
|
||||
declare did_mask_sys="$(grep -Eo '^MASK_SYS=[01]' "${state}" 2>/dev/null | cut -d= -f2 || printf '0')"
|
||||
|
||||
# shellcheck disable=SC2155
|
||||
declare had_cg="$(grep -Eo '^HAD_CG=[01]' "${state}" 2>/dev/null | cut -d= -f2 || printf '0')"
|
||||
declare did_unmount_cg="$(grep -Eo '^UNMOUNTED_CG=[01]' "${state}" 2>/dev/null | cut -d= -f2 || printf '0')"
|
||||
|
||||
fi
|
||||
|
||||
# Drop the mask if present (it is an exact mountpoint).
|
||||
cdi_umount_if_mountpoint /sys || return 132
|
||||
# 1) Drop '/sys' mask only if we actually overlaid it.
|
||||
if [[ "${did_mask_sys}" == "1" ]]; then
|
||||
|
||||
if [[ "${had_sys}" == "1" ]]; then
|
||||
if ! umount -l /sys 2>/dev/null; then
|
||||
|
||||
mount -t sysfs -o nosuid,nodev,noexec sysfs /sys \
|
||||
|| { printf 'ERROR: cannot mount sysfs on /sys\n' >&2; return 133; }
|
||||
printf 'WARN: could not unmount masked /sys (continuing)\n' >&2
|
||||
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
if [[ "${had_cg}" == "1" ]]; then
|
||||
# 2) Restore 'cgroup2' only if we unmounted it before.
|
||||
if [[ "${did_unmount_cg}" == "1" ]]; then
|
||||
|
||||
mkdir -p /sys/fs/cgroup
|
||||
mount -t cgroup2 -o rw,nosuid,nodev,noexec,relatime cgroup2 /sys/fs/cgroup \
|
||||
|| { printf 'ERROR: cannot mount cgroup2 on /sys/fs/cgroup\n' >&2; return 134; }
|
||||
|
||||
if ! mount -t cgroup2 -o rw,nosuid,nodev,noexec,relatime cgroup2 /sys/fs/cgroup 2>/dev/null; then
|
||||
|
||||
printf 'WARN: could not remount cgroup2 (continuing)\n' >&2
|
||||
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
rm -f -- "${state}"
|
||||
|
||||
echo "Inside chroot: cdi_sys_quick_leave() @ End."
|
||||
|
||||
return 0
|
||||
}
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user