blob: 2e6b5b8071a9239102f5ffe4a6ba1e0694246c90 [file] [log] [blame]
David Andersonedee8362018-05-16 13:43:22 -07001/*
2 * Copyright (C) 2018 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 "fsck_unshare_blocks.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <spawn.h>
22#include <string.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <unistd.h>
28
29#include <algorithm>
30#include <memory>
31#include <string>
32#include <vector>
33
34#include <android-base/logging.h>
35#include <android-base/properties.h>
36#include <android-base/unique_fd.h>
37#include <fstab/fstab.h>
38
39#include "roots.h"
40
41static constexpr const char* SYSTEM_E2FSCK_BIN = "/system/bin/e2fsck_static";
42static constexpr const char* TMP_E2FSCK_BIN = "/tmp/e2fsck.bin";
43
44static bool copy_file(const char* source, const char* dest) {
45 android::base::unique_fd source_fd(open(source, O_RDONLY));
46 if (source_fd < 0) {
47 PLOG(ERROR) << "open %s failed" << source;
48 return false;
49 }
50
51 android::base::unique_fd dest_fd(open(dest, O_CREAT | O_WRONLY, S_IRWXU));
52 if (dest_fd < 0) {
53 PLOG(ERROR) << "open %s failed" << dest;
54 return false;
55 }
56
57 for (;;) {
58 char buf[4096];
59 ssize_t rv = read(source_fd, buf, sizeof(buf));
60 if (rv < 0) {
61 PLOG(ERROR) << "read failed";
62 return false;
63 }
64 if (rv == 0) {
65 break;
66 }
67 if (write(dest_fd, buf, rv) != rv) {
68 PLOG(ERROR) << "write failed";
69 return false;
70 }
71 }
72 return true;
73}
74
75static bool run_e2fsck(const std::string& partition) {
76 Volume* volume = volume_for_mount_point(partition);
77 if (!volume) {
78 LOG(INFO) << "No fstab entry for " << partition << ", skipping.";
79 return true;
80 }
81
82 LOG(INFO) << "Running e2fsck on device " << volume->blk_device;
83
84 std::vector<std::string> args = { TMP_E2FSCK_BIN, "-p", "-E", "unshare_blocks",
85 volume->blk_device };
86 std::vector<char*> argv(args.size());
87 std::transform(args.cbegin(), args.cend(), argv.begin(),
88 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
89 argv.push_back(nullptr);
90
91 pid_t child;
92 char* env[] = { nullptr };
93 if (posix_spawn(&child, argv[0], nullptr, nullptr, argv.data(), env)) {
94 PLOG(ERROR) << "posix_spawn failed";
95 return false;
96 }
97
98 int status = 0;
99 int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
100 if (ret < 0) {
101 PLOG(ERROR) << "waitpid failed";
102 return false;
103 }
104 if (!WIFEXITED(status)) {
105 LOG(ERROR) << "e2fsck exited abnormally: " << status;
106 return false;
107 }
108 int return_code = WEXITSTATUS(status);
109 if (return_code >= 8) {
110 LOG(ERROR) << "e2fsck could not unshare blocks: " << return_code;
111 return false;
112 }
113
114 LOG(INFO) << "Successfully unshared blocks on " << partition;
115 return true;
116}
117
David Andersonedee8362018-05-16 13:43:22 -0700118bool do_fsck_unshare_blocks() {
119 // List of partitions we will try to e2fsck -E unshare_blocks.
120 std::vector<std::string> partitions = { "/odm", "/oem", "/product", "/vendor" };
121
122 // Temporarily mount system so we can copy e2fsck_static.
123 bool mounted = false;
124 if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900125 mounted = ensure_path_mounted_at("/", "/mnt/system") != -1;
David Andersonedee8362018-05-16 13:43:22 -0700126 partitions.push_back("/");
127 } else {
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900128 mounted = ensure_path_mounted_at("/system", "/mnt/system") != -1;
David Andersonedee8362018-05-16 13:43:22 -0700129 partitions.push_back("/system");
130 }
131 if (!mounted) {
132 LOG(ERROR) << "Failed to mount system image.";
133 return false;
134 }
135 if (!copy_file(SYSTEM_E2FSCK_BIN, TMP_E2FSCK_BIN)) {
136 LOG(ERROR) << "Could not copy e2fsck to /tmp.";
137 return false;
138 }
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900139 if (umount("/mnt/system") < 0) {
David Andersonedee8362018-05-16 13:43:22 -0700140 PLOG(ERROR) << "umount failed";
141 return false;
142 }
143
144 bool ok = true;
145 for (const auto& partition : partitions) {
146 ok &= run_e2fsck(partition);
147 }
148
149 if (ok) {
150 LOG(INFO) << "Finished running e2fsck.";
151 } else {
152 LOG(ERROR) << "Finished running e2fsck, but not all partitions succceeded.";
153 }
154 return ok;
155}