Files
CISS.debian.installer/func/partitioning/3200_partitioning.sh
Marc S. Weidner a4fcd2a658
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 41s
V8.00.000.2025.06.17
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
2025-07-25 20:44:10 +02:00

197 lines
6.9 KiB
Bash

#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; <msw@coresecret.dev>
# 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.; <msw@coresecret.dev>
# 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:
# HMP_PATH_PARTUUID
# 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
declare -a ary_devs ary_parts
declare -i i=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 "/dev/${var_dev}"; then
do_log "warn" "file_only" "Partition table: '/dev/${var_dev}' deletion failed with: 'blkdiscard' trying 'sgdisk' fallback."
if command -v sgdisk >/dev/null && sgdisk --zap-all "/dev/${var_dev}"; then
do_log "info" "file_only" "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" "Partition table: '/dev/${var_dev}' overwritten with zeros."
else
do_log "fatal" "file_only" "Partition table: '/dev/${var_dev}' deletion failed with: [blkdiscard, sgdisk and dd]."
return "${ERR_TABLE_DELETE}"
fi
else
do_log "info" "file_only" "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" "Partition table: '/dev/${var_dev}' creation failed."
return "${ERR_TABLE_CREATE}"
fi
do_log "info" "file_only" "Partition table: '/dev/${var_dev}' generated: '${VAR_RECIPE_TABLE}'."
;;
*)
do_log "fatal" "file_only" "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 landing zone of each partition.
if [[ "${var_end}" == "-1" ]]; then
var_end_arg="100%"
else
var_end_arg="${var_end}"
fi
if ! parted -s "/dev/${var_dev}" mkpart "${var_pri}" "${var_fs}" "${var_begin}" "${var_end_arg}"; then
do_log "fatal" "file_only" "Partition: '/dev/${var_dev}${var_part}' creation failed."
return "${ERR_PART_CREATE}"
fi
do_log "info" "file_only" "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" "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" "Partition: '/dev/${var_dev}${var_part}' GPT typecode '${typecode}' set for '${var_fs}'."
else
do_log "warn" "file_only" "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" "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" "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" "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
done
return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh