improve(lvm+cryptroot extensions): enable possibility for LVM on LUKS

This commit is contained in:
M.Schmidt
2024-12-03 21:47:11 +01:00
committed by Igor
parent 3ec24d40e8
commit 590b75fd6f
6 changed files with 55 additions and 43 deletions

View File

@@ -19,7 +19,7 @@ function check_loop_device() {
function check_loop_device_internal() {
local device="${1}"
display_alert "Checking look device" "${device}" "debug"
display_alert "Checking loop device" "${device}" "debug"
if [[ ! -b "${device}" ]]; then
if [[ $CONTAINER_COMPAT == yes && -b "/tmp/${device}" ]]; then
display_alert "Creating device node" "${device}"

View File

@@ -246,7 +246,6 @@ function prepare_partitions() {
fi
declare -g LOOP
call_extension_method "post_create_partitions" <<- 'POST_CREATE_PARTITIONS'
*called after all partitions are created, but not yet formatted*
POST_CREATE_PARTITIONS
@@ -256,10 +255,15 @@ function prepare_partitions() {
exec {FD}> /var/lock/armbian-debootstrap-losetup
flock -x $FD
#--partscan is using to force the kernel for scaning partition table in preventing of partprobe errors
LOOP=$(losetup --show --partscan --find "${SDCARD}".raw) || exit_with_error "Unable to find free loop device"
display_alert "Allocated loop device" "LOOP=${LOOP}"
#--partscan is using to force the kernel for scanning partition table in preventing of partprobe errors
if [[ -z $LOOP ]]; then
LOOP=$(losetup -f)
# LOOP=$(losetup --show --partscan --find "${SDCARD}".raw) || exit_with_error "Unable to find free loop device"
[[ -z $LOOP ]] && exit_with_error "Unable to find free loop device"
display_alert "Allocated loop device" "LOOP=${LOOP}"
check_loop_device "$LOOP"
losetup $LOOP ${SDCARD}.raw
fi
# loop device was grabbed here, unlock
flock -u $FD
@@ -279,8 +283,8 @@ function prepare_partitions() {
## ROOT PARTITION
##
if [[ -n $rootpart ]]; then
local physical_rootdevice="${LOOP}p${rootpart}"
local rootdevice="${LOOP}p${rootpart}"
local rootdevice=${LOOP}p${rootpart}
local physical_rootdevice=$rootdevice
call_extension_method "prepare_root_device" <<- 'PREPARE_ROOT_DEVICE'
*Specialized storage extensions typically transform the root device into a mapped device and should hook in here *
@@ -306,23 +310,20 @@ function prepare_partitions() {
root_part_uuid="$(blkid -s UUID -o value ${LOOP}p${rootpart})"
declare -g -r ROOT_PART_UUID="${root_part_uuid}"
physical_root_part_uuid="$(blkid -s UUID -o value $physical_rootdevice)"
declare -g -r PHYSICAL_ROOT_PART_UUID="${physical_root_part_uuid}"
display_alert "Physical root device" "$physical_rootdevice (UUID=${PHYSICAL_ROOT_PART_UUID})" "debug"
display_alert "Mounting rootfs" "$rootdevice (UUID=${ROOT_PART_UUID})"
run_host_command_logged mount ${fscreateopt} $rootdevice $MOUNT/
# create fstab (and crypttab) entry
local rootfs
if [[ $CRYPTROOT_ENABLE == yes ]]; then
# map the LUKS container partition via its UUID to be the 'cryptroot' device
physical_root_part_uuid="$(blkid -s UUID -o value $physical_rootdevice)"
echo "$CRYPTROOT_MAPPER UUID=${physical_root_part_uuid} none luks" >> $SDCARD/etc/crypttab
rootfs=$rootdevice # used in fstab
else
rootfs="UUID=$(blkid -s UUID -o value $rootdevice)"
run_host_command_logged cat $SDCARD/etc/crypttab
fi
rootfs="UUID=$(blkid -s UUID -o value $rootdevice)"
echo "$rootfs / ${mkfs[$ROOTFS_TYPE]} defaults,noatime${mountopts[$ROOTFS_TYPE]} 0 1" >> $SDCARD/etc/fstab
run_host_command_logged cat $SDCARD/etc/fstab
else
# update_initramfs will fail if /lib/modules/ doesn't exist
mount --bind --make-private $SDCARD $MOUNT/

View File

@@ -117,9 +117,8 @@ function create_image_from_sdcard_rootfs() {
fi
wait_for_disk_sync "before umount MOUNT"
umount_chroot_recursive "${MOUNT}" "MOUNT"
[[ $CRYPTROOT_ENABLE == yes ]] && cryptsetup luksClose "$CRYPTROOT_MAPPER"
call_extension_method "post_umount_final_image" "config_post_umount_final_image" <<- 'POST_UMOUNT_FINAL_IMAGE'
*allow config to hack into the image after the unmount*

View File

@@ -56,15 +56,18 @@ function trap_handler_cleanup_rootfs_and_image() {
debug_tmpfs_show_usage "before cleanup of rootfs"
cd "${SRC}" || echo "Failed to cwd to ${SRC}" # Move pwd away, so unmounts work
# those will loop until they're unmounted.
display_alert "Cleanup sdcard begin" "trap_handler_cleanup_rootfs_and_image" "cleanup"
umount_chroot_recursive "${SDCARD}" "SDCARD" || true
display_alert "Cleanup mount begin" "trap_handler_cleanup_rootfs_and_image" "cleanup"
umount_chroot_recursive "${MOUNT}" "MOUNT" || true
display_alert "Cleanup umount sdcard begin" "trap_handler_cleanup_rootfs_and_image" "cleanup"
# unmount tmpfs mounted on SDCARD if it exists. #@TODO: move to new tmpfs-utils scheme
mountpoint -q "${SDCARD}" && umount "${SDCARD}"
[[ $CRYPTROOT_ENABLE == yes ]] && cryptsetup luksClose "${CRYPTROOT_MAPPER}"
if [[ "${PRESERVE_SDCARD_MOUNT}" == "yes" ]]; then
display_alert "Preserving SD card mount" "trap_handler_cleanup_rootfs_and_image" "warn"
return 0
@@ -72,7 +75,7 @@ function trap_handler_cleanup_rootfs_and_image() {
# shellcheck disable=SC2153 # global var.
if [[ -b "${LOOP}" ]]; then
display_alert "Freeing loop" "trap_handler_cleanup_rootfs_and_image ${LOOP}" "wrn"
display_alert "Freeing loop" "trap_handler_cleanup_rootfs_and_image ${LOOP}" "warn"
free_loop_device_insistent "${LOOP}" || true
fi