Path: blob/next/scripts/build-cix-image.sh
18111 views
#!/usr/bin/env bash12# Copyright 2024 Cix Technology Group Co., Ltd.3# All Rights Reserved.4#5# The following programs are the sole property of Cix Technology Group Co., Ltd.,6# and contain its proprietary and confidential information.7#89function replace_line_if_exist() {10local src=$111local dst=$212local file=$31314if [[ -e "$file" ]]; then15set +E16local res=`grep -n "${src}" "${file}"`17#echo "res:$res"18if [[ ${#res} -gt 0 ]]; then19line=`echo "${res}" | cut -d ":" -f 1`20fi21set -E22#echo "line: $line"23if [[ $line -gt 0 ]]; then24sed -i "${line}c${dst}" "${file}"25fi26fi27}2829function create_cix_rootfs()30{31display_alert "Preparing" "cix rootfs" "info"3233local PATH_OUT=${SRC}/output/cix34local ROOT_FREE_SIZE=409600035rootfs_ext4=${PATH_OUT}/images/rootfs.ext43637mkdir -p ${PATH_OUT}/images > /dev/null 2>&13839mount --bind --make-private $SDCARD $MOUNT/4041local debianSize=`sudo du ${MOUNT} -d 0 -k | sudo awk -F ' ' '{print $1}'`42debianSize=`expr $debianSize \* 13 / 10`43if [[ ${debianSize} -lt 1024 ]]; then44totalsize='10240'45elif [[ ${debianSize} -lt 10240 ]]; then46totalsize=`expr $debianSize \* 50 / 10`47elif [[ ${debianSize} -lt 102400 ]]; then48totalsize=`expr $debianSize \* 25 / 10`49elif [[ ${debianSize} -lt 1024000 ]]; then50totalsize=`expr $debianSize \* 20 / 10`51else52totalsize=`expr $debianSize + ${ROOT_FREE_SIZE:-4096000}`53fi5455local count=`expr $totalsize / 4`56root_uuid=$(uuidgen)57boot_uuid=$(uuidgen | tr -d '-' | cut -c1-8 | tr 'a-f' 'A-F')58formatted_boot_uuid="${boot_uuid:0:4}-${boot_uuid:4:4}"59cat <<-END > ${SDCARD}/etc/fstab60# /etc/fstab: static file system information.61#62# Use 'blkid' to print the universally unique identifier for a device; this may63# be used with UUID= as a more robust way to name devices that works even if64# disks are added and removed. See fstab(5).65#66# <file system> <mount point> <type> <options> <dump> <pass>6768END69#echo "UUID=${formatted_boot_uuid} /boot vfat defaults,umask=0077 0 2" >> $MOUNT/etc/fstab70#echo "UUID=$root_uuid / ext4 errors=remount-ro 0 1" >> $MOUNT/etc/fstab7172dd if=/dev/zero of="${rootfs_ext4}" bs=4k count=$count > /dev/null 2>&173#mkfs.ext4 -U ${uuid} -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&174mkfs.ext4 -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&175fsck.ext4 -pvfD "${rootfs_ext4}" > /dev/null 2>&17677umount $MOUNT78rm -rf $MOUNT79}8081function create_cix_image()82{83display_alert "Creating" "cix image" "info"8485local PATH_OUT=${SRC}/output/cix86local gpt_btp=${PATH_OUT}/images/partition.bpt87local SCRIPT_DIR=${EXTER}/cache/sources/component_cix-${BRANCH}8889if [[ $SELECTED_CONFIGURATION == "cli_standard" ]]; then90IMAGE_TYPE=server91elif [[ $SELECTED_CONFIGURATION == "cli_minimal" ]]; then92IMAGE_TYPE=minimal93else94IMAGE_TYPE=desktop95fi96version="${BOARD^}_${REVISION}_${DISTRIBUTION,}_${RELEASE}_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"97mkdir -p ${SRC}/output/images/${version}98local cix_image_name="${SRC}/output/images/${version}/${version}.img"99100cp -f "${SCRIPT_DIR}/debian/fb/partition.bpt" "${gpt_btp}" || {101display_alert "Failed to copy BPT template" "${gpt_btp}" "err"102return 1103}104105local boot_start=$(expr $(sed -n '6p' ${gpt_btp} | awk '{print $2}') / 1024 / 1024)106local boot_size=$(expr $(sed -n '11p' ${gpt_btp} | awk -F '"' '{print $4}' | awk '{print $1}'))107108if [[ ! -f "${rootfs_ext4}" ]]; then109display_alert "Rootfs missing" "${rootfs_ext4}" "err"110return 1111fi112113local rootfs_ext4="${PATH_OUT}/images/rootfs.ext4"114local root_size=`du -s -b ${rootfs_ext4} | awk '{print $1}'`115root_size=`expr $root_size / 1024 / 1024 + 10`116local total_size=`expr $root_size + $boot_size + $boot_start + 1`117local root_start=`expr $boot_size + $boot_start`118local boot_end=$root_start119local root_end=$total_size120121#echo "total size: ${total_size}M"122#echo "boot: ${boot_start}, ${boot_end}M"123#echo "rootfs: ${root_start}, ${root_end}M"124125replace_line_if_exist "\"disk_size\":" "\ \ \ \ \ \ \ \ \"disk_size\": \"${total_size} MiB\"," "${gpt_btp}"126sudo "${SCRIPT_DIR}/debian/fb/bpttool" make_table --input "${gpt_btp}" --output_gpt "${PATH_OUT}/images/partition-table.img" --output_json "${PATH_OUT}/partition-table.json"127sudo chown ${USER}:${USER} "${PATH_OUT}/images/partition-table.img"128129boot_size=$(expr $boot_size \* 1024 \* 1024)130#echo "boot_start: ${boot_start}Mib, boot_size: ${boot_size} bytes"131132cp "${SCRIPT_DIR}/debian/grub-post-silicon.cfg" "${PATH_OUT}/images/grub.cfg"133local root_device_guid=$("${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" | grep "PARTITION1, guid:" | awk -F ":" '{print $2}')134sed -i "s:root=/dev/nvme0n1p2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"135sed -i "s:root=/dev/sda2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"136137local dtb_args=()138while IFS= read -r -d $'\0' dtb; do139dtb_args+=("${dtb}" "/$(basename "${dtb}")")140done < <(find "${SRC}/output/cix/" -maxdepth 1 -name "*.dtb" -print0)141142#sed -i '3cset default="0"' "${PATH_OUT}/images/grub.cfg"143"${SCRIPT_DIR}/tools/mk-part-fat" \144-o "${PATH_OUT}/images/boot.img" \145-s "${boot_size}" \146-l "ESP" \147"${SCRIPT_DIR}/grub.efi" "/EFI/BOOT/BOOTAA64.EFI" \148"${PATH_OUT}/images/grub.cfg" "/grub/grub.cfg" \149"${SRC}/output/cix/Image" "/Image" \150"${SCRIPT_DIR}/cix_binary/device/images/rootfs.cpio.gz" "/rootfs.cpio.gz" \151"${dtb_args[@]}" > /dev/null 2>&1152153fatlabel -i "${PATH_OUT}/images/boot.img" "0x${boot_uuid}"154155dd if=/dev/zero of="${cix_image_name}" bs=1M count=$total_size > /dev/null 2>&1156parted -s "${cix_image_name}" mklabel gpt > /dev/null 2>&1157158dd if="${PATH_OUT}/images/partition-table.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=0 > /dev/null 2>&1159160local boot_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p boot | awk '{print $1}'`161boot_offset=`expr $boot_offset \* 512 / 1024 / 1024`162#echo "boot offset: $boot_offset M bytes"163dd if="${PATH_OUT}/images/boot.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$boot_offset > /dev/null 2>&1164165local rootfs_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p root | awk '{print $1}'`166rootfs_offset=`expr $rootfs_offset \* 512 / 1024 / 1024`167#echo "root offset: $rootfs_offset M bytes"168dd if="${rootfs_ext4}" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$rootfs_offset > /dev/null 2>&1169170if [[ $COMPRESS_OUTPUTIMAGE == "" || $COMPRESS_OUTPUTIMAGE == no ]]; then171COMPRESS_OUTPUTIMAGE="sha,img"172elif [[ $COMPRESS_OUTPUTIMAGE == yes ]]; then173COMPRESS_OUTPUTIMAGE="sha,xz"174fi175176if [[ $COMPRESS_OUTPUTIMAGE == *xz* ]]; then177display_alert "Compressing" "${cix_image_name}.xz" "info"178# compressing consumes a lot of memory we don't have. Waiting for previous packing job to finish helps to run a lot more builds in parallel179available_cpu=$(grep -c 'processor' /proc/cpuinfo)180[[ ${available_cpu} -gt 8 ]] && available_cpu=8 # using more cpu cores for compressing is pointless181available_mem=$(LC_ALL=c free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}') # in percentage182183pixz -7 -p ${available_cpu} -f $(expr ${available_cpu} + 2) < $cix_image_name > ${cix_image_name}.xz184compression_type=".xz"185fi186187if [[ $COMPRESS_OUTPUTIMAGE == *sha* ]]; then188cd ${SRC}/output/images/${version}/189display_alert "SHA256 calculating" "${version}.img${compression_type}" "info"190sha256sum -b ${version}.img${compression_type} > ${version}.img${compression_type}.sha191fi192193display_alert "Done building" "$cix_image_name" "info"194}195196197