Adjust iso

This commit is contained in:
2025-04-23 04:24:06 -07:00
parent 1a6d3aeeed
commit b35f58abb8

View File

@@ -1,92 +1,69 @@
#!/bin/bash -e #!/bin/bash -e
ISO_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.iso" IMG_NAME="${IMG_FILENAME}${IMG_SUFFIX}"
IMG_FILE="${STAGE_WORK_DIR}/${IMG_NAME}.img"
unmount_image "${ISO_FILE}" MOUNT_DIR="${ROOTFS_DIR}"
rm -f "${ISO_FILE}"
rm -rf "${ROOTFS_DIR}"
mkdir -p "${ROOTFS_DIR}"
BOOT_SIZE=$((512 * 1024 * 1024)) BOOT_SIZE=$((512 * 1024 * 1024))
RECOVERY_SIZE=$((256 * 1024 * 1024)) # 256MB recovery partition RECOVERY_SIZE=$((256 * 1024 * 1024))
ROOT_SIZE=$(du -x --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot/firmware --block-size=1 | cut -f 1)
ALIGN=$((4 * 1024 * 1024)) ALIGN=$((4 * 1024 * 1024))
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
unmount_image "${IMG_FILE}"
rm -f "${IMG_FILE}"
rm -rf "${MOUNT_DIR}"
mkdir -p "${MOUNT_DIR}"
# Calculate rootfs size
ROOT_SIZE=$(du -x --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot/firmware --block-size=1 | cut -f 1)
ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 200 * 1024 * 1024) / 1" | bc)"
BOOT_PART_START=$((ALIGN)) BOOT_PART_START=$((ALIGN))
BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN)) BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE)) ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN)) ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN))
RECOVERY_PART_START=$((ROOT_PART_START + ROOT_PART_SIZE)) RECOVERY_PART_START=$((ROOT_PART_START + ROOT_PART_SIZE))
RECOVERY_PART_SIZE=$(((RECOVERY_SIZE + ALIGN - 1) / ALIGN * ALIGN)) RECOVERY_PART_SIZE=$(((RECOVERY_SIZE + ALIGN - 1) / ALIGN * ALIGN))
IMG_SIZE=$((RECOVERY_PART_START + RECOVERY_PART_SIZE)) IMG_SIZE=$((RECOVERY_PART_START + RECOVERY_PART_SIZE))
truncate -s "${IMG_SIZE}" "${ISO_FILE}" # Create raw image
truncate -s "${IMG_SIZE}" "${IMG_FILE}"
parted --script "${IMG_FILE}" mklabel msdos
parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${RECOVERY_PART_START}" "$((RECOVERY_PART_START + RECOVERY_PART_SIZE - 1))"
parted --script "${ISO_FILE}" mklabel msdos # Create loop device
parted --script "${ISO_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
parted --script "${ISO_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
parted --script "${ISO_FILE}" unit B mkpart primary ext4 "${RECOVERY_PART_START}" "$((RECOVERY_PART_START + RECOVERY_PART_SIZE - 1))"
echo "Creating loop device..."
cnt=0 cnt=0
until ensure_next_loopdev && LOOP_DEV="$(losetup --show --find --partscan "$ISO_FILE")"; do until ensure_next_loopdev && LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do
if [ $cnt -lt 5 ]; then ((cnt++))
cnt=$((cnt + 1)) if [ $cnt -ge 5 ]; then echo "ERROR: losetup failed"; exit 1; fi
echo "Error in losetup. Retrying..." echo "Retrying losetup..."; sleep 5
sleep 5
else
echo "ERROR: losetup failed; exiting"
exit 1
fi
done done
ensure_loopdev_partitions "$LOOP_DEV" ensure_loopdev_partitions "$LOOP_DEV"
BOOT_DEV="${LOOP_DEV}p1" BOOT_DEV="${LOOP_DEV}p1"
ROOT_DEV="${LOOP_DEV}p2" ROOT_DEV="${LOOP_DEV}p2"
RECOVERY_DEV="${LOOP_DEV}p3" RECOVERY_DEV="${LOOP_DEV}p3"
ROOT_FEATURES="^huge_file" # Format partitions
for FEATURE in 64bit; do FAT_SIZE=$([ "$BOOT_SIZE" -lt 134742016 ] && echo 16 || echo 32)
if grep -q "$FEATURE" /etc/mke2fs.conf; then mkdosfs -n bootfs -F "$FAT_SIZE" -s 4 "$BOOT_DEV"
ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES" mkfs.ext4 -L rootfs -O "^64bit,^huge_file" "$ROOT_DEV"
fi mkfs.ext4 -L recovery "$RECOVERY_DEV"
done
if [ "$BOOT_SIZE" -lt 134742016 ]; then # Mount and copy rootfs
FAT_SIZE=16 mount "$ROOT_DEV" "$MOUNT_DIR" -t ext4
else mkdir -p "${MOUNT_DIR}/boot"
FAT_SIZE=32 mount "$BOOT_DEV" "${MOUNT_DIR}/boot" -t vfat
fi rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot/firmware --exclude /boot/overlays "${EXPORT_ROOTFS_DIR}/" "${MOUNT_DIR}/"
mkdosfs -n bootfs -F "$FAT_SIZE" -s 4 -v "$BOOT_DEV" > /dev/null # Recovery partition
mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null mkdir -p "${MOUNT_DIR}/recovery"
mkfs.ext4 -L recovery "$RECOVERY_DEV" > /dev/null mount "$RECOVERY_DEV" "${MOUNT_DIR}/recovery" -t ext4
# Optionally copy recovery tools
# cp recovery-tools/recovery.sh "${MOUNT_DIR}/recovery/"
mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4 # Install GRUB (BIOS)
mkdir -p "${ROOTFS_DIR}/boot" mkdir -p "${MOUNT_DIR}/boot/grub"
mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat cat > "${MOUNT_DIR}/boot/grub/grub.cfg" <<EOF
rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot/firmware --exclude /boot/overlays "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
#rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/firmware/" "${ROOTFS_DIR}/boot/firmware/"
# Optionally mount and populate the recovery partition
mkdir -p "${ROOTFS_DIR}/recovery"
mount -v "$RECOVERY_DEV" "${ROOTFS_DIR}/recovery" -t ext4
# (Optional) Example: copy recovery script
# cp recovery-tools/recovery.sh "${ROOTFS_DIR}/recovery/"
# Create bootable ISO with GRUB
echo "Creating bootable ISO..."
mkdir -p "${ROOTFS_DIR}/boot/grub"
cat > "${ROOTFS_DIR}/boot/grub/grub.cfg" <<EOF
set default=0 set default=0
set timeout=5 set timeout=5
@@ -96,6 +73,32 @@ menuentry "Linux" {
} }
EOF EOF
grub-mkrescue -o "${ISO_FILE}" "${ROOTFS_DIR}" grub-install \
--target=i386-pc \
--boot-directory="${MOUNT_DIR}/boot" \
--modules="part_msdos ext2 fat normal biosdisk" \
--force \
--no-floppy \
--boot-directory="${MOUNT_DIR}/boot" \
"$LOOP_DEV"
# Optional: add UEFI bootloader if you have one
# mkdir -p "${MOUNT_DIR}/boot/efi/EFI/boot"
# cp bootx64.efi "${MOUNT_DIR}/boot/efi/EFI/boot/bootx64.efi"
# Unmount all
umount -l "${MOUNT_DIR}/boot"
umount -l "${MOUNT_DIR}/recovery"
umount -l "${MOUNT_DIR}"
losetup -d "$LOOP_DEV"
# Create hybrid ISO using xorriso (make sure it's installed)
ISO_FILE="${STAGE_WORK_DIR}/${IMG_NAME}.iso"
xorriso -as mkisofs \
-r -J -joliet -l \
-b boot/grub/i386-pc/eltorito.img \
-no-emul-boot -boot-load-size 4 -boot-info-table \
-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
-o "${ISO_FILE}" "${MOUNT_DIR}"
echo "Bootable ISO with recovery partition created at ${ISO_FILE}" echo "Bootable ISO with recovery partition created at ${ISO_FILE}"