blob: 655e8d20f6cd4fa474b8ea9780de2dfe092d7e7b [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 Cha652f73a2024-09-20 09:24:48 +000060 udev \
Jeongik Chab137a5f2024-10-02 12:53:05 +090061
Jiyong Park3d187f22024-09-24 18:55:58 +090062
Seungjae Yoo198a0fb2024-10-04 16:29:12 +090063 if [ ! -f $HOME/.cargo/bin/cargo ]; then
64 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
65 fi
66
67 source $HOME/.cargo/env
68 rustup target add aarch64-unknown-linux-gnu
Seungjae Yoo6aba7c02024-10-04 13:44:58 +090069
Jiyong Park3d187f22024-09-24 18:55:58 +090070 sed -i s/losetup\ -f/losetup\ -P\ -f/g /usr/sbin/fai-diskimage
Jeongik Cha30423ca2024-09-25 17:39:21 +090071 sed -i 's/wget \$/wget -t 0 \$/g' /usr/share/debootstrap/functions
Jiyong Park3120d7c2024-09-25 12:55:56 +090072
73 apt install --no-install-recommends --assume-yes curl
74 # just for testing
75 echo libseccomp: $(curl -is https://deb.debian.org/debian/pool/main/libs/libseccomp/libseccomp2_2.5.4-1+deb12u1_arm64.deb | head -n 1)
76 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 +090077 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 +090078}
79
80download_debian_cloud_image() {
81 local ver=master
82 local prj=debian-cloud-images
83 local url=https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz
84 local outdir=${debian_cloud_image}
85
86 mkdir -p ${outdir}
87 wget -O - ${url} | tar xz -C ${outdir} --strip-components=1
88}
89
Jiyong Park44dd28f2024-09-20 18:47:40 +090090copy_android_config() {
91 local src=$(dirname $0)/fai_config
92 local dst=${config_space}
93
94 cp -R ${src}/* ${dst}
Jeongik Cha37047c32024-09-20 23:09:16 +090095 cp $(dirname $0)/image.yaml ${resources_dir}
Jeongik Cha50952062024-09-23 18:13:38 +090096
97 local ttyd_version=1.7.7
98 local url=https://github.com/tsl0922/ttyd/releases/download/${ttyd_version}/ttyd.aarch64
99 mkdir -p ${dst}/files/usr/local/bin/ttyd
100 wget ${url} -O ${dst}/files/usr/local/bin/ttyd/AVF
101 chmod 777 ${dst}/files/usr/local/bin/ttyd/AVF
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900102
Seungjae Yoo5b9c99d2024-10-07 09:38:06 +0900103 pushd $(dirname $0)/forwarder_guest > /dev/null
Seungjae Yoo6aba7c02024-10-04 13:44:58 +0900104 RUSTFLAGS="-C linker=aarch64-linux-gnu-gcc" cargo build \
105 --target aarch64-unknown-linux-gnu \
106 --target-dir ${workdir}/forwarder_guest
107 mkdir -p ${dst}/files/usr/local/bin/forwarder_guest
108 cp ${workdir}/forwarder_guest/aarch64-unknown-linux-gnu/debug/forwarder_guest ${dst}/files/usr/local/bin/forwarder_guest/AVF
109 chmod 777 ${dst}/files/usr/local/bin/forwarder_guest/AVF
110 popd > /dev/null
Seungjae Yoo5b9c99d2024-10-07 09:38:06 +0900111
112 # Pasting files under port_listener into /etc/port_listener
113 pushd $(dirname $0) > /dev/null
114 for file in $(find port_listener -type f); do
115 mkdir -p ${dst}/files/etc/${file}
116 cp ${file} ${dst}/files/etc/${file}/AVF
117 done
118 popd > /dev/null
Jiyong Park44dd28f2024-09-20 18:47:40 +0900119}
120
Jiyong Parka128bad2024-09-20 16:53:57 +0900121run_fai() {
Jiyong Parka128bad2024-09-20 16:53:57 +0900122 local out=${built_image}
Jeongik Cha37047c32024-09-20 23:09:16 +0900123 make -C ${debian_cloud_image} image_bookworm_nocloud_arm64
124 mv ${debian_cloud_image}/image_bookworm_nocloud_arm64.raw ${out}
Jiyong Parka128bad2024-09-20 16:53:57 +0900125}
126
127clean_up() {
128 rm -rf ${workdir}
129}
130
131set -e
132trap clean_up EXIT
133
134built_image=image.raw
135workdir=$(mktemp -d)
136debian_cloud_image=${workdir}/debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900137debian_version=bookworm
138config_space=${debian_cloud_image}/config_space/${debian_version}
Jeongik Cha37047c32024-09-20 23:09:16 +0900139resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources
Jiyong Parka128bad2024-09-20 16:53:57 +0900140check_sudo
141parse_options $@
142install_prerequisites
143download_debian_cloud_image
Jiyong Park44dd28f2024-09-20 18:47:40 +0900144copy_android_config
Jiyong Park0e565ed2024-09-24 12:39:53 +0900145run_fai
Jiyong Park856e3be2024-09-24 21:59:45 +0900146fdisk -l image.raw