|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Install required packages if not installed |
| 4 | +echo "Checking and installing required packages..." |
| 5 | +apt update |
| 6 | +apt install -y udev parted kpartx |
| 7 | + |
| 8 | +# Read input parameters |
| 9 | +BOARD=${board_name} |
| 10 | +BOOT_IMG=${WORK_DIR}/${board_name}/output/boot-${board_name}.ext4 |
| 11 | +ROOTFS_IMG=${WORK_DIR}/${board_name}/output/root-${board_name}.ext4 |
| 12 | + |
| 13 | +# Check if boot and rootfs image files exist |
| 14 | +if [ ! -f "$BOOT_IMG" ]; then |
| 15 | + echo "Error: Boot image '$BOOT_IMG' not found!" |
| 16 | + return 1 |
| 17 | +fi |
| 18 | + |
| 19 | +if [ ! -f "$ROOTFS_IMG" ]; then |
| 20 | + echo "Error: RootFS image '$ROOTFS_IMG' not found!" |
| 21 | + return 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# Generate system image filename based on BOARD and RELEASE_TAG |
| 25 | +IMAGE="EIC7X-${BOARD}-${RELEASE_TAG}.img" |
| 26 | + |
| 27 | +# Fixed sizes for Boot and Swap partitions |
| 28 | +BOOT_SIZE=512MiB |
| 29 | +SWAP_SIZE=4G |
| 30 | + |
| 31 | +# Dynamically calculate RootFS size based on the rootfs image size (in MB) plus a 50 MB margin |
| 32 | +ROOT_SIZE=$(du -m "$ROOTFS_IMG" | awk '{print $1 + 50}')M |
| 33 | + |
| 34 | +# Calculate the end of the swap partition in MiB |
| 35 | +SWAP_END=$((${BOOT_SIZE%MiB} + ${SWAP_SIZE%G} * 1024)) |
| 36 | + |
| 37 | +# Total image size: Boot + Swap + RootFS + an extra 256M for buffer |
| 38 | +TOTAL_SIZE=$(echo "$SWAP_END + ${ROOT_SIZE%M} + 256" | bc)M |
| 39 | + |
| 40 | +echo "Creating minimal disk image: $IMAGE" |
| 41 | +echo "Total size: $TOTAL_SIZE (Boot: $BOOT_SIZE, Swap: $SWAP_SIZE, RootFS: $ROOT_SIZE)" |
| 42 | + |
| 43 | +# Create an image file with the total size |
| 44 | +truncate -s "$TOTAL_SIZE" "$IMAGE" |
| 45 | + |
| 46 | +BOOT_UUID=$(blkid -s UUID -o value "$BOOT_IMG") |
| 47 | + |
| 48 | +# Create partition table using parted |
| 49 | +parted -s "$IMAGE" mklabel gpt |
| 50 | +parted -s "$IMAGE" mkpart boot ext4 1MiB ${BOOT_SIZE} |
| 51 | +parted -s "$IMAGE" mkpart swap linux-swap ${BOOT_SIZE} ${SWAP_END}MiB |
| 52 | +parted -s "$IMAGE" mkpart rootfs ext4 ${SWAP_END}MiB 100% |
| 53 | + |
| 54 | +# Associate the image with a loop device (without --partscan) |
| 55 | +LOOPDEV=$(losetup --find --show "$IMAGE") |
| 56 | +echo "Using loop device: $LOOPDEV" |
| 57 | + |
| 58 | +# Use kpartx to create partition mappings under /dev/mapper/ |
| 59 | +kpartx -av "$LOOPDEV" |
| 60 | + |
| 61 | +# Format the partitions |
| 62 | +mkswap /dev/mapper/$(basename "$LOOPDEV")p2 |
| 63 | +mkfs.ext4 -F /dev/mapper/$(basename "$LOOPDEV")p3 |
| 64 | + |
| 65 | +ROOTFS_UUID=$(blkid -s UUID -o value "$ROOTFS_IMG") |
| 66 | +# 恢复 UUID |
| 67 | +if [ -n "$ROOTFS_UUID" ]; then |
| 68 | + yes | tune2fs -f -U "$ROOTFS_UUID" /dev/mapper/$(basename "$LOOPDEV")p3 |
| 69 | +fi |
| 70 | + |
| 71 | +# Get the UUID for swap partition |
| 72 | +SWAP_UUID=$(blkid -s UUID -o value /dev/mapper/$(basename "$LOOPDEV")p2) |
| 73 | +echo "Swap partition UUID: $SWAP_UUID" |
| 74 | + |
| 75 | +# Get the UUID for root partition |
| 76 | +ROOTFS_UUID=$(blkid -s UUID -o value /dev/mapper/$(basename "$LOOPDEV")p3) |
| 77 | +echo "RootFS partition UUID: $ROOTFS_UUID" |
| 78 | + |
| 79 | +# Mount and copy Boot data |
| 80 | +dd if="$BOOT_IMG" of=/dev/mapper/$(basename "$LOOPDEV")p1 bs=1M status=progress |
| 81 | + |
| 82 | +# Mount and copy RootFS data |
| 83 | +mkdir -p /mnt/rootfs |
| 84 | +mount /dev/mapper/$(basename "$LOOPDEV")p3 /mnt/rootfs |
| 85 | +mkdir -p /mnt/rootfs_ext4 |
| 86 | +mount -o loop "$ROOTFS_IMG" /mnt/rootfs_ext4 |
| 87 | +echo "Copying rootfs image content into rootfs partition..." |
| 88 | +cp -a /mnt/rootfs_ext4/. /mnt/rootfs/ |
| 89 | + |
| 90 | +FSTAB_FILE="/mnt/rootfs/etc/fstab" |
| 91 | +# swap |
| 92 | +if grep -q "swap" "$FSTAB_FILE"; then |
| 93 | + sed -i "s|UUID=[a-f0-9-]*[[:space:]]*none[[:space:]]*swap|UUID=$SWAP_UUID none swap|" "$FSTAB_FILE" |
| 94 | +else |
| 95 | + echo "UUID=$SWAP_UUID none swap sw 0 0" >> "$FSTAB_FILE" |
| 96 | +fi |
| 97 | + |
| 98 | +umount /mnt/rootfs_ext4 |
| 99 | +rmdir /mnt/rootfs_ext4 |
| 100 | +umount /mnt/rootfs |
| 101 | +rmdir /mnt/rootfs |
| 102 | + |
| 103 | +# Clean up: remove partition mappings and detach loop device |
| 104 | +kpartx -dv "$LOOPDEV" |
| 105 | +losetup -d "$LOOPDEV" |
| 106 | + |
| 107 | +echo "Disk image creation complete: $IMAGE" |
0 commit comments