From 2f29bd14cc0a60e3910a397d19a92b09d8c11808 Mon Sep 17 00:00:00 2001 From: oxmc7769 Date: Wed, 23 Apr 2025 05:09:23 -0700 Subject: [PATCH] fix iso- maybe?? --- build-docker.sh | 11 +- export-image/00-allow-rerun/00-run.sh | 14 +- export-image/05-finalise/01-run.sh | 10 + export-image/prerun-old.sh | 10 + export-image/prerun.sh | 254 +++++++++++++++++++++++++- 5 files changed, 285 insertions(+), 14 deletions(-) create mode 100755 export-image/prerun-old.sh mode change 100755 => 100644 export-image/prerun.sh diff --git a/build-docker.sh b/build-docker.sh index 92a0dd6..7e9828a 100755 --- a/build-docker.sh +++ b/build-docker.sh @@ -82,7 +82,16 @@ fi # Modify original build-options to allow config file to be mounted in the docker container BUILD_OPTS="$(echo "${BUILD_OPTS:-}" | sed -E 's@\-c\s?([^ ]+)@-c /config@')" -${DOCKER} build --build-arg BASE_IMAGE=debian:bookworm -t pi-gen "${DIR}" +# Check the arch of the machine we're running on. If it's 64-bit, use a 32-bit base image instead +case "$(uname -m)" in + x86_64|aarch64) + BASE_IMAGE=i386/debian:bookworm + ;; + *) + BASE_IMAGE=debian:bookworm + ;; +esac +${DOCKER} build --build-arg BASE_IMAGE=${BASE_IMAGE} -t pi-gen "${DIR}" if [ "${CONTAINER_EXISTS}" != "" ]; then DOCKER_CMDLINE_NAME="${CONTAINER_NAME}_cont" diff --git a/export-image/00-allow-rerun/00-run.sh b/export-image/00-allow-rerun/00-run.sh index dd67f4c..ffc0d39 100755 --- a/export-image/00-allow-rerun/00-run.sh +++ b/export-image/00-allow-rerun/00-run.sh @@ -1,9 +1,11 @@ #!/bin/bash -e -if [ ! -x "${ROOTFS_DIR}/usr/bin/qemu-arm-static" ]; then - cp /usr/bin/qemu-arm-static "${ROOTFS_DIR}/usr/bin/" -fi +if [[ "${ARCH}" == "arm64" || "${ARCH}" == "armhf" ]]; then + if [ ! -x "${ROOTFS_DIR}/usr/bin/qemu-arm-static" ]; then + cp /usr/bin/qemu-arm-static "${ROOTFS_DIR}/usr/bin/" + fi -if [ -e "${ROOTFS_DIR}/etc/ld.so.preload" ]; then - mv "${ROOTFS_DIR}/etc/ld.so.preload" "${ROOTFS_DIR}/etc/ld.so.preload.disabled" -fi + if [ -e "${ROOTFS_DIR}/etc/ld.so.preload" ]; then + mv "${ROOTFS_DIR}/etc/ld.so.preload" "${ROOTFS_DIR}/etc/ld.so.preload.disabled" + fi +fi \ No newline at end of file diff --git a/export-image/05-finalise/01-run.sh b/export-image/05-finalise/01-run.sh index 6121878..da6ab55 100755 --- a/export-image/05-finalise/01-run.sh +++ b/export-image/05-finalise/01-run.sh @@ -3,6 +3,7 @@ IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img" INFO_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.info" SBOM_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.sbom" +BMAP_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.bmap" on_chroot << EOF update-initramfs -k all -c @@ -100,6 +101,12 @@ zerofree "${ROOT_DEV}" unmount_image "${IMG_FILE}" +if hash bmaptool 2>/dev/null; then + bmaptool create \ + -o "${BMAP_FILE}" \ + "${IMG_FILE}" +fi + mkdir -p "${DEPLOY_DIR}" rm -f "${DEPLOY_DIR}/${ARCHIVE_FILENAME}${IMG_SUFFIX}.*" @@ -128,4 +135,7 @@ esac if [ -f "${SBOM_FILE}" ]; then xz -c "${SBOM_FILE}" > "$DEPLOY_DIR/image_$(basename "${SBOM_FILE}").xz" fi +if [ -f "${BMAP_FILE}" ]; then + xz -c "${BMAP_FILE}" > "$DEPLOY_DIR/image_$(basename "${BMAP_FILE}").xz" +fi cp "$INFO_FILE" "$DEPLOY_DIR/" diff --git a/export-image/prerun-old.sh b/export-image/prerun-old.sh new file mode 100755 index 0000000..c271f9f --- /dev/null +++ b/export-image/prerun-old.sh @@ -0,0 +1,10 @@ +#!/bin/bash -e + +# Check if the architecture is arm64 or armhf +if [[ "$ARCH" == "arm64" || "$ARCH" == "armhf" ]]; then + # If the architecture is arm64 or armhf, run mk-img.sh + ./mk-img.sh +else + # Otherwise, run mk-iso.sh + ./mk-iso.sh +fi \ No newline at end of file diff --git a/export-image/prerun.sh b/export-image/prerun.sh old mode 100755 new mode 100644 index c271f9f..f940ada --- a/export-image/prerun.sh +++ b/export-image/prerun.sh @@ -1,10 +1,250 @@ -#!/bin/bash -e +# Define file/directory names +IMG_NAME="${IMG_FILENAME}${IMG_SUFFIX}" +IMG_FILE="${STAGE_WORK_DIR}/${IMG_NAME}.img" +MOUNT_DIR="${ROOTFS_DIR}" # Consistent name for the main rootfs mount point -# Check if the architecture is arm64 or armhf -if [[ "$ARCH" == "arm64" || "$ARCH" == "armhf" ]]; then - # If the architecture is arm64 or armhf, run mk-img.sh - ./mk-img.sh +# Define partition sizes +BOOT_SIZE=$((512 * 1024 * 1024)) # 512MB +RECOVERY_SIZE=$((256 * 1024 * 1024)) # 256MB +ALIGN=$((4 * 1024 * 1024)) # 4MB alignment + +# --- Cleanup --- +echo "--- Cleaning up previous artifacts ---" +# Assume unmount_image handles unmounting potential mounts and detaching loop device +unmount_image "${IMG_FILE}" +rm -f "${IMG_FILE}" +rm -rf "${MOUNT_DIR}" +mkdir -p "${MOUNT_DIR}" + +# --- Calculate Image Size --- +echo "--- Calculating required image size ---" +# Calculate rootfs size excluding caches and firmware placeholder +ROOT_SIZE=$(du -x --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot/firmware --block-size=1 | cut -f 1) +# Add a margin for overhead and future growth (using mk-img.sh margin) +ROOT_MARGIN="$(echo "($ROOT_SIZE * 0.2 + 400 * 1024 * 1024) / 1" | bc)" + +# Calculate aligned partition sizes and starts +BOOT_PART_START=$((ALIGN)) +BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN)) +ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE)) +ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN)) +RECOVERY_PART_START=$((ROOT_PART_START + ROOT_PART_SIZE)) +RECOVERY_PART_SIZE=$(((RECOVERY_SIZE + ALIGN - 1) / ALIGN * ALIGN)) +IMG_SIZE=$((RECOVERY_PART_START + RECOVERY_PART_SIZE)) + +echo "Rootfs Size: ${ROOT_SIZE} B" +echo "Root Margin: ${ROOT_MARGIN} B" +echo "Calculated Image Size: ${IMG_SIZE} B" + +# --- Create Raw Image File --- +echo "--- Creating raw image file: ${IMG_FILE} ---" +truncate -s "${IMG_SIZE}" "${IMG_FILE}" + +# --- Partition the Image --- +echo "--- Partitioning the image ---" +parted --script "${IMG_FILE}" mklabel msdos +parted --script "${IMG_FILE}" unit B set 1 boot on # Set boot flag on partition 1 +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))" + +# --- Setup Loop Device --- +echo "--- Setting up loop device ---" +cnt=0 +# Assume ensure_next_loopdev() ensures a free loop device is available +until ensure_next_loopdev && LOOP_DEV="$(losetup --show --find --partscan "$IMG_FILE")"; do + ((cnt++)) + if [ $cnt -ge 5 ]; then + echo "ERROR: losetup failed after 5 attempts." + exit 1 + fi + echo "Error setting up loop device. Retrying (${cnt}/5)..." + sleep 5 +done +echo "Loop device created: ${LOOP_DEV}" +# Assume ensure_loopdev_partitions() waits for partition nodes (e.g., /dev/loopXp1) to appear +ensure_loopdev_partitions "$LOOP_DEV" +BOOT_DEV="${LOOP_DEV}p1" +ROOT_DEV="${LOOP_DEV}p2" +RECOVERY_DEV="${LOOP_DEV}p3" + +# --- Format Partitions --- +echo "--- Formatting partitions ---" +# Determine EXT4 features (disable 64bit if not supported/needed, keep ^huge_file) +ROOT_FEATURES="^huge_file" +if grep -q "64bit" /etc/mke2fs.conf; then + ROOT_FEATURES="^64bit,$ROOT_FEATURES" + echo "Enabling 64bit feature for ext4." +fi + +# Determine FAT size for boot partition +if [ "$BOOT_SIZE" -lt 134742016 ]; then # Approx 128MiB + FAT_SIZE=16 + echo "Using FAT16 for boot partition." else - # Otherwise, run mk-iso.sh - ./mk-iso.sh + FAT_SIZE=32 + echo "Using FAT32 for boot partition." +fi + +mkdosfs -n BOOT -F "$FAT_SIZE" -s 4 -v "$BOOT_DEV" > /dev/null +mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null +mkfs.ext4 -L recovery -O "$ROOT_FEATURES" "$RECOVERY_DEV" > /dev/null # Format recovery for all archs + +# --- Architecture-Specific Steps --- + +if [ "$ARCH" == "amd64" ]; then + # --- AMD64: Create IMG, Install GRUB, Create ISO --- + echo "--- Running AMD64 specific steps (IMG + GRUB + ISO) ---" + + ISO_STAGING_DIR="${STAGE_WORK_DIR}/iso_staging" # Temp dir for ISO contents + + # Mount partitions for AMD64 (boot on /boot) + echo "Mounting partitions for AMD64..." + mount -v "$ROOT_DEV" "${MOUNT_DIR}" -t ext4 + mkdir -p "${MOUNT_DIR}/boot" + mount -v "$BOOT_DEV" "${MOUNT_DIR}/boot" -t vfat + # Optional: Mount recovery if you need to add specific recovery tools to the ISO + # mkdir -p "${MOUNT_DIR}/recovery" + # mount -v "$RECOVERY_DEV" "${MOUNT_DIR}/recovery" -t ext4 + + # Copy root filesystem content (excluding caches, firmware, overlays which are typically ARM specific) + echo "Copying root filesystem..." + rsync -aHAXx --delete --exclude /var/cache/apt/archives --exclude /boot/firmware --exclude /boot/overlays "${EXPORT_ROOTFS_DIR}/" "${MOUNT_DIR}/" + + # Install GRUB bootloader for BIOS boot + echo "Installing GRUB (BIOS)..." + mkdir -p "${MOUNT_DIR}/boot/grub" # Ensure grub dir exists + # Basic GRUB config - **IMPORTANT**: Adjust kernel/initrd paths and root= parameter if needed! + # Using PARTUUID is generally more reliable than /dev/sdxN + ROOT_PARTUUID=$(blkid -s PARTUUID -o value "$ROOT_DEV") + cat > "${MOUNT_DIR}/boot/grub/grub.cfg" <