1#!/bin/bash
2
3show_help() {
4  echo "Usage: sudo $0 [OPTION]..."
5  echo "Builds a debian image and save it to image.raw."
6  echo "Options:"
7  echo "-h         Print usage and this help message and exit."
8  echo "-a ARCH    Architecture of the image [default is host arch: $(uname -m)]"
9  echo "-r         Release mode build"
10  echo "-s         Leave a shell open [default: only if the build fails]"
11  echo "-w         Save temp work directory in the container [for debugging]"
12}
13
14arch="$(uname -m)"
15release_flag=
16save_workdir_flag=
17shell_condition="||"
18
19while getopts "a:rsw" option; do
20  case ${option} in
21    a)
22      arch="$OPTARG"
23      ;;
24    h)
25      show_help ; exit
26      ;;
27    r)
28      release_flag="-r"
29      ;;
30    s)
31      shell_condition=";"
32      ;;
33    w)
34      save_workdir_flag="-w"
35      ;;
36    *)
37      echo "Invalid option: $OPTARG" ; exit 1
38      ;;
39  esac
40done
41
42if [[ "$arch" != "aarch64" && "$arch" != "x86_64" ]]; then
43  echo "Invalid architecture: $arch" ; exit 1
44fi
45
46if [ -z "$ANDROID_BUILD_TOP" ] ; then
47  echo '`ANDROID_BUILD_TOP` is undefined.'
48  echo 'Please `lunch` an Android target, or manually set the variable.'
49  exit 1
50fi
51
52docker run --privileged -it -v /dev:/dev \
53  -v "$ANDROID_BUILD_TOP/packages/modules/Virtualization:/root/Virtualization" \
54  --workdir /root/Virtualization/build/debian \
55  ubuntu:22.04 \
56  bash -c "/root/Virtualization/build/debian/build.sh -a $arch $release_flag $save_workdir_flag $shell_condition bash"
57