Merge "fs_mgr: overlay: test assumption wrong if verity is enabled" am: 4115c2caf6
am: 94cbddd155

Change-Id: I959643203f53c6a82cc5b9cfa7d43bbb0beeb127
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index d9e017c..c7c86eb 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -27,6 +27,7 @@
 GREEN="${ESCAPE}[38;5;40m"
 RED="${ESCAPE}[38;5;196m"
 ORANGE="${ESCAPE}[38;5;255:165:0m"
+BLUE="${ESCAPE}[35m"
 NORMAL="${ESCAPE}[0m"
 
 # Helper functions
@@ -138,6 +139,8 @@
 
 [ "USAGE: adb_root
 
+NB: This can be flakey on devices due to USB state
+
 Returns: true if device in root state" ]
 adb_root() {
   adb root >/dev/null </dev/null 2>/dev/null
@@ -159,6 +162,9 @@
     false
     return
   fi
+  if [ "${O}" != "${O#*FAILED}" ]; then
+    O="${1}: <empty>"
+  fi
   if [ -n "${2}" -a "${1}: ${2}" != "${O}" ]; then
     echo "${2} != ${O}" >&2
     false
@@ -167,14 +173,21 @@
   echo ${O} >&2
 }
 
-[ "USAGE: die [-t <epoch>] [message] >/dev/stderr
+[ "USAGE: die [-d|-t <epoch>] [message] >/dev/stderr
 
-If -t <epoch> argument is supplied, dump logcat.
+If -d, or -t <epoch> argument is supplied, dump logcat.
 
 Returns: exit failure, report status" ]
 die() {
-  if [ X"-t" = X"${1}" -a -n "${2}" ]; then
-    adb_logcat -b all -v nsec -t ${2} >&2
+  if [ X"-d" = X"${1}" ]; then
+    adb_logcat -b all -v nsec -d >&2
+    shift
+  elif [ X"-t" = X"${1}" ]; then
+    if [ -n "${2}" ]; then
+      adb_logcat -b all -v nsec -t ${2} >&2
+    else
+      adb_logcat -b all -v nsec -d >&2
+    fi
     shift 2
   fi
   echo "${RED}[  FAILED  ]${NORMAL} ${@}" >&2
@@ -238,15 +251,20 @@
     die "${@}"
 }
 
-[ "USAGE: skip_administrative_mounts < /proc/mounts
+[ "USAGE: skip_administrative_mounts [data] < /proc/mounts
 
 Filters out all administrative (eg: sysfs) mounts uninteresting to the test" ]
 skip_administrative_mounts() {
+  if [ "data" = "${1}" ]; then
+    grep -v " /data "
+  else
+    cat -
+  fi |
   grep -v \
     -e "^\(overlay\|tmpfs\|none\|sysfs\|proc\|selinuxfs\|debugfs\) " \
     -e "^\(bpf\|cg2_bpf\|pstore\|tracefs\|adb\|mtp\|ptp\|devpts\) " \
     -e "^\(/data/media\|/dev/block/loop[0-9]*\) " \
-    -e " /\(cache\|mnt/scratch\|mnt/vendor/persist\|metadata\|data\) "
+    -e " /\(cache\|mnt/scratch\|mnt/vendor/persist\|metadata\) "
 }
 
 if [ X"-s" = X"${1}" -a -n "${2}" ]; then
@@ -263,6 +281,9 @@
 isDebuggable || die "device not a debug build"
 
 # Do something
+
+echo "${GREEN}[ RUN      ]${NORMAL} Testing kernel support for overlayfs" >&2
+
 adb_wait || die "wait for device failed"
 adb_sh ls -d /sys/module/overlay </dev/null >/dev/null &&
   echo "${GREEN}[       OK ]${NORMAL} overlay module present" >&2 ||
@@ -272,6 +293,9 @@
   die "overlay module can not be used on ANDROID"
 adb_root ||
   die "initial setup"
+
+echo "${GREEN}[ RUN      ]${NORMAL} Checking current overlayfs status" >&2
+
 reboot=false
 OVERLAYFS_BACKING="cache mnt/scratch"
 for d in ${OVERLAYFS_BACKING}; do
@@ -297,64 +321,90 @@
   echo "${ORANGE}[  WARNING ]${NORMAL} overlays present before setup" >&2 ||
   echo "${GREEN}[       OK ]${NORMAL} no overlay present before setup" >&2
 adb_sh df -k `adb_sh cat /proc/mounts |
-                skip_administrative_mounts |
+                skip_administrative_mounts data |
                 cut -s -d' ' -f1`
 
+echo "${GREEN}[ RUN      ]${NORMAL} disable verity" >&2
+
 T=`adb_date`
 D=`adb disable-verity 2>&1`
 err=${?}
-echo "${D}"
 if [ ${err} != 0 -o X"${D}" != X"${D##*setup failed}" ]; then
+  echo "${D%?Now reboot your device for settings to take effect}"
   die -t ${T} "setup for overlay"
 fi
 if [ X"${D}" != X"${D##*using overlayfs}" ]; then
   echo "${GREEN}[       OK ]${NORMAL} using overlayfs" >&2
 fi
-adb_reboot &&
-  adb_wait &&
-  D=`adb_sh df -k </dev/null` &&
-  H=`echo "${D}" | head -1` &&
-  D=`echo "${D}" | grep "^overlay "` &&
-  echo "${H}" &&
-  echo "${D}" ||
-  die "overlay takeover failed"
-echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
-  echo "${ORANGE}[  WARNING ]${NORMAL} overlay takeover before remount not complete" >&2
-
-T=`adb_date`
-adb_root &&
-  adb remount &&
-  D=`adb_sh df -k </dev/null` ||
-  die -t ${T} "can not collect filesystem data"
-if echo "${D}" | grep " /mnt/scratch" >/dev/null; then
-  echo "${ORANGE}[     INFO ]${NORMAL} using scratch dynamic partition for overrides" >&2
-  H=`adb_sh cat /proc/mounts | sed -n 's@\([^ ]*\) /mnt/scratch \([^ ]*\) .*@\2 on \1@p'`
-  [ -n "${H}" ] &&
-    echo "${ORANGE}[     INFO ]${NORMAL} scratch filesystem ${H}"
+reboot=false
+if [ X"${D}" != X"${D##*Successfully disabled verity}" ]; then
+  echo "${GREEN}[       OK ]${NORMAL} disabled verity" >&2
+  reboot=true
+else
+  echo "${ORANGE}[  WARNING ]${NORMAL} verity already disabled" >&2
 fi
+D=`adb_sh df -k </dev/null` &&
+  H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay " | true` &&
+  [ -n "${D}" ] &&
+  ( echo "${H}" && echo "${D}" && true ) &&
+  die -t ${T} "overlay takeover unexpected"
+L=
+if ${reboot}; then
+  L=`adb_logcat -b all -v nsec -t ${T} 2>&1`
+  adb_reboot &&
+    adb_wait 2m ||
+    die "reboot after verity disabled failed"
+  T=
+fi
+
+echo "${GREEN}[ RUN      ]${NORMAL} remount" >&2
+
+adb_root &&
+  adb remount ||
+  ( [ -n "${L}" ] && echo "${L}" && false ) ||
+  die -t "${T}" "adb remount failed"
+D=`adb_sh df -k </dev/null` &&
+  H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay "` ||
+  ( [ -n "${L}" ] && echo "${L}" && false ) ||
+  die -t ${T} "overlay takeover failed"
+echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
+  echo "${ORANGE}[  WARNING ]${NORMAL} overlay takeover not complete" >&2
+scratch_partition=scratch
+if echo "${D}" | grep " /mnt/scratch" >/dev/null; then
+  echo "${BLUE}[     INFO ]${NORMAL} using ${scratch_partition} dynamic partition for overrides" >&2
+fi
+M=`adb_sh cat /proc/mounts | sed -n 's@\([^ ]*\) /mnt/scratch \([^ ]*\) .*@\2 on \1@p'`
+[ -n "${M}" ] &&
+  echo "${BLUE}[     INFO ]${NORMAL} scratch filesystem ${M}"
+scratch_size=`adb_sh df -k /mnt/scratch </dev/null 2>/dev/null |
+              while read device kblocks used available use mounted on; do
+                if [ "/mnt/scratch" = "\${mounted}" ]; then
+                  echo \${kblocks}
+                fi
+              done` &&
+  [ -n "${scratch_size}" ] ||
+  die "scratch size"
+echo "${BLUE}[     INFO ]${NORMAL} scratch size ${scratch_size}KB" >&2
 for d in ${OVERLAYFS_BACKING}; do
   if adb_sh ls -d /${d}/overlay/system/upper </dev/null >/dev/null 2>/dev/null; then
-    echo "${ORANGE}[     INFO ]${NORMAL} /${d}/overlay is setup" >&2
+    echo "${BLUE}[     INFO ]${NORMAL} /${d}/overlay is setup" >&2
   fi
 done
 
-H=`echo "${D}" | head -1` &&
-  D=`echo "${D}" | grep "^overlay "` &&
-  echo "${H}" &&
+echo "${H}" &&
   echo "${D}" &&
   echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
   die  "overlay takeover after remount"
 !(adb_sh grep "^overlay " /proc/mounts </dev/null | grep " overlay ro,") &&
-  !(adb_sh grep " rw," /proc/mounts </dev/null | skip_administrative_mounts) ||
+  !(adb_sh grep " rw," /proc/mounts </dev/null | skip_administrative_mounts data) ||
   die "remount overlayfs missed a spot (ro)"
 
-adb_su "sed -n '1,/overlay \\/system/p' /proc/mounts" </dev/null |
-  skip_administrative_mounts |
-  grep -v ' \(squashfs\|ext4\|f2fs\) ' &&
-  echo "${ORANGE}[  WARNING ]${NORMAL} overlay takeover after first stage init" >&2 ||
-  echo "${GREEN}[       OK ]${NORMAL} overlay takeover in first stage init" >&2
-
 # Check something
+
+echo "${GREEN}[ RUN      ]${NORMAL} push content to system and vendor" >&2
+
 A="Hello World! $(date)"
 echo "${A}" | adb_sh "cat - > /system/hello"
 echo "${A}" | adb_sh "cat - > /vendor/hello"
@@ -364,9 +414,26 @@
 B="`adb_cat /vendor/hello`" ||
   die "vendor hello"
 check_eq "${A}" "${B}" vendor before reboot
+
+echo "${GREEN}[ RUN      ]${NORMAL} reboot to confirm content persistent" >&2
+
 adb_reboot &&
-  adb_wait &&
-  B="`adb_cat /system/hello`" ||
+  adb_wait 2m ||
+  die "reboot after override content added failed"
+
+D=`adb_su df -k </dev/null` &&
+  H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay "` ||
+  ( echo "${L}" && false ) ||
+  die -d "overlay takeover failed after reboot"
+
+adb_su "sed -n '1,/overlay \\/system/p' /proc/mounts" </dev/null |
+  skip_administrative_mounts |
+  grep -v ' \(squashfs\|ext4\|f2fs\) ' &&
+  echo "${ORANGE}[  WARNING ]${NORMAL} overlay takeover after first stage init" >&2 ||
+  echo "${GREEN}[       OK ]${NORMAL} overlay takeover in first stage init" >&2
+
+B="`adb_cat /system/hello`" ||
   die "re-read system hello after reboot"
 check_eq "${A}" "${B}" system after reboot
 # Only root can read vendor if sepolicy permissions are as expected
@@ -378,23 +445,30 @@
   die "re-read vendor hello after reboot"
 check_eq "${A}" "${B}" vendor after reboot
 
-adb reboot-fastboot &&
+echo "${GREEN}[ RUN      ]${NORMAL} flash vendor, confirm its content disappears" >&2
+
+[ -n "${ANDROID_PRODUCT_OUT}" ] &&
+  adb reboot-fastboot &&
   fastboot_wait 2m &&
   fastboot flash vendor ||
-  die "fastbootd flash vendor"
-# check scratch via fastboot
-fastboot_getvar partition-type:scratch raw &&
-  fastboot_getvar has-slot:scratch no &&
-  fastboot_getvar is-logical:scratch yes ||
-  die "fastboot can not see scratch parameters"
-echo "${ORANGE}[     INFO ]${NORMAL} expect fastboot erase scratch to fail" >&2
-fastboot erase scratch &&
-  die "fastbootd can erase scratch"
-echo "${ORANGE}[     INFO ]${NORMAL} expect fastboot format scratch to fail" >&2
-fastboot format scratch &&
-  die "fastbootd can format scratch"
+  ( fastboot reboot && false) ||
+  die "fastboot flash vendor"
+# check ${scratch_partition} via fastboot
+fastboot_getvar partition-type:${scratch_partition} raw &&
+  fastboot_getvar has-slot:${scratch_partition} no &&
+  fastboot_getvar is-logical:${scratch_partition} yes ||
+  ( fastboot reboot && false) ||
+  die "fastboot can not see ${scratch_partition} parameters"
+echo "${BLUE}[     INFO ]${NORMAL} expect fastboot erase ${scratch_partition} to fail" >&2
+fastboot erase ${scratch_partition} &&
+  ( fastboot reboot || true) &&
+  die "fastboot can erase ${scratch_partition}"
+echo "${BLUE}[     INFO ]${NORMAL} expect fastboot format ${scratch_partition} to fail" >&2
+fastboot format ${scratch_partition} &&
+  ( fastboot reboot || true) &&
+  die "fastboot can format ${scratch_partition}"
 fastboot reboot ||
-  die "can not reboot out of fastbootd"
+  die "can not reboot out of fastboot"
 echo "${ORANGE}[  WARNING ]${NORMAL} adb after fastboot ... waiting 2 minutes"
 adb_wait 2m ||
   die "did not reboot after flash"
@@ -417,6 +491,8 @@
   die "re-read vendor hello after flash vendor"
 check_eq "cat: /vendor/hello: No such file or directory" "${B}" vendor after flash vendor
 
+echo "${GREEN}[ RUN      ]${NORMAL} remove test content (cleanup)" >&2
+
 T=`adb_date`
 adb remount &&
   ( adb_sh rm /vendor/hello </dev/null 2>/dev/null || true ) &&
@@ -429,17 +505,20 @@
   die "re-read vendor hello after rm"
 check_eq "cat: /vendor/hello: No such file or directory" "${B}" after flash rm
 
+echo "${GREEN}[ RUN      ]${NORMAL} test fastboot flash to ${scratch_partition}" >&2
+
 adb reboot-fastboot &&
-  dd if=/dev/zero of=adb-remount-test.img bs=4096 count=16 &&
+  dd if=/dev/zero of=/tmp/adb-remount-test.img bs=4096 count=16 2>/dev/null &&
   fastboot_wait 2m ||
-  die "reboot into fastbootd"
-fastboot flash scratch adb-remount-test.img
+  ( rm /tmp/adb-remount-test.img && false) ||
+  die "reboot into fastboot"
+fastboot flash ${scratch_partition} /tmp/adb-remount-test.img
 err=${?}
-rm adb-remount-test.img
-[ 0 -eq ${err} ] ||
-  die "fastbootd flash scratch"
+rm /tmp/adb-remount-test.img
 fastboot reboot ||
-  die "can not reboot out of fastbootd"
+  die "can not reboot out of fastboot"
+[ 0 -eq ${err} ] ||
+  die "fastboot flash ${scratch_partition}"
 adb_wait 2m &&
   adb_root ||
   die "did not reboot after flash"
@@ -449,7 +528,8 @@
 echo "${D}"
 [ ${err} = 0 ] &&
   [ X"${D}" = X"${D##*setup failed}" ] &&
-  [ X"${D}" != X"${D##*using overlayfs}" ] ||
+  [ X"${D}" != X"${D##*using overlayfs}" ] &&
+  echo "${GREEN}[       OK ]${NORMAL} ${scratch_partition} recreated" >&2 ||
   die -t ${T} "setup for overlayfs"
 
 echo "${GREEN}[  PASSED  ]${NORMAL} adb remount" >&2