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