Rebuild qemu build on the stable host image

BUG: 113599618
Test: Created base image with --variant=master. Had new qmeu
Change-Id: Iec1cf26e92e9d9d6683ce66604eff745d10acea2
(cherry picked from commit 8a668dd3dc704fa4d826cf21a381ad420ed40b9b)
diff --git a/tools/build-qemu-packages.sh b/tools/build-qemu-packages.sh
index 3d32a22..7d30541 100755
--- a/tools/build-qemu-packages.sh
+++ b/tools/build-qemu-packages.sh
@@ -31,7 +31,5 @@
 sudo mk-build-deps -i qemu/debian/control  -t "apt-get -y"
 rm -f ./qemu-build-deps_2.12+dfsg-3_amd64.deb
 pushd qemu
-dpkg-buildpackage -uc -us
+dpkg-buildpackage -uc -us --jobs=auto
 popd
-mkdir -p dist
-mv *.deb *.xz *.bz2 *.dsc *.buildinfo *.changes dist/
diff --git a/tools/create_base_image_gce.sh b/tools/create_base_image_gce.sh
index 7f81185..ab652ed 100755
--- a/tools/create_base_image_gce.sh
+++ b/tools/create_base_image_gce.sh
@@ -32,9 +32,26 @@
 
 # Now gather all of the *.deb files to copy them into the image
 debs=(*.deb)
+
+# See if we are using a local build of qemu.
+if [[ -x build-qemu-packages.sh ]]; then
+  ./build-qemu-packages.sh
+  # We want only a subset of the debs that will be generated
+  for i in *.deb; do
+    case "$i" in
+      qemu-kvm_*) debs+=("$i") ;;
+      qemu-system-common_*) debs+=("$i") ;;
+      qemu-system-x86_*) debs+=("$i") ;;
+      qemu-utils_*) debs+=("$i") ;;
+      qemu-system-data_*) debs+=("$i") ;;
+      libvirglrenderer0*) debs+=("$i") ;;
+    esac
+  done                         
+fi
+
 tmp_debs=()
 for i in "${debs[@]}"; do
-  tmp_debs+=(/tmp/"$i")
+  tmp_debs+=(/tmp/"$(basename "$i")")
 done
 
 # Now install the packages on the disk
diff --git a/tools/create_base_image_hostlib.sh b/tools/create_base_image_hostlib.sh
index 6032c0e..26bf4f5 100755
--- a/tools/create_base_image_hostlib.sh
+++ b/tools/create_base_image_hostlib.sh
@@ -23,6 +23,8 @@
   "URL to the repository with host changes" "u"
 DEFINE_string repository_branch master \
   "Branch to check out" "b"
+DEFINE_string variant master \
+  "Variant to build: generally master or stable"
 
 wait_for_instance() {
   alive=""
@@ -63,10 +65,15 @@
   package_source "${FLAGS_repository_url}" "${FLAGS_repository_branch}" \
     "cuttlefish-common_${FLAGS_version}"
   popd
+  source_files=(
+    "${ANDROID_BUILD_TOP}/device/google/cuttlefish_common/tools/create_base_image_gce.sh"
+    ${scratch_dir}/*
+  )
+  if [[ "${FLAGS_variant}" == master ]]; then
+    source_files+=("${ANDROID_BUILD_TOP}/device/google/cuttlefish_common/tools/build-qemu-packages.sh")
+  fi
   if [[ -n "${INTERNAL_extra_source}" ]]; then
-    source_files=("${INTERNAL_extra_source}"/* ${scratch_dir}/*)
-  else
-    source_files=(${scratch_dir}/*)
+    source_files+=("${INTERNAL_extra_source}"/*)
   fi
 
   delete_instances=("${FLAGS_build_instance}" "${FLAGS_dest_image}")
@@ -87,8 +94,10 @@
     "${FLAGS_dest_image}"
   gcloud compute instances create \
     "${dest_project_flag[@]}" \
+    --machine-type=n1-standard-16 \
     --image-family="${FLAGS_source_image_family}" \
     --image-project="${FLAGS_source_image_project}" \
+    --boot-disk-size=200GiB \
     "${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
@@ -98,7 +107,6 @@
       "${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}" -- \
@@ -115,7 +123,7 @@
   if [[ -n "${FLAGS_launch_instance}" ]]; then
     gcloud compute instances create "${dest_project_flag[@]}" \
       --image="${FLAGS_dest_image}" \
-      --machine-type=n1-standard-2 \
+      --machine-type=n1-standard-4 \
       --scopes storage-ro \
       "${FLAGS_launch_instance}"
   fi