Skip to content

Commit b47445c

Browse files
sashasimkinigorpecovnik
authored andcommitted
add CRYPTROOT_AUTOUNLOCK option and fix CRYPTROOT for uefi builds
fixes #6280
1 parent 3fcb8ef commit b47445c

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

extensions/fs-cryptroot-support.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ function extension_prepare_config__prepare_cryptroot() {
2424
function prepare_root_device__250_encrypt_root_device() {
2525
# We encrypt the rootdevice (currently a loop device) and return the new mapped rootdevice
2626
check_loop_device "$rootdevice"
27-
display_alert "Extension: ${EXTENSION}: Encrypting root partition with LUKS..." "cryptsetup luksFormat $rootdevice" ""
28-
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice -
29-
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksOpen $rootdevice $CRYPTROOT_MAPPER -
27+
display_alert "Extension: ${EXTENSION}: Encrypting root partition with LUKS..." "cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice" ""
28+
if [[ $CRYPTROOT_AUTOUNLOCK == "yes" ]]; then
29+
display_alert "Extension: ${EXTENSION}: configuring LUKS autounlock" ""
30+
declare -g cryptroot_autounlock_key_file=$(mktemp)
31+
openssl rand -base64 32 > "$cryptroot_autounlock_key_file"
32+
cryptsetup luksFormat $CRYPTROOT_PARAMETERS "$rootdevice" "$cryptroot_autounlock_key_file"
33+
cryptsetup luksOpen --key-file "$cryptroot_autounlock_key_file" "$rootdevice" $CRYPTROOT_MAPPER
34+
else # CRYPTROOT_PASSPHRASE case
35+
display_alert "Extension: ${EXTENSION}: configuring LUKS password" ""
36+
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksFormat $CRYPTROOT_PARAMETERS $rootdevice -
37+
echo -n $CRYPTROOT_PASSPHRASE | cryptsetup luksOpen $rootdevice $CRYPTROOT_MAPPER -
38+
fi
3039
add_cleanup_handler cleanup_cryptroot
3140
display_alert "Extension: ${EXTENSION}: Root partition encryption complete." "" "ext"
3241
# TODO: pass /dev/mapper to Docker
@@ -90,4 +99,4 @@ function post_umount_final_image__750_cryptroot_cleanup(){
9099
function cleanup_cryptroot(){
91100
cryptsetup luksClose "${CRYPTROOT_MAPPER}" 2>&1
92101
display_alert "Cryptroot closed ${CRYPTROOT_MAPPER}" "${EXTENSION}" "info"
93-
}
102+
}

extensions/grub.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function extension_prepare_config__prepare_grub_standard() {
2121
declare -g IMAGE_PARTITION_TABLE="gpt" # GPT partition table is essential for many UEFI-like implementations, eg Apple+Intel stuff.
2222
declare -g UEFISIZE=260 # in MiB - grub EFI is tiny - but some EFI BIOSes ignore small too small EFI partitions
2323
declare -g BOOTSIZE=0 # No separate /boot when using UEFI.
24+
if [[ $BOOTPART_REQUIRED == "yes" ]]; then # It is important to place this into /boot to have unified boot partition, especially when CRYPTROOT is used
25+
declare -g UEFI_MOUNT_POINT=/boot
26+
fi
2427
declare -g EXTRA_BSP_NAME="${EXTRA_BSP_NAME}-grub" # Unique bsp name.
2528
declare -g UEFI_GRUB_TARGET_BIOS="" # Target for BIOS GRUB install, set to i386-pc when UEFI_ENABLE_BIOS_AMD64=yes and target is amd64
2629

@@ -162,7 +165,7 @@ pre_umount_final_image__install_grub() {
162165
}
163166
fi
164167

165-
local install_grub_cmdline="grub-install --target=${UEFI_GRUB_TARGET} --no-nvram --removable" # nvram is global to the host, even across chroot. take care.
168+
local install_grub_cmdline="grub-install --target=${UEFI_GRUB_TARGET} --efi-directory=${UEFI_MOUNT_POINT} --no-nvram --removable" # nvram is global to the host, even across chroot. take care.
166169
display_alert "Extension: ${EXTENSION}: Installing GRUB EFI..." "${UEFI_GRUB_TARGET}" ""
167170
chroot_custom "$chroot_target" "$install_grub_cmdline" || {
168171
exit_with_error "${install_grub_cmdline} failed!"
@@ -174,7 +177,12 @@ pre_umount_final_image__install_grub() {
174177
# Irony: let's use grub-probe to find out the UUID of the root partition, and then create a symlink to it.
175178
# Another: on some systems (eg, not Docker) the thing might already exist due to udev actually working.
176179
# shellcheck disable=SC2016 # some wierd escaping going on there.
180+
# Root is needed so that UUID of the unlocked /dev/mapper/armbian-root is discovered by grub-update,
181+
# UUID is then put into grub.cfg instead of raw /dev/mapper/armbian-root which will fail further sanity check
177182
chroot_custom "$chroot_target" mkdir -pv '/dev/disk/by-uuid/"$(grub-probe --target=fs_uuid /)"' "||" true
183+
# Include /boot that might point to a separate boot partition in case one exists (lvm, cryptroot)
184+
# Even if boot partition doesn't exist - the command will be the same as mkdir for / above
185+
chroot_custom "$chroot_target" mkdir -pv '/dev/disk/by-uuid/"$(grub-probe --target=fs_uuid /boot)"' "||" true
178186

179187
display_alert "Extension: ${EXTENSION}: Creating GRUB config..." "grub-mkconfig" ""
180188
chroot_custom "$chroot_target" update-grub || {
@@ -285,6 +293,8 @@ configure_grub() {
285293
GRUB_DISABLE_OS_PROBER=false # Have to be explicit about enabling os-prober
286294
GRUB_FONT="/usr/share/grub/unicode.pf2" # Be explicit about the font to use so Ubuntu does not freak out and mess gfxterm
287295
GRUB_GFXPAYLOAD=keep
296+
GRUB_DISABLE_UUID=false # Be explicit about wanting UUID
297+
GRUB_DISABLE_LINUX_UUID=false # Be explicit about wanting UUID
288298
grubCfgFrag
289299

290300
if [[ "a${UEFI_GRUB_DISABLE_OS_PROBER}" != "a" ]]; then

lib/functions/configuration/main-config.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ function do_main_configuration() {
169169
# Support for LUKS / cryptroot
170170
if [[ $CRYPTROOT_ENABLE == yes ]]; then
171171
enable_extension "fs-cryptroot-support" # add the tooling needed, cryptsetup
172-
if [[ -z $CRYPTROOT_PASSPHRASE ]]; then # a passphrase is mandatory if rootfs encryption is enabled
173-
exit_with_error "Root encryption is enabled but CRYPTROOT_PASSPHRASE is not set"
172+
if [[ -z $CRYPTROOT_PASSPHRASE ]] && [[ -z $CRYPTROOT_AUTOUNLOCK ]]; then # a passphrase is mandatory if rootfs encryption is enabled, unless CRYPTROOT_AUTOUNLOCK is wanted
173+
exit_with_error "Root encryption is enabled but CRYPTROOT_PASSPHRASE or CRYPTROOT_AUTOUNLOCK is not set"
174174
fi
175175
[[ -z $CRYPTROOT_MAPPER ]] && CRYPTROOT_MAPPER="armbian-root" # TODO: fixed name can't be used for parallel image building (rpardini: ?)
176176
[[ -z $CRYPTROOT_SSH_UNLOCK ]] && CRYPTROOT_SSH_UNLOCK=yes

lib/functions/image/initrd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ update_initramfs() {
6262
[[ -d "${chroot_target}/etc/dropbear-initramfs/" ]] && initrd_files_to_hash+=("${chroot_target}/etc/dropbear-initramfs/")
6363
[[ -d "${chroot_target}/etc/dropbear/initramfs/" ]] && initrd_files_to_hash+=("${chroot_target}/etc/dropbear/initramfs/")
6464
fi
65+
initrd_files_to_hash+=("${chroot_target}/etc/crypttab") # for updates to rootdev UUID
6566
fi
6667

6768
# Find all the affected files; parallel md5sum sum them; invert hash and path, and remove chroot prefix.

lib/functions/image/partitioning.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,19 @@ function prepare_partitions() {
324324

325325
# create fstab (and crypttab) entry
326326
if [[ $CRYPTROOT_ENABLE == yes ]]; then
327+
luks_key_file="none"
328+
if [[ $CRYPTROOT_AUTOUNLOCK == yes ]]; then
329+
luks_key_file="/etc/rootfs.key"
330+
display_alert "Saving rootfs.key and configuration for autounlock" "(location=${luks_key_file})"
331+
mv ${cryptroot_autounlock_key_file:?} ${SDCARD}${luks_key_file}
332+
mkdir -p $SDCARD/etc/initramfs-tools/conf.d/
333+
echo "UMASK=0077" > $SDCARD/etc/initramfs-tools/conf.d/key-umask.conf
334+
echo "" >> $SDCARD/etc/cryptsetup-initramfs/conf-hook
335+
echo "KEYFILE_PATTERN=${luks_key_file}" >> $SDCARD/etc/cryptsetup-initramfs/conf-hook
336+
fi
327337
# map the LUKS container partition via its UUID to be the 'cryptroot' device
328338
physical_root_part_uuid="$(blkid -s UUID -o value $physical_rootdevice)"
329-
echo "$CRYPTROOT_MAPPER UUID=${physical_root_part_uuid} none luks" >> $SDCARD/etc/crypttab
339+
echo "$CRYPTROOT_MAPPER UUID=${physical_root_part_uuid} ${luks_key_file} luks" >> $SDCARD/etc/crypttab
330340
run_host_command_logged cat $SDCARD/etc/crypttab
331341
fi
332342

packages/bsp/common/usr/lib/armbian/armbian-resize-filesystem

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ do_resize_crypt()
189189
# It's probably no need to run 'cryptsetup resize'.
190190
# After reboot, it will auto resize to adapte the partition
191191
# 'cryptsetup resize' requires passphrase, so it will fail.
192-
cryptsetup resize $name
192+
# if /etc/rootfs.key is present - it can be done unattended
193+
if [ -f /etc/rootfs.key ]; then
194+
cryptsetup resize --key-file /etc/rootfs.key "$name"
195+
else
196+
cryptsetup resize "$name"
197+
fi
193198

194199
local parentsize=$(lsblk -n -b -o SIZE $parentdev | head -1)
195200

0 commit comments

Comments
 (0)