blob: d85f38469832d913d2fce07a910f2f11c91ad4bc [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:
5# - Support x86_64 architecture
6# - Add Android-specific packages via a new class
7# - Use a stable release from debian-cloud-images
8
9show_help() {
10 echo Usage: $0 [OPTION]... [FILE]
11 echo Builds a debian image and save it to FILE.
12 echo Options:
13 echo -h Pring usage and this help message and exit.
14}
15
16check_sudo() {
17 if [ "$EUID" -ne 0 ]; then
18 echo "Please run as root."
19 exit
20 fi
21}
22
23parse_options() {
24 while getopts ":h" option; do
25 case ${option} in
26 h)
27 show_help
28 exit;;
29 esac
30 done
31 if [ -n "$1" ]; then
32 built_image=$1
33 fi
34}
35
36install_prerequisites() {
Jiyong Park44dd28f2024-09-20 18:47:40 +090037 DEBIAN_FRONTEND=noninteractive \
Jiyong Parka128bad2024-09-20 16:53:57 +090038 apt install --no-install-recommends --assume-yes \
39 ca-certificates \
40 debsums \
41 dosfstools \
42 fai-server \
43 fai-setup-storage \
44 fdisk \
45 make \
46 python3 \
47 python3-libcloud \
48 python3-marshmallow \
49 python3-pytest \
50 python3-yaml \
51 qemu-utils \
Jeongik Cha652f73a2024-09-20 09:24:48 +000052 udev \
53 qemu-system-arm \
54 qemu-user-static
Jiyong Parka128bad2024-09-20 16:53:57 +090055}
56
57download_debian_cloud_image() {
58 local ver=master
59 local prj=debian-cloud-images
60 local url=https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz
61 local outdir=${debian_cloud_image}
62
63 mkdir -p ${outdir}
64 wget -O - ${url} | tar xz -C ${outdir} --strip-components=1
65}
66
Jiyong Park44dd28f2024-09-20 18:47:40 +090067copy_android_config() {
68 local src=$(dirname $0)/fai_config
69 local dst=${config_space}
70
71 cp -R ${src}/* ${dst}
Jeongik Cha37047c32024-09-20 23:09:16 +090072 cp $(dirname $0)/image.yaml ${resources_dir}
Jeongik Cha50952062024-09-23 18:13:38 +090073
74 local ttyd_version=1.7.7
75 local url=https://github.com/tsl0922/ttyd/releases/download/${ttyd_version}/ttyd.aarch64
76 mkdir -p ${dst}/files/usr/local/bin/ttyd
77 wget ${url} -O ${dst}/files/usr/local/bin/ttyd/AVF
78 chmod 777 ${dst}/files/usr/local/bin/ttyd/AVF
Jiyong Park44dd28f2024-09-20 18:47:40 +090079}
80
Jiyong Parka128bad2024-09-20 16:53:57 +090081run_fai() {
Jiyong Parka128bad2024-09-20 16:53:57 +090082 local out=${built_image}
Jeongik Cha37047c32024-09-20 23:09:16 +090083 make -C ${debian_cloud_image} image_bookworm_nocloud_arm64
84 mv ${debian_cloud_image}/image_bookworm_nocloud_arm64.raw ${out}
Jiyong Parka128bad2024-09-20 16:53:57 +090085}
86
87clean_up() {
88 rm -rf ${workdir}
89}
90
91set -e
92trap clean_up EXIT
93
94built_image=image.raw
95workdir=$(mktemp -d)
96debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +090097debian_version=bookworm
98config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +090099resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
Jiyong Parka128bad2024-09-20 16:53:57 +0900100check_sudo
101parse_options $@
102install_prerequisites
103download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900104copy_android_config
Jeongik Cha50952062024-09-23 18:13:38 +0900105run_fai