Add an AOSP version of create_base_image.sh

BUG: 111369769
Test: Creates really old images
Change-Id: Ibc08563dd97f8e2c7f229c16a8a00005ff9e1295
diff --git a/tools/create_base_image.sh b/tools/create_base_image.sh
new file mode 100755
index 0000000..950b993
--- /dev/null
+++ b/tools/create_base_image.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+# Creates a base image suitable for booting cuttlefish on GCE
+
+source "${ANDROID_BUILD_TOP}/device/google/cuttlefish_common/tools/create_base_image_hostlib.sh"
+
+FLAGS "$@" || exit 1
+main "${FLAGS_ARGV[@]}"
diff --git a/tools/create_base_image_gce.sh b/tools/create_base_image_gce.sh
new file mode 100755
index 0000000..7f81185
--- /dev/null
+++ b/tools/create_base_image_gce.sh
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+set -x
+set -o errexit
+
+sudo apt-get update
+
+# Stuff we need to get build support
+
+sudo apt install -y debhelper ubuntu-dev-tools equivs "${extra_packages[@]}"
+
+# Install the cuttlefish build deps
+
+for dsc in *.dsc; do
+  yes | sudo mk-build-deps -i "${dsc}" -t apt-get
+done
+
+# Installing the build dependencies left some .deb files around. Remove them
+# to keep them from landing on the image.
+yes | rm -f *.deb
+
+for dsc in *.dsc; do
+  # Unpack the source and build it
+
+  dpkg-source -x "${dsc}"
+  dir="$(basename "${dsc}" .dsc)"
+  dir="${dir/_/-}"
+  pushd "${dir}/"
+  debuild -uc -us
+  popd
+done
+
+# Now gather all of the *.deb files to copy them into the image
+debs=(*.deb)
+tmp_debs=()
+for i in "${debs[@]}"; do
+  tmp_debs+=(/tmp/"$i")
+done
+
+# Now install the packages on the disk
+sudo mkdir /mnt/image
+sudo mount /dev/sdb1 /mnt/image
+cp "${debs[@]}" /mnt/image/tmp
+sudo mount -t sysfs none /mnt/image/sys
+sudo mount -t proc none /mnt/image/proc
+sudo mount --bind /dev/ /mnt/image/dev
+sudo mount --bind /dev/pts /mnt/image/dev/pts
+sudo mount --bind /run /mnt/image/run
+# resolv.conf is needed on Debian but not Ubuntu
+sudo cp /etc/resolv.conf /mnt/image/etc/
+sudo chroot /mnt/image /usr/bin/apt update
+sudo chroot /mnt/image /usr/bin/apt install -y "${tmp_debs[@]}"
+sudo chroot /mnt/image /usr/bin/find /home -ls
+# Clean up the builder's version of resolv.conf
+sudo rm /mnt/image/etc/resolv.conf
+
+# Skip unmounting:
+#  Sometimes systemd starts, making it hard to unmount
+#  In any case we'll unmount cleanly when the instance shuts down
+echo IMAGE_WAS_CREATED
diff --git a/tools/create_base_image_hostlib.sh b/tools/create_base_image_hostlib.sh
new file mode 100755
index 0000000..6032c0e
--- /dev/null
+++ b/tools/create_base_image_hostlib.sh
@@ -0,0 +1,122 @@
+#!/bin/bash
+
+# Common code to build a host image on GCE
+
+# INTERNAL_extra_source may be set to a directory containing the source for
+# extra package to build.
+
+source "${ANDROID_BUILD_TOP}/external/shflags/src/shflags"
+
+DEFINE_string build_instance \
+  "${USER}-build" "Instance name to create for the build" "i"
+DEFINE_string dest_image "vsoc-host-scratch-${USER}" "Image to create" "o"
+DEFINE_string dest_family "" "Image family to add the image to" "f"
+DEFINE_string dest_project "" "Project to use for the new image" "p"
+DEFINE_string launch_instance "" \
+  "Name of the instance to launch with the new image" "l"
+DEFINE_string source_image_family debian-9 "Image familty to use as the base" \
+  "s"
+DEFINE_string source_image_project debian-cloud \
+  "Project holding the base image" "m"
+DEFINE_string repository_url \
+  https://github.com/google/android-cuttlefish.git \
+  "URL to the repository with host changes" "u"
+DEFINE_string repository_branch master \
+  "Branch to check out" "b"
+
+wait_for_instance() {
+  alive=""
+  while [[ -z "${alive}" ]]; do
+    sleep 5
+    alive="$(gcloud compute ssh "$@" -- uptime || true)"
+  done
+}
+
+package_source() {
+  local url="$1"
+  local branch="$2"
+  local repository_dir="${url/*\//}"
+  local debian_dir="$(basename "${repository_dir}" .git)"
+  if [[ $# -eq 4 ]]; then
+    debian_dir="${repository_dir}/$4"
+  fi
+  git clone "${url}" -b "${branch}"
+  dpkg-source -b "${debian_dir}"
+  rm -rf "${debian_dir}"
+}
+
+main() {
+  set -o errexit
+  set -x
+  if [[ -n "${FLAGS_dest_project}" ]]; then
+    dest_project_flag=("--project=${FLAGS_dest_project}")
+  else
+    dest_project_flag=()
+  fi
+  if [[ -n "${FLAGS_dest_family}" ]]; then
+    dest_family_flag=("--family=${FLAGS_dest_family}")
+  else
+    dest_family_flag=()
+  fi
+  scratch_dir="$(mktemp -d)"
+  pushd "${scratch_dir}"
+  package_source "${FLAGS_repository_url}" "${FLAGS_repository_branch}" \
+    "cuttlefish-common_${FLAGS_version}"
+  popd
+  if [[ -n "${INTERNAL_extra_source}" ]]; then
+    source_files=("${INTERNAL_extra_source}"/* ${scratch_dir}/*)
+  else
+    source_files=(${scratch_dir}/*)
+  fi
+
+  delete_instances=("${FLAGS_build_instance}" "${FLAGS_dest_image}")
+  if [[ -n "${FLAGS_launch_instance}" ]]; then
+    delete_instances+=("${FLAGS_launch_instance}")
+  fi
+  gcloud compute instances delete -q \
+    "${dest_project_flag[@]}" "${delete_instances[@]}" || \
+      echo Not running
+  gcloud compute disks delete -q \
+    "${dest_project_flag[@]}" "${FLAGS_dest_image}" || echo No scratch disk
+  gcloud compute images delete -q \
+    "${dest_project_flag[@]}" "${FLAGS_dest_image}" || echo Not respinning
+  gcloud compute disks create \
+    "${dest_project_flag[@]}" \
+    --image-family="${FLAGS_source_image_family}" \
+    --image-project="${FLAGS_source_image_project}" \
+    "${FLAGS_dest_image}"
+  gcloud compute instances create \
+    "${dest_project_flag[@]}" \
+    --image-family="${FLAGS_source_image_family}" \
+    --image-project="${FLAGS_source_image_project}" \
+    "${FLAGS_build_instance}"
+  wait_for_instance "${dest_project_flag[@]}" "${FLAGS_build_instance}"
+  # Ubuntu tends to mount the wrong disk as root, so help it by waiting until
+  # it has booted before giving it access to the clean image disk
+  gcloud compute instances attach-disk \
+      "${dest_project_flag[@]}" \
+      "${FLAGS_build_instance}" --disk="${FLAGS_dest_image}"
+  gcloud compute scp "${dest_project_flag[@]}" \
+    "${source_files[@]}" \
+    "${ANDROID_BUILD_TOP}/device/google/cuttlefish_common/tools/create_base_image_gce.sh" \
+    "${FLAGS_build_instance}:"
+  gcloud compute ssh \
+    "${dest_project_flag[@]}" "${FLAGS_build_instance}" -- \
+    ./create_base_image_gce.sh
+  gcloud compute instances delete -q \
+    "${dest_project_flag[@]}" "${FLAGS_build_instance}"
+  gcloud compute images create "${dest_project_flag[@]}" \
+    --source-disk="${FLAGS_dest_image}" \
+    --licenses=https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx \
+    "${dest_family_flag[@]}" \
+    "${FLAGS_dest_image}"
+  gcloud compute disks delete -q "${dest_project_flag[@]}" \
+    "${FLAGS_dest_image}"
+  if [[ -n "${FLAGS_launch_instance}" ]]; then
+    gcloud compute instances create "${dest_project_flag[@]}" \
+      --image="${FLAGS_dest_image}" \
+      --machine-type=n1-standard-2 \
+      --scopes storage-ro \
+      "${FLAGS_launch_instance}"
+  fi
+}