Merge "fs_mgr: overlayfs_mount_scratch don't write to system_other" am: 55c083901d
am: 5526da9d44

Change-Id: I6fadc067495be2b63f29f3540a4959cb1c38310f
diff --git a/fs_mgr/fs_mgr_overlayfs.cpp b/fs_mgr/fs_mgr_overlayfs.cpp
index ef7405a..7dae7f1 100644
--- a/fs_mgr/fs_mgr_overlayfs.cpp
+++ b/fs_mgr/fs_mgr_overlayfs.cpp
@@ -575,8 +575,14 @@
 }
 
 // Mount kScratchMountPoint
-bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::string mnt_type) {
-    if (!fs_mgr_rw_access(device_path)) return false;
+bool fs_mgr_overlayfs_mount_scratch(const std::string& device_path, const std::string mnt_type,
+                                    bool readonly = false) {
+    if (readonly) {
+        if (!fs_mgr_access(device_path)) return false;
+    } else {
+        if (!fs_mgr_rw_access(device_path)) return false;
+    }
+
     if (setfscreatecon(kOverlayfsFileContext)) {
         PERROR << "setfscreatecon " << kOverlayfsFileContext;
     }
@@ -589,6 +595,7 @@
     entry.mount_point = kScratchMountPoint;
     entry.fs_type = mnt_type;
     entry.flags = MS_RELATIME;
+    if (readonly) entry.flags |= MS_RDONLY;
     auto save_errno = errno;
     auto mounted = fs_mgr_do_mount_one(entry) == 0;
     if (!mounted) {
@@ -806,11 +813,15 @@
             scratch_can_be_mounted = false;
             auto scratch_device = fs_mgr_overlayfs_scratch_device();
             if (fs_mgr_overlayfs_scratch_can_be_mounted(scratch_device) &&
-                fs_mgr_wait_for_file(scratch_device, 10s) &&
-                fs_mgr_overlayfs_mount_scratch(scratch_device,
-                                               fs_mgr_overlayfs_scratch_mount_type()) &&
-                !fs_mgr_access(kScratchMountPoint + kOverlayTopDir)) {
-                fs_mgr_overlayfs_umount_scratch();
+                fs_mgr_wait_for_file(scratch_device, 10s)) {
+                const auto mount_type = fs_mgr_overlayfs_scratch_mount_type();
+                if (fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type,
+                                                   true /* readonly */)) {
+                    auto has_overlayfs_dir = fs_mgr_access(kScratchMountPoint + kOverlayTopDir);
+                    fs_mgr_overlayfs_umount_scratch();
+                    if (has_overlayfs_dir)
+                        fs_mgr_overlayfs_mount_scratch(scratch_device, mount_type);
+                }
             }
         }
         if (fs_mgr_overlayfs_mount(mount_point)) ret = true;