25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prerun.sh 2.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash -e
  2. IMG_FILE="${STAGE_WORK_DIR}/${IMG_FILENAME}${IMG_SUFFIX}.img"
  3. unmount_image "${IMG_FILE}"
  4. rm -f "${IMG_FILE}"
  5. rm -rf "${ROOTFS_DIR}"
  6. mkdir -p "${ROOTFS_DIR}"
  7. BOOT_SIZE="$((256 * 1024 * 1024))"
  8. ROOT_SIZE=$(du --apparent-size -s "${EXPORT_ROOTFS_DIR}" --exclude var/cache/apt/archives --exclude boot --block-size=1 | cut -f 1)
  9. # All partition sizes and starts will be aligned to this size
  10. ALIGN="$((4 * 1024 * 1024))"
  11. # Add this much space to the calculated file size. This allows for
  12. # some overhead (since actual space usage is usually rounded up to the
  13. # filesystem block size) and gives some free space on the resulting
  14. # image.
  15. ROOT_MARGIN=$((800*1024*1024))
  16. BOOT_PART_START=$((ALIGN))
  17. BOOT_PART_SIZE=$(((BOOT_SIZE + ALIGN - 1) / ALIGN * ALIGN))
  18. ROOT_PART_START=$((BOOT_PART_START + BOOT_PART_SIZE))
  19. ROOT_PART_SIZE=$(((ROOT_SIZE + ROOT_MARGIN + ALIGN - 1) / ALIGN * ALIGN))
  20. IMG_SIZE=$((BOOT_PART_START + BOOT_PART_SIZE + ROOT_PART_SIZE))
  21. truncate -s "${IMG_SIZE}" "${IMG_FILE}"
  22. parted --script "${IMG_FILE}" mklabel msdos
  23. parted --script "${IMG_FILE}" unit B mkpart primary fat32 "${BOOT_PART_START}" "$((BOOT_PART_START + BOOT_PART_SIZE - 1))"
  24. parted --script "${IMG_FILE}" unit B mkpart primary ext4 "${ROOT_PART_START}" "$((ROOT_PART_START + ROOT_PART_SIZE - 1))"
  25. PARTED_OUT=$(parted -sm "${IMG_FILE}" unit b print)
  26. BOOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 2 | tr -d B)
  27. BOOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^1:' | cut -d':' -f 4 | tr -d B)
  28. ROOT_OFFSET=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 2 | tr -d B)
  29. ROOT_LENGTH=$(echo "$PARTED_OUT" | grep -e '^2:' | cut -d':' -f 4 | tr -d B)
  30. BOOT_DEV=$(losetup --show -f -o "${BOOT_OFFSET}" --sizelimit "${BOOT_LENGTH}" "${IMG_FILE}")
  31. ROOT_DEV=$(losetup --show -f -o "${ROOT_OFFSET}" --sizelimit "${ROOT_LENGTH}" "${IMG_FILE}")
  32. echo "/boot: offset $BOOT_OFFSET, length $BOOT_LENGTH"
  33. echo "/: offset $ROOT_OFFSET, length $ROOT_LENGTH"
  34. ROOT_FEATURES="^huge_file"
  35. for FEATURE in metadata_csum 64bit; do
  36. if grep -q "$FEATURE" /etc/mke2fs.conf; then
  37. ROOT_FEATURES="^$FEATURE,$ROOT_FEATURES"
  38. fi
  39. done
  40. mkdosfs -n boot -F 32 -v "$BOOT_DEV" > /dev/null
  41. mkfs.ext4 -L rootfs -O "$ROOT_FEATURES" "$ROOT_DEV" > /dev/null
  42. mount -v "$ROOT_DEV" "${ROOTFS_DIR}" -t ext4
  43. mkdir -p "${ROOTFS_DIR}/boot"
  44. mount -v "$BOOT_DEV" "${ROOTFS_DIR}/boot" -t vfat
  45. rsync -aHAXx --exclude /var/cache/apt/archives --exclude /boot "${EXPORT_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
  46. rsync -rtx "${EXPORT_ROOTFS_DIR}/boot/" "${ROOTFS_DIR}/boot/"