Path: blob/next/scripts/build-cix-image.sh
15073 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.ext43637rm -rf ${PATH_OUT} > /dev/null 2>&138mkdir -p ${PATH_OUT}/images > /dev/null 2>&13940mount --bind --make-private $SDCARD $MOUNT/4142local debianSize=`sudo du ${MOUNT} -d 0 -k | sudo awk -F ' ' '{print $1}'`43debianSize=`expr $debianSize \* 13 / 10`44if [[ ${debianSize} -lt 1024 ]]; then45totalsize='10240'46elif [[ ${debianSize} -lt 10240 ]]; then47totalsize=`expr $debianSize \* 50 / 10`48elif [[ ${debianSize} -lt 102400 ]]; then49totalsize=`expr $debianSize \* 25 / 10`50elif [[ ${debianSize} -lt 1024000 ]]; then51totalsize=`expr $debianSize \* 20 / 10`52else53totalsize=`expr $debianSize + ${ROOT_FREE_SIZE:-4096000}`54fi5556local count=`expr $totalsize / 4`57root_uuid=$(uuidgen)58boot_uuid=$(uuidgen | tr -d '-' | cut -c1-8 | tr 'a-f' 'A-F')59formatted_boot_uuid="${boot_uuid:0:4}-${boot_uuid:4:4}"60cat <<-END > ${SDCARD}/etc/fstab61# /etc/fstab: static file system information.62#63# Use 'blkid' to print the universally unique identifier for a device; this may64# be used with UUID= as a more robust way to name devices that works even if65# disks are added and removed. See fstab(5).66#67# <file system> <mount point> <type> <options> <dump> <pass>6869END70#echo "UUID=${formatted_boot_uuid} /boot vfat defaults,umask=0077 0 2" >> $MOUNT/etc/fstab71#echo "UUID=$root_uuid / ext4 errors=remount-ro 0 1" >> $MOUNT/etc/fstab7273dd if=/dev/zero of="${rootfs_ext4}" bs=4k count=$count > /dev/null 2>&174#mkfs.ext4 -U ${uuid} -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&175mkfs.ext4 -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&176fsck.ext4 -pvfD "${rootfs_ext4}" > /dev/null 2>&17778umount $MOUNT79rm -rf $MOUNT80}8182function create_cix_image()83{84display_alert "Creating" "cix image" "info"8586local PATH_OUT=${SRC}/output/cix87local gpt_btp=${PATH_OUT}/images/partition.bpt88local SCRIPT_DIR=${EXTER}/cache/sources/component_cix-${BRANCH}8990if [[ $SELECTED_CONFIGURATION == "cli_standard" ]]; then91IMAGE_TYPE=server92elif [[ $SELECTED_CONFIGURATION == "cli_minimal" ]]; then93IMAGE_TYPE=minimal94else95IMAGE_TYPE=desktop96fi97version="${BOARD^}_${REVISION}_${DISTRIBUTION,}_${RELEASE}_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"98mkdir -p ${SRC}/output/images/${version}99local cix_image_name="${SRC}/output/images/${version}/${version}.img"100101cp -f "${SCRIPT_DIR}/debian/fb/partition.bpt" "${gpt_btp}" || {102display_alert "Failed to copy BPT template" "${gpt_btp}" "err"103return 1104}105106local boot_start=$(expr $(sed -n '6p' ${gpt_btp} | awk '{print $2}') / 1024 / 1024)107local boot_size=$(expr $(sed -n '11p' ${gpt_btp} | awk -F '"' '{print $4}' | awk '{print $1}'))108109if [[ ! -f "${rootfs_ext4}" ]]; then110display_alert "Rootfs missing" "${rootfs_ext4}" "err"111return 1112fi113114local rootfs_ext4="${PATH_OUT}/images/rootfs.ext4"115local root_size=`du -s -b ${rootfs_ext4} | awk '{print $1}'`116root_size=`expr $root_size / 1024 / 1024 + 10`117local total_size=`expr $root_size + $boot_size + $boot_start + 1`118local root_start=`expr $boot_size + $boot_start`119local boot_end=$root_start120local root_end=$total_size121122#echo "total size: ${total_size}M"123#echo "boot: ${boot_start}, ${boot_end}M"124#echo "rootfs: ${root_start}, ${root_end}M"125126replace_line_if_exist "\"disk_size\":" "\ \ \ \ \ \ \ \ \"disk_size\": \"${total_size} MiB\"," "${gpt_btp}"127sudo "${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"128sudo chown ${USER}:${USER} "${PATH_OUT}/images/partition-table.img"129130boot_size=$(expr $boot_size \* 1024 \* 1024)131#echo "boot_start: ${boot_start}Mib, boot_size: ${boot_size} bytes"132133cp "${SCRIPT_DIR}/debian/grub-post-silicon.cfg" "${PATH_OUT}/images/grub.cfg"134local root_device_guid=$("${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" | grep "PARTITION1, guid:" | awk -F ":" '{print $2}')135sed -i "s:root=/dev/nvme0n1p2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"136sed -i "s:root=/dev/sda2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"137138local dtb_args=()139while IFS= read -r -d $'\0' dtb; do140dtb_args+=("${dtb}" "/$(basename "${dtb}")")141done < <(find "${LINUXSOURCEDIR}/arch/arm64/boot/dts/cix/" -maxdepth 1 -name "*.dtb" -print0)142143sed -i '3cset default="0"' "${PATH_OUT}/images/grub.cfg"144"${SCRIPT_DIR}/tools/mk-part-fat" \145-o "${PATH_OUT}/images/boot.img" \146-s "${boot_size}" \147-l "ESP" \148"${SCRIPT_DIR}/grub.efi" "/EFI/BOOT/BOOTAA64.EFI" \149"${PATH_OUT}/images/grub.cfg" "/grub/grub.cfg" \150"${LINUXSOURCEDIR}/arch/arm64/boot/Image" "/Image" \151"${SCRIPT_DIR}/cix_binary/device/images/rootfs.cpio.gz" "/rootfs.cpio.gz" \152"${dtb_args[@]}" > /dev/null 2>&1153154fatlabel -i "${PATH_OUT}/images/boot.img" "0x${boot_uuid}"155156dd if=/dev/zero of="${cix_image_name}" bs=1M count=$total_size > /dev/null 2>&1157parted -s "${cix_image_name}" mklabel gpt > /dev/null 2>&1158159dd if="${PATH_OUT}/images/partition-table.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=0 > /dev/null 2>&1160161local boot_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p boot | awk '{print $1}'`162boot_offset=`expr $boot_offset \* 512 / 1024 / 1024`163#echo "boot offset: $boot_offset M bytes"164dd if="${PATH_OUT}/images/boot.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$boot_offset > /dev/null 2>&1165166local rootfs_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p root | awk '{print $1}'`167rootfs_offset=`expr $rootfs_offset \* 512 / 1024 / 1024`168#echo "root offset: $rootfs_offset M bytes"169dd if="${rootfs_ext4}" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$rootfs_offset > /dev/null 2>&1170171if [[ $COMPRESS_OUTPUTIMAGE == "" || $COMPRESS_OUTPUTIMAGE == no ]]; then172COMPRESS_OUTPUTIMAGE="sha,img"173elif [[ $COMPRESS_OUTPUTIMAGE == yes ]]; then174COMPRESS_OUTPUTIMAGE="sha,xz"175fi176177if [[ $COMPRESS_OUTPUTIMAGE == *xz* ]]; then178display_alert "Compressing" "${cix_image_name}.xz" "info"179# 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 parallel180available_cpu=$(grep -c 'processor' /proc/cpuinfo)181[[ ${available_cpu} -gt 8 ]] && available_cpu=8 # using more cpu cores for compressing is pointless182available_mem=$(LC_ALL=c free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}') # in percentage183184pixz -7 -p ${available_cpu} -f $(expr ${available_cpu} + 2) < $cix_image_name > ${cix_image_name}.xz185compression_type=".xz"186fi187188if [[ $COMPRESS_OUTPUTIMAGE == *sha* ]]; then189cd ${SRC}/output/images/${version}/190display_alert "SHA256 calculating" "${version}.img${compression_type}" "info"191sha256sum -b ${version}.img${compression_type} > ${version}.img${compression_type}.sha192fi193194display_alert "Done building" "$cix_image_name" "info"195}196197198