blob: 5a542fd2dd9bf14d4d4e73847e5ff8498b6c8cf8 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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
Tao Baoe3f09a72019-10-01 11:55:36 -070017#include "recovery_utils/roots.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070018
Tao Baobb10e582017-07-22 16:30:34 -070019#include <fcntl.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053020#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <sys/stat.h>
24#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070025#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <unistd.h>
Simon Shieldsa940e982019-10-02 00:21:45 +100027#include <sys/mount.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Yifan Hongd81b8e32018-12-17 14:29:06 -080029#include <iostream>
Tao Bao3c00fac2017-07-22 16:46:54 -070030#include <string>
31#include <vector>
32
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070033#include <android-base/logging.h>
Daniel Rosenbergf25b9772019-12-16 18:32:00 -080034#include <android-base/properties.h>
Jin Qianded2dac2017-04-21 14:36:12 -070035#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070036#include <android-base/unique_fd.h>
Alessandro Astonef9d52d02020-03-01 13:33:27 +010037#include <blkid/blkid.h>
Eric Biggersa762e142021-11-08 16:45:49 -080038#include <ext4_utils/ext4_utils.h>
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -080039#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040#include <fs_mgr.h>
Yifan Hong0f339e22018-12-03 13:44:01 -080041#include <fs_mgr/roots.h>
Erfan Abdi189b2ed2020-04-14 23:07:33 +043042#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070043
Tao Bao3d69f0d2018-12-20 09:44:06 -080044#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tom Cherry72a114a2019-01-30 15:59:53 -080046using android::fs_mgr::Fstab;
47using android::fs_mgr::FstabEntry;
48using android::fs_mgr::ReadDefaultFstab;
aleastof9a5f052021-01-17 20:31:22 +010049using android::dm::DeviceMapper;
50using android::dm::DmDeviceState;
Tom Cherry72a114a2019-01-30 15:59:53 -080051
Steve Kondik2d46dfb2013-11-24 21:40:09 -080052static void write_fstab_entry(const FstabEntry& entry, FILE* file) {
53 if (entry.fs_type != "emmc" && !entry.fs_mgr_flags.vold_managed && !entry.blk_device.empty() &&
54 entry.blk_device[0] == '/' && !entry.mount_point.empty() && entry.mount_point[0] == '/') {
55 fprintf(file, "%s ", entry.blk_device.c_str());
56 fprintf(file, "%s ", entry.mount_point.c_str());
57 fprintf(file, "%s ", entry.fs_type.c_str());
58 fprintf(file, "%s 0 0\n", !entry.fs_options.empty() ? entry.fs_options.c_str() : "defaults");
59 }
60}
61
Yifan Hongd81b8e32018-12-17 14:29:06 -080062static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063
Tianjie Xu164c60a2019-05-15 13:59:39 -070064constexpr const char* CACHE_ROOT = "/cache";
65
Alessandro Astonef9d52d02020-03-01 13:33:27 +010066FstabEntry* fstab_entry_for_mount_point_detect_fs(const std::string& path) {
67 FstabEntry* found = android::fs_mgr::GetEntryForMountPoint(&fstab, path);
68 if (found == nullptr) {
69 return nullptr;
70 }
71
72 if (char* detected_fs_type = blkid_get_tag_value(nullptr, "TYPE", found->blk_device.c_str())) {
73 for (auto& entry : fstab) {
74 if (entry.mount_point == path && entry.fs_type == detected_fs_type) {
75 found = &entry;
76 break;
77 }
78 }
79 free(detected_fs_type);
80 }
81
82 return found;
83}
84
Tao Baobb10e582017-07-22 16:30:34 -070085void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080086 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070087 LOG(ERROR) << "Failed to read default fstab";
88 return;
89 }
Doug Zongker2810ced2011-02-17 15:55:21 -080090
Yifan Hongd81b8e32018-12-17 14:29:06 -080091 fstab.emplace_back(FstabEntry{
Nick Desaulniers1cb510d2019-10-10 16:33:58 -070092 .blk_device = "ramdisk",
93 .mount_point = "/tmp",
94 .fs_type = "ramdisk",
95 .length = 0,
96 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070097
Alessandro Astonef9d52d02020-03-01 13:33:27 +010098 Fstab fake_fstab;
Yifan Hongd81b8e32018-12-17 14:29:06 -080099 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
100 for (size_t i = 0; i < fstab.size(); ++i) {
101 const auto& entry = fstab[i];
102 std::cout << " " << i << " " << entry.mount_point << " "
103 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
104 << std::endl;
Alessandro Astonef9d52d02020-03-01 13:33:27 +0100105
106 if (std::find_if(fake_fstab.begin(), fake_fstab.end(), [entry](const FstabEntry& e) {
107 return entry.mount_point == e.mount_point;
108 }) == fake_fstab.end()) {
109 FstabEntry* entry_detectfs = fstab_entry_for_mount_point_detect_fs(entry.mount_point);
110 if (entry_detectfs == &entry) {
111 fake_fstab.emplace_back(entry);
112 }
Steve Kondik2d46dfb2013-11-24 21:40:09 -0800113 }
Tao Baobb10e582017-07-22 16:30:34 -0700114 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800115 std::cout << std::endl;
Steve Kondik2d46dfb2013-11-24 21:40:09 -0800116
Alessandro Astonef9d52d02020-03-01 13:33:27 +0100117 // Create a boring /etc/fstab so tools like Busybox work
118 FILE* file = fopen("/etc/fstab", "w");
Steve Kondik2d46dfb2013-11-24 21:40:09 -0800119 if (file) {
Alessandro Astonef9d52d02020-03-01 13:33:27 +0100120 for (auto& entry : fake_fstab) {
121 write_fstab_entry(entry, file);
122 }
Steve Kondik2d46dfb2013-11-24 21:40:09 -0800123 fclose(file);
Alessandro Astonef9d52d02020-03-01 13:33:27 +0100124 } else {
125 LOG(ERROR) << "Unable to create /etc/fstab";
Steve Kondik2d46dfb2013-11-24 21:40:09 -0800126 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700127}
128
Tao Bao3e18f2b2017-09-29 17:11:13 -0700129Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -0800130 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -0700131}
132
Tao Baoabb8f772015-07-30 14:43:27 -0700133// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800134int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
135 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136}
137
Yifan Hongd81b8e32018-12-17 14:29:06 -0800138int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -0700139 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800140 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -0700141}
142
Yifan Hongd81b8e32018-12-17 14:29:06 -0800143int ensure_path_unmounted(const std::string& path) {
144 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700145}
146
Simon Shieldsa940e982019-10-02 00:21:45 +1000147int ensure_volume_unmounted(const std::string& blk_device) {
148 android::fs_mgr::Fstab mounted_fstab;
149 if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounted_fstab)) {
150 LOG(ERROR) << "Failed to read /proc/mounts";
151 return -1;
152 }
153
154 /* find any entries with the volume */
155 for (auto& entry : mounted_fstab) {
156 if (entry.blk_device == blk_device) {
157 int result = umount(entry.mount_point.c_str());
158 if (result == -1) {
159 LOG(ERROR) << "Failed to unmount " << blk_device << " from " << entry.mount_point << ": "
160 << errno;
161 return -1;
162 }
163 }
164 }
165 return 0;
166}
167
Tao Bao3c00fac2017-07-22 16:46:54 -0700168static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800169 CHECK(!args.empty());
170 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -0700171
172 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800173 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700174 execv(argv[0], argv.data());
175 _exit(EXIT_FAILURE);
176 }
177
178 int status;
179 waitpid(child, &status, 0);
180 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
181 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
182 }
183 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700184}
185
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530186static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700187 struct stat buf;
188 int ret = fstat(fd, &buf);
189 if (ret) return 0;
190
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530191 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700192 if (S_ISREG(buf.st_mode)) {
193 computed_size = buf.st_size - reserve_len;
194 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530195 uint64_t block_device_size = get_block_device_size(fd);
196 if (block_device_size < reserve_len ||
197 block_device_size > std::numeric_limits<int64_t>::max()) {
198 computed_size = 0;
199 } else {
200 computed_size = block_device_size - reserve_len;
201 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700202 } else {
203 computed_size = 0;
204 }
205
206 return computed_size;
207}
208
Kelvin Zhang73549942024-01-04 16:43:53 -0800209int format_volume(const std::string& volume, const std::string& directory,
210 std::string_view new_fstype) {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800211 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700212 if (v == nullptr) {
213 LOG(ERROR) << "unknown volume \"" << volume << "\"";
214 return -1;
215 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800216 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700217 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
218 return -1;
219 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800220 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700221 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
222 return -1;
223 }
Alessandro Astone698aaae2020-03-30 00:01:21 +0200224 if (ensure_volume_unmounted(v->blk_device) != 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700225 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
226 return -1;
227 }
Jis G Jacob02dc2f92024-04-10 11:51:17 -0400228 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
229 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800230 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700231 }
232
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800233 bool needs_casefold = false;
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800234
235 if (volume == "/data") {
Martijn Coenen5a4a7ff2020-04-15 11:52:21 +0200236 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800237 }
238
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530239 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800240 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700241 length = v->length;
Eric Biggersa762e142021-11-08 16:45:49 -0800242 } else if (v->length < 0) {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800243 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700244 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530245 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700246 return -1;
247 }
Eric Biggersa762e142021-11-08 16:45:49 -0800248 length = get_file_size(fd.get(), -v->length);
Tao Bao3c00fac2017-07-22 16:46:54 -0700249 if (length <= 0) {
xunchang24788852019-03-22 16:08:52 -0700250 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700251 return -1;
252 }
253 }
254
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800255 // If the raw disk will be used as a metadata encrypted device mapper target,
Kelvin Zhangaa0a88d2024-01-13 21:25:15 +0000256 // next boot will do encrypt_in_place the raw disk. While fs_mgr mounts /data
257 // as RO to avoid write file operations before encrypt_inplace, this code path
258 // is not well tested so we would like to avoid it if possible. For safety,
259 // let vold do the formatting on boot for metadata encrypted devices, except
260 // when user specified a new fstype. Because init formats /data according
261 // to fstab, it's difficult to override the fstab in init.
262 if (!v->metadata_key_dir.empty() && length == 0 && new_fstype.empty()) {
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800263 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR));
264 if (fd == -1) {
265 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
266 return -1;
267 }
268 int64_t device_size = get_file_size(fd.get(), 0);
269 if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) {
270 LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size "
271 << device_size;
272 return 0;
273 }
274 }
275
Dan Shic89b4e42024-01-11 02:46:37 +0000276 if ((v->fs_type == "ext4" && new_fstype.empty()) || new_fstype == "ext4") {
Kelvin Zhang73549942024-01-04 16:43:53 -0800277 LOG(INFO) << "Formatting " << v->blk_device << " as ext4";
Tao Bao3c00fac2017-07-22 16:46:54 -0700278 static constexpr int kBlockSize = 4096;
279 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900280 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700281 };
282
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000283 // Following is added for Project ID's quota as they require wider inodes.
284 // The Quotas themselves are enabled by tune2fs on boot.
285 mke2fs_args.push_back("-I");
286 mke2fs_args.push_back("512");
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800287
Jaegeuk Kima8d36e12020-02-11 15:24:54 -0800288 if (v->fs_mgr_flags.ext_meta_csum) {
289 mke2fs_args.push_back("-O");
290 mke2fs_args.push_back("metadata_csum");
291 mke2fs_args.push_back("-O");
292 mke2fs_args.push_back("64bit");
293 mke2fs_args.push_back("-O");
294 mke2fs_args.push_back("extent");
295 }
296
Tao Bao3c00fac2017-07-22 16:46:54 -0700297 int raid_stride = v->logical_blk_size / kBlockSize;
298 int raid_stripe_width = v->erase_blk_size / kBlockSize;
299 // stride should be the max of 8KB and logical block size
300 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
301 raid_stride = 8192 / kBlockSize;
302 }
303 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
304 mke2fs_args.push_back("-E");
305 mke2fs_args.push_back(
306 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
307 }
308 mke2fs_args.push_back(v->blk_device);
309 if (length != 0) {
310 mke2fs_args.push_back(std::to_string(length / kBlockSize));
311 }
312
313 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800314 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700315 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900316 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700317 };
318 result = exec_cmd(e2fsdroid_args);
319 }
320
321 if (result != 0) {
322 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
323 return -1;
324 }
325 return 0;
326 }
327
328 // Has to be f2fs because we checked earlier.
Kelvin Zhang73549942024-01-04 16:43:53 -0800329 LOG(INFO) << "Formatting " << v->blk_device << " as f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700330 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700331 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800332 "/system/bin/make_f2fs",
333 "-g",
334 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700335 };
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000336
337 make_f2fs_cmd.push_back("-O");
338 make_f2fs_cmd.push_back("project_quota,extra_attr");
339
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800340 if (needs_casefold) {
341 make_f2fs_cmd.push_back("-O");
342 make_f2fs_cmd.push_back("casefold");
343 make_f2fs_cmd.push_back("-C");
344 make_f2fs_cmd.push_back("utf8");
345 }
Jaegeuk Kim32106002020-01-14 11:00:37 -0800346 if (v->fs_mgr_flags.fs_compress) {
347 make_f2fs_cmd.push_back("-O");
348 make_f2fs_cmd.push_back("compression");
349 make_f2fs_cmd.push_back("-O");
350 make_f2fs_cmd.push_back("extra_attr");
351 }
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800352 make_f2fs_cmd.push_back(v->blk_device);
Jaegeuk Kim43582622018-04-02 13:37:35 -0700353 if (length >= kSectorSize) {
354 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700355 }
356
Tao Baoc674dfb2018-12-20 14:25:15 -0800357 if (exec_cmd(make_f2fs_cmd) != 0) {
358 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700359 return -1;
360 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800361 if (!directory.empty()) {
362 std::vector<std::string> sload_f2fs_cmd = {
363 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
364 };
365 if (exec_cmd(sload_f2fs_cmd) != 0) {
366 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
367 return -1;
368 }
369 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700370 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700372
Yifan Hongd81b8e32018-12-17 14:29:06 -0800373int format_volume(const std::string& volume) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800374 return format_volume(volume, "", "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800375}
376
Doug Zongker239ac6a2013-08-20 16:03:25 -0700377int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800378 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700379 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
380 return -1;
381 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800382 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700383 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800384 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700385 continue;
386 }
387
Yifan Hongd81b8e32018-12-17 14:29:06 -0800388 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
389 if (ensure_path_mounted(entry.mount_point) != 0) {
390 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700391 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700392 }
393 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800394 if (ensure_path_unmounted(entry.mount_point) != 0) {
395 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700396 return -1;
397 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700398 }
Tao Bao57130c42017-05-10 12:11:21 -0700399 }
400 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700401}
Tianjie Xu164c60a2019-05-15 13:59:39 -0700402
403bool HasCache() {
404 CHECK(!fstab.empty());
405 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
406 return has_cache;
407}
Erfan Abdi189b2ed2020-04-14 23:07:33 +0430408
aleastof9a5f052021-01-17 20:31:22 +0100409static bool logical_partitions_auto_mapped = false;
410
411void map_logical_partitions() {
412 if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
413 !logical_partitions_mapped()) {
414 std::string super_name = fs_mgr_get_super_partition_name();
415 if (!android::fs_mgr::CreateLogicalPartitions("/dev/block/by-name/" + super_name)) {
416 LOG(ERROR) << "Failed to map logical partitions";
417 } else {
418 logical_partitions_auto_mapped = true;
419 }
420 }
421}
422
423bool dm_find_system() {
424 auto rec = GetEntryForPath(&fstab, android::fs_mgr::GetSystemRoot());
425 if (!rec->fs_mgr_flags.logical) {
426 return false;
427 }
428 // If the fstab entry for system it's a path instead of a name, then it was already mapped
429 if (rec->blk_device[0] != '/') {
430 if (DeviceMapper::Instance().GetState(rec->blk_device) == DmDeviceState::INVALID) {
431 return false;
432 }
433 }
434 return true;
435}
436
Erfan Abdi189b2ed2020-04-14 23:07:33 +0430437bool logical_partitions_mapped() {
aleastof9a5f052021-01-17 20:31:22 +0100438 return android::fs_mgr::LogicalPartitionsMapped() || logical_partitions_auto_mapped ||
439 dm_find_system();
Erfan Abdi189b2ed2020-04-14 23:07:33 +0430440}