fs_mgr: remount: add -T fstab argument

Allow selection of an fstab file.  This is to mirror the built-in
functionality associated with init mount_all <fstab> command.

Test: adb-remount-test.sh
Bug: 122602260
Change-Id: I5fc2f3707c1dafd687c826eaccbaab03a408035b
diff --git a/fs_mgr/fs_mgr_remount.cpp b/fs_mgr/fs_mgr_remount.cpp
index 1cec543..6e9ab2a 100644
--- a/fs_mgr/fs_mgr_remount.cpp
+++ b/fs_mgr/fs_mgr_remount.cpp
@@ -38,8 +38,9 @@
 
 [[noreturn]] void usage(int exit_status) {
     LOG(INFO) << getprogname()
-              << " [-h]\n"
+              << " [-h] [-T fstab_file]\n"
                  "\t-h --help\tthis help\n"
+                 "\t-T --fstab\tcustom fstab file location\n"
                  "\n"
                  "Remount all partitions read-write.\n"
                  "Verity must be disabled.";
@@ -134,12 +135,22 @@
         return NOT_USERDEBUG;
     }
 
+    const char* fstab_file = nullptr;
+
     struct option longopts[] = {
+            {"fstab", required_argument, nullptr, 'T'},
             {"help", no_argument, nullptr, 'h'},
             {0, 0, nullptr, 0},
     };
-    for (int opt; (opt = ::getopt_long(argc, argv, "h", longopts, nullptr)) != -1;) {
+    for (int opt; (opt = ::getopt_long(argc, argv, "hT:", longopts, nullptr)) != -1;) {
         switch (opt) {
+            case 'T':
+                if (fstab_file) {
+                    LOG(ERROR) << "Cannot supply two fstabs: -T " << fstab_file << " -T" << optarg;
+                    usage(BADARG);
+                }
+                fstab_file = optarg;
+                break;
             default:
                 LOG(ERROR) << "Bad Argument -" << char(opt);
                 usage(BADARG);
@@ -161,10 +172,16 @@
         return NOT_ROOT;
     }
 
+    // Read the selected fstab.
     android::fs_mgr::Fstab fstab;
-    auto fstab_read = android::fs_mgr::ReadDefaultFstab(&fstab);
+    auto fstab_read = false;
+    if (fstab_file) {
+        fstab_read = android::fs_mgr::ReadFstabFromFile(fstab_file, &fstab);
+    } else {
+        fstab_read = android::fs_mgr::ReadDefaultFstab(&fstab);
+    }
     if (!fstab_read || fstab.empty()) {
-        PLOG(ERROR) << "Failed to read default fstab";
+        PLOG(ERROR) << "Failed to read fstab";
         return NO_FSTAB;
     }