Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9e8c41c5 authored by David Anderson's avatar David Anderson
Browse files

remount: Move more stuff out of do_remount.

This moves more logic out of do_remount and into main(). This eliminates
some redundant code.

Bug: 241179247
Test: remount
Change-Id: I212bdb0e97016dec50618962d7c24f46d35764c7
parent f78b2d17
Loading
Loading
Loading
Loading
+93 −93
Original line number Diff line number Diff line
@@ -91,12 +91,8 @@ void MyLogger(android::base::LogId id, android::base::LogSeverity severity, cons
    logd(id, severity, tag, file, line, message);
}

[[noreturn]] void reboot(bool overlayfs = false) {
    if (overlayfs) {
        LOG(INFO) << "Successfully setup overlayfs\nrebooting device";
    } else {
        LOG(INFO) << "Successfully disabled verity\nrebooting device";
    }
[[noreturn]] void reboot() {
    LOG(INFO) << "Rebooting device for new settings to take effect";
    ::sync();
    android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,remount");
    ::sleep(60);
@@ -136,7 +132,6 @@ enum RemountStatus {
    BINDER_ERROR,
    CHECKPOINTING,
    GSID_ERROR,
    CLEAN_SCRATCH_FILES,
};

static bool ReadFstab(const char* fstab_file, android::fs_mgr::Fstab* fstab) {
@@ -266,6 +261,7 @@ struct RemountCheckResult {
    bool setup_overlayfs = false;
    bool disabled_verity = false;
    bool verity_error = false;
    bool remounted_anything = false;
};

static RemountStatus CheckVerity(const FstabEntry& entry, RemountCheckResult* result) {
@@ -422,7 +418,69 @@ static RemountStatus RemountPartition(Fstab& fstab, Fstab& mounts, FstabEntry& e
    return REMOUNT_FAILED;
}

static int do_remount(int argc, char* argv[]) {
static int do_remount(Fstab& fstab, const std::vector<std::string>& partition_args,
                      RemountCheckResult* check_result) {
    Fstab partitions;
    if (partition_args.empty()) {
        partitions = GetAllRemountablePartitions(fstab);
    } else {
        if (auto rv = GetRemountList(fstab, partition_args, &partitions); rv != REMOUNT_SUCCESS) {
            return rv;
        }
    }

    // Check verity and optionally setup overlayfs backing.
    auto retval = CheckVerityAndOverlayfs(&partitions, check_result);

    if (partitions.empty() || check_result->disabled_verity) {
        if (partitions.empty()) {
            LOG(WARNING) << "No remountable partitions were found.";
        }
        return retval;
    }

    // Mount overlayfs.
    errno = 0;
    if (!fs_mgr_overlayfs_mount_all(&partitions) && errno) {
        PLOG(ERROR) << "Can not mount overlayfs for partitions";
        return BAD_OVERLAY;
    }

    // Get actual mounts _after_ overlayfs has been added.
    android::fs_mgr::Fstab mounts;
    if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounts) || mounts.empty()) {
        PLOG(ERROR) << "Failed to read /proc/mounts";
        return NO_MOUNTS;
    }

    // Remount selected partitions.
    for (auto& entry : partitions) {
        if (auto rv = RemountPartition(fstab, mounts, entry); rv != REMOUNT_SUCCESS) {
            retval = rv;
        } else {
            check_result->remounted_anything = true;
        }
    }
    return retval;
}

static int do_clean_scratch_files() {
    android::fs_mgr::CleanupOldScratchFiles();
    return 0;
}

int main(int argc, char* argv[]) {
    android::base::InitLogging(argv, MyLogger);
    if (argc > 0 && android::base::Basename(argv[0]) == "clean_scratch_files"s) {
        return do_clean_scratch_files();
    }

    // Make sure we are root.
    if (::getuid() != 0) {
        LOG(ERROR) << "Not running as root. Try \"adb root\" first.";
        return NOT_ROOT;
    }

    // If somehow this executable is delivered on a "user" build, it can
    // not function, so providing a clear message to the caller rather than
    // letting if fall through and provide a lot of confusing failure messages.
@@ -432,7 +490,7 @@ static int do_remount(int argc, char* argv[]) {
    }

    const char* fstab_file = nullptr;
    auto can_reboot = false;
    auto auto_reboot = false;
    std::vector<std::string> partition_args;

    struct option longopts[] = {
@@ -449,7 +507,7 @@ static int do_remount(int argc, char* argv[]) {
                usage(SUCCESS);
                break;
            case 'R':
                can_reboot = true;
                auto_reboot = true;
                break;
            case 'T':
                if (fstab_file) {
@@ -462,7 +520,7 @@ static int do_remount(int argc, char* argv[]) {
                verbose = true;
                break;
            case 'C':
                return CLEAN_SCRATCH_FILES;
                return do_clean_scratch_files();
            default:
                LOG(ERROR) << "Bad Argument -" << char(opt);
                usage(BADARG);
@@ -474,10 +532,9 @@ static int do_remount(int argc, char* argv[]) {
        partition_args.emplace_back(argv[optind]);
    }

    // Make sure we are root.
    if (::getuid() != 0) {
        LOG(ERROR) << "Not running as root. Try \"adb root\" first.";
        return NOT_ROOT;
    // Make sure checkpointing is disabled if necessary.
    if (auto rv = VerifyCheckpointing(); rv != REMOUNT_SUCCESS) {
        return rv;
    }

    // Read the selected fstab.
@@ -487,92 +544,35 @@ static int do_remount(int argc, char* argv[]) {
        return NO_FSTAB;
    }

    if (auto rv = VerifyCheckpointing(); rv != REMOUNT_SUCCESS) {
        return rv;
    }

    Fstab partitions;
    if (partition_args.empty()) {
        partitions = GetAllRemountablePartitions(fstab);
    } else {
        if (auto rv = GetRemountList(fstab, partition_args, &partitions); rv != REMOUNT_SUCCESS) {
            return rv;
        }
    }

    // Check verity and optionally setup overlayfs backing.
    RemountCheckResult check_result;
    auto retval = CheckVerityAndOverlayfs(&partitions, &check_result);
    int result = do_remount(fstab, partition_args, &check_result);

    bool auto_reboot = check_result.reboot_later && can_reboot;
    if (check_result.disabled_verity && check_result.setup_overlayfs) {
        LOG(INFO) << "Verity disabled; overlayfs enabled.";
    } else if (check_result.disabled_verity) {
        LOG(INFO) << "Verity disabled.";
    } else if (check_result.setup_overlayfs) {
        LOG(INFO) << "Overlayfs enabled.";
    }

    if (check_result.reboot_later) {
        if (auto_reboot) {
            // If (1) remount requires a reboot to take effect, (2) system is currently
            // running a DSU guest and (3) DSU is disabled, then enable DSU so that the
            // next reboot would not take us back to the host system but stay within
            // the guest system.
    if (auto_reboot) {
            if (auto rv = EnableDsuIfNeeded(); rv != REMOUNT_SUCCESS) {
                LOG(ERROR) << "Unable to automatically enable DSU";
                return rv;
            }
    }

    if (partitions.empty() || check_result.disabled_verity) {
        if (auto_reboot) {
            reboot(check_result.setup_overlayfs);
        }
        if (check_result.reboot_later) {
            return MUST_REBOOT;
        }
        LOG(WARNING) << "No partitions to remount";
        return retval;
    }

    // Mount overlayfs.
    errno = 0;
    if (!fs_mgr_overlayfs_mount_all(&partitions) && errno) {
        PLOG(ERROR) << "Can not mount overlayfs for partitions";
        return BAD_OVERLAY;
    }

    // Get actual mounts _after_ overlayfs has been added.
    android::fs_mgr::Fstab mounts;
    if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounts) || mounts.empty()) {
        PLOG(ERROR) << "Failed to read /proc/mounts";
        return NO_MOUNTS;
    }

    // Remount selected partitions.
    for (auto& entry : partitions) {
        if (auto rv = RemountPartition(fstab, mounts, entry); rv != REMOUNT_SUCCESS) {
            retval = rv;
        }
    }

    if (auto_reboot) reboot(check_result.setup_overlayfs);
    if (check_result.reboot_later) {
            reboot();
        } else {
            LOG(INFO) << "Now reboot your device for settings to take effect";
        }
    return retval;
}

static int do_clean_scratch_files() {
    android::fs_mgr::CleanupOldScratchFiles();
    return 0;
}

int main(int argc, char* argv[]) {
    android::base::InitLogging(argv, MyLogger);
    if (argc > 0 && android::base::Basename(argv[0]) == "clean_scratch_files"s) {
        return do_clean_scratch_files();
        return MUST_REBOOT;
    }
    int result = do_remount(argc, argv);
    if (result == MUST_REBOOT) {
        LOG(INFO) << "Now reboot your device for settings to take effect";
        result = 0;
    } else if (result == REMOUNT_SUCCESS) {
    if (result == REMOUNT_SUCCESS) {
        printf("remount succeeded\n");
    } else if (result == CLEAN_SCRATCH_FILES) {
        return do_clean_scratch_files();
    } else {
        printf("remount failed\n");
    }