blob: 5829ecc2d608f93d6d62b4d90edf78b96ae0b44e [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."
13 echo "-a ARCH Architecture of the image [default is aarch64]"
Jiyong Parka128bad2024-09-20 16:53:57 +090014}
15
16check_sudo() {
17 if [ "$EUID" -ne 0 ]; then
18 echo "Please run as root."
19 exit
20 fi
21}
22
23parse_options() {
maciek swiech0fdd0512024-10-11 15:12:44 +000024 while getopts "ha:" option; do
Jiyong Parka128bad2024-09-20 16:53:57 +090025 case ${option} in
26 h)
27 show_help
28 exit;;
maciek swiech0fdd0512024-10-11 15:12:44 +000029 a)
30 if [[ "$OPTARG" != "aarch64" && "$OPTARG" != "x86_64" ]]; then
31 echo "Invalid architecture: $OPTARG"
32 exit
33 fi
34 arch="$OPTARG"
35 if [[ "$arch" == "x86_64" ]]; then
36 debian_arch="amd64"
37 fi
38 ;;
39 *)
40 echo "Invalid option: $OPTARG"
41 exit
42 ;;
Jiyong Parka128bad2024-09-20 16:53:57 +090043 esac
44 done
maciek swiech0fdd0512024-10-11 15:12:44 +000045 if [[ "${*:$OPTIND:1}" ]]; then
46 built_image="${*:$OPTIND:1}"
Jiyong Parka128bad2024-09-20 16:53:57 +090047 fi
48}
49
50install_prerequisites() {
Jiyong Park0e565ed2024-09-24 12:39:53 +090051 apt update
maciek swiech0fdd0512024-10-11 15:12:44 +000052 packages=(
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090053 automake
maciek swiech0fdd0512024-10-11 15:12:44 +000054 binfmt-support
55 build-essential
56 ca-certificates
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090057 cmake
maciek swiech0fdd0512024-10-11 15:12:44 +000058 curl
59 debsums
60 dosfstools
61 fai-server
62 fai-setup-storage
63 fdisk
Jeongik Cha7e7f19d2024-10-31 20:50:24 +090064 git
65 libjson-c-dev
66 libtool
67 libwebsockets-dev
maciek swiech0fdd0512024-10-11 15:12:44 +000068 make
Jeongik Chace3a3962024-10-12 03:47:23 +090069 protobuf-compiler
maciek swiech0fdd0512024-10-11 15:12:44 +000070 python3
71 python3-libcloud
72 python3-marshmallow
73 python3-pytest
74 python3-yaml
75 qemu-user-static
76 qemu-utils
77 sudo
78 udev
79 )
80 if [[ "$arch" == "aarch64" ]]; then
81 packages+=(
82 gcc-aarch64-linux-gnu
83 libc6-dev-arm64-cross
84 qemu-system-arm
85 )
86 else
87 packages+=(
Jeongik Cha904d9622024-10-21 11:16:37 +090088 qemu-system
Jeongik Cha8e711982024-10-20 12:45:35 +090089 )
90 fi
91
92 # TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
93 if [[ "$arch" == "x86_64" ]]; then
94 packages+=(
95 libguestfs-tools
maciek swiech0fdd0512024-10-11 15:12:44 +000096 )
97 fi
Jiyong Park44dd28f2024-09-20 18:47:40 +090098 DEBIAN_FRONTEND=noninteractive \
maciek swiech0fdd0512024-10-11 15:12:44 +000099 apt install --no-install-recommends --assume-yes "${packages[@]}"
Jeongik Chab137a5f2024-10-02 12:53:05 +0900100
maciek swiech0fdd0512024-10-11 15:12:44 +0000101 if [ ! -f $"HOME"/.cargo/bin/cargo ]; then
Seungjae Yoo198a0fb2024-10-04 16:29:12 +0900102 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
103 fi
104
maciek swiech0fdd0512024-10-11 15:12:44 +0000105 source "$HOME"/.cargo/env
106 rustup target add "${arch}"-unknown-linux-gnu
Jiyong Parka128bad2024-09-20 16:53:57 +0900107}
108
109download_debian_cloud_image() {
110 local ver=master
111 local prj=debian-cloud-images
maciek swiech0fdd0512024-10-11 15:12:44 +0000112 local url="https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz"
113 local outdir="${debian_cloud_image}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900114
maciek swiech0fdd0512024-10-11 15:12:44 +0000115 mkdir -p "${outdir}"
116 wget -O - "${url}" | tar xz -C "${outdir}" --strip-components=1
Jiyong Parka128bad2024-09-20 16:53:57 +0900117}
118
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900119build_rust_binary_and_copy() {
120 pushd "$(dirname "$0")/../../guest/$1" > /dev/null
121 RUSTFLAGS="-C linker=${arch}-linux-gnu-gcc" cargo build \
122 --target "${arch}-unknown-linux-gnu" \
123 --target-dir "${workdir}/$1"
124 mkdir -p "${dst}/files/usr/local/bin/$1"
125 cp "${workdir}/$1/${arch}-unknown-linux-gnu/debug/$1" "${dst}/files/usr/local/bin/$1/AVF"
126 chmod 777 "${dst}/files/usr/local/bin/$1/AVF"
127 popd > /dev/null
128}
129
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900130build_ttyd() {
131 local ttyd_version=1.7.7
132 local url="https://github.com/tsl0922/ttyd/archive/refs/tags/${ttyd_version}.tar.gz"
133 cp -r $(dirname $0)/ttyd ${workdir}/ttyd
134
135 pushd "${workdir}" > /dev/null
136 wget "${url}" -O - | tar xz
137 cp ttyd/* ttyd-${ttyd_version}/scripts
138 pushd "$workdir/ttyd-${ttyd_version}" > /dev/null
139 bash -c "env BUILD_TARGET=${arch} ./scripts/cross-build.sh"
140 mkdir -p "${dst}/files/usr/local/bin/ttyd"
141 cp /tmp/stage/${arch}-linux-musl/bin/ttyd "${dst}/files/usr/local/bin/ttyd/AVF"
142 chmod 777 "${dst}/files/usr/local/bin/ttyd/AVF"
143 popd > /dev/null
144 popd > /dev/null
145}
146
Jiyong Park44dd28f2024-09-20 18:47:40 +0900147copy_android_config() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000148 local src="$(dirname "$0")/fai_config"
149 local dst="${config_space}"
Jiyong Park44dd28f2024-09-20 18:47:40 +0900150
maciek swiech0fdd0512024-10-11 15:12:44 +0000151 cp -R "${src}"/* "${dst}"
152 cp "$(dirname "$0")/image.yaml" "${resources_dir}"
Jeongik Cha50952062024-09-23 18:13:38 +0900153
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900154 build_ttyd
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900155 build_rust_binary_and_copy forwarder_guest
156 build_rust_binary_and_copy forwarder_guest_launcher
157 build_rust_binary_and_copy ip_addr_reporter
Jiyong Park44dd28f2024-09-20 18:47:40 +0900158}
159
Jiyong Parka128bad2024-09-20 16:53:57 +0900160run_fai() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000161 local out="${built_image}"
162 make -C "${debian_cloud_image}" "image_bookworm_nocloud_${debian_arch}"
163 mv "${debian_cloud_image}/image_bookworm_nocloud_${debian_arch}.raw" "${out}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900164}
165
166clean_up() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000167 rm -rf "${workdir}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900168}
169
170set -e
171trap clean_up EXIT
172
173built_image=image.raw
174workdir=$(mktemp -d)
175debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900176debian_version=bookworm
177config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900178resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
maciek swiech0fdd0512024-10-11 15:12:44 +0000179arch=aarch64
180debian_arch=arm64
181parse_options "$@"
Jiyong Parka128bad2024-09-20 16:53:57 +0900182check_sudo
Jiyong Parka128bad2024-09-20 16:53:57 +0900183install_prerequisites
184download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900185copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900186run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900187fdisk -l image.raw
Jeongik Cha8e711982024-10-20 12:45:35 +0900188images=(image.raw)
189# TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
190if [[ "$arch" == "x86_64" ]]; then
191 virt-get-kernel -a image.raw
192 mv vmlinuz* vmlinuz
193 mv initrd.img* initrd.img
194 images+=(
195 vmlinuz
196 initrd.img
197 )
198fi
Jeongik Cha904d9622024-10-21 11:16:37 +0900199
200cp $(dirname $0)/vm_config.json.${arch} vm_config.json
Jeongik Cha8e711982024-10-20 12:45:35 +0900201# --sparse option isn't supported in apache-commons-compress
Jeongik Chad5cba892024-10-21 16:21:13 +0900202tar czv -f images.tar.gz ${images[@]} vm_config.json