Ferrochrome: Re-enable tests with more safe guards

This CL includes following changes
  - Ensure whether adb root success
  - Find and launch VM launcher based on intent action
  - Ensure that both host and DUT have enough space
  - Ensure arm64 arch
  - Ensure non-virtual device
  - Remove pushed image when test is finished (kill vmlauncher for it)
  - Enable verbose log by 'set -x'

Bug: 346742552
Change-Id: I74eb02821d3979f7548e15551a3e1c51938d9f71
Test: Locally on aosp_shiba, \
  tangorpro with go/abtd/run/L12000030004706790
diff --git a/tests/ferrochrome/ferrochrome-precondition-checker.sh b/tests/ferrochrome/ferrochrome-precondition-checker.sh
new file mode 100644
index 0000000..d3f7f5a
--- /dev/null
+++ b/tests/ferrochrome/ferrochrome-precondition-checker.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+# Copyright 2024 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+## Precondition checks for running ferrochrome
+## Used by CI for skipping tests.
+
+REQUIRED_DISK_SPACE=7340032    # Requires 7G, while image is 6.5G
+
+# `adb root` always returns exit code 0
+if [[ "$(adb root)" == *"cannot"* ]]; then
+  >&2 echo "Failed to run adb root"
+  exit 1
+fi
+
+# `pm resolve-activity` always returns exit code 0
+resolved_activity=$(adb shell pm resolve-activity -a android.virtualization.VM_LAUNCHER)
+if [[ "${resolved_activity}" == "No activity found" ]]; then
+  >&2 echo "Failed to find vmlauncher"
+  exit 1
+fi
+
+free_space=$(adb shell df /data/local | tail -1 | awk '{print $4}')
+if [[ ${free_space} -lt ${REQUIRED_DISK_SPACE} ]]; then
+  >&2 echo "Insufficient space on DUT. Need ${REQUIRED_DISK_SPACE}, but was ${free_space}"
+  exit 1
+fi
+
+free_space=$(df /tmp | tail -1 | awk '{print $4}')
+if [[ ${free_space} -lt ${REQUIRED_DISK_SPACE} ]]; then
+  >&2 echo "Insufficient space on host. Need ${REQUIRED_DISK_SPACE}, but was ${free_space}"
+  exit 1
+fi
+
+cpu_abi=$(adb shell getprop ro.product.cpu.abi)
+if [[ "${cpu_abi}" != "arm64"* ]]; then
+  >&2 echo "Unsupported architecture. Requires arm64, but was ${cpu_abi}"
+  exit 1
+fi
+
+device=$(adb shell getprop ro.product.vendor.device)
+if [[ "${device}" == "vsock_"* ]]; then
+  >&2 echo "Unsupported device. Cuttlefish isn't supported"
+  exit 1
+fi