Skip to content

Commit cf82b38

Browse files
committed
refactor(convert): reuse setup_chroot_mounts in step::update_initrd
Replace ~70 lines of duplicated mount/unmount code with a single call to setup_chroot_mounts, adding optional boot_override_part and efi_override_part parameters to handle the update_initrd's output-device mount needs.
1 parent e335a0d commit cf82b38

1 file changed

Lines changed: 27 additions & 69 deletions

File tree

cryptpilot-convert.sh

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -708,12 +708,16 @@ disk::install_deb_on_rootfs() {
708708
# $2 - Root filesystem device or image file to mount (e.g., /dev/sda2 or ./root.img)
709709
# $3 - EFI partition device path (optional; e.g., /dev/sda1) — only used if efi_part_exist=true
710710
# $4 - Boot file/device path (e.g., /dev/sda2 or ./boot.img) — used when boot_part_exist=false
711+
# $5 - (Optional) Boot partition override — if set, use this instead of boot_part/boot_file_path
712+
# $6 - (Optional) EFI partition override — if set, use this instead of efi_part
711713
#
712714
setup_chroot_mounts() {
713715
local rootfs="$1"
714716
local rootfs_file_or_part="$2"
715717
local efi_part="$3"
716718
local boot_file_path="$4"
719+
local boot_override_part="${5:-}"
720+
local efi_override_part="${6:-}"
717721

718722
log::info "Preparing chroot environment at $rootfs"
719723

@@ -742,12 +746,14 @@ setup_chroot_mounts() {
742746
esac
743747
done
744748

745-
# Mount /boot — either from dedicated partition, from a file/image, or skip in UKI mode if no boot partition
749+
# Mount /boot — use override if provided, otherwise follow existing logic
746750
local boot_target="$rootfs/boot"
747751
mkdir -p "$boot_target"
748752
proc::hook_exit "mountpoint -q '$boot_target' && disk::umount_wait_busy '$boot_target'"
749753

750-
if [ "$boot_part_exist" = "false" ]; then
754+
if [ -n "$boot_override_part" ]; then
755+
mount "$boot_override_part" "$boot_target"
756+
elif [ "$boot_part_exist" = "false" ]; then
751757
if [ -n "$boot_file_path" ]; then
752758
# /boot is part of root or stored as a file (e.g., in embedded systems)
753759
mount "$boot_file_path" "$boot_target"
@@ -758,7 +764,12 @@ setup_chroot_mounts() {
758764
fi
759765

760766
# Conditionally mount EFI system partition under /boot/efi
761-
if [ "$efi_part_exist" = "true" ] && [ -n "$efi_part" ]; then
767+
if [ -n "$efi_override_part" ]; then
768+
local efi_target="$rootfs/boot/efi"
769+
mkdir -p "$efi_target"
770+
proc::hook_exit "mountpoint -q '$efi_target' && disk::umount_wait_busy '$efi_target'"
771+
mount "$efi_override_part" "$efi_target"
772+
elif [ "$efi_part_exist" = "true" ] && [ -n "$efi_part" ]; then
762773
local efi_target="$rootfs/boot/efi"
763774
mkdir -p "$efi_target"
764775
proc::hook_exit "mountpoint -q '$efi_target' && disk::umount_wait_busy '$efi_target'"
@@ -1128,76 +1139,23 @@ EOF
11281139
local rootfs_mount_point="${workdir}/rootfs"
11291140
local source_write_rootfs_part="${source_write_device}p${source_rootfs_part_num}"
11301141

1131-
# Clear the read-only flag set by tune2fs during shrink, so we can mount rw for dracut
1132-
log::info "Clearing read-only flag on source-write rootfs"
1133-
tune2fs -O ^read-only "${source_write_rootfs_part}" >/dev/null 2>&1 || true
1134-
1135-
mkdir -p "${rootfs_mount_point}"
1136-
1137-
# Mount rootfs from source-write
1138-
mount "${source_write_rootfs_part}" "${rootfs_mount_point}"
1139-
proc::hook_exit "mountpoint -q ${rootfs_mount_point} && disk::umount_wait_busy ${rootfs_mount_point}"
1140-
1141-
# Mount required pseudo-filesystems
1142-
for dir in dev dev/pts proc run sys tmp; do
1143-
local target="${rootfs_mount_point}/$dir"
1144-
mkdir -p "$target"
1145-
proc::hook_exit "mountpoint -q '$target' && disk::umount_wait_busy '$target'"
1146-
case "$dir" in
1147-
dev) mount -t devtmpfs devtmpfs "$target" ;;
1148-
dev/pts) mount -t devpts devpts "$target" ;;
1149-
proc) mount -t proc proc "$target" ;;
1150-
run) mount -t tmpfs tmpfs "$target" ;;
1151-
sys) mount -t sysfs sysfs "$target" ;;
1152-
tmp) mount -t tmpfs tmpfs "$target" ;;
1153-
esac
1154-
done
1155-
1156-
# Mount /boot (from output boot partition or from source-write rootfs)
1157-
local boot_target="${rootfs_mount_point}/boot"
1158-
mkdir -p "$boot_target"
1159-
if [ "$uki" = false ] && [ -n "${boot_part_num:-}" ]; then
1160-
# Boot partition exists on output - mount it so dracut can find kernel files
1161-
mount "${output_device}p${boot_part_num}" "$boot_target"
1162-
proc::hook_exit "mountpoint -q '$boot_target' && disk::umount_wait_busy '$boot_target'"
1163-
fi
1142+
# Clear the read-only flag set by tune2fs during shrink, so we can mount rw for dracut
1143+
log::info "Clearing read-only flag on source-write rootfs"
1144+
tune2fs -O ^read-only "${source_write_rootfs_part}" >/dev/null 2>&1 || true
11641145

1165-
# Mount EFI from output
1166-
if [ "$efi_part_exist" = "true" ]; then
1167-
local efi_target="${rootfs_mount_point}/boot/efi"
1168-
mkdir -p "$efi_target"
1169-
mount "${output_device}p${efi_part_num}" "$efi_target"
1170-
proc::hook_exit "mountpoint -q '$efi_target' && disk::umount_wait_busy '$efi_target'"
1171-
fi
1146+
# Determine boot partition for output device (empty in UKI mode — /boot lives in rootfs)
1147+
local boot_override_part=""
1148+
if [ "$uki" = false ] && [ -n "${boot_part_num:-}" ]; then
1149+
boot_override_part="${output_device}p${boot_part_num}"
1150+
fi
11721151

1173-
# Bind-mount network config
1174-
for file in resolv.conf hosts; do
1175-
local src="/etc/$file"
1176-
local dst="${rootfs_mount_point}/etc/$file"
1177-
local backup="${dst}.cryptpilot"
1178-
mv "$dst" "$backup" 2>/dev/null || true
1179-
touch "$dst"
1180-
proc::hook_exit "mountpoint -q '$dst' && disk::umount_wait_busy '$dst'"
1181-
mount -o bind,ro "$(realpath "$src")" "$dst"
1182-
done
1152+
setup_chroot_mounts "${rootfs_mount_point}" "${source_write_rootfs_part}" "${output_device}p${efi_part_num}" "${boot_file_path}" "${boot_override_part}" "${output_device}p${efi_part_num}"
11831153

1184-
# Run dracut
1185-
log::info "Executing dracut in chroot"
1186-
update_initrd_inner "${rootfs_mount_point}" "${uki}" "${uki_append_cmdline}"
1154+
# Run dracut
1155+
log::info "Executing dracut in chroot"
1156+
update_initrd_inner "${rootfs_mount_point}" "${uki}" "${uki_append_cmdline}"
11871157

1188-
# Cleanup mounts (reverse order)
1189-
for dir in etc/hosts etc/resolv.conf boot/efi boot sys run proc dev/pts dev; do
1190-
disk::umount_wait_busy "${rootfs_mount_point}/$dir" 2>/dev/null || true
1191-
done
1192-
for file in resolv.conf hosts; do
1193-
local dst="${rootfs_mount_point}/etc/$file"
1194-
local backup="${dst}.cryptpilot"
1195-
if [ -f "$backup" ]; then
1196-
rm -f "$dst"
1197-
mv "$backup" "$dst"
1198-
fi
1199-
done
1200-
disk::umount_wait_busy "${rootfs_mount_point}"
1158+
cleanup_chroot_mounts "${rootfs_mount_point}"
12011159
}
12021160

12031161
step::shrink_rootfs() {

0 commit comments

Comments
 (0)