Add a script to set Cold Boot or Quick Boot & patch the data disk size am: de92e47897 am: f80583918d am: 479691075c am: 35d27f5580

Original change: https://android-review.googlesource.com/c/platform/tools/aadevtools/+/1690086

Change-Id: I469821d1c540b4d0342191f212757f2098799dce
diff --git a/avd/README.md b/avd/README.md
index 1f836d2..132665b 100644
--- a/avd/README.md
+++ b/avd/README.md
@@ -6,8 +6,11 @@
 
 ### Add new AVD images
 * add_avd_img.sh adds an AVD image to Android SDK dir.
-    * test_avd_avd_image.sh is an test & also an example how to use it.
+    * test_avd_avd_image.sh is the test & also examples of how to use it.
 * batch_add_avd_img.sh adds all AVD images listed in the input CSV file & properly patch to be used for Automotive device profiles.
     * avd_img_list.csv is the default CSV file if non is provided & also as an example.
-* patch_avd.sh changes an AVD configuration for more RAM & heap. So it can perform properly.
-* patch_all_avds.sh changes all AVD configuration for more RAM & heap. So it can perform properly.
\ No newline at end of file
+* patch_avd.sh changes an AVD configuration for bigger RAM, heap & data disk. So it can perform properly.
+* patch_all_avds.sh changes all AVD configuration for more RAM & heap. So it can perform properly.
+    * test_patch_all_avds.sh is the test & also examples of how to use it.
+* set_avds_force_cold_boot.sh sets Cold Boot or Quick Boot for all AVD configurations.
+    * test_set_avds_force_cold_boot.sh is the test & also examples of how to use it.
diff --git a/avd/patch_all_avds.sh b/avd/patch_all_avds.sh
index 70156e6..11f79b6 100755
--- a/avd/patch_all_avds.sh
+++ b/avd/patch_all_avds.sh
@@ -16,10 +16,10 @@
 
 readme() {
   echo '''
-Change all AVD configuration for more RAM & heap. So it can perform properly.
+Change all AVD configuration for bigger RAM, heap & data-disk sizes. So it can perform better.
 If TAG_ID of AVD is empty, it will change all AVDs. e.g.
 
-RAM=4096 HEAP=576 AVD_ROOT_DIR="$HOME/.android/avd" TAG_ID="android-automotive" ./patch_all_avds.sh
+RAM=4096 HEAP=576 DATA_DISK=6000 AVD_ROOT_DIR="$HOME/.android/avd" TAG_ID="android-automotive" ./patch_all_avds.sh
 '''
 }
 
@@ -40,9 +40,9 @@
 SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME}
 echo Running from $SCRIPT_DIR
 
-# Export VAR with the value of the key in a file as: getValue "VAR" "KEY" "FILE"
+# Export VAR with the value of the key in a file as:
+# getValue "VAR" "KEY" "config.ini FILE"
 # e.g. getValue "MY_AVD_ID" "AvdId" "$avd_dir/config.ini"
-# For: AvdId=Automotive_10_landscape_API_30
 getValue() {
     VAR=$1
     KEY=$2
@@ -68,6 +68,11 @@
 fi
 echo "HEAP=$HEAP"
 
+if [[ -z $DATA_DISK ]]; then
+    DATA_DISK=6000
+fi
+echo "DATA_DISK=$DATA_DISK"
+
 for file in $(ls "$AVD_ROOT_DIR"); do
   avd_dir="$AVD_ROOT_DIR/$file"
   if [[ $file == *.avd ]]; then
@@ -78,10 +83,11 @@
       getValue "MY_TAG_ID" "tag.id" $config_file
       if [[ $TAG_ID != $MY_TAG_ID ]]; then
         echo "SKIP: $MY_AVD_ID is $MY_TAG_ID rather $TAG_ID "
+        echo
         continue
       fi
     fi
-    RAM=$RAM HEAP=$HEAP AVD_DIR=$avd_dir $SCRIPT_DIR/patch_avd.sh
+    RAM=$RAM HEAP=$HEAP DATA_DISK=$DATA_DISK AVD_DIR=$avd_dir $SCRIPT_DIR/patch_avd.sh
     echo
   fi
 done
\ No newline at end of file
diff --git a/avd/patch_avd.sh b/avd/patch_avd.sh
index f744ef3..87dfd12 100755
--- a/avd/patch_avd.sh
+++ b/avd/patch_avd.sh
@@ -16,8 +16,9 @@
 
 readme() {
   echo '''
-Change an AVD configuration for more RAM & heap. So it can perform properly. e.g.
-RAM=4096 HEAP=576 AVD_DIR="$HOME/.android/avd/Automotive_10_landscape_API_30.avd" ./patch_avd.sh
+Change an AVD configuration for bigger RAM, heap & data-disk sizes. So it can perform better. e.g.
+RAM=4096 HEAP=576 DATA_DISK=6000 AVD_DIR="$HOME/.android/avd/Automotive_10_landscape_API_30.avd" ./patch_avd.sh
+
 '''
 }
 
@@ -53,6 +54,11 @@
 fi
 echo "HEAP=$HEAP"
 
+if [[ -z $DATA_DISK ]]; then
+    DATA_DISK=6000
+fi
+echo "DATA_DISK=$DATA_DISK"
+
 AVD_CONFIG="$AVD_DIR/config.ini"
 if [[ ! -e $AVD_CONFIG ]]; then
   echo "ERROR: no AVD config file at: $AVD_CONFIG"
@@ -60,10 +66,13 @@
 fi
 echo "AVD_CONFIG=$AVD_CONFIG"
 
-echo "CHANGE: hw.ramSize=$RAM & vm.heapSize=$HEAP in $AVD_CONFIG"
+echo "CHANGE: hw.ramSize=$RAM, vm.heapSize=$HEAP & disk.dataPartition.size=${DATA_DISK}M in $AVD_CONFIG"
 $SED_I_CMD '/^hw.ramSize/d' $AVD_CONFIG
 $SED_I_CMD '/^vm.heapSize/d' $AVD_CONFIG
+$SED_I_CMD '/^disk.dataPartition.size/d' $AVD_CONFIG
 echo "hw.ramSize=$RAM" >> $AVD_CONFIG
 echo "vm.heapSize=$HEAP" >> $AVD_CONFIG
+echo "disk.dataPartition.size=${DATA_DISK}M" >> $AVD_CONFIG
 cat $AVD_CONFIG | grep "hw.ramSize"
 cat $AVD_CONFIG | grep "vm.heapSize"
+cat $AVD_CONFIG | grep "disk.dataPartition.size"
diff --git a/avd/set_avds_force_cold_boot.sh b/avd/set_avds_force_cold_boot.sh
new file mode 100755
index 0000000..7deed08
--- /dev/null
+++ b/avd/set_avds_force_cold_boot.sh
@@ -0,0 +1,122 @@
+#!/bin/bash
+
+# Copyright (C) 2021 The Android Open Source Project
+#
+# 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.
+
+readme() {
+  echo '''
+Change forceColdBoot for all AVD config.ini files.
+If TAG_ID of AVD is empty, it will change all AVDs. e.g.
+FORCE_COLD_BOOT="yes" AVD_ROOT_DIR="$HOME/.android/avd" TAG_ID="android-automotive" ./set_avds_force_cold_boot.sh
+'''
+}
+
+# set up for Linux or macOS
+OS="$(uname -s)"
+echo "Running on $OS"
+if [[ $OS == "Linux" ]]; then
+  SED_I_CMD="sed -i "
+elif [[ $OS == "Darwin" ]]; then
+  SED_I_CMD="sed -i ''"
+else
+  echo "ERROR: this does not work on $OS"
+  exit
+fi
+
+MY_NAME=$0
+SCRIPT_NAME=${MY_NAME##*/}
+SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME}
+echo Running from $SCRIPT_DIR
+
+# Export VAR with the value of the key in a file as:
+# getValue "VAR" "KEY" "config.ini FILE"
+# e.g. getValue "MY_AVD_ID" "AvdId" "$avd_dir/config.ini"
+getValue() {
+  VAR=$1
+  KEY=$2
+  FILE=$3
+  LINE=$(cat $FILE | grep $KEY)
+  VALUE=${LINE##*=}
+  export "${VAR}=$VALUE"
+  echo "${VAR}=$VALUE"
+}
+
+
+# Set Cold Boot or Quick/Fast Boot for an AVD as:
+# setForceColdBoot "ForceColdBoot" "ForceFastBoot" "config.ini FILE"
+# e.g. setForceColdBoot "yes" "no" "$avd_dir/config.ini"
+setForceColdBoot() {
+  forceColdBoot=$1
+  forceFastBoot=$2
+  avdConfig=$3
+
+  echo "CHANGE: fastboot.forceColdBoot=$forceColdBoot & fastboot.forceFastBoot=$forceFastBoot in $avdConfig"
+  $SED_I_CMD '/^fastboot.forceColdBoot/d' $avdConfig
+  $SED_I_CMD '/^fastboot.forceFastBoot/d' $avdConfig
+  echo "fastboot.forceColdBoot=$forceColdBoot" >> $avdConfig
+  echo "fastboot.forceFastBoot=$forceFastBoot" >> $avdConfig
+  cat $avdConfig | grep "fastboot.forceColdBoot"
+  cat $avdConfig | grep "fastboot.forceFastBoot"
+}
+
+# Set Cold Boot or Quick/Fast Boot for an AVD as:
+# setAllAVDsForceColdBoot "ForceColdBoot" "ForceFastBoot" "AVD/ROOT/DIR" "TAG.ID"
+# e.g. setAllAVDsForceColdBoot "yes" "no" "$HOME/.android/avd" "android-automotive"
+setAllAVDsForceColdBoot() {
+  forceColdBoot=$1
+  forceFastBoot=$2
+  avdRootDir=$3
+  avdTagId=$4
+
+  for file in $(ls "$avdRootDir"); do
+    avd_dir="$avdRootDir/$file"
+    if [[ $file == *.avd ]]; then
+      # Filter AVD type by TAG_ID
+      config_file="$avd_dir/config.ini"
+      getValue "myAvdId"  "AvdId" $config_file
+      if [[ -n $avdTagId  ]]; then
+        getValue "myAvdTagId" "tag.id" $config_file
+        if [[ $myAvdTagId != $avdTagId ]]; then
+          echo "SKIP: $myAvdId is $myAvdTagId rather $avdTagId"
+          echo
+          continue
+        fi
+      fi
+      setForceColdBoot $forceColdBoot $forceFastBoot $config_file
+      echo
+    fi
+  done
+}
+
+if [[ -z $AVD_ROOT_DIR ]]; then
+  AVD_ROOT_DIR="$HOME/.android/avd"
+fi
+echo "AVD_ROOT_DIR=$AVD_ROOT_DIR"
+
+if [[ -z $FORCE_COLD_BOOT ]]; then
+  FORCE_COLD_BOOT="yes"
+fi
+echo "FORCE_COLD_BOOT=$FORCE_COLD_BOOT"
+
+case "$FORCE_COLD_BOOT" in
+  yes)
+    setAllAVDsForceColdBoot "yes" "no" $AVD_ROOT_DIR $TAG_ID
+    ;;
+  no)
+    setAllAVDsForceColdBoot "no" "yes" $AVD_ROOT_DIR $TAG_ID
+    ;;
+  *)
+    readme
+    ;;
+esac
diff --git a/avd/test_patch_all_avds.sh b/avd/test_patch_all_avds.sh
new file mode 100755
index 0000000..17092a2
--- /dev/null
+++ b/avd/test_patch_all_avds.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+
+# Copyright (C) 2021 The Android Open Source Project
+#
+# 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.
+
+
+MY_NAME=$0
+SCRIPT_NAME=${MY_NAME##*/}
+SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME}
+
+echo "Testing patch_all_avds.sh"
+
+echo "Set all AVDs with RAM=4096 HEAP=576 DATA_DISK=6000"
+$SCRIPT_DIR/patch_all_avds.sh
+
+echo "Set all AVDs back to AE default RAM=1536 HEAP=256 DATA_DISK=2048"
+RAM=1536 HEAP=256 DATA_DISK=2048 $SCRIPT_DIR/patch_all_avds.sh
+
+echo "Set all Aotomotive AVDs with RAM=4096 HEAP=576 DATA_DISK=6000"
+RAM=4096 HEAP=576 DATA_DISK=6000 TAG_ID="android-automotive" $SCRIPT_DIR/patch_all_avds.sh
diff --git a/avd/test_set_avds_force_cold_boot.sh b/avd/test_set_avds_force_cold_boot.sh
new file mode 100755
index 0000000..3da4ad9
--- /dev/null
+++ b/avd/test_set_avds_force_cold_boot.sh
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+# Copyright (C) 2021 The Android Open Source Project
+#
+# 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.
+
+
+MY_NAME=$0
+SCRIPT_NAME=${MY_NAME##*/}
+SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME}
+
+echo "Testing set_avds_force_cold_boot.sh"
+echo "set all AVDs to Cold Boot"
+$SCRIPT_DIR/set_avds_force_cold_boot.sh
+
+echo "set all AVDs to Quick/Fast Boot"
+FORCE_COLD_BOOT="no" $SCRIPT_DIR/set_avds_force_cold_boot.sh
+
+echo "set all automotive AVDs to Quick/Fast Boot"
+FORCE_COLD_BOOT="yes" AVD_ROOT_DIR="$HOME/.android/avd" TAG_ID="android-automotive" $SCRIPT_DIR/set_avds_force_cold_boot.sh