blob: aef1347eef8ecd883f3354fcfb9145d2cbe5b98c [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 Shieldseb476a02019-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 Astone9062a0c2020-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 Abdif40ff082020-04-14 23:07:33 +043042#include <fs_mgr_dm_linear.h>
Kelvin Zhang4b240f12024-07-30 11:09:52 -070043#include <fstab/fstab.h>
Tao Baode40ba52016-10-05 23:17:01 -070044
Tao Bao3d69f0d2018-12-20 09:44:06 -080045#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080046
Tom Cherry72a114a2019-01-30 15:59:53 -080047using android::fs_mgr::Fstab;
48using android::fs_mgr::FstabEntry;
49using android::fs_mgr::ReadDefaultFstab;
aleasto157f92d2021-01-17 20:31:22 +010050using android::dm::DeviceMapper;
51using android::dm::DmDeviceState;
Tom Cherry72a114a2019-01-30 15:59:53 -080052
Steve Kondik50ac1c82013-11-24 21:40:09 -080053static void write_fstab_entry(const FstabEntry& entry, FILE* file) {
54 if (entry.fs_type != "emmc" && !entry.fs_mgr_flags.vold_managed && !entry.blk_device.empty() &&
55 entry.blk_device[0] == '/' && !entry.mount_point.empty() && entry.mount_point[0] == '/') {
56 fprintf(file, "%s ", entry.blk_device.c_str());
57 fprintf(file, "%s ", entry.mount_point.c_str());
58 fprintf(file, "%s ", entry.fs_type.c_str());
59 fprintf(file, "%s 0 0\n", !entry.fs_options.empty() ? entry.fs_options.c_str() : "defaults");
60 }
61}
62
Yifan Hongd81b8e32018-12-17 14:29:06 -080063static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080064
Tianjie Xu164c60a2019-05-15 13:59:39 -070065constexpr const char* CACHE_ROOT = "/cache";
66
Alessandro Astone9062a0c2020-03-01 13:33:27 +010067FstabEntry* fstab_entry_for_mount_point_detect_fs(const std::string& path) {
68 FstabEntry* found = android::fs_mgr::GetEntryForMountPoint(&fstab, path);
69 if (found == nullptr) {
70 return nullptr;
71 }
72
73 if (char* detected_fs_type = blkid_get_tag_value(nullptr, "TYPE", found->blk_device.c_str())) {
74 for (auto& entry : fstab) {
75 if (entry.mount_point == path && entry.fs_type == detected_fs_type) {
76 found = &entry;
77 break;
78 }
79 }
80 free(detected_fs_type);
81 }
82
83 return found;
84}
85
Tao Baobb10e582017-07-22 16:30:34 -070086void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080087 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070088 LOG(ERROR) << "Failed to read default fstab";
89 return;
90 }
Doug Zongker2810ced2011-02-17 15:55:21 -080091
Yifan Hongd81b8e32018-12-17 14:29:06 -080092 fstab.emplace_back(FstabEntry{
Nick Desaulniers1cb510d2019-10-10 16:33:58 -070093 .blk_device = "ramdisk",
94 .mount_point = "/tmp",
95 .fs_type = "ramdisk",
96 .length = 0,
97 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070098
Alessandro Astone9062a0c2020-03-01 13:33:27 +010099 Fstab fake_fstab;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800100 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
101 for (size_t i = 0; i < fstab.size(); ++i) {
102 const auto& entry = fstab[i];
103 std::cout << " " << i << " " << entry.mount_point << " "
104 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
105 << std::endl;
Alessandro Astone9062a0c2020-03-01 13:33:27 +0100106
107 if (std::find_if(fake_fstab.begin(), fake_fstab.end(), [entry](const FstabEntry& e) {
108 return entry.mount_point == e.mount_point;
109 }) == fake_fstab.end()) {
110 FstabEntry* entry_detectfs = fstab_entry_for_mount_point_detect_fs(entry.mount_point);
111 if (entry_detectfs == &entry) {
112 fake_fstab.emplace_back(entry);
113 }
Steve Kondik50ac1c82013-11-24 21:40:09 -0800114 }
Tao Baobb10e582017-07-22 16:30:34 -0700115 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800116 std::cout << std::endl;
Steve Kondik50ac1c82013-11-24 21:40:09 -0800117
Alessandro Astone9062a0c2020-03-01 13:33:27 +0100118 // Create a boring /etc/fstab so tools like Busybox work
119 FILE* file = fopen("/etc/fstab", "w");
Steve Kondik50ac1c82013-11-24 21:40:09 -0800120 if (file) {
Alessandro Astone9062a0c2020-03-01 13:33:27 +0100121 for (auto& entry : fake_fstab) {
122 write_fstab_entry(entry, file);
123 }
Steve Kondik50ac1c82013-11-24 21:40:09 -0800124 fclose(file);
Alessandro Astone9062a0c2020-03-01 13:33:27 +0100125 } else {
126 LOG(ERROR) << "Unable to create /etc/fstab";
Steve Kondik50ac1c82013-11-24 21:40:09 -0800127 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700128}
129
Tao Bao3e18f2b2017-09-29 17:11:13 -0700130Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -0800131 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -0700132}
133
Tao Baoabb8f772015-07-30 14:43:27 -0700134// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800135int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
136 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137}
138
Yifan Hongd81b8e32018-12-17 14:29:06 -0800139int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -0700140 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800141 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -0700142}
143
Yifan Hongd81b8e32018-12-17 14:29:06 -0800144int ensure_path_unmounted(const std::string& path) {
145 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700146}
147
Simon Shieldseb476a02019-10-02 00:21:45 +1000148int ensure_volume_unmounted(const std::string& blk_device) {
149 android::fs_mgr::Fstab mounted_fstab;
150 if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounted_fstab)) {
151 LOG(ERROR) << "Failed to read /proc/mounts";
152 return -1;
153 }
154
155 /* find any entries with the volume */
156 for (auto& entry : mounted_fstab) {
157 if (entry.blk_device == blk_device) {
158 int result = umount(entry.mount_point.c_str());
159 if (result == -1) {
160 LOG(ERROR) << "Failed to unmount " << blk_device << " from " << entry.mount_point << ": "
161 << errno;
162 return -1;
163 }
164 }
165 }
166 return 0;
167}
168
Tao Bao3c00fac2017-07-22 16:46:54 -0700169static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800170 CHECK(!args.empty());
171 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -0700172
173 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800174 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700175 execv(argv[0], argv.data());
176 _exit(EXIT_FAILURE);
177 }
178
179 int status;
180 waitpid(child, &status, 0);
181 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
182 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
183 }
184 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700185}
186
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530187static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700188 struct stat buf;
189 int ret = fstat(fd, &buf);
190 if (ret) return 0;
191
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530192 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700193 if (S_ISREG(buf.st_mode)) {
194 computed_size = buf.st_size - reserve_len;
195 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530196 uint64_t block_device_size = get_block_device_size(fd);
197 if (block_device_size < reserve_len ||
198 block_device_size > std::numeric_limits<int64_t>::max()) {
199 computed_size = 0;
200 } else {
201 computed_size = block_device_size - reserve_len;
202 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700203 } else {
204 computed_size = 0;
205 }
206
207 return computed_size;
208}
209
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700210static FstabEntry* LocateFormattableEntry(const std::vector<FstabEntry*>& entries) {
211 if (entries.empty()) {
212 return nullptr;
213 }
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700214 FstabEntry* f2fs_entry = nullptr;
215 for (auto&& entry : entries) {
216 if (getpagesize() != 4096 && entry->fs_type == "f2fs") {
217 f2fs_entry = entry;
218 continue;
219 }
220 if (f2fs_entry) {
221 LOG(INFO) << "Skipping F2FS format for block device " << entry->blk_device << " @ "
222 << entry->mount_point
223 << " in non-4K mode for dev option enabled devices, "
224 "as these devices need to toggle between 4K/16K mode, and F2FS does "
225 "not support page_size != block_size configuration.";
226 }
227 return entry;
228 }
229 if (f2fs_entry) {
230 LOG(INFO) << "Using F2FS for " << f2fs_entry->blk_device << " @ " << f2fs_entry->mount_point
231 << " even though we are in non-4K mode. Device might require a data wipe after "
232 "going back to 4K mode, as F2FS does not support page_size != block_size";
233 }
234 return f2fs_entry;
235}
236
237bool WipeBlockDevice(const char* path) {
238 android::base::unique_fd fd(open(path, O_RDWR));
239 if (fd == -1) {
240 PLOG(ERROR) << "WipeBlockDevice: failed to open " << path;
241 return false;
242 }
243 int64_t device_size = get_file_size(fd.get(), 0);
244 if (device_size < 0) {
245 PLOG(ERROR) << "WipeBlockDevice: failed to determine size of " << device_size;
246 return false;
247 }
248 if (device_size == 0) {
249 PLOG(ERROR) << "WipeBlockDevice: block device " << device_size << " has 0 length, skip wiping";
250 return false;
251 }
252 if (!wipe_block_device(fd.get(), device_size)) {
253 return true;
254 }
255 PLOG(ERROR) << "Failed to wipe " << path;
256 return false;
257}
258
Kelvin Zhang73549942024-01-04 16:43:53 -0800259int format_volume(const std::string& volume, const std::string& directory,
260 std::string_view new_fstype) {
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700261 const auto entries = android::fs_mgr::GetEntriesForPath(&fstab, volume);
262 if (entries.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700263 LOG(ERROR) << "unknown volume \"" << volume << "\"";
264 return -1;
265 }
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700266
267 const FstabEntry* v = LocateFormattableEntry(entries);
268 if (v == nullptr) {
269 LOG(ERROR) << "Unable to find formattable entry for \"" << volume << "\"";
270 return -1;
271 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800272 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700273 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
274 return -1;
275 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800276 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700277 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
278 return -1;
279 }
Alessandro Astonef32ae702020-03-30 00:01:21 +0200280 if (ensure_volume_unmounted(v->blk_device) != 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700281 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
282 return -1;
283 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800284 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700285 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800286 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700287 }
288
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800289 bool needs_casefold = false;
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800290
291 if (volume == "/data") {
Martijn Coenen5a4a7ff2020-04-15 11:52:21 +0200292 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800293 }
294
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530295 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800296 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700297 length = v->length;
Eric Biggersa762e142021-11-08 16:45:49 -0800298 } else if (v->length < 0) {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800299 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700300 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530301 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700302 return -1;
303 }
Eric Biggersa762e142021-11-08 16:45:49 -0800304 length = get_file_size(fd.get(), -v->length);
Tao Bao3c00fac2017-07-22 16:46:54 -0700305 if (length <= 0) {
xunchang24788852019-03-22 16:08:52 -0700306 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700307 return -1;
308 }
309 }
310
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800311 // If the raw disk will be used as a metadata encrypted device mapper target,
Kelvin Zhangaa0a88d2024-01-13 21:25:15 +0000312 // next boot will do encrypt_in_place the raw disk. While fs_mgr mounts /data
313 // as RO to avoid write file operations before encrypt_inplace, this code path
314 // is not well tested so we would like to avoid it if possible. For safety,
315 // let vold do the formatting on boot for metadata encrypted devices, except
316 // when user specified a new fstype. Because init formats /data according
317 // to fstab, it's difficult to override the fstab in init.
318 if (!v->metadata_key_dir.empty() && length == 0 && new_fstype.empty()) {
319 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR));
320 if (fd == -1) {
321 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
322 return -1;
323 }
324 int64_t device_size = get_file_size(fd.get(), 0);
325 if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) {
326 LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size "
327 << device_size;
328 return 0;
329 }
330 }
Jaegeuk Kim80a1d8e2021-11-30 18:39:31 -0800331
Dan Shic89b4e42024-01-11 02:46:37 +0000332 if ((v->fs_type == "ext4" && new_fstype.empty()) || new_fstype == "ext4") {
Kelvin Zhang73549942024-01-04 16:43:53 -0800333 LOG(INFO) << "Formatting " << v->blk_device << " as ext4";
Tao Bao3c00fac2017-07-22 16:46:54 -0700334 static constexpr int kBlockSize = 4096;
335 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900336 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700337 };
338
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000339 // Following is added for Project ID's quota as they require wider inodes.
340 // The Quotas themselves are enabled by tune2fs on boot.
341 mke2fs_args.push_back("-I");
342 mke2fs_args.push_back("512");
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800343
Jaegeuk Kima8d36e12020-02-11 15:24:54 -0800344 if (v->fs_mgr_flags.ext_meta_csum) {
345 mke2fs_args.push_back("-O");
346 mke2fs_args.push_back("metadata_csum");
347 mke2fs_args.push_back("-O");
348 mke2fs_args.push_back("64bit");
349 mke2fs_args.push_back("-O");
350 mke2fs_args.push_back("extent");
351 }
352
Tao Bao3c00fac2017-07-22 16:46:54 -0700353 int raid_stride = v->logical_blk_size / kBlockSize;
354 int raid_stripe_width = v->erase_blk_size / kBlockSize;
355 // stride should be the max of 8KB and logical block size
356 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
357 raid_stride = 8192 / kBlockSize;
358 }
359 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
360 mke2fs_args.push_back("-E");
361 mke2fs_args.push_back(
362 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
363 }
364 mke2fs_args.push_back(v->blk_device);
365 if (length != 0) {
366 mke2fs_args.push_back(std::to_string(length / kBlockSize));
367 }
368
369 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800370 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700371 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900372 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700373 };
374 result = exec_cmd(e2fsdroid_args);
375 }
376
377 if (result != 0) {
378 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
379 return -1;
380 }
381 return 0;
382 }
383
384 // Has to be f2fs because we checked earlier.
Kelvin Zhang73549942024-01-04 16:43:53 -0800385 LOG(INFO) << "Formatting " << v->blk_device << " as f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700386 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700387 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800388 "/system/bin/make_f2fs",
389 "-g",
390 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700391 };
Shikha Malhotrad21e5162022-02-02 13:43:39 +0000392
393 make_f2fs_cmd.push_back("-O");
394 make_f2fs_cmd.push_back("project_quota,extra_attr");
395
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800396 if (needs_casefold) {
397 make_f2fs_cmd.push_back("-O");
398 make_f2fs_cmd.push_back("casefold");
399 make_f2fs_cmd.push_back("-C");
400 make_f2fs_cmd.push_back("utf8");
401 }
Jaegeuk Kim32106002020-01-14 11:00:37 -0800402 if (v->fs_mgr_flags.fs_compress) {
403 make_f2fs_cmd.push_back("-O");
404 make_f2fs_cmd.push_back("compression");
405 make_f2fs_cmd.push_back("-O");
406 make_f2fs_cmd.push_back("extra_attr");
407 }
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700408 make_f2fs_cmd.push_back("-b");
409 make_f2fs_cmd.push_back(std::to_string(getpagesize()));
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800410 make_f2fs_cmd.push_back(v->blk_device);
Jaegeuk Kim43582622018-04-02 13:37:35 -0700411 if (length >= kSectorSize) {
412 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700413 }
414
Tao Baoc674dfb2018-12-20 14:25:15 -0800415 if (exec_cmd(make_f2fs_cmd) != 0) {
Kelvin Zhang4b240f12024-07-30 11:09:52 -0700416 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device
417 << " wiping the block device to avoid leaving partially formatted data.";
418 WipeBlockDevice(v->blk_device.c_str());
Tao Bao3c00fac2017-07-22 16:46:54 -0700419 return -1;
420 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800421 if (!directory.empty()) {
422 std::vector<std::string> sload_f2fs_cmd = {
423 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
424 };
425 if (exec_cmd(sload_f2fs_cmd) != 0) {
426 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
427 return -1;
428 }
429 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700430 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800431}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700432
Yifan Hongd81b8e32018-12-17 14:29:06 -0800433int format_volume(const std::string& volume) {
Kelvin Zhang73549942024-01-04 16:43:53 -0800434 return format_volume(volume, "", "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800435}
436
Doug Zongker239ac6a2013-08-20 16:03:25 -0700437int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800438 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700439 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
440 return -1;
441 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800442 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700443 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800444 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700445 continue;
446 }
447
Yumi Yukimura7ebecd02024-08-04 00:58:07 +0800448 // We may load update package from virtiofs mount point.
449 if (entry.mount_point == "/mnt/vendor/shared") {
450 continue;
451 }
452
Yifan Hongd81b8e32018-12-17 14:29:06 -0800453 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
454 if (ensure_path_mounted(entry.mount_point) != 0) {
455 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700456 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700457 }
458 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800459 if (ensure_path_unmounted(entry.mount_point) != 0) {
460 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700461 return -1;
462 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700463 }
Tao Bao57130c42017-05-10 12:11:21 -0700464 }
465 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700466}
Tianjie Xu164c60a2019-05-15 13:59:39 -0700467
468bool HasCache() {
469 CHECK(!fstab.empty());
470 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
471 return has_cache;
472}
Erfan Abdif40ff082020-04-14 23:07:33 +0430473
aleasto157f92d2021-01-17 20:31:22 +0100474static bool logical_partitions_auto_mapped = false;
475
476void map_logical_partitions() {
477 if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
478 !logical_partitions_mapped()) {
479 std::string super_name = fs_mgr_get_super_partition_name();
480 if (!android::fs_mgr::CreateLogicalPartitions("/dev/block/by-name/" + super_name)) {
481 LOG(ERROR) << "Failed to map logical partitions";
482 } else {
483 logical_partitions_auto_mapped = true;
484 }
485 }
486}
487
488bool dm_find_system() {
489 auto rec = GetEntryForPath(&fstab, android::fs_mgr::GetSystemRoot());
490 if (!rec->fs_mgr_flags.logical) {
491 return false;
492 }
493 // If the fstab entry for system it's a path instead of a name, then it was already mapped
494 if (rec->blk_device[0] != '/') {
495 if (DeviceMapper::Instance().GetState(rec->blk_device) == DmDeviceState::INVALID) {
496 return false;
497 }
498 }
499 return true;
500}
501
Erfan Abdif40ff082020-04-14 23:07:33 +0430502bool logical_partitions_mapped() {
aleasto157f92d2021-01-17 20:31:22 +0100503 return android::fs_mgr::LogicalPartitionsMapped() || logical_partitions_auto_mapped ||
504 dm_find_system();
Erfan Abdif40ff082020-04-14 23:07:33 +0430505}