blob: 71a9d3bd9e66b3b11cfc53547f3161552e2f58f0 [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+=(
82 qemu-system
83 )
84 fi
Jiyong Park44dd28f2024-09-20 18:47:40 +090085 DEBIAN_FRONTEND=noninteractive \
maciek swiech0fdd0512024-10-11 15:12:44 +000086 apt install --no-install-recommends --assume-yes "${packages[@]}"
Jeongik Chab137a5f2024-10-02 12:53:05 +090087
maciek swiech0fdd0512024-10-11 15:12:44 +000088 if [ ! -f $"HOME"/.cargo/bin/cargo ]; then
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090089 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
90 fi
91
maciek swiech0fdd0512024-10-11 15:12:44 +000092 source "$HOME"/.cargo/env
93 rustup target add "${arch}"-unknown-linux-gnu
Jiyong Parka128bad2024-09-20 16:53:57 +090094}
95
96download_debian_cloud_image() {
97 local ver=master
98 local prj=debian-cloud-images
maciek swiech0fdd0512024-10-11 15:12:44 +000099 local url="https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz"
100 local outdir="${debian_cloud_image}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900101
maciek swiech0fdd0512024-10-11 15:12:44 +0000102 mkdir -p "${outdir}"
103 wget -O - "${url}" | tar xz -C "${outdir}" --strip-components=1
Jiyong Parka128bad2024-09-20 16:53:57 +0900104}
105
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900106build_rust_binary_and_copy() {
107 pushd "$(dirname "$0")/../../guest/$1" > /dev/null
108 RUSTFLAGS="-C linker=${arch}-linux-gnu-gcc" cargo build \
109 --target "${arch}-unknown-linux-gnu" \
110 --target-dir "${workdir}/$1"
111 mkdir -p "${dst}/files/usr/local/bin/$1"
112 cp "${workdir}/$1/${arch}-unknown-linux-gnu/debug/$1" "${dst}/files/usr/local/bin/$1/AVF"
113 chmod 777 "${dst}/files/usr/local/bin/$1/AVF"
114 popd > /dev/null
115}
116
Jiyong Park44dd28f2024-09-20 18:47:40 +0900117copy_android_config() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000118 local src="$(dirname "$0")/fai_config"
119 local dst="${config_space}"
Jiyong Park44dd28f2024-09-20 18:47:40 +0900120
maciek swiech0fdd0512024-10-11 15:12:44 +0000121 cp -R "${src}"/* "${dst}"
122 cp "$(dirname "$0")/image.yaml" "${resources_dir}"
Jeongik Cha50952062024-09-23 18:13:38 +0900123
124 local ttyd_version=1.7.7
maciek swiech0fdd0512024-10-11 15:12:44 +0000125 local url="https://github.com/tsl0922/ttyd/releases/download/${ttyd_version}/ttyd.${arch}"
126 mkdir -p "${dst}/files/usr/local/bin/ttyd"
127 wget "${url}" -O "${dst}/files/usr/local/bin/ttyd/AVF"
128 chmod 777 "${dst}/files/usr/local/bin/ttyd/AVF"
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900129
Seungjae Yoo1cfcb582024-10-17 14:06:58 +0900130 build_rust_binary_and_copy forwarder_guest
131 build_rust_binary_and_copy forwarder_guest_launcher
132 build_rust_binary_and_copy ip_addr_reporter
Jiyong Park44dd28f2024-09-20 18:47:40 +0900133}
134
Jiyong Parka128bad2024-09-20 16:53:57 +0900135run_fai() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000136 local out="${built_image}"
137 make -C "${debian_cloud_image}" "image_bookworm_nocloud_${debian_arch}"
138 mv "${debian_cloud_image}/image_bookworm_nocloud_${debian_arch}.raw" "${out}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900139}
140
141clean_up() {
maciek swiech0fdd0512024-10-11 15:12:44 +0000142 rm -rf "${workdir}"
Jiyong Parka128bad2024-09-20 16:53:57 +0900143}
144
145set -e
146trap clean_up EXIT
147
148built_image=image.raw
149workdir=$(mktemp -d)
150debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900151debian_version=bookworm
152config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900153resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
maciek swiech0fdd0512024-10-11 15:12:44 +0000154arch=aarch64
155debian_arch=arm64
156parse_options "$@"
Jiyong Parka128bad2024-09-20 16:53:57 +0900157check_sudo
Jiyong Parka128bad2024-09-20 16:53:57 +0900158install_prerequisites
159download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900160copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900161run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900162fdisk -l image.raw