distribution/scripts/mkimage

329 lines
12 KiB
Text
Raw Normal View History

2022-02-05 14:23:32 +00:00
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2016 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2016-present Team LibreELEC (https://libreelec.tv)
2022-05-27 22:33:28 +00:00
# Copyright (C) 2022-present Fewtarius
# Copyright (C) 2022-present UnofficialOS
2022-02-05 14:23:32 +00:00
################################################################################
# variables such as ${ROOT} ${PATH} etc... that are required for this
# script to work must be passed via env ... in scripts/image
################################################################################
# set variables
LE_TMP=$(mktemp -d -p ${TARGET_IMG})
2022-02-05 14:23:32 +00:00
SAVE_ERROR="${LE_TMP}/save_error"
2022-03-26 14:07:10 +00:00
. projects/${PROJECT}/devices/${DEVICE}/options
2022-02-05 14:23:32 +00:00
if [ -z "${SYSTEM_SIZE}" -o -z "${SYSTEM_PART_START}" ]; then
echo "mkimage: SYSTEM_SIZE and SYSTEM_PART_START must be configured!"
exit 1
fi
if [ "${PARTITION_TABLE}" = "gpt" ]; then
DISK_GPT_PADDING=1
else
DISK_GPT_PADDING=0
fi
2022-02-05 14:23:32 +00:00
DISK_START_PADDING=$(( (${SYSTEM_PART_START} + 2048 - 1) / 2048 ))
DISK_SIZE=$(( ${DISK_START_PADDING} + ${SYSTEM_SIZE} + ${STORAGE_SIZE} + ${DISK_GPT_PADDING} ))
DISK_BASENAME="${TARGET_IMG}/${IMAGE_NAME}"
if [ -n "${SUBDEVICE}" ]; then
DISK_BASENAME="${DISK_BASENAME}-${SUBDEVICE}"
fi
DISK="${DISK_BASENAME}.img"
# functions
cleanup() {
echo -e "image: cleanup...\n"
rm -rf "${LE_TMP}"
}
show_error() {
echo "image: An error has occurred..."
echo
if [ -s "${SAVE_ERROR}" ]; then
cat "${SAVE_ERROR}"
else
echo "Folder ${LE_TMP} might be out of free space..."
fi
echo
cleanup
exit 1
}
trap cleanup SIGINT
# create an image
echo -e "\nimage: creating sparse file for disk image ${DISK##*/}..."
dd if=/dev/zero of="${DISK}" bs=1M count=0 seek="${DISK_SIZE}" conv=fsync >"${SAVE_ERROR}" 2>&1 || show_error
2022-02-05 14:23:32 +00:00
# write a disklabel
echo "image: creating ${PARTITION_TABLE} partition table..."
2022-05-27 22:33:28 +00:00
parted -s "${DISK}" mklabel ${PARTITION_TABLE}
sync
2022-05-27 22:33:28 +00:00
# create partitions
echo "image: creating partitions..."
2022-05-27 22:33:28 +00:00
if [ "${PARTITION_TABLE}" = "gpt" ] && [ "${BOOTLOADER}" = "u-boot" ]
then
echo "image: creating SPL partition(s)."
parted -s "${DISK}" unit s mkpart ${UBOOT_LABEL} 16384 24575 >/dev/null 2>&1
parted -s "${DISK}" unit s mkpart ${TRUST_LABEL} 24576 32767 >/dev/null 2>&1
2022-05-27 22:33:28 +00:00
fi
2022-02-05 14:23:32 +00:00
SYSTEM_PART_END=$(( ${SYSTEM_PART_START} + (${SYSTEM_SIZE} * 1024 * 1024 / 512) - 1 ))
STORAGE_PART_START=$(( ${SYSTEM_PART_END} + 1 ))
STORAGE_PART_END=$(( ${STORAGE_PART_START} + (${STORAGE_SIZE} * 1024 * 1024 / 512) - 1 ))
if [ "${PARTITION_TABLE}" = "gpt" ]; then
2022-05-31 12:45:33 +00:00
echo "image: Create boot partition."
parted -s "${DISK}" -a min unit s mkpart boot fat32 ${SYSTEM_PART_START} ${SYSTEM_PART_END}
else
2022-05-31 12:45:33 +00:00
echo "image: Create primary partition."
parted -s "${DISK}" -a min unit s mkpart primary fat32 ${SYSTEM_PART_START} ${SYSTEM_PART_END}
parted -s "${DISK}" set 1 boot on
2022-02-05 14:23:32 +00:00
fi
2023-01-08 22:49:32 +00:00
if [ "${PARTITION_TABLE}" = "gpt" ]; then
2022-05-31 12:45:33 +00:00
echo "image: Create storage partition."
parted -s "${DISK}" -a min unit s mkpart storage ext4 ${STORAGE_PART_START} ${STORAGE_PART_END}
else
echo "image: Create primary partition."
parted -s "${DISK}" -a min unit s mkpart primary ext4 ${STORAGE_PART_START} ${STORAGE_PART_END}
fi
2022-02-05 14:23:32 +00:00
sync
2023-01-09 22:26:15 +00:00
if [ "${PARTITION_TABLE}" = "gpt" ] && [ "${BOOTLOADER}" = "u-boot" ]; then
echo "image: Set OS partition as boot"
parted -s "${DISK}" set 3 boot on
2023-01-08 22:49:32 +00:00
fi
sync
echo "image: creating sparse file for part2..."
STORAGE_PART_COUNT=$(( ${STORAGE_PART_END} - ${STORAGE_PART_START} + 1 ))
sync
dd if="${DISK}" of="${LE_TMP}/part2.ext4" bs=512 count=0 seek="${STORAGE_PART_COUNT}" conv=fsync >"${SAVE_ERROR}" 2>&1 || show_error
# create filesystem on part2
echo "image: creating filesystem on part2..."
mke2fs -F -q -t ext4 -m 0 "${LE_TMP}/part2.ext4"
tune2fs -L "${DISTRO_DISKLABEL}" -U ${UUID_STORAGE} "${LE_TMP}/part2.ext4" >"${SAVE_ERROR}" 2>&1 || show_error
e2fsck -n "${LE_TMP}/part2.ext4" >"${SAVE_ERROR}" 2>&1 || show_error
sync
# add resize mark
mkdir "${LE_TMP}/part2.fs"
touch "${LE_TMP}/part2.fs/.please_resize_me"
echo "image: populating filesystem on part2..."
populatefs -U -d "${LE_TMP}/part2.fs" "${LE_TMP}/part2.ext4" >"${SAVE_ERROR}" 2>&1 || show_error
sync
e2fsck -n "${LE_TMP}/part2.ext4" >"${SAVE_ERROR}" 2>&1 || show_error
# merge part2 into disk image
echo "image: merging part2 into disk image..."
dd if="${LE_TMP}/part2.ext4" of="${DISK}" bs=512 seek="${STORAGE_PART_START}" conv=fsync,notrunc >"${SAVE_ERROR}" 2>&1 || show_error
# create disk image for virtual appliance
if [ "${PROJECT}" = "Generic" ]; then
echo "image: creating open virtual appliance..."
# duplicate ${DISK} so anything we do to it directly doesn't effect original
dd if="${DISK}" of="${DISK_BASENAME}.tmp" bs=1M conv=fsync >"${SAVE_ERROR}" 2>&1 || show_error
fi
# create part1 to format and copy files
echo "image: creating sparse file for part1..."
SYSTEM_PART_COUNT=$(( ${SYSTEM_PART_END} - ${SYSTEM_PART_START} + 1 ))
sync
dd if=/dev/zero of="${LE_TMP}/part1.fat" bs=512 count=0 seek="${SYSTEM_PART_COUNT}" conv=fsync >"${SAVE_ERROR}" 2>&1 || show_error
2022-02-05 14:23:32 +00:00
shopt -s expand_aliases # enables alias expansion in script
alias mcopy='mcopy -i "${LE_TMP}/part1.fat" -o'
alias mmd='mmd -i "${LE_TMP}/part1.fat"'
# create filesystem on part1
echo "image: creating filesystem on part1..."
2022-02-05 14:23:32 +00:00
if [ "${BOOTLOADER}" = "syslinux" -o "${BOOTLOADER}" = "bcm2835-bootloader" -o "${BOOTLOADER}" = "u-boot" ]; then
mformat -i "${LE_TMP}/part1.fat" -v "${DISTRO_BOOTLABEL}" -N "${UUID_SYSTEM//-/}" :: >"${SAVE_ERROR}" 2>&1 || show_error
2022-02-05 14:23:32 +00:00
fi
sync
if [ "${BOOTLOADER}" = "syslinux" ]; then
# create bootloader configuration
echo "image: creating bootloader configuration..."
cat << EOF > "${LE_TMP}/syslinux.cfg"
DEFAULT run
TIMEOUT 0
PROMPT 0
NOESCAPE 1
2022-02-05 14:23:32 +00:00
LABEL run
KERNEL /${KERNEL_NAME}
2022-07-01 20:17:33 +00:00
APPEND boot=UUID=${UUID_SYSTEM} disk=UUID=${UUID_STORAGE} portable ${EXTRA_CMDLINE}
2022-02-05 14:23:32 +00:00
EOF
cat << EOF > "${LE_TMP}/grub.cfg"
set timeout="0"
set default="Run"
2022-02-05 14:23:32 +00:00
menuentry "Run" {
search --set -f /KERNEL
linux /KERNEL boot=UUID=${UUID_SYSTEM} disk=UUID=${UUID_STORAGE} grub_portable ${EXTRA_CMDLINE}
2022-02-05 14:23:32 +00:00
}
EOF
mcopy "${LE_TMP}/syslinux.cfg" ::
# install syslinux
echo "image: installing syslinux to part1..."
syslinux.mtools -i "${LE_TMP}/part1.fat" >"${SAVE_ERROR}" 2>&1 || show_error
2022-02-05 14:23:32 +00:00
# copy files
echo "image: copying files to part1..."
mcopy "${TARGET_IMG}/${BUILD_NAME}.kernel" "::/${KERNEL_NAME}"
mcopy "${TARGET_IMG}/${BUILD_NAME}.system" ::/SYSTEM
mcopy "${RELEASE_DIR}/target/KERNEL.md5" "::/${KERNEL_NAME}.md5"
mcopy "${RELEASE_DIR}/target/SYSTEM.md5" ::/SYSTEM.md5
mmd EFI EFI/BOOT
mcopy "${TOOLCHAIN}/share/syslinux/bootx64.efi" ::/EFI/BOOT
mcopy "${TOOLCHAIN}/share/syslinux/ldlinux.e64" ::/EFI/BOOT
mcopy "${TOOLCHAIN}/share/grub/bootia32.efi" ::/EFI/BOOT
mcopy "${LE_TMP}/grub.cfg" ::/EFI/BOOT
elif [ "${BOOTLOADER}" = "bcm2835-bootloader" ]; then
# create bootloader configuration
echo "image: creating bootloader configuration..."
cat << EOF > "${LE_TMP}/cmdline.txt"
boot=UUID=${UUID_SYSTEM} disk=UUID=${UUID_STORAGE} quiet ${EXTRA_CMDLINE}
EOF
mcopy "${LE_TMP}/cmdline.txt" ::
# copy files
echo "image: copying files to part1..."
mcopy "${TARGET_IMG}/${BUILD_NAME}.kernel" "::/${KERNEL_NAME}"
mcopy "${TARGET_IMG}/${BUILD_NAME}.system" ::/SYSTEM
mcopy "${RELEASE_DIR}/target/KERNEL.md5" "::/${KERNEL_NAME}.md5"
mcopy "${RELEASE_DIR}/target/SYSTEM.md5" ::/SYSTEM.md5
mcopy "${RELEASE_DIR}/3rdparty/bootloader/bootcode.bin" ::
mcopy "${RELEASE_DIR}/3rdparty/bootloader/fixup.dat" ::
mcopy "${RELEASE_DIR}/3rdparty/bootloader/start.elf" ::
mcopy "${RELEASE_DIR}/3rdparty/bootloader/config.txt" ::
mcopy "${RELEASE_DIR}/3rdparty/bootloader/distroconfig.txt" ::
if [ -f "${RELEASE_DIR}/3rdparty/bootloader/dt-blob.bin" ]; then
mcopy "${RELEASE_DIR}/3rdparty/bootloader/dt-blob.bin" ::
fi
for dtb in "${RELEASE_DIR}/3rdparty/bootloader/"*.dtb ; do
if [ -f "${dtb}" ]; then
mcopy "${dtb}" ::/$(basename "${dtb}")
fi
done
if [ -d "${RELEASE_DIR}/3rdparty/bootloader/overlays" ]; then
mcopy -s "${RELEASE_DIR}/3rdparty/bootloader/overlays" ::
fi
2022-03-26 14:07:10 +00:00
elif [ "${BOOTLOADER}" = "u-boot" -a \( -n "${UBOOT_CONFIG}" -o "${UBOOT_VERSION}" = "vendor" \) ]; then
2022-02-05 14:23:32 +00:00
# create bootloader configuration
echo "image: creating bootloader configuration... (u-boot)"
if [ "${UBOOT_VERSION}" != "vendor" ]; then
for DTB in ${DEVICE_DTB[@]}
do
if [ -f "${RELEASE_DIR}/3rdparty/bootloader/${DTB}.dtb" ]; then
mcopy "${RELEASE_DIR}/3rdparty/bootloader/${DTB}.dtb" ::
fi
done
2022-02-05 14:23:32 +00:00
fi
if [ -f "${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/bootloader/mkimage" ]; then
. "${PROJECT_DIR}/${PROJECT}/devices/${DEVICE}/bootloader/mkimage"
elif [ -f "${PROJECT_DIR}/${PROJECT}/bootloader/mkimage" ]; then
. "${PROJECT_DIR}/${PROJECT}/bootloader/mkimage"
else
echo "image: skipping u-boot. no mkimage script found"
fi
echo "image: copying files to part1..."
mcopy "${TARGET_IMG}/${BUILD_NAME}.kernel" "::/${KERNEL_NAME}"
mcopy "${TARGET_IMG}/${BUILD_NAME}.system" ::/SYSTEM
mcopy "${RELEASE_DIR}/target/KERNEL.md5" "::/${KERNEL_NAME}.md5"
mcopy "${RELEASE_DIR}/target/SYSTEM.md5" ::/SYSTEM.md5
elif [ "${BOOTLOADER}" = "u-boot" ]; then
echo "to make an image using u-boot UBOOT_SYSTEM must be set"
cleanup
exit
fi # bootloader
# run fsck
2022-02-05 14:23:32 +00:00
echo "image: checking filesystem on part1..."
sync
2022-02-05 14:23:32 +00:00
fsck.fat -n "${LE_TMP}/part1.fat" >"${SAVE_ERROR}" 2>&1 || show_error
# merge part1 into disk image
echo "image: merging part1 into disk image..."
dd if="${LE_TMP}/part1.fat" of="${DISK}" bs=512 seek="${SYSTEM_PART_START}" conv=fsync,notrunc >"${SAVE_ERROR}" 2>&1 || show_error
# finalize virtual appliance
2022-02-05 14:23:32 +00:00
if [ "${PROJECT}" = "Generic" ]; then
# change syslinux default to 'run'
echo "image: modifying files on part1 for open virtual appliance..."
2022-02-05 14:23:32 +00:00
sed -e "/DEFAULT/ s/installer/run/" -i "${LE_TMP}/syslinux.cfg"
sed -e "/set default=/s/\"Installer\"/\"Run\"/" -i "${LE_TMP}/grub.cfg"
mcopy "${LE_TMP}/syslinux.cfg" :: >"${SAVE_ERROR}" 2>&1 || show_error
mcopy "${LE_TMP}/grub.cfg" ::/EFI/BOOT >"${SAVE_ERROR}" 2>&1 || show_error
2022-02-05 14:23:32 +00:00
sync
# run fsck
echo "image: checking filesystem on part1..."
fsck.fat -n "${LE_TMP}/part1.fat" >"${SAVE_ERROR}" 2>&1 || show_error
# merge modified part1 into tmp disk image
echo "image: merging part1 into open virtual appliance..."
2022-02-05 14:23:32 +00:00
dd if="${LE_TMP}/part1.fat" of="${DISK_BASENAME}.tmp" bs=512 seek="${SYSTEM_PART_START}" conv=fsync,notrunc >"${SAVE_ERROR}" 2>&1 || show_error
# create vmdk from tmp ${DISK}
echo "image: creating vmdk for open virtual appliance..."
2022-02-05 14:23:32 +00:00
qemu-img convert -O vmdk -o subformat=streamOptimized "${DISK_BASENAME}.tmp" "${DISK_BASENAME}.vmdk"
# generate ovf from template
sed -e "s,@DISTRO@,${DISTRO},g" -e "s,@DISK@,${IMAGE_NAME},g" \
-e "s,@OVA_SIZE@,$((${OVA_SIZE}*1024*1024)),g" \
"${PROJECT_DIR}/${PROJECT}/config/ovf.template" > "${DISK_BASENAME}.ovf"
# combine ovf and vmdk into official ova
tar -C "${TARGET_IMG}" -cf "${DISK_BASENAME}.ova" "${IMAGE_NAME}.ovf" "${IMAGE_NAME}.vmdk"
# create sha256 checksum of ova image
(
cd "${TARGET_IMG}"
2022-02-05 14:23:32 +00:00
sha256sum "${IMAGE_NAME}.ova" > "${IMAGE_NAME}.ova.sha256"
)
echo "image: cleaning up open virtual appliance..."
2022-02-05 14:23:32 +00:00
# remove tmp ${DISK}, vmdk and ovf
rm "${DISK_BASENAME}.tmp" "${DISK_BASENAME}.vmdk" "${DISK_BASENAME}.ovf"
fi
# gzip
echo "image: compressing..."
pigz --best --force "${DISK}"
2022-02-05 14:23:32 +00:00
# create sha256 checksum of image
(
cd "${TARGET_IMG}"
sha256sum "${DISK##*/}.gz" > "${DISK##*/}.gz.sha256"
2022-02-05 14:23:32 +00:00
)
if [ -n "${LOCAL_SSH_KEYS_FILE}" ] || \
[ -n "${LOCAL_WIFI_SSID}" ] || \
[ -n "${LOCAL_WIFI_KEY}" ]
then
echo "image: CONTAINS SENSITIVE DATA, DO NOT DISTRIBUTE."
fi
2022-02-05 14:23:32 +00:00
# cleanup
cleanup
exit