Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 1 | #!/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 | |
| 9 | show_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 | |
| 16 | check_sudo() { |
| 17 | if [ "$EUID" -ne 0 ]; then |
| 18 | echo "Please run as root." |
| 19 | exit |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | parse_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 | |
| 36 | install_prerequisites() { |
Jiyong Park | 44dd28f | 2024-09-20 18:47:40 +0900 | [diff] [blame] | 37 | DEBIAN_FRONTEND=noninteractive \ |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 38 | 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 Cha | 652f73a | 2024-09-20 09:24:48 +0000 | [diff] [blame] | 52 | udev \ |
| 53 | qemu-system-arm \ |
| 54 | qemu-user-static |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | download_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 Park | 44dd28f | 2024-09-20 18:47:40 +0900 | [diff] [blame] | 67 | copy_android_config() { |
| 68 | local src=$(dirname $0)/fai_config |
| 69 | local dst=${config_space} |
| 70 | |
| 71 | cp -R ${src}/* ${dst} |
Jeongik Cha | 37047c3 | 2024-09-20 23:09:16 +0900 | [diff] [blame] | 72 | cp $(dirname $0)/image.yaml ${resources_dir} |
Jeongik Cha | 5095206 | 2024-09-23 18:13:38 +0900 | [diff] [blame] | 73 | |
| 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 Park | 44dd28f | 2024-09-20 18:47:40 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 81 | run_fai() { |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 82 | local out=${built_image} |
Jeongik Cha | 37047c3 | 2024-09-20 23:09:16 +0900 | [diff] [blame] | 83 | make -C ${debian_cloud_image} image_bookworm_nocloud_arm64 |
| 84 | mv ${debian_cloud_image}/image_bookworm_nocloud_arm64.raw ${out} |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | clean_up() { |
| 88 | rm -rf ${workdir} |
| 89 | } |
| 90 | |
| 91 | set -e |
| 92 | trap clean_up EXIT |
| 93 | |
| 94 | built_image=image.raw |
| 95 | workdir=$(mktemp -d) |
| 96 | debian_cloud_image=${workdir}/debian_cloud_image |
Jiyong Park | 44dd28f | 2024-09-20 18:47:40 +0900 | [diff] [blame] | 97 | debian_version=bookworm |
| 98 | config_space=${debian_cloud_image}/config_space/${debian_version} |
Jeongik Cha | 37047c3 | 2024-09-20 23:09:16 +0900 | [diff] [blame] | 99 | resources_dir=${debian_cloud_image}/src/debian_cloud_images/resources |
Jiyong Park | a128bad | 2024-09-20 16:53:57 +0900 | [diff] [blame] | 100 | check_sudo |
| 101 | parse_options $@ |
| 102 | install_prerequisites |
| 103 | download_debian_cloud_image |
Jiyong Park | 44dd28f | 2024-09-20 18:47:40 +0900 | [diff] [blame] | 104 | copy_android_config |
Jeongik Cha | 5095206 | 2024-09-23 18:13:38 +0900 | [diff] [blame] | 105 | run_fai |