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() { |
| 37 | apt install --no-install-recommends --assume-yes \ |
| 38 | ca-certificates \ |
| 39 | debsums \ |
| 40 | dosfstools \ |
| 41 | fai-server \ |
| 42 | fai-setup-storage \ |
| 43 | fdisk \ |
| 44 | make \ |
| 45 | python3 \ |
| 46 | python3-libcloud \ |
| 47 | python3-marshmallow \ |
| 48 | python3-pytest \ |
| 49 | python3-yaml \ |
| 50 | qemu-utils \ |
| 51 | udev |
| 52 | } |
| 53 | |
| 54 | download_debian_cloud_image() { |
| 55 | local ver=master |
| 56 | local prj=debian-cloud-images |
| 57 | local url=https://salsa.debian.org/cloud-team/${prj}/-/archive/${ver}/${prj}-${ver}.tar.gz |
| 58 | local outdir=${debian_cloud_image} |
| 59 | |
| 60 | mkdir -p ${outdir} |
| 61 | wget -O - ${url} | tar xz -C ${outdir} --strip-components=1 |
| 62 | } |
| 63 | |
| 64 | run_fai() { |
| 65 | local ver=bookworm |
| 66 | local cspace=${debian_cloud_image}/config_space/${ver} |
| 67 | local out=${built_image} |
| 68 | |
| 69 | fai-diskimage \ |
| 70 | --verbose \ |
| 71 | --size 2G \ |
| 72 | --class BASE,DEBIAN,NOCLOUD,ARM64,LINUX_VERSION_BASE+LINUX_VARIANT_CLOUD,BOOKWORM,BUILD_IMAGE,SYSTEM_BOOT \ |
| 73 | --cspace ${cspace} \ |
| 74 | ${out} |
| 75 | } |
| 76 | |
| 77 | clean_up() { |
| 78 | rm -rf ${workdir} |
| 79 | } |
| 80 | |
| 81 | set -e |
| 82 | trap clean_up EXIT |
| 83 | |
| 84 | built_image=image.raw |
| 85 | workdir=$(mktemp -d) |
| 86 | debian_cloud_image=${workdir}/debian_cloud_image |
| 87 | |
| 88 | check_sudo |
| 89 | parse_options $@ |
| 90 | install_prerequisites |
| 91 | download_debian_cloud_image |
| 92 | run_fai |