blob: c50e5e5a6b4626e0cb73b02e53763b332a61647d [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 Park0e565ed2024-09-24 12:39:53 +090037 apt update
Jiyong Park44dd28f2024-09-20 18:47:40 +090038 DEBIAN_FRONTEND=noninteractive \
Jiyong Parka128bad2024-09-20 16:53:57 +090039 apt install --no-install-recommends --assume-yes \
Jeongik Chab137a5f2024-10-02 12:53:05 +090040 binfmt-support \
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090041 build-essential \
Jiyong Parka128bad2024-09-20 16:53:57 +090042 ca-certificates \
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090043 curl \
Jiyong Parka128bad2024-09-20 16:53:57 +090044 debsums \
45 dosfstools \
46 fai-server \
47 fai-setup-storage \
48 fdisk \
Seungjae Yoo6aba7c02024-10-04 13:44:58 +090049 gcc-aarch64-linux-gnu \
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090050 libc6-dev-arm64-cross \
Jiyong Parka128bad2024-09-20 16:53:57 +090051 make \
52 python3 \
53 python3-libcloud \
54 python3-marshmallow \
55 python3-pytest \
56 python3-yaml \
Jeongik Chab137a5f2024-10-02 12:53:05 +090057 qemu-system-arm \
58 qemu-user-static \
Jiyong Parka128bad2024-09-20 16:53:57 +090059 qemu-utils \
Jeongik Chad8f46552024-10-04 20:23:57 +090060 sudo \
Jeongik Cha652f73a2024-09-20 09:24:48 +000061 udev \
Jeongik Chab137a5f2024-10-02 12:53:05 +090062
Jiyong Park3d187f22024-09-24 18:55:58 +090063
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090064 if [ ! -f $HOME/.cargo/bin/cargo ]; then
65 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
66 fi
67
68 source $HOME/.cargo/env
69 rustup target add aarch64-unknown-linux-gnu
Seungjae Yoo6aba7c02024-10-04 13:44:58 +090070
Jiyong Park3d187f22024-09-24 18:55:58 +090071 sed -i s/losetup\ -f/losetup\ -P\ -f/g /usr/sbin/fai-diskimage
Jeongik Cha30423ca2024-09-25 17:39:21 +090072 sed -i 's/wget \$/wget -t 0 \$/g' /usr/share/debootstrap/functions
Jiyong Park3120d7c2024-09-25 12:55:56 +090073
74 apt install --no-install-recommends --assume-yes curl
75 # just for testing
76 echo libseccomp: $(curl -is https://deb.debian.org/debian/pool/main/libs/libseccomp/libseccomp2_2.5.4-1+deb12u1_arm64.deb | head -n 1)
77 echo libsemanage-common: $(curl -is https://deb.debian.org/debian/pool/main/libs/libsemanage/libsemanage-common_3.4-1_all.deb | head -n 1)
Jeongik Cha36253792024-09-25 21:05:15 +090078 echo libpcre2: $(curl -is https://deb.debian.org/debian/pool/main/p/pcre2/libpcre2-8-0_10.42-1_arm64.deb | head -n 1)
Jiyong Parka128bad2024-09-20 16:53:57 +090079}
80
81download_debian_cloud_image() {
82 local ver=master
83 local prj=debian-cloud-images
84 local url=https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz
85 local outdir=${debian_cloud_image}
86
87 mkdir -p ${outdir}
88 wget -O - ${url} | tar xz -C ${outdir} --strip-components=1
89}
90
Jiyong Park44dd28f2024-09-20 18:47:40 +090091copy_android_config() {
92 local src=$(dirname $0)/fai_config
93 local dst=${config_space}
94
95 cp -R ${src}/* ${dst}
Jeongik Cha37047c32024-09-20 23:09:16 +090096 cp $(dirname $0)/image.yaml ${resources_dir}
Jeongik Cha50952062024-09-23 18:13:38 +090097
98 local ttyd_version=1.7.7
99 local url=https://github.com/tsl0922/ttyd/releases/download/${ttyd_version}/ttyd.aarch64
100 mkdir -p ${dst}/files/usr/local/bin/ttyd
101 wget ${url} -O ${dst}/files/usr/local/bin/ttyd/AVF
102 chmod 777 ${dst}/files/usr/local/bin/ttyd/AVF
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900103
Seungjae Yoo1f086a82024-10-08 08:17:14 +0000104 pushd $(dirname $0)/forwarder_guest > /dev/null
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900105 RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" cargo build \
106 --target aarch64-unknown-linux-gnu \
107 --target-dir ${workdir}/forwarder_guest
108 mkdir -p ${dst}/files/usr/local/bin/forwarder_guest
109 cp ${workdir}/forwarder_guest/aarch64-unknown-linux-gnu/debug/forwarder_guest ${dst}/files/usr/local/bin/forwarder_guest/AVF
110 chmod 777 ${dst}/files/usr/local/bin/forwarder_guest/AVF
111 popd > /dev/null
Jiyong Park44dd28f2024-09-20 18:47:40 +0900112}
113
Jiyong Parka128bad2024-09-20 16:53:57 +0900114run_fai() {
Jiyong Parka128bad2024-09-20 16:53:57 +0900115 local out=${built_image}
Jeongik Cha37047c32024-09-20 23:09:16 +0900116 make -C ${debian_cloud_image} image_bookworm_nocloud_arm64
117 mv ${debian_cloud_image}/image_bookworm_nocloud_arm64.raw ${out}
Jiyong Parka128bad2024-09-20 16:53:57 +0900118}
119
120clean_up() {
121 rm -rf ${workdir}
122}
123
124set -e
125trap clean_up EXIT
126
127built_image=image.raw
128workdir=$(mktemp -d)
129debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900130debian_version=bookworm
131config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900132resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
Jiyong Parka128bad2024-09-20 16:53:57 +0900133check_sudo
134parse_options $@
135install_prerequisites
136download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900137copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900138run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900139fdisk -l image.raw