#!/bin/sh # bashsupport disable=BP5007 # shellcheck disable=SC2249 # shellcheck shell=sh # SPDX-Version: 3.0 # SPDX-CreationInfo: 2025-11-12; WEIDNER, Marc S.; # SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.git # SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency # SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; # SPDX-FileType: SOURCE # SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0 # SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework. # SPDX-PackageName: CISS.debian.live.builder # SPDX-Security-Contact: security@coresecret.eu # Purpose: Open /live/ciss_rootfs.crypt (LUKS) and present its SquashFS as /run/live/rootfs # Phase : premount (executed by live-boot inside the initramfs) set -eu printf "\e[95m[INFO] Starting: [/usr/lib/live/boot/0024-ciss-crypt-squash] ... \n\e[0m" ####################################### # Premount logging helper. # Globals: # None # Arguments: # *: String to log. ####################################### log() { msg="$*" if [ -w /dev/kmsg ]; then printf '<6>%s: %s\n' '0024-ciss-crypt-squash' "${msg}" > /dev/kmsg else printf '%s: %s\n' '0024-ciss-crypt-squash' "${msg}" fi } ### Declare variables. --------------------------------------------------------------------------------------------------------- export CDLB_ISO_LABEL="CISS.debian.live" export CDLB_LUKS_FS="/live/ciss_rootfs.crypt" export CDLB_MAPPER_NAME="crypt_liveiso" export CDLB_MAPPER_DEV="/dev/mapper/${CDLB_MAPPER_NAME}" CDLB_REMOTE_WAIT_SECS="${CDLB_REMOTE_WAIT_SECS:-10}" CDLB_MAX_TRIES="${CDLB_MAX_TRIES:-3}" MNT_MEDIUM="/run/live/medium" MNT_ROOTFS="/run/live/rootfs" _PARAMETER="" _dev="" ### Read the kernel cmdline once. ---------------------------------------------------------------------------------------------- CMDLINE="$(cat /proc/cmdline 2>/dev/null || printf '')" for _PARAMETER in ${CMDLINE}; do case "${_PARAMETER}" in ciss_crypt_path=*) export CDLB_LUKS_FS="${_PARAMETER#ciss_crypt_path=}";; ciss_iso_label=* ) export CDLB_ISO_LABEL="${_PARAMETER#ciss_iso_label=}";; esac done printf "\e[92m[INFO] CDLB_LUKS_FS : [%s] \n\e[0m" "${CDLB_LUKS_FS}" printf "\e[92m[INFO] CDLB_ISO_LABEL : [%s] \n\e[0m" "${CDLB_ISO_LABEL}" mkdir -p "${MNT_MEDIUM}" "${MNT_ROOTFS}" ### Mount the live medium (ISO) read-only, unless already mounted. ------------------------------------------------------------- if ! mountpoint -q "${MNT_MEDIUM}"; then if [ -n "${CDLB_ISO_LABEL}" ] && [ -e "/dev/disk/by-label/${CDLB_ISO_LABEL}" ]; then mount -r -t iso9660 "/dev/disk/by-label/${CDLB_ISO_LABEL}" "${MNT_MEDIUM}" 2>/dev/null \ || mount -r -t udf "/dev/disk/by-label/${CDLB_ISO_LABEL}" "${MNT_MEDIUM}" 2>/dev/null \ || log "could not mount label=${CDLB_ISO_LABEL} (iso9660/udf)" fi fi if ! mountpoint -q "${MNT_MEDIUM}"; then ### Fallback scan (covers SR drives and loop-mounted ISOs that udev exposed). for _dev in /dev/sr* /dev/cdrom /dev/disk/by-label/*; do ### Skip non-block entries early. [ -b "${_dev}" ] || continue ### Try ISO9660 first, then UDF; only unmount on failure. if mount -r -t iso9660 "${_dev}" "${MNT_MEDIUM}" 2>/dev/null || mount -r -t udf "${_dev}" "${MNT_MEDIUM}" 2>/dev/null; then mountpoint -q "${MNT_MEDIUM}" 2>/dev/null && break else umount "${MNT_MEDIUM}" 2>/dev/null || true fi done fi if ! mountpoint -q "${MNT_MEDIUM}"; then log "No live medium mounted, defer to default live-boot path." printf "\e[91m[FATAL] No live medium mounted, defer to default live-boot path. \n\e[0m" exit 0 fi printf "\e[92m[INFO] MNT_MEDIUM : [%s] \n\e[0m" "${MNT_MEDIUM}" ### Locate the encrypted root container on the medium. ------------------------------------------------------------------------- if [ ! -f "${MNT_MEDIUM}${CDLB_LUKS_FS}" ]; then log "Encrypted root not found at: [${MNT_MEDIUM}/${CDLB_LUKS_FS}]" printf "\e[91m[FATAL] Encrypted root not found at: [%s%s] \n\e[0m" "${MNT_MEDIUM}" "${CDLB_LUKS_FS}" exit 0 fi printf "\e[92m[INFO] CISS LUKS FS : [%s%s] \n\e[0m" "${MNT_MEDIUM}" "${CDLB_LUKS_FS}" ### Attach a loop device read-only to the encrypted file. ---------------------------------------------------------------------- LOOP="$(losetup -f --show -r "${MNT_MEDIUM}${CDLB_LUKS_FS}")" || { log "losetup failed"; exit 42; } ### Expose the loop device for unlock-wrapper.sh, dropbear forced-command. ----------------------------------------------------- mkdir -p /run 2>/dev/null || true echo "${LOOP}" >| /run/ciss-loopdev 2>/dev/null || true chmod 0600 /run/ciss-loopdev 2>/dev/null || true ### Wait briefly for the LUKS container to open via dropbear. ------------------------------------------------------------------ if [ ! -b "${CDLB_MAPPER_DEV}" ]; then REMAINING="${CDLB_REMOTE_WAIT_SECS}" while [ "${REMAINING}" -gt 0 ]; do if [ -b "${CDLB_MAPPER_DEV}" ]; then log "LUKS mapping [${CDLB_MAPPER_DEV}] opened remotely by dropbear unlock_wrapper.sh." printf "\e[92m[INFO] LUKS mapping : [%s] opened remotely by dropbear unlock_wrapper.sh. \n\e[0m" "${CDLB_MAPPER_DEV}" break fi sleep 1 REMAINING=$((REMAINING - 1)) done fi ### Fallback: interactive password entry on the console. ----------------------------------------------------------------------- if [ ! -b "${CDLB_MAPPER_DEV}" ]; then attempts=0 while [ "${attempts}" -lt "${CDLB_MAX_TRIES}" ] && [ ! -b "${LUKS_MAPPER_DEV}" ]; do attempts=$((attempts + 1)) printf "\e[93m[INFO] Unlock : [%s] Attempts: [%s/%s] Enter Passphrase on console: \n\e[0m" \ "${CDLB_LUKS_FS}" "${attempts}" "${CDLB_MAX_TRIES}" >/dev/console 2>/dev/null || true ### Let cryptsetup handle the prompt on /dev/console; no keyfile, no FIFO. if cryptsetup open --type luks --readonly "${LOOP}" "${LUKS_MAPPER_NAME}"; then log "LUKS mapping [${CDLB_MAPPER_DEV}] opened via local console." printf "\e[92m[INFO] LUKS mapping : [%s] Opened via local console. \n\e[0m" "${CDLB_MAPPER_DEV}" break fi done fi ### Final sanity check. -------------------------------------------------------------------------------------------------------- if [ ! -b "${CDLB_MAPPER_DEV}" ]; then log "Failed to unlock encrypted root [${CDLB_LUKS_FS}] via dropbear and console." printf "\e[91m[WARN] Failed unlock : [%s] via dropbear and console. \n\e[0m" "${CDLB_LUKS_FS}" exit 42 fi ### Expose the decrypted root device for live-boot overlay. The live-boot components will pick this up in '9990-overlay.sh'. --- echo "${CDLB_MAPPER_DEV}" >| /run/ciss-rootdev chmod 0644 /run/ciss-rootdev 2>/dev/null || true export CISS_ROOT_DEV="${CDLB_MAPPER_DEV}" export CISS_ROOT_DIR="" log "Decrypted root device exposed at [/run/ciss-rootdev] -> [${CDLB_MAPPER_DEV}]" printf "\e[92m[INFO] Decrypted root device exposed at: [/run/ciss-rootdev] -> [%s] \n\e[0m" "${CDLB_MAPPER_DEV}" printf "\e[92m[INFO] Successfully applied: [/usr/lib/live/boot/0024-ciss-crypt-squash] \n\e[0m" # vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh