@@ -92,16 +92,6 @@ reencrypt_existing_partition() {
fi
}
-mount_partition() {
- partition_dev_path=$1
- partition_mountpoint=$2
- echo "mount device: '$partition' to '$partition_mountpoint'"
- if ! mount -t "$(get_fstype "${partition_dev_path}")" "${partition_dev_path}" \
- "${partition_mountpoint}"; then
- panic "Can't mount encrypted partition '${partition_dev_path}'!"
- fi
-}
-
for candidate in /dev/tpm*; do
if ! tpm2_pcrread -T device:"$candidate" "$pcr_bank_hash_type":7 --quiet 2>/dev/null; then
continue
@@ -115,19 +105,6 @@ done
if [ ! -e "$tpm_device" ]; then
if [ "$tpm_encryption_optional" = "true" ]; then
echo "No tpm_device exists abort optional encryption"
- for partition_set in $partition_sets; do
- partition_label="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[1]}')"
- partition_mountpoint="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[2]}')"
- partition_format="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[3]}')"
- partition=/dev/disk/by-partlabel/"$partition_label"
- case "${partition_format}" in
- "reencrypt")
- mount_partition "$partition" "$rootmnt""$partition_mountpoint"
- ;;
- *)
- echo "cannot mount partition '$partition' as it is marked for formatting."
- esac
- done
exit 0
fi
panic "No tpm device exists or supports pcr_hash '$pcr_bank_hash_type' or '$tpm_key_algorithm' - cannot create a encrypted device!"
@@ -140,7 +117,6 @@ fi
for partition_set in $partition_sets; do
partition_label="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[1]}')"
- partition_mountpoint="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[2]}')"
partition_format="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[3]}')"
partition=/dev/disk/by-partlabel/"$partition_label"
crypt_mount_name="encrypted_$partition_label"
@@ -151,7 +127,6 @@ for partition_set in $partition_sets; do
if /usr/sbin/cryptsetup luksDump --batch-mode "$partition" \
| grep -q "clevis"; then
open_tpm2_partition "$part_device"
- mount_partition "$decrypted_part" "${rootmnt}""$partition_mountpoint"
continue
fi
@@ -184,8 +159,6 @@ for partition_set in $partition_sets; do
;;
esac
- mount_partition "$decrypted_part" "${rootmnt}""$partition_mountpoint"
-
# delete initial key
# afterwards no new keys can be enrolled
cryptsetup -v luksKillSlot -q "$part_device" 0
@@ -105,16 +105,6 @@ reencrypt_existing_partition() {
fi
}
-mount_partition() {
- partition_dev_path=$1
- partition_mountpoint=$2
- echo "mount device: '$partition' to '$partition_mountpoint'"
- if ! mount -t "$(get_fstype "${partition_dev_path}")" "${partition_dev_path}" \
- "${partition_mountpoint}"; then
- panic "Can't mount encrypted partition '${partition_dev_path}'!"
- fi
-}
-
for candidate in /dev/tpm*; do
if ! tpm2_pcrread -T device:"$candidate" "$pcr_bank_hash_type":7 --quiet 2>/dev/null; then
continue
@@ -128,19 +118,6 @@ done
if [ ! -e "$tpm_device" ]; then
if [ "$tpm_encryption_optional" = "true" ]; then
echo "No tpm_device exists abort optional encryption"
- for partition_set in $partition_sets; do
- partition_label="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[1]}')"
- partition_mountpoint="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[2]}')"
- partition_format="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[3]}')"
- partition=/dev/disk/by-partlabel/"$partition_label"
- case "${partition_format}" in
- "reencrypt")
- mount_partition "$partition" "$rootmnt""$partition_mountpoint"
- ;;
- *)
- echo "cannot mount partition '$partition' as it is marked for formatting."
- esac
- done
exit 0
fi
panic "No tpm device exists or supports pcr_hash '$pcr_bank_hash_type' or '$tpm_key_algorithm' - cannot create a encrypted device!"
@@ -149,7 +126,6 @@ fi
for partition_set in $partition_sets; do
partition_label="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[1]}')"
- partition_mountpoint="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[2]}')"
partition_format="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[3]}')"
partition=/dev/disk/by-partlabel/"$partition_label"
crypt_mount_name="encrypted_$partition_label"
@@ -160,7 +136,6 @@ for partition_set in $partition_sets; do
if /usr/sbin/cryptsetup luksDump --batch-mode "$partition" \
| grep -q "systemd-tpm2"; then
open_tpm2_partition "$part_device"
- mount_partition "$decrypted_part" "${rootmnt}""$partition_mountpoint"
continue
fi
@@ -193,8 +168,6 @@ for partition_set in $partition_sets; do
;;
esac
- mount_partition "$decrypted_part" "${rootmnt}""$partition_mountpoint"
-
# delete initial key
# afterwards no new keys can be enrolled
/usr/bin/systemd-cryptenroll "$partition" --wipe-slot=0
new file mode 100644
@@ -0,0 +1,61 @@
+#!/bin/sh
+#
+# CIP Core, generic profile
+#
+# Copyright (c) Siemens AG, 2024
+#
+# Authors:
+# Quirin Gylstorff <quirin.gylstorff@siemens.com>
+#
+# SPDX-License-Identifier: MIT
+
+prereqs()
+{
+ # Make sure that this script is run last in local-top
+ local req
+ for req in "${0%/*}"/*; do
+ script="${req##*/}"
+ if [ "$script" != "${0##*/}" ]; then
+ printf '%s\n' "$script"
+ fi
+ done
+}
+case $1 in
+prereqs)
+ prereqs
+ exit 0
+ ;;
+esac
+
+. /scripts/functions
+
+# get configuration variables
+. /usr/share/encrypt_partition/encrypt_partition.env
+
+mount_partition() {
+ partition_dev_path=$1
+ partition_mountpoint=$2
+ echo "mount device: '$partition' to '$partition_mountpoint'"
+ if ! mountpoint -q "${partition_mountpoint}"; then
+ if ! mount -t "$(get_fstype "${partition_dev_path}")" "${partition_dev_path}" \
+ "${partition_mountpoint}"; then
+ panic "Can't mount encrypted partition '${partition_dev_path}'!"
+ fi
+ fi
+}
+partition_sets="$PARTITIONS"
+for partition_set in $partition_sets; do
+ partition_label="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[1]}')"
+ partition_mountpoint="$(awk -v var="$partition_set" 'BEGIN{split(var,a,":"); print a[2]}')"
+ partition=/dev/disk/by-partlabel/"$partition_label"
+ part_device=$(readlink -f "$partition")
+ crypt_mount_name="encrypted_$partition_label"
+ decrypted_part=/dev/mapper/"$crypt_mount_name"
+
+ if /usr/sbin/cryptsetup luksDump --batch-mode "$partition" \
+ | grep -q "systemd-tpm2"; then
+ mount_partition "$decrypted_part" "${rootmnt}""$partition_mountpoint"
+ else
+ mount_partition "$part_device" "$rootmnt""$partition_mountpoint"
+ fi
+done
@@ -35,6 +35,7 @@ CRYPT_BACKEND = "systemd"
SRC_URI += "file://encrypt_partition.env.tmpl \
file://encrypt_partition.${CRYPT_BACKEND}.script \
+ file://mount_crypt_partitions.script \
file://encrypt_partition.${CRYPT_BACKEND}.hook \
file://pwquality.conf"
@@ -60,12 +61,15 @@ TEMPLATE_FILES = "encrypt_partition.env.tmpl"
do_install[cleandirs] += " \
${D}/usr/share/initramfs-tools/hooks \
${D}/usr/share/encrypt_partition \
+ ${D}/usr/share/initramfs-tools/scripts/local-top \
${D}/usr/share/initramfs-tools/scripts/local-bottom"
do_install() {
install -m 0600 "${WORKDIR}/encrypt_partition.env" "${D}/usr/share/encrypt_partition/encrypt_partition.env"
install -m 0755 "${WORKDIR}/encrypt_partition.${CRYPT_BACKEND}.script" \
- "${D}/usr/share/initramfs-tools/scripts/local-bottom/encrypt_partition"
+ "${D}/usr/share/initramfs-tools/scripts/local-top/encrypt_partition"
+ install -m 0755 "${WORKDIR}/mount_crypt_partitions.script" \
+ "${D}/usr/share/initramfs-tools/scripts/local-bottom/mount_decrypted_partition"
install -m 0755 "${WORKDIR}/encrypt_partition.${CRYPT_BACKEND}.hook" \
"${D}/usr/share/initramfs-tools/hooks/encrypt_partition"
install -m 0644 "${WORKDIR}/pwquality.conf" "${D}/usr/share/encrypt_partition/pwquality.conf"
@@ -9,7 +9,7 @@
# Quirin Gylstorff <quirin.gylstorff@siemens.com>
#
-PREREQ="encrypt_partition"
+PREREQ=""
prereqs()
{