build/debian: Disable ballooning on images with a generic kernel

As described in the linked bug report, enabling memory ballooning
on a generic kernel (without our patches) results in strange
corruption issues on Arm64.

In this CL, we update our build script to:
1. make building with our custom kernel the default build option,
2. disable ballooning on Arm64 images with unpatched generic kernel.

Bug: 396233393
Test: Deployed and tested on Komodo:
Test: - ./build_in_container.sh -a aarch64
Test: - ./build_in_container.sh -g -a aarch64

Change-Id: I4441dbf5886675251836ed772eaa804d0520bea9
diff --git a/build/debian/build.sh b/build/debian/build.sh
index 616dd00..63035ae 100755
--- a/build/debian/build.sh
+++ b/build/debian/build.sh
@@ -13,7 +13,7 @@
 	echo "Options:"
 	echo "-h         Print usage and this help message and exit."
 	echo "-a ARCH    Architecture of the image [default is host arch: $(uname -m)]"
-	echo "-k         Build and use our custom kernel [default is cloud kernel]"
+	echo "-g         Use Debian generic kernel [default is our custom kernel]"
 	echo "-r         Release mode build"
 	echo "-w         Save temp work directory [for debugging]"
 }
@@ -25,7 +25,7 @@
 }
 
 parse_options() {
-	while getopts "a:hkrw" option; do
+	while getopts "a:ghrw" option; do
 		case ${option} in
 			h)
 				show_help ; exit
@@ -33,8 +33,8 @@
 			a)
 				arch="$OPTARG"
 				;;
-			k)
-				use_custom_kernel=1
+			g)
+				use_generic_kernel=1
 				;;
 			r)
 				mode=release
@@ -122,7 +122,7 @@
 		)
 	fi
 
-	if [[ "$use_custom_kernel" -eq 1 ]]; then
+	if [[ "$use_generic_kernel" != 1 ]]; then
 		packages+=(
 			bc
 			bison
@@ -215,20 +215,20 @@
 }
 
 package_custom_kernel() {
-	if [[ "$use_custom_kernel" != 1 ]]; then
-		# NOTE: Install generic headers for the default Debian kernel.
+	if [[ "$use_generic_kernel" == 1 ]]; then
+		# NOTE: For bpfcc-tools, install generic headers for the generic kernel.
 		cat > "${config_space}/package_config/LAST" <<EOF
 PACKAGES install
 linux-headers-generic
 EOF
 		return
-	else
-		# NOTE: Prevent FAI from installing a default Debian kernel, by removing
-		#       linux-image meta package names from arch-specific class files.
-		sed -i "/linux-image.*-${debian_arch}/d" \
-		    "${config_space}/package_config/${debian_arch^^}"
 	fi
 
+	# NOTE: Prevent FAI from installing a default Debian kernel, by removing
+	#       linux-image meta package names from arch-specific class files.
+	sed -i "/linux-image.*-${debian_arch}/d" \
+	    "${config_space}/package_config/${debian_arch^^}"
+
 	local deb_base_url="https://deb.debian.org/debian"
 	local deb_security_base_url="https://security.debian.org/debian-security"
 
@@ -344,6 +344,11 @@
 	losetup -d "${loop}"
 
 	cp ${vm_config} vm_config.json
+	# TODO(b/363985291): remove this when ballooning is supported on generic kernel
+	if [[ "$use_generic_kernel" == 1 ]] && [[ "$arch" == "aarch64" ]]; then
+		sed -i 's/"auto_memory_balloon": true/"auto_memory_balloon": false/g' vm_config.json
+	fi
+
 	sed -i "s/{root_part_guid}/$(sfdisk --part-uuid $raw_disk_image $root_partition_num)/g" vm_config.json
 	if [[ "$arch" == "x86_64" ]]; then
 		sed -i "s/{bios_part_guid}/$(sfdisk --part-uuid $raw_disk_image $bios_partition_num)/g" vm_config.json
@@ -395,7 +400,7 @@
 arch="$(uname -m)"
 mode=debug
 save_workdir=0
-use_custom_kernel=0
+use_generic_kernel=0
 
 parse_options "$@"
 check_sudo
diff --git a/build/debian/build_in_container.sh b/build/debian/build_in_container.sh
index e3adcae..739d2dd 100755
--- a/build/debian/build_in_container.sh
+++ b/build/debian/build_in_container.sh
@@ -6,7 +6,7 @@
   echo "Options:"
   echo "-h         Print usage and this help message and exit."
   echo "-a ARCH    Architecture of the image [default is host arch: $(uname -m)]"
-  echo "-k         Build and use our custom kernel [default is cloud kernel]"
+  echo "-g         Use Debian generic kernel [default is our custom kernel]"
   echo "-r         Release mode build"
   echo "-s         Leave a shell open [default: only if the build fails]"
   echo "-w         Save temp work directory in the container [for debugging]"
@@ -18,17 +18,17 @@
 save_workdir_flag=
 shell_condition="||"
 
-while getopts "a:hkrsw" option; do
+while getopts "a:ghrsw" option; do
   case ${option} in
     a)
       arch="$OPTARG"
       ;;
+    g)
+      kernel_flag="-g"
+      ;;
     h)
       show_help ; exit
       ;;
-    k)
-      kernel_flag="-k"
-      ;;
     r)
       release_flag="-r"
       ;;
diff --git a/build/debian/kokoro/gcp_ubuntu_docker/aarch64/build.sh b/build/debian/kokoro/gcp_ubuntu_docker/aarch64/build.sh
index 7476fc3..d9d1e8f 100644
--- a/build/debian/kokoro/gcp_ubuntu_docker/aarch64/build.sh
+++ b/build/debian/kokoro/gcp_ubuntu_docker/aarch64/build.sh
@@ -5,7 +5,7 @@
 cd "${KOKORO_ARTIFACTS_DIR}/git/avf/build/debian/"
 sudo losetup -D
 grep vmx /proc/cpuinfo || true
-sudo ./build.sh -a aarch64 -k -r
+sudo ./build.sh -a aarch64 -r
 sudo mv images.tar.gz ${KOKORO_ARTIFACTS_DIR} || true
 mkdir -p ${KOKORO_ARTIFACTS_DIR}/logs
 sudo cp -r /var/log/fai/* ${KOKORO_ARTIFACTS_DIR}/logs || true
diff --git a/build/debian/kokoro/gcp_ubuntu_docker/x86_64/build.sh b/build/debian/kokoro/gcp_ubuntu_docker/x86_64/build.sh
index a935591..a22e194 100644
--- a/build/debian/kokoro/gcp_ubuntu_docker/x86_64/build.sh
+++ b/build/debian/kokoro/gcp_ubuntu_docker/x86_64/build.sh
@@ -5,7 +5,7 @@
 cd "${KOKORO_ARTIFACTS_DIR}/git/avf/build/debian/"
 sudo losetup -D
 grep vmx /proc/cpuinfo || true
-sudo ./build.sh -a x86_64 -k -r
+sudo ./build.sh -a x86_64 -r
 sudo mv images.tar.gz ${KOKORO_ARTIFACTS_DIR} || true
 mkdir -p ${KOKORO_ARTIFACTS_DIR}/logs
 sudo cp -r /var/log/fai/* ${KOKORO_ARTIFACTS_DIR}/logs || true