Skip to content

Commit 63c6165

Browse files
vunnysobrianmcgillion
authored andcommitted
feat(installer): implement deferred disk encryption trigger
This commit introduces an opt-in deferred disk encryption mechanism for the installer. The `ghaf-installer.sh` script now includes `-e` flag, when used, sets up the system for deferred encryption. It does this by creating `.ghaf-installer-encrypt` marker file on the ESP partition after the image is written to the disk. The `deferred-disk-encryption.nix` module is updated to check for this marker on boot. The encryption process will only proceed if the marker is found, preventing encryption on non-installer boots. Upon completion or failure of the encryption process, the marker is removed to prevent the process from running again on subsequent reboots. Signed-off-by: Vunny Sodhi <[email protected]>
1 parent 8c49439 commit 63c6165

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

modules/partitioning/deferred-disk-encryption.nix

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ let
5050
pkgs.util-linux
5151
];
5252
text = ''
53-
set -euo pipefail
54-
5553
DEVICE="${lvmPartition}"
5654
5755
echo "Checking device $DEVICE for LUKS header..." > /dev/console
@@ -104,8 +102,6 @@ let
104102
pkgs.kmod
105103
];
106104
text = ''
107-
set -euo pipefail
108-
109105
LVM_PV="${lvmPartition}"
110106
111107
# Wait for device to appear
@@ -161,6 +157,32 @@ let
161157
exit 1
162158
fi
163159
160+
# Check for installer/completion markers on the ESP partition.
161+
ESP_DEVICE=""
162+
for i in {1..10}; do
163+
ESP_DEVICE="$(lsblk -pn -o PATH,PARTLABEL | awk 'tolower($2) ~ /esp/ { print $1; exit }')"
164+
[ -n "$ESP_DEVICE" ] && break
165+
sleep 1
166+
done
167+
168+
if [ -z "$ESP_DEVICE" ]; then
169+
echo "ESP partition not found, cannot check for markers. Skipping deferred encryption."
170+
exit 0
171+
fi
172+
173+
mkdir -p /mnt/esp
174+
if ! mount "$ESP_DEVICE" /mnt/esp; then
175+
echo "Failed to mount ESP to check for markers. Skipping deferred encryption."
176+
exit 0
177+
fi
178+
179+
# If it's not an installer-based boot, we also do nothing.
180+
if [ ! -f "/mnt/esp/.ghaf-installer-encrypt" ]; then
181+
echo "Not an installer-based installation (marker not found on ESP). Skipping deferred encryption."
182+
umount /mnt/esp
183+
exit 0
184+
fi
185+
164186
# Stop Plymouth to show encryption progress
165187
if command -v plymouth >/dev/null 2>&1; then
166188
plymouth quit || true
@@ -331,6 +353,8 @@ let
331353
--pbkdf argon2id \
332354
--pbkdf-memory 1048576 \
333355
--pbkdf-parallel 4 \
356+
--progress-frequency 5 \
357+
--verbose \
334358
"$LVM_PV" \
335359
--key-file=- || {
336360
echo "! Encryption failed!"
@@ -560,6 +584,11 @@ let
560584
echo "The system will reboot to complete the setup."
561585
echo ""
562586
587+
# Remove the installer marker so we don't run again if this fails.
588+
rm -f /mnt/esp/.ghaf-installer-encrypt
589+
umount /mnt/esp
590+
rmdir /mnt/esp
591+
563592
${
564593
if config.ghaf.profiles.debug.enable then
565594
''

packages/pkgs-by-name/ghaf-installer/ghaf-installer.sh

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bash
22
# SPDX-FileCopyrightText: 2022-2026 TII (SSRC) and the Ghaf contributors
33
# SPDX-License-Identifier: Apache-2.0
4-
54
if [ "$EUID" -ne 0 ]; then
65
echo "Please run as root"
76
exit
@@ -15,18 +14,23 @@ fi
1514

1615
usage() {
1716
echo " "
18-
echo "Usage: $(basename "$0") [-w]"
17+
echo "Usage: $(basename "$0") [-w] [-e]"
1918
echo " -w Wipe only"
19+
echo " -e Install with disk encryption"
2020
exit 1
2121
}
2222

2323
WIPE_ONLY=false
24+
ENCRYPTED_INSTALL=false
2425

25-
while getopts "w" opt; do
26+
while getopts "we" opt; do
2627
case $opt in
2728
w)
2829
WIPE_ONLY=true
2930
;;
31+
e)
32+
ENCRYPTED_INSTALL=true
33+
;;
3034
?)
3135
usage
3236
;;
@@ -152,4 +156,43 @@ raw_file=("$IMG_PATH"/*.raw.zst)
152156

153157
zstdcat "${raw_file[0]}" | dd of="$DEVICE_NAME" bs=32M status=progress
154158

159+
if [ "$ENCRYPTED_INSTALL" = true ]; then
160+
echo "Setting up deferred encryption..."
161+
162+
# Give udev time to process new partitions
163+
udevadm settle
164+
sleep 2
165+
166+
ESP_DEVICE=""
167+
for i in {1..5}; do
168+
echo "Attempt $i: Listing partitions for ${DEVICE_NAME}..."
169+
170+
# Find ESP partition by its partition label (case-insensitive)
171+
ESP_DEVICE="$(lsblk -pn -o PATH,PARTLABEL "${DEVICE_NAME}" | awk 'tolower($2) ~ /esp/ { print $1; exit }')"
172+
173+
if [ -n "$ESP_DEVICE" ]; then
174+
echo "Found ESP partition: $ESP_DEVICE"
175+
break
176+
fi
177+
178+
echo "Waiting for partitions to appear..."
179+
partprobe "${DEVICE_NAME}"
180+
sleep 2
181+
done
182+
183+
if [ -z "$ESP_DEVICE" ]; then
184+
echo "Error: Could not find ESP partition by label to create installer marker."
185+
exit 1
186+
fi
187+
188+
mkdir -p /mnt/esp
189+
mount "$ESP_DEVICE" /mnt/esp || {
190+
echo "Failed to mount ESP partition"
191+
exit 1
192+
}
193+
touch /mnt/esp/.ghaf-installer-encrypt
194+
umount /mnt/esp
195+
echo "Deferred encryption setup complete."
196+
fi
197+
155198
echo "Installation done. Please remove the installation media and reboot"

packages/pkgs-by-name/ghaf-installer/package.nix

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-FileCopyrightText: 2022-2026 TII (SSRC) and the Ghaf contributors
22
# SPDX-License-Identifier: Apache-2.0
33
{
4-
lib,
54
coreutils,
65
util-linux,
76
hwinfo,
@@ -22,13 +21,7 @@ writeShellApplication {
2221
lvm2 # Needed for vgchange, pvremove
2322
parted # Needed for partprobe
2423
];
25-
text = builtins.readFile (
26-
lib.fileset.toSource {
27-
root = ./.;
28-
fileset = ./ghaf-installer.sh;
29-
}
30-
+ "/ghaf-installer.sh"
31-
);
24+
text = builtins.readFile ./ghaf-installer.sh;
3225
meta = {
3326
description = "Installer script for the Ghaf project";
3427
platforms = [

0 commit comments

Comments
 (0)