blob: b4436c199537aa37614336eac3f80fc0a0f42715 [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
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900107 cargo install cargo-license
Jiyong Parka128bad2024-09-20 16:53:57 +0900108}
109
110download_debian_cloud_image() {
111 local ver=master
112 local prj=debian-cloud-images
maciek swiech0fdd0512024-10-11 15:12:44 +0000113 local url="https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz"
114 local outdir="${debian_cloud_image}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900115
maciek swiech0fdd0512024-10-11 15:12:44 +0000116 mkdir -p "${outdir}"
117 wget -O - "${url}" | tar xz -C "${outdir}" --strip-components=1
Jiyong Parka128bad2024-09-20 16:53:57 +0900118}
119
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900120build_rust_binary_and_copy() {
121 pushd "$(dirname "$0")/../../guest/$1" > /dev/null
122 RUSTFLAGS="-C linker=${arch}-linux-gnu-gcc" cargo build \
123 --target "${arch}-unknown-linux-gnu" \
124 --target-dir "${workdir}/$1"
125 mkdir -p "${dst}/files/usr/local/bin/$1"
126 cp "${workdir}/$1/${arch}-unknown-linux-gnu/debug/$1" "${dst}/files/usr/local/bin/$1/AVF"
127 chmod 777 "${dst}/files/usr/local/bin/$1/AVF"
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900128
129 mkdir -p "${dst}/files/usr/share/doc/$1"
130 cargo license > "${dst}/files/usr/share/doc/$1/copyright"
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900131 popd > /dev/null
132}
133
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900134build_ttyd() {
135 local ttyd_version=1.7.7
136 local url="https://github.com/tsl0922/ttyd/archive/refs/tags/${ttyd_version}.tar.gz"
137 cp -r $(dirname $0)/ttyd ${workdir}/ttyd
138
139 pushd "${workdir}" > /dev/null
140 wget "${url}" -O - | tar xz
141 cp ttyd/* ttyd-${ttyd_version}/scripts
142 pushd "$workdir/ttyd-${ttyd_version}" > /dev/null
143 bash -c "env BUILD_TARGET=${arch} ./scripts/cross-build.sh"
144 mkdir -p "${dst}/files/usr/local/bin/ttyd"
145 cp /tmp/stage/${arch}-linux-musl/bin/ttyd "${dst}/files/usr/local/bin/ttyd/AVF"
146 chmod 777 "${dst}/files/usr/local/bin/ttyd/AVF"
Jeongik Cha139ddfd2024-11-01 23:16:44 +0900147 mkdir -p "${dst}/files/usr/share/doc/ttyd"
148 cp LICENSE "${dst}/files/usr/share/doc/ttyd/copyright"
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900149 popd > /dev/null
150 popd > /dev/null
151}
152
Jiyong Park44dd28f2024-09-20 18:47:40 +0900153copy_android_config() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000154 local src="$(dirname "$0")/fai_config"
155 local dst="${config_space}"
Jiyong Park44dd28f2024-09-20 18:47:40 +0900156
maciek swiech0fdd0512024-10-11 15:12:44 +0000157 cp -R "${src}"/* "${dst}"
158 cp "$(dirname "$0")/image.yaml" "${resources_dir}"
Jeongik Cha50952062024-09-23 18:13:38 +0900159
Jeongik Cha7e7f19d2024-10-31 20:50:24 +0900160 build_ttyd
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900161 build_rust_binary_and_copy forwarder_guest
162 build_rust_binary_and_copy forwarder_guest_launcher
163 build_rust_binary_and_copy ip_addr_reporter
Jiyong Park44dd28f2024-09-20 18:47:40 +0900164}
165
Jiyong Parka128bad2024-09-20 16:53:57 +0900166run_fai() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000167 local out="${built_image}"
168 make -C "${debian_cloud_image}" "image_bookworm_nocloud_${debian_arch}"
169 mv "${debian_cloud_image}/image_bookworm_nocloud_${debian_arch}.raw" "${out}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900170}
171
172clean_up() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000173 rm -rf "${workdir}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900174}
175
176set -e
177trap clean_up EXIT
178
179built_image=image.raw
180workdir=$(mktemp -d)
181debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900182debian_version=bookworm
183config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900184resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
maciek swiech0fdd0512024-10-11 15:12:44 +0000185arch=aarch64
186debian_arch=arm64
187parse_options "$@"
Jiyong Parka128bad2024-09-20 16:53:57 +0900188check_sudo
Jiyong Parka128bad2024-09-20 16:53:57 +0900189install_prerequisites
190download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900191copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900192run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900193fdisk -l image.raw
Jeongik Cha8e711982024-10-20 12:45:35 +0900194images=(image.raw)
195# TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
196if [[ "$arch" == "x86_64" ]]; then
197 virt-get-kernel -a image.raw
198 mv vmlinuz* vmlinuz
199 mv initrd.img* initrd.img
200 images+=(
201 vmlinuz
202 initrd.img
203 )
204fi
Jeongik Cha904d9622024-10-21 11:16:37 +0900205
206cp $(dirname $0)/vm_config.json.${arch} vm_config.json
Jeongik Cha8e711982024-10-20 12:45:35 +0900207# --sparse option isn't supported in apache-commons-compress
Jeongik Chad5cba892024-10-21 16:21:13 +0900208tar czv -f images.tar.gz ${images[@]} vm_config.json