All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m25s
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
184 lines
7.8 KiB
Bash
184 lines
7.8 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
|
|
|
|
#######################################
|
|
# Function to generate btrfs-subvolumes.
|
|
# Globals:
|
|
# ERR_BTRFS_SUBVOL
|
|
# TARGET
|
|
# Arguments:
|
|
# $1: MOUNT_PATH
|
|
# $2: SUBVOLUME
|
|
#######################################
|
|
create_btrfs_subvolume() {
|
|
declare var_mount_path="$1"
|
|
declare var_subvolume="$2"
|
|
|
|
btrfs subvolume create "${TARGET}${var_mount_path}/${var_subvolume}" || {
|
|
do_log "error" "false" "Error occurred at creation of subvolume: '${var_subvolume}' in: '${TARGET}${var_mount_path}'."
|
|
exit "${ERR_BTRFS_SUBVOL}"
|
|
}
|
|
do_log "info" "false" "Created: '${var_subvolume}' at: '${TARGET}${var_mount_path}'."
|
|
}
|
|
|
|
#######################################
|
|
# Function to create the mount path and mount the respective device on it.
|
|
# Globals:
|
|
# ERR_MOUNTING_DEV
|
|
# TARGET
|
|
# Arguments:
|
|
# $1: MOUNT_PATH
|
|
# $2: MOUNT_DEVICE
|
|
# $3: MOUNT_OPTIONS
|
|
#######################################
|
|
mount_with_dir() {
|
|
declare var_mount_path="$1"
|
|
declare var_mount_device="$2"
|
|
declare var_mount_options="$3"
|
|
|
|
if [[ "${var_mount_path}" == "/" ]]; then
|
|
var_mount_path=""
|
|
fi
|
|
|
|
mkdir -p "${TARGET}${var_mount_path}"
|
|
|
|
mount "${var_mount_options:+-o $var_mount_options}" "${var_mount_device}" "${TARGET}${var_mount_path}" || {
|
|
do_log "error" "false" "Error occurred at mounting '${var_mount_device}' on: '${TARGET}${var_mount_path}'."
|
|
exit "${ERR_MOUNTING_DEV}"
|
|
}
|
|
do_log "info" "false" "Mounted: '${var_mount_device}' on: '${TARGET}${var_mount_path}' incl. options: '${var_mount_options}'."
|
|
}
|
|
|
|
#######################################
|
|
# Function for mounting all partitions for debootstrap, including the generation of btrfs subvolumes.
|
|
# Globals:
|
|
# ERR_MOUNTING_ROOT
|
|
# HMP_MOUNTPATH_DEV
|
|
# TARGET
|
|
# VAR_RECIPE_STRING
|
|
# VAR_SETUP_PART
|
|
# var_fs_options
|
|
# Arguments:
|
|
# None
|
|
#######################################
|
|
mount_partition() {
|
|
### Mount "/"-filesystem
|
|
declare -r var_mount_path_root="/"
|
|
|
|
if [[ -n ${HMP_MOUNTPATH_DEV[$var_mount_path_root]} ]]; then
|
|
|
|
mount_with_dir "${var_mount_path_root}" "${HMP_MOUNTPATH_DEV[$var_mount_path_root]}"
|
|
|
|
else
|
|
|
|
do_log "error" "false" "Root-filesystem '${var_mount_path_root}' not found in Hashmap."
|
|
exit "${ERR_MOUNTING_ROOT}"
|
|
|
|
fi
|
|
|
|
### Ensure order of "/boot" and "/boot/efi"
|
|
declare var_path
|
|
|
|
for var_path in "/boot" "/boot/efi"; do
|
|
|
|
if [[ -n ${HMP_MOUNTPATH_DEV[${var_path}]} ]]; then
|
|
|
|
mount_with_dir "${var_path}" "${HMP_MOUNTPATH_DEV[${var_path}]}"
|
|
|
|
else
|
|
|
|
do_log "info" "false" "Entry '${var_path}' not found in Hashmap."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
declare var_dev var_part var_fs_btrfs_compress var_fs_btrfs_level var_encryption_enable var_encryption_label \
|
|
var_fs_label var_fs_version var_mount_path var_mount_options var_mount_subvolume
|
|
|
|
### Iterate over all devices in the recipe.
|
|
for var_dev in $(yq e ".recipe.${VAR_RECIPE_STRING}.dev | keys | .[]" "${VAR_SETUP_PART}"); do
|
|
|
|
### Iterate over all partitions for this device.
|
|
for var_part in $(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev} | keys | .[]" "${VAR_SETUP_PART}"); do
|
|
|
|
### Extract parameters from YAML.
|
|
var_fs_btrfs_compress=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.btrfs.compress" "${VAR_SETUP_PART}")
|
|
var_fs_btrfs_level=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.btrfs.level" "${VAR_SETUP_PART}")
|
|
var_encryption_enable=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.encryption.enable" "${VAR_SETUP_PART}")
|
|
var_encryption_label=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.encryption.label" "${VAR_SETUP_PART}")
|
|
var_fs_label=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.label" "${VAR_SETUP_PART}")
|
|
var_fs_version=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.version" "${VAR_SETUP_PART}")
|
|
var_mount_path=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.mount.path" "${VAR_SETUP_PART}")
|
|
var_mount_options=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.mount.options" "${VAR_SETUP_PART}")
|
|
var_mount_subvolume=$(yq e ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.mount.subvolume" "${VAR_SETUP_PART}")
|
|
|
|
### Skip already mounted paths ("/", "/boot", "/boot/efi") and skip ("/recovery")
|
|
if [[ "${var_mount_path}" == "/" || "${var_mount_path}" == "/boot" || "${var_mount_path}" == "/boot/efi" || "${var_mount_path}" == "/recovery" ]]; then
|
|
continue
|
|
fi
|
|
|
|
if [[ "${var_mount_path}" == "SWAP" ]]; then
|
|
|
|
cryptsetup open --type plain --key-file /dev/random \
|
|
--offset 2048 --cipher aes-xts-plain64 --key-size 512 \
|
|
--sector-size 4096 "/dev/disk/by-label/${var_fs_label}" "${var_encryption_label}"
|
|
mkswap "/dev/mapper/${var_encryption_label}"
|
|
swapon "/dev/mapper/${var_encryption_label}"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/mapper/${var_encryption_label}'."
|
|
|
|
elif [[ "${var_mount_path}" == "/tmp" ]]; then
|
|
|
|
cryptsetup open --type plain --key-file /dev/random \
|
|
--offset 2048 --cipher aes-xts-plain64 --key-size 512 \
|
|
--sector-size 4096 "/dev/disk/by-label/${var_fs_label}" "${var_encryption_label}"
|
|
mkdir -p "${TARGET}/tmp"
|
|
mkfs.ext4 "/dev/mapper/${var_encryption_label}" "${var_fs_options:+ $var_fs_options}"
|
|
mount "${var_mount_options:+-o $var_mount_options}" "/dev/mapper/${var_encryption_label}" "${TARGET}/tmp"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/mapper/${var_encryption_label}'."
|
|
|
|
fi
|
|
|
|
if [[ "${var_fs_version,,}" == "btrfs" && "${var_encryption_enable}" == "true" ]]; then
|
|
|
|
declare var_btrfs_compression_options="compress=${var_fs_btrfs_compress}:${var_fs_btrfs_level}"
|
|
mount_with_dir "${var_mount_path}" "/dev/mapper/${var_encryption_label}" "${var_btrfs_compression_options}"
|
|
[[ -n ${var_mount_subvolume} ]] && create_btrfs_subvolume "${var_mount_path}" "${var_mount_subvolume}"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/mapper/${var_encryption_label}'."
|
|
|
|
elif [[ "${var_fs_version,,}" == "btrfs" && "${var_encryption_enable}" == "false" ]]; then
|
|
|
|
declare var_btrfs_compression_options="compress=${var_fs_btrfs_compress}:${var_fs_btrfs_level}"
|
|
mount_with_dir "${var_mount_path}" "/dev/${var_dev}${var_part}" "${var_btrfs_compression_options}"
|
|
[[ -n ${var_mount_subvolume} ]] && create_btrfs_subvolume "${var_mount_path}" "${var_mount_subvolume}"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/${var_dev}${var_part}'."
|
|
|
|
elif [[ "${var_fs_version,,}" == "ext4" && "${var_encryption_enable}" == "true" ]]; then
|
|
|
|
mount_with_dir "${var_mount_path}" "/dev/mapper/${var_encryption_label}"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/mapper/${var_encryption_label}'."
|
|
|
|
elif [[ "${var_fs_version,,}" == "ext4" && "${var_encryption_enable}" == "false" ]]; then
|
|
|
|
mount_with_dir "${var_mount_path}" "/dev/${var_dev}${var_part}"
|
|
do_log "info" "false" "Mounted: '${var_mount_path}' on: '/dev/${var_dev}${var_part}'."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
}
|
|
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
|