blob: 684958e3813ba6f12414a843721f3f6bf6b2bccd [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";
Mark Salyzynfdea2422018-08-29 10:44:33 -070043static constexpr const char* SYSTEM_ROOT = "/system";
David Andersonedee8362018-05-16 13:43:22 -070044
45static bool copy_file(const char* source, const char* dest) {
46 android::base::unique_fd source_fd(open(source, O_RDONLY));
47 if (source_fd < 0) {
48 PLOG(ERROR) << "open %s failed" << source;
49 return false;
50 }
51
52 android::base::unique_fd dest_fd(open(dest, O_CREAT | O_WRONLY, S_IRWXU));
53 if (dest_fd < 0) {
54 PLOG(ERROR) << "open %s failed" << dest;
55 return false;
56 }
57
58 for (;;) {
59 char buf[4096];
60 ssize_t rv = read(source_fd, buf, sizeof(buf));
61 if (rv < 0) {
62 PLOG(ERROR) << "read failed";
63 return false;
64 }
65 if (rv == 0) {
66 break;
67 }
68 if (write(dest_fd, buf, rv) != rv) {
69 PLOG(ERROR) << "write failed";
70 return false;
71 }
72 }
73 return true;
74}
75
76static bool run_e2fsck(const std::string& partition) {
77 Volume* volume = volume_for_mount_point(partition);
78 if (!volume) {
79 LOG(INFO) << "No fstab entry for " << partition << ", skipping.";
80 return true;
81 }
82
83 LOG(INFO) << "Running e2fsck on device " << volume->blk_device;
84
85 std::vector<std::string> args = { TMP_E2FSCK_BIN, "-p", "-E", "unshare_blocks",
86 volume->blk_device };
87 std::vector<char*> argv(args.size());
88 std::transform(args.cbegin(), args.cend(), argv.begin(),
89 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
90 argv.push_back(nullptr);
91
92 pid_t child;
93 char* env[] = { nullptr };
94 if (posix_spawn(&child, argv[0], nullptr, nullptr, argv.data(), env)) {
95 PLOG(ERROR) << "posix_spawn failed";
96 return false;
97 }
98
99 int status = 0;
100 int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
101 if (ret < 0) {
102 PLOG(ERROR) << "waitpid failed";
103 return false;
104 }
105 if (!WIFEXITED(status)) {
106 LOG(ERROR) << "e2fsck exited abnormally: " << status;
107 return false;
108 }
109 int return_code = WEXITSTATUS(status);
110 if (return_code >= 8) {
111 LOG(ERROR) << "e2fsck could not unshare blocks: " << return_code;
112 return false;
113 }
114
115 LOG(INFO) << "Successfully unshared blocks on " << partition;
116 return true;
117}
118
David Andersonedee8362018-05-16 13:43:22 -0700119bool do_fsck_unshare_blocks() {
120 // List of partitions we will try to e2fsck -E unshare_blocks.
121 std::vector<std::string> partitions = { "/odm", "/oem", "/product", "/vendor" };
122
123 // Temporarily mount system so we can copy e2fsck_static.
124 bool mounted = false;
Mark Salyzynfdea2422018-08-29 10:44:33 -0700125 if (volume_for_mount_point(SYSTEM_ROOT) == nullptr) {
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900126 mounted = ensure_path_mounted_at("/", "/mnt/system") != -1;
David Andersonedee8362018-05-16 13:43:22 -0700127 partitions.push_back("/");
128 } else {
Mark Salyzynfdea2422018-08-29 10:44:33 -0700129 mounted = ensure_path_mounted_at(SYSTEM_ROOT, "/mnt/system") != -1;
130 partitions.push_back(SYSTEM_ROOT);
David Andersonedee8362018-05-16 13:43:22 -0700131 }
132 if (!mounted) {
133 LOG(ERROR) << "Failed to mount system image.";
134 return false;
135 }
136 if (!copy_file(SYSTEM_E2FSCK_BIN, TMP_E2FSCK_BIN)) {
137 LOG(ERROR) << "Could not copy e2fsck to /tmp.";
138 return false;
139 }
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900140 if (umount("/mnt/system") < 0) {
David Andersonedee8362018-05-16 13:43:22 -0700141 PLOG(ERROR) << "umount failed";
142 return false;
143 }
144
145 bool ok = true;
146 for (const auto& partition : partitions) {
147 ok &= run_e2fsck(partition);
148 }
149
150 if (ok) {
151 LOG(INFO) << "Finished running e2fsck.";
152 } else {
153 LOG(ERROR) << "Finished running e2fsck, but not all partitions succceeded.";
154 }
155 return ok;
156}