#!/bin/bash # SPDX-Version: 3.0 # SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; # SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.installer.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.installer # SPDX-Security-Contact: security@coresecret.eu guard_sourcing ####################################### # EFI System Partition | EF00 | UEFI Bootloader (ESP, FAT32) # BIOS Boot Partition | EF02 | BIOS Bootloader area (GRUB) # Linux SWAP | 8200 | Linux Swap # Linux ext4/btrfs | 8300 | Linux Filesystem (root, home) ####################################### ####################################### # Function that generates each partition on each device according to the chosen recipe string. # Globals: # DIR_LOG # HMP_PATH_PARTUUID # VAR_RECIPE_FIRMWARE # VAR_RECIPE_STRING # VAR_RECIPE_TABLE # VAR_SETUP_PART # Arguments: # None # Returns: # ERR_PARTITIONTBL # ERR_PART_CREATE # ERR_PART_READ # ERR_TABLE_CREATE # ERR_TABLE_DELETE # 0: on success ####################################### partitioning() { ### Declare Arrays and Variables. declare -Ag HMP_PATH_PARTUUID declare var_dev var_part var_end_arg var_begin var_end var_fs var_boot var_pri var_uuid var_mount_path var_label var_end_mib declare -a ary_devs ary_parts declare -i i=0 var_dev_size=0 var_dev_end=0 ### Iterate over all devices in the recipe. # shellcheck disable=SC2312 readarray -t ary_devs < <(yq e -r ".recipe.${VAR_RECIPE_STRING}.dev | keys | .[]" "${VAR_SETUP_PART}") for var_dev in "${ary_devs[@]}"; do ### All current data for the respective device will be deleted. if ! blkdiscard -f "/dev/${var_dev}"; then do_log "warn" "file_only" "3200() Partition table: '/dev/${var_dev}' deletion failed with: 'blkdiscard -f' trying 'sgdisk' fallback." if command -v sgdisk >/dev/null && sgdisk --zap-all "/dev/${var_dev}"; then do_log "info" "file_only" "3200() Partition table: '/dev/${var_dev}' wiped with 'sgdisk --zap-all'." elif dd if=/dev/zero of="/dev/${var_dev}" bs=1M count=8 status=none; then do_log "info" "file_only" "3200() Partition table: '/dev/${var_dev}' overwritten with zeros." else do_log "fatal" "file_only" "3200() Partition table: '/dev/${var_dev}' deletion failed with: [blkdiscard, sgdisk and dd]." return "${ERR_TABLE_DELETE}" fi else do_log "info" "file_only" "3200() Partition table: '/dev/${var_dev}' discarded (blkdiscard)." fi case "${VAR_RECIPE_TABLE,,}" in gpt|mbr) if ! parted -s "/dev/${var_dev}" mklabel "${VAR_RECIPE_TABLE,,}"; then do_log "fatal" "file_only" "3200() [parted -s /dev/${var_dev} mklabel ${VAR_RECIPE_TABLE,,}] failed." do_log "fatal" "file_only" "3200() Partition table: '/dev/${var_dev}' creation failed." return "${ERR_TABLE_CREATE}" fi do_log "info" "file_only" "3200() Partition table: '/dev/${var_dev}' generated: '${VAR_RECIPE_TABLE}'." ;; *) do_log "fatal" "file_only" "3200() No valid partition table chosen. String was '${VAR_RECIPE_TABLE}'." return "${ERR_PARTITIONTBL}" ;; esac ### Iterate over all partitions for this device. # shellcheck disable=SC2312 readarray -t ary_parts < <(yq e -r ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev} | keys | .[]" "${VAR_SETUP_PART}") for var_part in "${ary_parts[@]}"; do ### Extract parameters from YAML. var_begin=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.begin" "${VAR_SETUP_PART}") var_end=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.end" "${VAR_SETUP_PART}") var_fs=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.version" "${VAR_SETUP_PART}") var_boot=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.bootable" "${VAR_SETUP_PART}") var_pri=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.primary" "${VAR_SETUP_PART}") var_mount_path=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.mount.path" "${VAR_SETUP_PART}") ### Assign the start zone of the first partition and skip the first 2 MiB as best practice. if [[ "${var_begin,,}" == "min" ]]; then var_begin="2MiB" fi ### Assign the landing zone of the last partition and reserve 16 MiB for GPT and mdadm binary metadata. if [[ "${var_end,,}" == "max" ]]; then var_dev_size=$(blockdev --getsize64 "/dev/${var_dev}") var_dev_end=$(( var_dev_size - 16 * 1024 * 1024 )) var_end_mib=$(( var_dev_end / 1024 / 1024 )) var_end_arg="${var_end_mib}MiB" else var_end_arg="${var_end}" fi case "${VAR_RECIPE_TABLE,,}" in gpt) var_label=$(get_label "${var_mount_path}" "${var_fs}" "part") if ! parted -s "/dev/${var_dev}" mkpart "${var_label}" "${var_fs}" "${var_begin}" "${var_end_arg}"; then do_log "fatal" "file_only" "3200() [parted -s /dev/${var_dev} mkpart ${var_label} ${var_fs} ${var_begin} ${var_end_arg}] failed." do_log "fatal" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' creation failed." return "${ERR_PART_CREATE}" fi ;; mbr|msdos) if ! parted -s "/dev/${var_dev}" mkpart "${var_pri}" "${var_fs}" "${var_begin}" "${var_end_arg}"; then do_log "fatal" "file_only" "3200() [parted -s /dev/${var_dev} mkpart ${var_pri} ${var_fs} ${var_begin} ${var_end_arg}] failed." do_log "fatal" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' creation failed." return "${ERR_PART_CREATE}" fi ;; esac do_log "info" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' generated | begin: '${var_begin}' | end: '${var_end_arg}'." ### Assign the correct GPT typecode via sgdisk if table is GPT. if [[ "${VAR_RECIPE_TABLE,,}" == "gpt" ]]; then declare typecode="8300" # Default: Linux FS case "${var_fs,,}" in fat32) typecode="EF00" ;; ### EFI System Partition swap) typecode="8200" ;; ### Linux SWAP bios) typecode="EF02" ;; ### BIOS Boot Partition ext4|btrfs) typecode="8300" ;; ### Linux native FS *) do_log "warn" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' unknown FS type: '${var_fs}', using default GPT FS '8300'." ;; esac if sgdisk --typecode="${var_part}:${typecode}" "/dev/${var_dev}" &>/dev/null; then do_log "info" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' GPT typecode '${typecode}' set for '${var_fs}'." else do_log "warn" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' GPT typecode '${typecode}' failed to set." fi fi ### Set the bootable flag if necessary. if [[ "${var_boot,,}" == "true" ]]; then case "${VAR_RECIPE_TABLE,,}:${VAR_RECIPE_FIRMWARE,,}" in gpt:uefi|mbr:uefi) do_log "info" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' | [${VAR_RECIPE_TABLE^^}:UEFI] no bootable flag required." ;; gpt:bios|mbr:bios) parted -s "/dev/${var_dev}" set "${var_part}" boot on do_log "info" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' | [${VAR_RECIPE_TABLE^^}:BIOS] marked as bootable." ;; esac fi ### Store PARTUUID of the partition. udevadm settle for i in {1..10}; do var_uuid=$(blkid -s PARTUUID -o value "/dev/${var_dev}${var_part}") && [[ -n "${var_uuid}" ]] && break sleep 0.25 done if [[ -z "${var_uuid}" ]]; then do_log "fatal" "file_only" "3200() Partition: '/dev/${var_dev}${var_part}' could not read PARTUUID." return "${ERR_PART_READ}" else HMP_PATH_PARTUUID["PARTUUID_${var_mount_path}"]="${var_uuid}" fi done lsblk -o NAME,START,SIZE,PHY-SEC,LOG-SEC,ALIGNMENT "/dev/${var_dev}" >| "${DIR_LOG}/${var_dev}_alignment.log" sgdisk -p "/dev/${var_dev}" >| "${DIR_LOG}/${var_dev}_info.log" done return 0 } # vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh