blob: b4e8b2f351d0afc55c0b55f0d9157d0cdd17a5f2 [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=(
53 binfmt-support
54 build-essential
55 ca-certificates
56 curl
57 debsums
58 dosfstools
59 fai-server
60 fai-setup-storage
61 fdisk
62 make
Jeongik Chace3a3962024-10-12 03:47:23 +090063 protobuf-compiler
maciek swiech0fdd0512024-10-11 15:12:44 +000064 python3
65 python3-libcloud
66 python3-marshmallow
67 python3-pytest
68 python3-yaml
69 qemu-user-static
70 qemu-utils
71 sudo
72 udev
73 )
74 if [[ "$arch" == "aarch64" ]]; then
75 packages+=(
76 gcc-aarch64-linux-gnu
77 libc6-dev-arm64-cross
78 qemu-system-arm
79 )
80 else
81 packages+=(
Jeongik Cha904d9622024-10-21 11:16:37 +090082 qemu-system
Jeongik Cha8e711982024-10-20 12:45:35 +090083 )
84 fi
85
86 # TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
87 if [[ "$arch" == "x86_64" ]]; then
88 packages+=(
89 libguestfs-tools
maciek swiech0fdd0512024-10-11 15:12:44 +000090 )
91 fi
Jiyong Park44dd28f2024-09-20 18:47:40 +090092 DEBIAN_FRONTEND=noninteractive \
maciek swiech0fdd0512024-10-11 15:12:44 +000093 apt install --no-install-recommends --assume-yes "${packages[@]}"
Jeongik Chab137a5f2024-10-02 12:53:05 +090094
maciek swiech0fdd0512024-10-11 15:12:44 +000095 if [ ! -f $"HOME"/.cargo/bin/cargo ]; then
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090096 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
97 fi
98
maciek swiech0fdd0512024-10-11 15:12:44 +000099 source "$HOME"/.cargo/env
100 rustup target add "${arch}"-unknown-linux-gnu
Jiyong Parka128bad2024-09-20 16:53:57 +0900101}
102
103download_debian_cloud_image() {
104 local ver=master
105 local prj=debian-cloud-images
maciek swiech0fdd0512024-10-11 15:12:44 +0000106 local url="https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz"
107 local outdir="${debian_cloud_image}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900108
maciek swiech0fdd0512024-10-11 15:12:44 +0000109 mkdir -p "${outdir}"
110 wget -O - "${url}" | tar xz -C "${outdir}" --strip-components=1
Jiyong Parka128bad2024-09-20 16:53:57 +0900111}
112
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900113build_rust_binary_and_copy() {
114 pushd "$(dirname "$0")/../../guest/$1" > /dev/null
115 RUSTFLAGS="-C linker=${arch}-linux-gnu-gcc" cargo build \
116 --target "${arch}-unknown-linux-gnu" \
117 --target-dir "${workdir}/$1"
118 mkdir -p "${dst}/files/usr/local/bin/$1"
119 cp "${workdir}/$1/${arch}-unknown-linux-gnu/debug/$1" "${dst}/files/usr/local/bin/$1/AVF"
120 chmod 777 "${dst}/files/usr/local/bin/$1/AVF"
121 popd > /dev/null
122}
123
Jiyong Park44dd28f2024-09-20 18:47:40 +0900124copy_android_config() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000125 local src="$(dirname "$0")/fai_config"
126 local dst="${config_space}"
Jiyong Park44dd28f2024-09-20 18:47:40 +0900127
maciek swiech0fdd0512024-10-11 15:12:44 +0000128 cp -R "${src}"/* "${dst}"
129 cp "$(dirname "$0")/image.yaml" "${resources_dir}"
Jeongik Cha50952062024-09-23 18:13:38 +0900130
131 local ttyd_version=1.7.7
maciek swiech0fdd0512024-10-11 15:12:44 +0000132 local url="https://github.com/tsl0922/ttyd/releases/download/${ttyd_version}/ttyd.${arch}"
133 mkdir -p "${dst}/files/usr/local/bin/ttyd"
134 wget "${url}" -O "${dst}/files/usr/local/bin/ttyd/AVF"
135 chmod 777 "${dst}/files/usr/local/bin/ttyd/AVF"
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900136
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900137 build_rust_binary_and_copy forwarder_guest
138 build_rust_binary_and_copy forwarder_guest_launcher
139 build_rust_binary_and_copy ip_addr_reporter
Jiyong Park44dd28f2024-09-20 18:47:40 +0900140}
141
Jiyong Parka128bad2024-09-20 16:53:57 +0900142run_fai() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000143 local out="${built_image}"
144 make -C "${debian_cloud_image}" "image_bookworm_nocloud_${debian_arch}"
145 mv "${debian_cloud_image}/image_bookworm_nocloud_${debian_arch}.raw" "${out}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900146}
147
148clean_up() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000149 rm -rf "${workdir}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900150}
151
152set -e
153trap clean_up EXIT
154
155built_image=image.raw
156workdir=$(mktemp -d)
157debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900158debian_version=bookworm
159config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900160resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
maciek swiech0fdd0512024-10-11 15:12:44 +0000161arch=aarch64
162debian_arch=arm64
163parse_options "$@"
Jiyong Parka128bad2024-09-20 16:53:57 +0900164check_sudo
Jiyong Parka128bad2024-09-20 16:53:57 +0900165install_prerequisites
166download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900167copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900168run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900169fdisk -l image.raw
Jeongik Cha8e711982024-10-20 12:45:35 +0900170images=(image.raw)
171# TODO(b/365955006): remove these lines when uboot supports x86_64 EFI application
172if [[ "$arch" == "x86_64" ]]; then
173 virt-get-kernel -a image.raw
174 mv vmlinuz* vmlinuz
175 mv initrd.img* initrd.img
176 images+=(
177 vmlinuz
178 initrd.img
179 )
180fi
Jeongik Cha904d9622024-10-21 11:16:37 +0900181
182cp $(dirname $0)/vm_config.json.${arch} vm_config.json
Jeongik Cha8e711982024-10-20 12:45:35 +0900183# --sparse option isn't supported in apache-commons-compress
Jeongik Chad5cba892024-10-21 16:21:13 +0900184tar czv -f images.tar.gz ${images[@]} vm_config.json