aboutsummaryrefslogtreecommitdiff
path: root/lt-qcom-linux-test/builders.sh
blob: 8028658a11ae577295c0210cccffc6a08dff74ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash

set -x

wget_error() {
	wget --timeout=60 -c $1 -P out/
	retcode=$?
	if [ $retcode -ne 0 ]; then
		exit $retcode
	fi
}

function copy_archive_to_rootfs() {
	archive_file=$1
	archive_file_type=$2
	target_file=$3
	target_file_type=$4

	if [[ $target_file_type = *"cpio archive"* ]]; then
		mkdir -p out/archive
		if [[ $archive_file_type = *"Debian binary package"* ]]; then
			dpkg-deb -x $archive_file out/archive
		else
			tar -xvf $archive_file -C out/archive
		fi
		cd out/archive
		find . | cpio -R 0:0 -oA -H newc -F ../../$target_file
		cd ../../
		rm -rf out/archive
	else
		set -e
		archive_tmpd="out/archive"

		if [[ $archive_file_type = *"Debian binary package"* ]]; then
			required_size=$(dpkg -f $archive_file Installed-Size)
		else
			required_size=$(${GZ} -l $archive_file | tail -1 | awk '{print $2}')
		fi
		required_size=$(( $required_size / 1024 ))

		sudo e2fsck -y -f $target_file
		block_count=$(sudo dumpe2fs -h $target_file | grep "Block count" | awk '{print $3}')
		block_size=$(sudo dumpe2fs -h $target_file | grep "Block size" | awk '{print $3}')
		current_size=$(( $block_size * $block_count / 1024 ))

		final_size=$(( $current_size + $required_size + 32768 ))
		sudo resize2fs -p $target_file "$final_size"K

		sudo mkdir -p $archive_tmpd
		if [[ $archive_file_type = *"Debian binary package"* ]]; then
			sudo dpkg-deb -x $archive_file $archive_tmpd
		else
			sudo tar -xvf $archive_file -C $archive_tmpd
		fi

		cdir=$(pwd)
		pushd $cdir
		cd $archive_tmpd
		for f in $(find . -type f)
		do
			e2cp -a -p -G 0 -O 0 -v $f $cdir/$target_file:/
		done
		for l in $(find . -type l)
		do
			f=$(readlink -f $l) || continue
			if [ -f "$f" ]; then
				e2cp -p -G 0 -O 0 -v $f $cdir/$target_file:/$l
			fi
		done
		popd
		sudo rm -rf $archive_tmpd

		set +e
	fi
}

function remove_unused_firmware() {
	target_file=$1
	target_file_type=$2

	# Remove all not needed firmware by platform, In db845c it ran out of space causing
	# boot failure.
	mkdir -p out/archive
	firmware_list_file=$(realpath ./configs/lt-qcom-linux-test/firmware.list/${MACHINE})

	cd out/archive
	cpio -idv -H newc < ../../$target_file

	if [ -f "$firmware_list_file" ]; then
		for f in $(find ./lib/firmware/ -type f)
		do
			if ! grep -qxFe "$f" $firmware_list_file; then
				rm -fv "$f"
			fi
		done
		find lib/firmware/ -xtype l -delete
		find lib/firmware/ -type d -empty -delete

	else
		rm -rf lib/firmware
	fi

	find . | cpio -R 0:0 -ov -H newc > ../../$target_file
	cd ../../
	rm -rf out/archive
}

function create_ramdisk_from_folder() {
	ramdisk_name=$1
	ramdisk_folder=$2
	ramdisk="$ramdisk_name.cpio"

	cd $ramdisk_folder
	find . | cpio -R 0:0 -ov -H newc > "../../out/$ramdisk"
	${GZ} "../../out/$ramdisk"
	ramdisk=$ramdisk.gz
	echo "$ramdisk"
	cd ../
}

function overlay_ramdisk_from_git() {
	git_repo=$1
	git_branch=$2

	# clone git repo and get revision details
	project_name="$(basename "$git_repo" .git)"
	project_folder="$project_name"
	project_ramdisk_folder="$(realpath $project_folder)/rootfs"
	git clone -b "$git_branch" --depth 1 "$git_repo" "$project_folder"
	cd "$project_folder"
	DESTDIR="$project_ramdisk_folder" prefix="/usr" make install 2>&1 > /dev/null
	project_name="$project_name-$(git rev-parse --short HEAD)"

	# created the overlayed ramdisk involves the creation of new ramdisk from folder and
	# concat both into a single file
	project_ramdisk_overlay=$(create_ramdisk_from_folder $project_name $project_ramdisk_folder)
	cd ../

	overlayed_ramdisk_file="$(basename $ramdisk_file)+$(basename $project_ramdisk_overlay)"
	cat "$ramdisk_file" "out/$project_ramdisk_overlay" > "out/$overlayed_ramdisk_file"
	echo "$overlayed_ramdisk_file"
	rm -rf "$project_folder"
}

function overlay_ramdisk_from_file() {
	file_name=$1
	file_cpio="out/$2.cpio"

	echo $file_name | cpio -R 0:0 -ov -H newc > $file_cpio
	${GZ} $file_cpio
	file_cpio=$file_cpio.gz

	overlayed_ramdisk_file="$(basename $ramdisk_file)+$(basename $file_cpio)"
	cat "$ramdisk_file" "$file_cpio" > "out/$overlayed_ramdisk_file"
	echo "$overlayed_ramdisk_file"
}

# Set default tools to use
if [ -z "${GZ}" ]; then
	export GZ=gzip
fi

# Generic/default variables
BOOTIMG_PAGESIZE=2048
BOOTIMG_BASE=0x80000000
RAMDISK_BASE=0x84000000
SERIAL_CONSOLE=ttyMSM0
KERNEL_DT_URL="${KERNEL_DT_URL}/qcom/${MACHINE}.dtb"
KERNEL_CMDLINE_APPEND=
ROOTFS_PARTITION=/dev/disk/by-partlabel/rootfs

# Set per MACHINE configuration
case "${MACHINE}" in
	apq8016-sbc|qrb5165-rb5)
		;;
	apq8096-db820c)
		BOOTIMG_PAGESIZE=4096
		;;
	msm8998-mtp|qcs404-evb-1000|qcs404-evb-4000|sm8150-mtp|sm8250-mtp|sm8350-mtp|sc7180-idp|sdm845-mtp)
		ROOTFS_PARTITION=/dev/disk/by-partlabel/userdata
		;;
	sdm845-db845c)
		BOOTIMG_PAGESIZE=4096

		KERNEL_CMDLINE_APPEND="clk_ignore_unused pd_ignore_unused"
		;;
	*)
		echo "Currently MACHINE: ${MACHINE} isn't supported"
		exit 1
		;;
esac

# Validate required parameters
if [ -z "${KERNEL_IMAGE_URL}" ]; then
	echo "ERROR: KERNEL_IMAGE_URL is empty"
	exit 1
fi

# find rootfs and ramdisk to use
case "${MACHINE}" in
	apq8016-sbc|apq8096-db820c|sdm845-db845c|qrb5165-rb5)
		./configs/lt-qcom-linux-test/get_latest_testimage.py
	;;
	*)
		./configs/lt-qcom-linux-test/get_latest_testimage.py https://snapshots.linaro.org/member-builds/qcomlt/testimages/arm64/
	;;
esac
RAMDISK_URL=$(cat output.log  | grep RAMDISK_URL | cut -d= -f2)
ROOTFS_URL=$(cat output.log  | grep ROOTFS_URL | cut -d= -f2)
ROOTFS_DESKTOP_URL=$(cat output.log  | grep ROOTFS_DESKTOP_URL | cut -d= -f2)

# Build information
mkdir -p out
cat > out/HEADER.textile << EOF

h4. QCOM Landing Team - $BUILD_DISPLAY_NAME

Build description:
* Build URL: "$BUILD_URL":$BUILD_URL
* Kernel image URL: $KERNEL_IMAGE_URL
* Kernel dt URL: $KERNEL_DT_URL
* kernel modules URL: $KERNEL_MODULES_URL
* Ramdisk URL: $RAMDISK_URL
* RootFS URL: $ROOTFS_URL
EOF

# Ramdisk/RootFS image and modules populate, download step
wget_error ${RAMDISK_URL}
ramdisk_file=out/$(basename ${RAMDISK_URL})
ramdisk_file_type=$(file $ramdisk_file)

wget_error ${ROOTFS_URL}
rootfs_file=out/$(basename ${ROOTFS_URL})
rootfs_file_type=$(file $rootfs_file)
wget_error ${ROOTFS_DESKTOP_URL}
rootfs_desktop_file=out/$(basename ${ROOTFS_DESKTOP_URL})
rootfs_desktop_file_type=$(file $rootfs_desktop_file)

if [[ ! -z "${KERNEL_MODULES_URL}" ]]; then
	wget_error ${KERNEL_MODULES_URL}
	modules_file="out/$(basename ${KERNEL_MODULES_URL})"

	# XXX: Compress modules to gzip for use copy_archive_to_rootfs
	# generic code to calculate size in ext4 filesystem
	modules_file_type=$(file $modules_file)
	if [[ $modules_file_type = *"XZ compressed data"* ]]; then
		xz -d $modules_file
		modules_file="out/$(basename ${KERNEL_MODULES_URL} .xz)"
		${GZ} $modules_file
		modules_file=$modules_file.gz
	elif [[ $modules_file_type = *"bzip2 compressed data"* ]]; then
		bzip2 -d $modules_file
		modules_file="out/$(basename ${KERNEL_MODULES_URL} .bz2)"
		${GZ} $modules_file
		modules_file=$modules_file.gz
	fi
fi

# Uncompress images to be able populate with modules
rootfs_desktop_comp=''
if [[ $rootfs_desktop_file_type = *"gzip compressed data"* ]]; then
	${GZ} -d $rootfs_desktop_file
	rootfs_desktop_file=out/$(basename ${ROOTFS_DESKTOP_URL} .gz)
	rootfs_desktop_file_type=$(file $rootfs_desktop_file)
	rootfs_desktop_comp='gz'
fi
rootfs_comp=''
if [[ $rootfs_file_type = *"gzip compressed data"* ]]; then
	${GZ} -d $rootfs_file
	rootfs_file=out/$(basename ${ROOTFS_URL} .gz)
	rootfs_file_type=$(file $rootfs_file)
	rootfs_comp='gz'
fi
if [[ $ramdisk_file_type = *"gzip compressed data"* ]]; then
	${GZ} -d $ramdisk_file
	ramdisk_file=out/$(basename ${RAMDISK_URL} .gz)
	ramdisk_file_type=$(file $ramdisk_file)
	ramdisk_comp='gz'
fi


# If rootfs is Android sparse image convert to ext4 to populate with modules
if [[ $rootfs_desktop_file_type = *"Android sparse image"* ]]; then
	rootfs_desktop_file_ext4=out/$(basename ${rootfs_desktop_file} .img).ext4
	simg2img $rootfs_desktop_file $rootfs_desktop_file_ext4
	rootfs_desktop_file=$rootfs_desktop_file_ext4
elif [[ $rootfs_desktop_file_type = *"ext4 filesystem data"* ]]; then
	true
else
	echo "ERROR: ROOTFS_IMAGE type isn't supported: $rootfs_file_type"
	exit 1
fi
if [[ $rootfs_file_type = *"Android sparse image"* ]]; then
	rootfs_file_ext4=out/$(basename ${rootfs_file} .img).ext4
	simg2img $rootfs_file $rootfs_file_ext4
	rootfs_file=$rootfs_file_ext4
elif [[ $rootfs_file_type = *"ext4 filesystem data"* ]]; then
	true
else
	echo "ERROR: ROOTFS_IMAGE type isn't supported: $rootfs_file_type"
	exit 1
fi

# Populate modules and remove not used firmware in ramdisk
remove_unused_firmware "$ramdisk_file" "$ramdisk_file_type"
if [[ ! -z "$modules_file" ]]; then
	modules_file_type=$(file $modules_file)
	copy_archive_to_rootfs "$modules_file" "$modules_file_type" "$ramdisk_file" "$ramdisk_file_type"
	copy_archive_to_rootfs "$modules_file" "$modules_file_type" "$rootfs_file" "$rootfs_file_type"
	copy_archive_to_rootfs "$modules_file" "$modules_file_type" "$rootfs_desktop_file" "$rootfs_desktop_file_type"
fi

# If rootfs was Android sparse image trasform from ext4
if [[ $rootfs_desktop_file_type = *"Android sparse image"* ]]; then
	rootfs_desktop_file_img=out/$(basename $rootfs_desktop_file .ext4).img
	img2simg $rootfs_desktop_file $rootfs_desktop_file_img
	rm $rootfs_desktop_file
	rootfs_desktop_file=$rootfs_desktop_file_img
fi
if [[ $rootfs_file_type = *"Android sparse image"* ]]; then
	rootfs_file_img=out/$(basename $rootfs_file .ext4).img
	img2simg $rootfs_file $rootfs_file_img
	rm $rootfs_file
	rootfs_file=$rootfs_file_img
fi


# Compress ramdisk/rootfs images
if [[ $ramdisk_comp = "gz" ]]; then
	${GZ} $ramdisk_file
	ramdisk_file="$ramdisk_file".gz
	ramdisk_file_type=$(file $ramdisk_file)
	ramdisk_comp=""
fi
if [[ $rootfs_comp = "gz" ]]; then
	${GZ} $rootfs_file
	rootfs_file="$rootfs_file".gz
	rootfs_file_type=$(file $rootfs_file)
	rootfs_comp=""
fi
if [[ $rootfs_desktop_comp = "gz" ]]; then
	${GZ} $rootfs_desktop_file
	rootfs_desktop_file="$rootfs_desktop_file".gz
	rootfs_desktop_file_type=$(file $rootfs_desktop_file)
	rootfs_desktop_comp=""
fi

# Compress kernel image if isn't
wget_error ${KERNEL_IMAGE_URL}
kernel_file=out/$(basename ${KERNEL_IMAGE_URL})
kernel_file_type=$(file $kernel_file)
if [[ ! $kernel_file_type = *"gzip compressed data"* ]]; then
	${GZ} -kf $kernel_file
	kernel_file=$kernel_file.gz
fi

# Making android boot img
dt_mkbootimg_arg=""
if [[ ! -z "${KERNEL_DT_URL}" ]]; then
	wget_error ${KERNEL_DT_URL}
	dt_mkbootimg_arg="--dt out/$(basename ${KERNEL_DT_URL})"
fi

# Overlay ramdisk to install tools, artifacts, etc
if [[ ! -z "${BOOTRR_GIT_REPO}" ]]; then
	overlayed_ramdisk_file="out/$(overlay_ramdisk_from_git "${BOOTRR_GIT_REPO}" "${BOOTRR_GIT_BRANCH}")"
	ramdisk_file=$overlayed_ramdisk_file
fi

# Create boot image (bootrr), uses systemd autologin root
boot_file=boot-${KERNEL_TEST_FLAVOR}-${KERNEL_VERSION}-${BUILD_NUMBER}-${MACHINE}.img
skales-mkbootimg \
	--kernel $kernel_file \
	--ramdisk $overlayed_ramdisk_file \
	--output out/$boot_file \
	$dt_mkbootimg_arg \
	--pagesize "${BOOTIMG_PAGESIZE}" \
	--base "${BOOTIMG_BASE}" \
	--ramdisk_base "${RAMDISK_BASE}" \
	--cmdline "root=/dev/ram0 init=/sbin/init rw console=tty0 console=${SERIAL_CONSOLE},115200n8 earlycon debug net.ifnames=0 ${KERNEL_CMDLINE_APPEND}"

# Create boot image (functional), sdm845-mtp requires an initramfs to mount the rootfs and then
# exec switch_rootfs, use the same method in other boards too
boot_rootfs_file=boot-rootfs-${KERNEL_TEST_FLAVOR}-${KERNEL_VERSION}-${BUILD_NUMBER}-${MACHINE}.img

mkdir -p etc
initrd_release_file=etc/initrd-release
touch $initrd_release_file
overlayed_ramdisk_file="out/$(overlay_ramdisk_from_file "$initrd_release_file" "init_rootfs")"

skales-mkbootimg \
	--kernel $kernel_file \
	--ramdisk $overlayed_ramdisk_file \
	--output out/$boot_rootfs_file \
	$dt_mkbootimg_arg \
	--pagesize "${BOOTIMG_PAGESIZE}" \
	--base "${BOOTIMG_BASE}" \
	--ramdisk_base "${RAMDISK_BASE}" \
	--cmdline "root=${ROOTFS_PARTITION} init=/sbin/init rw console=tty0 console=${SERIAL_CONSOLE},115200n8 earlycon debug net.ifnames=0 ${KERNEL_CMDLINE_APPEND}"

echo BOOT_FILE=$boot_file >> builders_out_parameters
echo BOOT_ROOTFS_FILE=$boot_rootfs_file >> builders_out_parameters
echo ROOTFS_FILE="$(basename $rootfs_file)" >> builders_out_parameters
echo ROOTFS_DESKTOP_FILE="$(basename $rootfs_desktop_file)" >> builders_out_parameters

# Parameters for LAVA jobs
echo KERNEL_IMAGE="$(basename $KERNEL_IMAGE_URL)" >> builders_out_parameters
echo KERNEL_DT="$(basename $KERNEL_DT_URL)" >> builders_out_parameters
echo RAMDISK_URL="${RAMDISK_URL}" >> builders_out_parameters
echo KERNEL_DT_URL="${KERNEL_DT_URL}" >> builders_out_parameters

ls -l out/