blob: 0f8ffface8ec980bdab4eefd218c4c719696a086 [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>
David Andersonedee8362018-05-16 13:43:22 -070037
xunchang24788852019-03-22 16:08:52 -070038#include "otautil/roots.h"
David Andersonedee8362018-05-16 13:43:22 -070039
40static constexpr const char* SYSTEM_E2FSCK_BIN = "/system/bin/e2fsck_static";
41static constexpr const char* TMP_E2FSCK_BIN = "/tmp/e2fsck.bin";
42
43static bool copy_file(const char* source, const char* dest) {
44 android::base::unique_fd source_fd(open(source, O_RDONLY));
45 if (source_fd < 0) {
46 PLOG(ERROR) << "open %s failed" << source;
47 return false;
48 }
49
50 android::base::unique_fd dest_fd(open(dest, O_CREAT | O_WRONLY, S_IRWXU));
51 if (dest_fd < 0) {
52 PLOG(ERROR) << "open %s failed" << dest;
53 return false;
54 }
55
56 for (;;) {
57 char buf[4096];
58 ssize_t rv = read(source_fd, buf, sizeof(buf));
59 if (rv < 0) {
60 PLOG(ERROR) << "read failed";
61 return false;
62 }
63 if (rv == 0) {
64 break;
65 }
66 if (write(dest_fd, buf, rv) != rv) {
67 PLOG(ERROR) << "write failed";
68 return false;
69 }
70 }
71 return true;
72}
73
74static bool run_e2fsck(const std::string& partition) {
75 Volume* volume = volume_for_mount_point(partition);
76 if (!volume) {
77 LOG(INFO) << "No fstab entry for " << partition << ", skipping.";
78 return true;
79 }
80
81 LOG(INFO) << "Running e2fsck on device " << volume->blk_device;
82
83 std::vector<std::string> args = { TMP_E2FSCK_BIN, "-p", "-E", "unshare_blocks",
84 volume->blk_device };
85 std::vector<char*> argv(args.size());
86 std::transform(args.cbegin(), args.cend(), argv.begin(),
87 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
88 argv.push_back(nullptr);
89
90 pid_t child;
91 char* env[] = { nullptr };
92 if (posix_spawn(&child, argv[0], nullptr, nullptr, argv.data(), env)) {
93 PLOG(ERROR) << "posix_spawn failed";
94 return false;
95 }
96
97 int status = 0;
98 int ret = TEMP_FAILURE_RETRY(waitpid(child, &status, 0));
99 if (ret < 0) {
100 PLOG(ERROR) << "waitpid failed";
101 return false;
102 }
103 if (!WIFEXITED(status)) {
104 LOG(ERROR) << "e2fsck exited abnormally: " << status;
105 return false;
106 }
107 int return_code = WEXITSTATUS(status);
108 if (return_code >= 8) {
109 LOG(ERROR) << "e2fsck could not unshare blocks: " << return_code;
110 return false;
111 }
112
113 LOG(INFO) << "Successfully unshared blocks on " << partition;
114 return true;
115}
116
David Andersonedee8362018-05-16 13:43:22 -0700117bool do_fsck_unshare_blocks() {
118 // List of partitions we will try to e2fsck -E unshare_blocks.
119 std::vector<std::string> partitions = { "/odm", "/oem", "/product", "/vendor" };
120
121 // Temporarily mount system so we can copy e2fsck_static.
Yifan Hong49327802018-11-26 14:59:09 -0800122 std::string system_root = get_system_root();
Yifan Hongd81b8e32018-12-17 14:29:06 -0800123 bool mounted = ensure_path_mounted_at(system_root, "/mnt/system") != -1;
Yifan Hong49327802018-11-26 14:59:09 -0800124 partitions.push_back(system_root);
125
David Andersonedee8362018-05-16 13:43:22 -0700126 if (!mounted) {
127 LOG(ERROR) << "Failed to mount system image.";
128 return false;
129 }
130 if (!copy_file(SYSTEM_E2FSCK_BIN, TMP_E2FSCK_BIN)) {
131 LOG(ERROR) << "Could not copy e2fsck to /tmp.";
132 return false;
133 }
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900134 if (umount("/mnt/system") < 0) {
David Andersonedee8362018-05-16 13:43:22 -0700135 PLOG(ERROR) << "umount failed";
136 return false;
137 }
138
139 bool ok = true;
140 for (const auto& partition : partitions) {
141 ok &= run_e2fsck(partition);
142 }
143
144 if (ok) {
145 LOG(INFO) << "Finished running e2fsck.";
146 } else {
147 LOG(ERROR) << "Finished running e2fsck, but not all partitions succceeded.";
148 }
149 return ok;
150}