Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong
GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/scripts/build-cix-image.sh
15073 views
1
#!/usr/bin/env bash
2
3
# Copyright 2024 Cix Technology Group Co., Ltd.
4
# All Rights Reserved.
5
#
6
# The following programs are the sole property of Cix Technology Group Co., Ltd.,
7
# and contain its proprietary and confidential information.
8
#
9
10
function replace_line_if_exist() {
11
local src=$1
12
local dst=$2
13
local file=$3
14
15
if [[ -e "$file" ]]; then
16
set +E
17
local res=`grep -n "${src}" "${file}"`
18
#echo "res:$res"
19
if [[ ${#res} -gt 0 ]]; then
20
line=`echo "${res}" | cut -d ":" -f 1`
21
fi
22
set -E
23
#echo "line: $line"
24
if [[ $line -gt 0 ]]; then
25
sed -i "${line}c${dst}" "${file}"
26
fi
27
fi
28
}
29
30
function create_cix_rootfs()
31
{
32
display_alert "Preparing" "cix rootfs" "info"
33
34
local PATH_OUT=${SRC}/output/cix
35
local ROOT_FREE_SIZE=4096000
36
rootfs_ext4=${PATH_OUT}/images/rootfs.ext4
37
38
rm -rf ${PATH_OUT} > /dev/null 2>&1
39
mkdir -p ${PATH_OUT}/images > /dev/null 2>&1
40
41
mount --bind --make-private $SDCARD $MOUNT/
42
43
local debianSize=`sudo du ${MOUNT} -d 0 -k | sudo awk -F ' ' '{print $1}'`
44
debianSize=`expr $debianSize \* 13 / 10`
45
if [[ ${debianSize} -lt 1024 ]]; then
46
totalsize='10240'
47
elif [[ ${debianSize} -lt 10240 ]]; then
48
totalsize=`expr $debianSize \* 50 / 10`
49
elif [[ ${debianSize} -lt 102400 ]]; then
50
totalsize=`expr $debianSize \* 25 / 10`
51
elif [[ ${debianSize} -lt 1024000 ]]; then
52
totalsize=`expr $debianSize \* 20 / 10`
53
else
54
totalsize=`expr $debianSize + ${ROOT_FREE_SIZE:-4096000}`
55
fi
56
57
local count=`expr $totalsize / 4`
58
root_uuid=$(uuidgen)
59
boot_uuid=$(uuidgen | tr -d '-' | cut -c1-8 | tr 'a-f' 'A-F')
60
formatted_boot_uuid="${boot_uuid:0:4}-${boot_uuid:4:4}"
61
cat <<-END > ${SDCARD}/etc/fstab
62
# /etc/fstab: static file system information.
63
#
64
# Use 'blkid' to print the universally unique identifier for a device; this may
65
# be used with UUID= as a more robust way to name devices that works even if
66
# disks are added and removed. See fstab(5).
67
#
68
# <file system> <mount point> <type> <options> <dump> <pass>
69
70
END
71
#echo "UUID=${formatted_boot_uuid} /boot vfat defaults,umask=0077 0 2" >> $MOUNT/etc/fstab
72
#echo "UUID=$root_uuid / ext4 errors=remount-ro 0 1" >> $MOUNT/etc/fstab
73
74
dd if=/dev/zero of="${rootfs_ext4}" bs=4k count=$count > /dev/null 2>&1
75
#mkfs.ext4 -U ${uuid} -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&1
76
mkfs.ext4 -F -i 4096 "${rootfs_ext4}" -d "${MOUNT}" > /dev/null 2>&1
77
fsck.ext4 -pvfD "${rootfs_ext4}" > /dev/null 2>&1
78
79
umount $MOUNT
80
rm -rf $MOUNT
81
}
82
83
function create_cix_image()
84
{
85
display_alert "Creating" "cix image" "info"
86
87
local PATH_OUT=${SRC}/output/cix
88
local gpt_btp=${PATH_OUT}/images/partition.bpt
89
local SCRIPT_DIR=${EXTER}/cache/sources/component_cix-${BRANCH}
90
91
if [[ $SELECTED_CONFIGURATION == "cli_standard" ]]; then
92
IMAGE_TYPE=server
93
elif [[ $SELECTED_CONFIGURATION == "cli_minimal" ]]; then
94
IMAGE_TYPE=minimal
95
else
96
IMAGE_TYPE=desktop
97
fi
98
version="${BOARD^}_${REVISION}_${DISTRIBUTION,}_${RELEASE}_${IMAGE_TYPE}"${DESKTOP_ENVIRONMENT:+_$DESKTOP_ENVIRONMENT}"_linux$(grab_version "$LINUXSOURCEDIR")"
99
mkdir -p ${SRC}/output/images/${version}
100
local cix_image_name="${SRC}/output/images/${version}/${version}.img"
101
102
cp -f "${SCRIPT_DIR}/debian/fb/partition.bpt" "${gpt_btp}" || {
103
display_alert "Failed to copy BPT template" "${gpt_btp}" "err"
104
return 1
105
}
106
107
local boot_start=$(expr $(sed -n '6p' ${gpt_btp} | awk '{print $2}') / 1024 / 1024)
108
local boot_size=$(expr $(sed -n '11p' ${gpt_btp} | awk -F '"' '{print $4}' | awk '{print $1}'))
109
110
if [[ ! -f "${rootfs_ext4}" ]]; then
111
display_alert "Rootfs missing" "${rootfs_ext4}" "err"
112
return 1
113
fi
114
115
local rootfs_ext4="${PATH_OUT}/images/rootfs.ext4"
116
local root_size=`du -s -b ${rootfs_ext4} | awk '{print $1}'`
117
root_size=`expr $root_size / 1024 / 1024 + 10`
118
local total_size=`expr $root_size + $boot_size + $boot_start + 1`
119
local root_start=`expr $boot_size + $boot_start`
120
local boot_end=$root_start
121
local root_end=$total_size
122
123
#echo "total size: ${total_size}M"
124
#echo "boot: ${boot_start}, ${boot_end}M"
125
#echo "rootfs: ${root_start}, ${root_end}M"
126
127
replace_line_if_exist "\"disk_size\":" "\ \ \ \ \ \ \ \ \"disk_size\": \"${total_size} MiB\"," "${gpt_btp}"
128
sudo "${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"
129
sudo chown ${USER}:${USER} "${PATH_OUT}/images/partition-table.img"
130
131
boot_size=$(expr $boot_size \* 1024 \* 1024)
132
#echo "boot_start: ${boot_start}Mib, boot_size: ${boot_size} bytes"
133
134
cp "${SCRIPT_DIR}/debian/grub-post-silicon.cfg" "${PATH_OUT}/images/grub.cfg"
135
local root_device_guid=$("${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" | grep "PARTITION1, guid:" | awk -F ":" '{print $2}')
136
sed -i "s:root=/dev/nvme0n1p2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"
137
sed -i "s:root=/dev/sda2:root=PARTUUID=${root_device_guid}:g" "${PATH_OUT}/images/grub.cfg"
138
139
local dtb_args=()
140
while IFS= read -r -d $'\0' dtb; do
141
dtb_args+=("${dtb}" "/$(basename "${dtb}")")
142
done < <(find "${LINUXSOURCEDIR}/arch/arm64/boot/dts/cix/" -maxdepth 1 -name "*.dtb" -print0)
143
144
sed -i '3cset default="0"' "${PATH_OUT}/images/grub.cfg"
145
"${SCRIPT_DIR}/tools/mk-part-fat" \
146
-o "${PATH_OUT}/images/boot.img" \
147
-s "${boot_size}" \
148
-l "ESP" \
149
"${SCRIPT_DIR}/grub.efi" "/EFI/BOOT/BOOTAA64.EFI" \
150
"${PATH_OUT}/images/grub.cfg" "/grub/grub.cfg" \
151
"${LINUXSOURCEDIR}/arch/arm64/boot/Image" "/Image" \
152
"${SCRIPT_DIR}/cix_binary/device/images/rootfs.cpio.gz" "/rootfs.cpio.gz" \
153
"${dtb_args[@]}" > /dev/null 2>&1
154
155
fatlabel -i "${PATH_OUT}/images/boot.img" "0x${boot_uuid}"
156
157
dd if=/dev/zero of="${cix_image_name}" bs=1M count=$total_size > /dev/null 2>&1
158
parted -s "${cix_image_name}" mklabel gpt > /dev/null 2>&1
159
160
dd if="${PATH_OUT}/images/partition-table.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=0 > /dev/null 2>&1
161
162
local boot_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p boot | awk '{print $1}'`
163
boot_offset=`expr $boot_offset \* 512 / 1024 / 1024`
164
#echo "boot offset: $boot_offset M bytes"
165
dd if="${PATH_OUT}/images/boot.img" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$boot_offset > /dev/null 2>&1
166
167
local rootfs_offset=`"${SCRIPT_DIR}/debian/cix_tool" --flash-tool -d "${PATH_OUT}/images/partition-table.img" -p root | awk '{print $1}'`
168
rootfs_offset=`expr $rootfs_offset \* 512 / 1024 / 1024`
169
#echo "root offset: $rootfs_offset M bytes"
170
dd if="${rootfs_ext4}" of="${cix_image_name}" conv=notrunc,fsync bs=1M seek=$rootfs_offset > /dev/null 2>&1
171
172
if [[ $COMPRESS_OUTPUTIMAGE == "" || $COMPRESS_OUTPUTIMAGE == no ]]; then
173
COMPRESS_OUTPUTIMAGE="sha,img"
174
elif [[ $COMPRESS_OUTPUTIMAGE == yes ]]; then
175
COMPRESS_OUTPUTIMAGE="sha,xz"
176
fi
177
178
if [[ $COMPRESS_OUTPUTIMAGE == *xz* ]]; then
179
display_alert "Compressing" "${cix_image_name}.xz" "info"
180
# 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 parallel
181
available_cpu=$(grep -c 'processor' /proc/cpuinfo)
182
[[ ${available_cpu} -gt 8 ]] && available_cpu=8 # using more cpu cores for compressing is pointless
183
available_mem=$(LC_ALL=c free | grep Mem | awk '{print $4/$2 * 100.0}' | awk '{print int($1)}') # in percentage
184
185
pixz -7 -p ${available_cpu} -f $(expr ${available_cpu} + 2) < $cix_image_name > ${cix_image_name}.xz
186
compression_type=".xz"
187
fi
188
189
if [[ $COMPRESS_OUTPUTIMAGE == *sha* ]]; then
190
cd ${SRC}/output/images/${version}/
191
display_alert "SHA256 calculating" "${version}.img${compression_type}" "info"
192
sha256sum -b ${version}.img${compression_type} > ${version}.img${compression_type}.sha
193
fi
194
195
display_alert "Done building" "$cix_image_name" "info"
196
}
197
198