1 /*
2 * Copyright (C) 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/mount.h>
18 #include <unistd.h>
19
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23
main(int,char ** argv)24 int main(int /*argc*/, char** argv) {
25 android::base::InitLogging(argv, &android::base::KernelLogger);
26 LOG(INFO) << "Overlay remounter will remount all overlay mount points in the overlay_remounter "
27 "domain";
28
29 // Remount ouerlayfs
30 std::string contents;
31 auto result = android::base::ReadFileToString("/proc/mounts", &contents, true);
32
33 auto lines = android::base::Split(contents, "\n");
34 for (auto const& line : lines) {
35 if (!android::base::StartsWith(line, "overlay")) {
36 continue;
37 }
38 auto bits = android::base::Split(line, " ");
39 if (int result = umount(bits[1].c_str()); result == -1) {
40 PLOG(FATAL) << "umount FAILED: " << bits[1];
41 }
42 std::string options;
43 for (auto const& option : android::base::Split(bits[3], ",")) {
44 if (option == "ro" || option == "seclabel" || option == "noatime") continue;
45 if (!options.empty()) options += ',';
46 options += option;
47 }
48 result = mount("overlay", bits[1].c_str(), "overlay", MS_RDONLY | MS_NOATIME,
49 options.c_str());
50 if (result == 0) {
51 LOG(INFO) << "mount succeeded: " << bits[1] << " " << options;
52 } else {
53 PLOG(FATAL) << "mount FAILED: " << bits[1] << " " << bits[3];
54 }
55 }
56
57 const char* path = "/system/bin/init";
58 const char* args[] = {path, "second_stage", nullptr};
59 execv(path, const_cast<char**>(args));
60
61 // execv() only returns if an error happened, in which case we
62 // panic and never return from this function.
63 PLOG(FATAL) << "execv(\"" << path << "\") failed";
64
65 return 1;
66 }
67