Merge "fs_mgr: overlay backing, force filesystem for f2fs"
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index f06b819..49ecc06 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -20,6 +20,7 @@
 #include <linux/fs.h>
 #include <selinux/selinux.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <sys/mount.h>
 #include <sys/param.h>
@@ -692,13 +693,21 @@
         errno = 0;
     }
 
-    auto ret = system((mnt_type == "f2fs")
-                              ? ((kMkF2fs + " -d1 " + scratch_device).c_str())
-                              : ((kMkExt4 + " -b 4096 -t ext4 -m 0 -M " + kScratchMountPoint +
-                                  " -O has_journal " + scratch_device)
-                                         .c_str()));
+    // Force mkfs by design for overlay support of adb remount, simplify and
+    // thus do not rely on fsck to correct problems that could creep in.
+    auto command = ""s;
+    if (mnt_type == "f2fs") {
+        command = kMkF2fs + " -w 4096 -f -d1 -l" + android::base::Basename(kScratchMountPoint);
+    } else if (mnt_type == "ext4") {
+        command = kMkExt4 + " -b 4096 -t ext4 -m 0 -O has_journal -M " + kScratchMountPoint;
+    } else {
+        LERROR << mnt_type << " has no mkfs cookbook";
+        return false;
+    }
+    command += " " + scratch_device;
+    auto ret = system(command.c_str());
     if (ret) {
-        LERROR << "make " << mnt_type << " filesystem on " << scratch_device << " error=" << ret;
+        LERROR << "make " << mnt_type << " filesystem on " << scratch_device << " return=" << ret;
         return false;
     }
 
diff --git a/fs_mgr/tests/adb-remount-test.sh b/fs_mgr/tests/adb-remount-test.sh
index b6a8eef..6b908d3 100755
--- a/fs_mgr/tests/adb-remount-test.sh
+++ b/fs_mgr/tests/adb-remount-test.sh
@@ -198,39 +198,80 @@
 
 # Do something
 adb_wait || die "wait for device failed"
-adb_sh ls -d /sys/module/overlay </dev/null || die "overlay module not present"
-adb_su ls /sys/module/overlay/parameters/override_creds </dev/null ||
+adb_sh ls -d /sys/module/overlay </dev/null >/dev/null &&
+  echo "${GREEN}[       OK ]${NORMAL} overlay module present" >&2 ||
+  die "overlay module not present"
+adb_su ls /sys/module/overlay/parameters/override_creds </dev/null >/dev/null &&
+  echo "${GREEN}[       OK ]${NORMAL} overlay module supports override_creds" >&2 ||
   die "overlay module can not be used on ANDROID"
 adb_root &&
-  adb_wait &&
-  D=`adb disable-verity 2>&1` ||
-    die "setup for overlay"
+  adb_wait ||
+  die "initial setup"
+reboot=false
+OVERLAYFS_BACKING="cache mnt/scratch"
+for d in ${OVERLAYFS_BACKING}; do
+  if adb_sh ls -d /${d}/overlay </dev/null >/dev/null 2>&1; then
+    echo "${ORANGE}[  WARNING ]${NORMAL} /${d}/overlay is setup, wiping" >&2
+    adb_sh rm -rf /${d}/overlay </dev/null ||
+      die "/${d}/overlay wipe"
+    reboot=true
+  fi
+done
+if ${reboot}; then
+  echo "${ORANGE}[  WARNING ]${NORMAL} rebooting before test" >&2
+  adb_reboot &&
+    adb_wait 2m &&
+    adb_root &&
+    adb_wait ||
+    die "reboot after wipe"
+fi
+D=`adb_sh df -k </dev/null` &&
+  H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay "` &&
+  echo "${H}" &&
+  echo "${D}" &&
+  echo "${ORANGE}[  WARNING ]${NORMAL} overlays present before setup" >&2 ||
+  echo "${GREEN}[       OK ]${NORMAL} no overlay present before setup" >&2
+
+D=`adb disable-verity 2>&1` ||
+  die "setup for overlay ${D}"
 echo "${D}"
 if [ X"${D}" != X"${D##*using overlayfs}" ]; then
   echo "${GREEN}[       OK ]${NORMAL} using overlayfs" >&2
 fi
-if adb_sh ls -d /data/overlay </dev/null >/dev/null 2>&1; then
-  echo "/data/overlay setup, clearing out" >&2
-  adb_sh rm -rf /data/overlay </dev/null ||
-    die "/data/overlay removal"
-fi
-adb_sh ls -d /cache/overlay </dev/null >/dev/null 2>&1 ||
-  adb_sh ls -d /mnt/scratch/overlay </dev/null >/dev/null 2>&1 ||
-  die "overlay directory setup"
 adb_reboot &&
   adb_wait &&
-  adb_sh df -k </dev/null | head -1 &&
-  adb_sh df -k </dev/null | grep "^overlay " ||
+  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"
-adb_sh df -k </dev/null | grep "^overlay .* /system\$" >/dev/null ||
+echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
   echo "${ORANGE}[  WARNING ]${NORMAL} overlay takeover before remount not complete" >&2
 
 adb_root &&
   adb_wait &&
   adb remount &&
-  adb_sh df -k </dev/null | head -1 &&
-  adb_sh df -k </dev/null | grep "^overlay " &&
-  adb_sh df -k </dev/null | grep "^overlay .* /system\$" >/dev/null ||
+  D=`adb_sh df -k </dev/null` ||
+  die "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}"
+fi
+for d in ${OVERLAYFS_BACKING}; do
+  if adb_sh ls -d /${d}/overlay/system/upper </dev/null >/dev/null 2>&1; then
+    echo "${ORANGE}[     INFO ]${NORMAL} /${d}/overlay is setup" >&2
+  fi
+done
+
+H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay "` &&
+  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 |
@@ -275,11 +316,14 @@
 adb_wait &&
   adb_root &&
   adb_wait &&
-  adb_sh df -k </dev/null | head -1 &&
-  adb_sh df -k </dev/null | grep "^overlay " &&
-  adb_sh df -k </dev/null | grep "^overlay .* /system\$" >/dev/null ||
+  D=`adb_sh df -k </dev/null` &&
+  H=`echo "${D}" | head -1` &&
+  D=`echo "${D}" | grep "^overlay "` &&
+  echo "${H}" &&
+  echo "${D}" &&
+  echo "${D}" | grep "^overlay .* /system\$" >/dev/null ||
   die  "overlay system takeover after flash vendor"
-adb_sh df -k </dev/null | grep "^overlay .* /vendor\$" >/dev/null &&
+echo "${D}" | grep "^overlay .* /vendor\$" >/dev/null &&
   die  "overlay minus vendor takeover after flash vendor"
 B="`adb_cat /system/hello`" ||
   die "re-read system hello after flash vendor"