blob: 3f33ec83253752cb095736aeae6501690d1c6741 [file] [log] [blame]
Jiyong Parka128bad2024-09-20 16:53:57 +09001#!/bin/bash
2
3# This is a script to build a Debian image that can run in a VM created via AVF.
4# TODOs:
Jiyong Parka128bad2024-09-20 16:53:57 +09005# - Add Android-specific packages via a new class
6# - Use a stable release from debian-cloud-images
7
8show_help() {
maciek swiech0fdd0512024-10-11 15:12:44 +00009 echo "Usage: sudo $0 [OPTION]... [FILE]"
10 echo "Builds a debian image and save it to FILE. [sudo is required]"
11 echo "Options:"
12 echo "-h Print usage and this help message and exit."
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000013 echo "-a ARCH Architecture of the image [default is host arch: $(uname -m)]"
Saswat Padhiac62a8b2024-12-06 00:48:07 +000014 echo "-k Build and use our custom kernel [default is cloud kernel]"
Jeongik Cha06f4ac52024-11-12 15:56:05 +090015 echo "-r Release mode build"
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000016 echo "-w Save temp work directory [for debugging]"
Jiyong Parka128bad2024-09-20 16:53:57 +090017}
18
19check_sudo() {
20 if [ "$EUID" -ne 0 ]; then
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000021 echo "Please run as root." ; exit 1
Jiyong Parka128bad2024-09-20 16:53:57 +090022 fi
23}
24
25parse_options() {
Saswat Padhiac62a8b2024-12-06 00:48:07 +000026 while getopts "a:hkrw" option; do
Jiyong Parka128bad2024-09-20 16:53:57 +090027 case ${option} in
28 h)
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000029 show_help ; exit
30 ;;
maciek swiech0fdd0512024-10-11 15:12:44 +000031 a)
maciek swiech0fdd0512024-10-11 15:12:44 +000032 arch="$OPTARG"
maciek swiech0fdd0512024-10-11 15:12:44 +000033 ;;
Saswat Padhiac62a8b2024-12-06 00:48:07 +000034 k)
35 use_custom_kernel=1
36 ;;
Jeongik Cha06f4ac52024-11-12 15:56:05 +090037 r)
38 mode=release
39 ;;
Saswat Padhi26c4ef32024-11-29 19:46:53 +000040 w)
41 save_workdir=1
42 ;;
maciek swiech0fdd0512024-10-11 15:12:44 +000043 *)
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000044 echo "Invalid option: $OPTARG" ; exit 1
maciek swiech0fdd0512024-10-11 15:12:44 +000045 ;;
Jiyong Parka128bad2024-09-20 16:53:57 +090046 esac
47 done
Saswat Padhibf8a9cb2024-12-04 02:32:33 +000048 case "$arch" in
49 aarch64)
50 debian_arch="arm64"
51 ;;
52 x86_64)
53 debian_arch="amd64"
54 ;;
55 *)
56 echo "Invalid architecture: $arch" ; exit 1
57 ;;
58 esac
maciek swiech0fdd0512024-10-11 15:12:44 +000059 if [[ "${*:$OPTIND:1}" ]]; then
Jiyong Park0b3a2ba2024-12-23 12:54:21 +090060 output="${*:$OPTIND:1}"
Jiyong Parka128bad2024-09-20 16:53:57 +090061 fi
62}
63
Jiyong Park879ee4a2024-11-29 14:00:47 +090064prepare_build_id() {
Jiyong Park879ee4a2024-11-29 14:00:47 +090065 if [ -z "${KOKORO_BUILD_NUMBER}" ]; then
Jiyong Parkec71e582024-12-23 14:21:57 +090066 echo eng-$(hostname)-$(date --utc)
Jiyong Park879ee4a2024-11-29 14:00:47 +090067 else
Jiyong Parkec71e582024-12-23 14:21:57 +090068 echo ${KOKORO_BUILD_NUMBER}
Jiyong Park879ee4a2024-11-29 14:00:47 +090069 fi
Jiyong Park879ee4a2024-11-29 14:00:47 +090070}
71
Jiyong Parka128bad2024-09-20 16:53:57 +090072install_prerequisites() {
Jiyong Park0e565ed2024-09-24 12:39:53 +090073 apt update
maciek swiech0fdd0512024-10-11 15:12:44 +000074 packages=(
Jeongik Cha1542d152024-12-04 13:22:09 +090075 apt-utils
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090076 automake
maciek swiech0fdd0512024-10-11 15:12:44 +000077 binfmt-support
78 build-essential
79 ca-certificates
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090080 cmake
maciek swiech0fdd0512024-10-11 15:12:44 +000081 curl
82 debsums
83 dosfstools
84 fai-server
85 fai-setup-storage
86 fdisk
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090087 git
88 libjson-c-dev
89 libtool
90 libwebsockets-dev
maciek swiech0fdd0512024-10-11 15:12:44 +000091 make
Jeongik Chace3a3962024-10-12 03:47:23 +090092 protobuf-compiler
maciek swiech0fdd0512024-10-11 15:12:44 +000093 python3
94 python3-libcloud
95 python3-marshmallow
96 python3-pytest
97 python3-yaml
98 qemu-user-static
99 qemu-utils
100 sudo
101 udev
102 )
103 if [[ "$arch" == "aarch64" ]]; then
104 packages+=(
105 gcc-aarch64-linux-gnu
106 libc6-dev-arm64-cross
107 qemu-system-arm
108 )
109 else
110 packages+=(
Jeongik Cha904d9622024-10-21 11:16:37 +0900111 qemu-system
Jeongik Cha8e711982024-10-20 12:45:35 +0900112 )
113 fi
114
115 # TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
116 if [[ "$arch" == "x86_64" ]]; then
117 packages+=(
118 libguestfs-tools
Saswat Padhi79f52132024-11-27 03:56:40 +0000119 linux-image-generic
maciek swiech0fdd0512024-10-11 15:12:44 +0000120 )
121 fi
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000122
123 if [[ "$use_custom_kernel" -eq 1 ]]; then
124 packages+=(
125 bc
126 bison
127 debhelper
128 dh-exec
129 flex
130 gcc-12
131 kernel-wedge
132 libelf-dev
133 libpci-dev
134 lz4
135 pahole
136 python3-jinja2
137 python3-docutils
138 quilt
139 rsync
140 )
141 if [[ "$arch" == "aarch64" ]]; then
142 packages+=(
143 gcc-arm-linux-gnueabihf
144 gcc-12-aarch64-linux-gnu
145 )
146 fi
147 fi
148
Jiyong Park44dd28f2024-09-20 18:47:40 +0900149 DEBIAN_FRONTEND=noninteractive \
maciek swiech0fdd0512024-10-11 15:12:44 +0000150 apt install --no-install-recommends --assume-yes "${packages[@]}"
Jeongik Chab137a5f2024-10-02 12:53:05 +0900151
maciek swiech0fdd0512024-10-11 15:12:44 +0000152 if [ ! -f $"HOME"/.cargo/bin/cargo ]; then
Seungjae Yoo198a0fb2024-10-04 16:29:12 +0900153 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
154 fi
155
maciek swiech0fdd0512024-10-11 15:12:44 +0000156 source "$HOME"/.cargo/env
157 rustup target add "${arch}"-unknown-linux-gnu
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900158 cargo install cargo-license
Jiyong Parka128bad2024-09-20 16:53:57 +0900159}
160
161download_debian_cloud_image() {
162 local ver=master
163 local prj=debian-cloud-images
maciek swiech0fdd0512024-10-11 15:12:44 +0000164 local url="https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz"
165 local outdir="${debian_cloud_image}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900166
maciek swiech0fdd0512024-10-11 15:12:44 +0000167 mkdir -p "${outdir}"
168 wget -O - "${url}" | tar xz -C "${outdir}" --strip-components=1
Jiyong Parka128bad2024-09-20 16:53:57 +0900169}
170
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900171build_rust_binary_and_copy() {
172 pushd "$(dirname "$0")/../../guest/$1" > /dev/null
Jeongik Cha06f4ac52024-11-12 15:56:05 +0900173 local release_flag=
174 local artifact_mode=debug
175 if [[ "$mode" == "release" ]]; then
176 release_flag="--release"
177 artifact_mode=release
178 fi
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900179 RUSTFLAGS="-C linker=${arch}-linux-gnu-gcc" cargo build \
180 --target "${arch}-unknown-linux-gnu" \
Jeongik Cha06f4ac52024-11-12 15:56:05 +0900181 --target-dir "${workdir}/$1" ${release_flag}
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900182 mkdir -p "${dst}/files/usr/local/bin/$1"
Jeongik Cha5d399fb2024-11-12 19:44:33 +0900183 cp "${workdir}/$1/${arch}-unknown-linux-gnu/${artifact_mode}/$1" "${dst}/files/usr/local/bin/$1/AVF"
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900184 chmod 777 "${dst}/files/usr/local/bin/$1/AVF"
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900185
186 mkdir -p "${dst}/files/usr/share/doc/$1"
187 cargo license > "${dst}/files/usr/share/doc/$1/copyright"
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900188 popd > /dev/null
189}
190
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900191build_ttyd() {
192 local ttyd_version=1.7.7
193 local url="https://github.com/tsl0922/ttyd/archive/refs/tags/${ttyd_version}.tar.gz"
maciek swieche17e59f2024-11-25 20:13:23 +0000194 cp -r "$(dirname "$0")/ttyd" "${workdir}/ttyd"
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900195
196 pushd "${workdir}" > /dev/null
197 wget "${url}" -O - | tar xz
198 cp ttyd/* ttyd-${ttyd_version}/scripts
199 pushd "$workdir/ttyd-${ttyd_version}" > /dev/null
200 bash -c "env BUILD_TARGET=${arch} ./scripts/cross-build.sh"
201 mkdir -p "${dst}/files/usr/local/bin/ttyd"
maciek swieche17e59f2024-11-25 20:13:23 +0000202 cp "/tmp/stage/${arch}-linux-musl/bin/ttyd" "${dst}/files/usr/local/bin/ttyd/AVF"
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900203 chmod 777 "${dst}/files/usr/local/bin/ttyd/AVF"
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900204 mkdir -p "${dst}/files/usr/share/doc/ttyd"
205 cp LICENSE "${dst}/files/usr/share/doc/ttyd/copyright"
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900206 popd > /dev/null
207 popd > /dev/null
208}
209
Jiyong Park44dd28f2024-09-20 18:47:40 +0900210copy_android_config() {
maciek swieche17e59f2024-11-25 20:13:23 +0000211 local src
212 local dst
213 src="$(dirname "$0")/fai_config"
214 dst="${config_space}"
Jiyong Park44dd28f2024-09-20 18:47:40 +0900215
maciek swiech0fdd0512024-10-11 15:12:44 +0000216 cp -R "${src}"/* "${dst}"
217 cp "$(dirname "$0")/image.yaml" "${resources_dir}"
Jeongik Cha50952062024-09-23 18:13:38 +0900218
Jeongik Cha1542d152024-12-04 13:22:09 +0900219 cp -R "$(dirname "$0")/localdebs/" "${debian_cloud_image}/"
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900220 build_ttyd
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900221 build_rust_binary_and_copy forwarder_guest
222 build_rust_binary_and_copy forwarder_guest_launcher
Jeongik Cha3cea4812024-12-05 23:04:30 +0900223 build_rust_binary_and_copy shutdown_runner
Jiyong Park44dd28f2024-09-20 18:47:40 +0900224}
225
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000226package_custom_kernel() {
227 if [[ "$use_custom_kernel" != 1 ]]; then
228 echo "linux-headers-generic" >> "${config_space}/package_config/AVF"
229 return
230 fi
231
232 # NOTE: 6.1 is the latest LTS kernel for which Debian's kernel build scripts
233 # work on Python 3.10, the default version on our Ubuntu 22.04 builders.
234 local debian_kver="6.1.119-1"
235 local custom_flavour="avf"
236 local ksrc_base_url="https://deb.debian.org/debian/pool/main/l/linux"
237
Saswat Padhi74b3ac92024-12-11 13:41:59 -0800238 local dsc_url="${ksrc_base_url}/linux_${debian_kver}.dsc"
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000239 local debian_ksrc_url="${ksrc_base_url}/linux_${debian_kver}.debian.tar.xz"
240 local orig_ksrc_url="${ksrc_base_url}/linux_${debian_kver%-*}.orig.tar.xz"
241
Saswat Padhi74b3ac92024-12-11 13:41:59 -0800242 # 0. Grab the kernel sources, and the latest debian keyrings
243 mkdir -p "${workdir}/kernel"
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000244 pushd "${workdir}/kernel" > /dev/null
Saswat Padhi74b3ac92024-12-11 13:41:59 -0800245 wget "$dsc_url"
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000246 wget "$orig_ksrc_url"
Saswat Padhi74b3ac92024-12-11 13:41:59 -0800247 wget "$debian_ksrc_url"
248 rsync -az --progress keyring.debian.org::keyrings/keyrings/ /usr/share/keyrings/
249
250 # 1. Verify, extract and merge patches into the original kernel sources
251 dpkg-source --require-strong-checksums \
252 --require-valid-signature \
253 --extract linux_${debian_kver}.dsc
254 pushd "linux-${debian_kver%-*}" > /dev/null
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000255 # TODO: Copy our own kernel patches to debian/patches
256 # and add patch file names in the desired order to debian/patches/series
257 ./debian/rules orig
258
259 local abi_kver="$(sed -nE 's;Package: linux-support-(.*);\1;p' debian/control)"
260 local debarch_flavour="${custom_flavour}-${debian_arch}"
261 local abi_flavour="${abi_kver}-${debarch_flavour}"
262
263 # 2. Define our custom flavour and regenerate control file
264 # NOTE: Our flavour extends Debian's `cloud` config on the `none` featureset.
265 cat > debian/config/${debian_arch}/config.${debarch_flavour} <<EOF
266# TODO: Add our custom kernel config to this file
267EOF
268
269 sed -z "s;\[base\]\nflavours:;[base]\nflavours:\n ${debarch_flavour};" \
270 -i debian/config/${debian_arch}/none/defines
271 cat >> debian/config/${debian_arch}/none/defines <<EOF
272[${debarch_flavour}_image]
273configs:
274 config.cloud
275 ${debian_arch}/config.${debarch_flavour}
276EOF
277 cat >> debian/config/${debian_arch}/defines <<EOF
278[${debarch_flavour}_description]
279hardware: ${arch} AVF
280hardware-long: ${arch} Android Virtualization Framework
281EOF
282 ./debian/rules debian/control || true
283
284 # 3. Build the kernel and generate Debian packages
285 ./debian/rules source
286 [[ "$arch" == "$(uname -m)" ]] || export $(dpkg-architecture -a $debian_arch)
287 make -j$(nproc) -f debian/rules.gen \
288 "binary-arch_${debian_arch}_none_${debarch_flavour}"
289
290 # 4. Copy the packages to localdebs and add their names to package_config/AVF
291 popd > /dev/null
292 cp "linux-headers-${abi_flavour}_${debian_kver}_${debian_arch}.deb" \
293 "linux-image-${abi_flavour}-unsigned_${debian_kver}_${debian_arch}.deb" \
294 "${debian_cloud_image}/localdebs/"
295 popd > /dev/null
296 cat >> "${config_space}/package_config/AVF" <<EOF
297linux-headers-${abi_flavour}
298linux-image-${abi_flavour}-unsigned
299EOF
300}
301
Jiyong Parka128bad2024-09-20 16:53:57 +0900302run_fai() {
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900303 local out="${raw_disk_image}"
maciek swiech0fdd0512024-10-11 15:12:44 +0000304 make -C "${debian_cloud_image}" "image_bookworm_nocloud_${debian_arch}"
305 mv "${debian_cloud_image}/image_bookworm_nocloud_${debian_arch}.raw" "${out}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900306}
307
Jiyong Parkdfec0202024-12-23 13:24:01 +0900308generate_output_package() {
309 fdisk -l "${raw_disk_image}"
Jiyong Park159b8f42024-12-26 18:05:22 +0900310 local vm_config="$(realpath $(dirname "$0"))/vm_config.json.${arch}"
311 local root_partition_num=1
312 local bios_partition_num=14
313 local efi_partition_num=15
Mu-Le Lee955b6582024-11-01 15:40:58 +0800314
Jiyong Parkec71e582024-12-23 14:21:57 +0900315 pushd ${workdir} > /dev/null
316
317 echo ${build_id} > build_id
318
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900319 loop=$(losetup -f --show --partscan $raw_disk_image)
maciek swieche17e59f2024-11-25 20:13:23 +0000320 dd if="${loop}p$root_partition_num" of=root_part
321 if [[ "$arch" == "x86_64" ]]; then
322 dd if="${loop}p$bios_partition_num" of=bios_part
323 fi
324 dd if="${loop}p$efi_partition_num" of=efi_part
325 losetup -d "${loop}"
Mu-Le Lee955b6582024-11-01 15:40:58 +0800326
Jiyong Parkec71e582024-12-23 14:21:57 +0900327 cp ${vm_config} vm_config.json
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900328 sed -i "s/{root_part_guid}/$(sfdisk --part-uuid $raw_disk_image $root_partition_num)/g" vm_config.json
maciek swieche17e59f2024-11-25 20:13:23 +0000329 if [[ "$arch" == "x86_64" ]]; then
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900330 sed -i "s/{bios_part_guid}/$(sfdisk --part-uuid $raw_disk_image $bios_partition_num)/g" vm_config.json
maciek swieche17e59f2024-11-25 20:13:23 +0000331 fi
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900332 sed -i "s/{efi_part_guid}/$(sfdisk --part-uuid $raw_disk_image $efi_partition_num)/g" vm_config.json
Jiyong Parkdfec0202024-12-23 13:24:01 +0900333
334 images=()
335 if [[ "$arch" == "aarch64" ]]; then
336 images+=(
337 root_part
338 efi_part
339 )
340 # TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
341 elif [[ "$arch" == "x86_64" ]]; then
342 rm -f vmlinuz initrd.img
343 virt-get-kernel -a "${raw_disk_image}"
344 mv vmlinuz* vmlinuz
345 mv initrd.img* initrd.img
346 images+=(
347 bios_part
348 root_part
349 efi_part
350 vmlinuz
351 initrd.img
352 )
353 fi
354
Jiyong Parkec71e582024-12-23 14:21:57 +0900355 popd > /dev/null
356
Jiyong Parkdfec0202024-12-23 13:24:01 +0900357 # --sparse option isn't supported in apache-commons-compress
Jiyong Parkec71e582024-12-23 14:21:57 +0900358 tar czv -f ${output} -C ${workdir} build_id "${images[@]}" vm_config.json
Mu-Le Lee955b6582024-11-01 15:40:58 +0800359}
360
Jiyong Parka128bad2024-09-20 16:53:57 +0900361clean_up() {
Saswat Padhibf8a9cb2024-12-04 02:32:33 +0000362 [ "$save_workdir" -eq 1 ] || rm -rf "${workdir}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900363}
364
365set -e
366trap clean_up EXIT
367
Jiyong Park0b3a2ba2024-12-23 12:54:21 +0900368output=images.tar.gz
Jiyong Parka128bad2024-09-20 16:53:57 +0900369workdir=$(mktemp -d)
Jiyong Parkec71e582024-12-23 14:21:57 +0900370raw_disk_image=${workdir}/image.raw
Jiyong Park879ee4a2024-11-29 14:00:47 +0900371build_id=$(prepare_build_id)
Jiyong Parka128bad2024-09-20 16:53:57 +0900372debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900373debian_version=bookworm
374config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900375resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
Saswat Padhibf8a9cb2024-12-04 02:32:33 +0000376arch="$(uname -m)"
Jeongik Cha06f4ac52024-11-12 15:56:05 +0900377mode=debug
Saswat Padhi26c4ef32024-11-29 19:46:53 +0000378save_workdir=0
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000379use_custom_kernel=0
Jiyong Park879ee4a2024-11-29 14:00:47 +0900380
maciek swiech0fdd0512024-10-11 15:12:44 +0000381parse_options "$@"
Jiyong Parka128bad2024-09-20 16:53:57 +0900382check_sudo
Jiyong Parka128bad2024-09-20 16:53:57 +0900383install_prerequisites
384download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900385copy_android_config
Saswat Padhiac62a8b2024-12-06 00:48:07 +0000386package_custom_kernel
Jiyong Park0e565ed2024-09-24 12:39:53 +0900387run_fai
Jiyong Parkdfec0202024-12-23 13:24:01 +0900388generate_output_package