The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 17 | #include "recovery_utils/roots.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 18 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 20 | #include <stdint.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Tao Bao | ad774b2 | 2017-09-29 10:39:08 -0700 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 28 | #include <iostream> |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 33 | #include <android-base/properties.h> |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 36 | #include <cryptfs.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 37 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 38 | #include <fs_mgr.h> |
Yifan Hong | 0f339e2 | 2018-12-03 13:44:01 -0800 | [diff] [blame] | 39 | #include <fs_mgr/roots.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 40 | |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 41 | #include "otautil/sysutil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 42 | |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 43 | using android::fs_mgr::Fstab; |
| 44 | using android::fs_mgr::FstabEntry; |
| 45 | using android::fs_mgr::ReadDefaultFstab; |
| 46 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 47 | static Fstab fstab; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 48 | |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 49 | constexpr const char* CACHE_ROOT = "/cache"; |
| 50 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 51 | void load_volume_table() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 52 | if (!ReadDefaultFstab(&fstab)) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 53 | LOG(ERROR) << "Failed to read default fstab"; |
| 54 | return; |
| 55 | } |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 56 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 57 | fstab.emplace_back(FstabEntry{ |
Nick Desaulniers | 1cb510d | 2019-10-10 16:33:58 -0700 | [diff] [blame] | 58 | .blk_device = "ramdisk", |
| 59 | .mount_point = "/tmp", |
| 60 | .fs_type = "ramdisk", |
| 61 | .length = 0, |
| 62 | }); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 63 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 64 | std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl; |
| 65 | for (size_t i = 0; i < fstab.size(); ++i) { |
| 66 | const auto& entry = fstab[i]; |
| 67 | std::cout << " " << i << " " << entry.mount_point << " " |
| 68 | << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length |
| 69 | << std::endl; |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 70 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 71 | std::cout << std::endl; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 74 | Volume* volume_for_mount_point(const std::string& mount_point) { |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 75 | return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point); |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 78 | // Mount the volume specified by path at the given mount_point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 79 | int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) { |
| 80 | return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 83 | int ensure_path_mounted(const std::string& path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 84 | // Mount at the default mount point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 85 | return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1; |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 88 | int ensure_path_unmounted(const std::string& path) { |
| 89 | return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 92 | static int exec_cmd(const std::vector<std::string>& args) { |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 93 | CHECK(!args.empty()); |
| 94 | auto argv = StringVectorToNullTerminatedArray(args); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 95 | |
| 96 | pid_t child; |
George Burgess IV | 1cfb361 | 2018-02-17 17:48:45 -0800 | [diff] [blame] | 97 | if ((child = fork()) == 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 98 | execv(argv[0], argv.data()); |
| 99 | _exit(EXIT_FAILURE); |
| 100 | } |
| 101 | |
| 102 | int status; |
| 103 | waitpid(child, &status, 0); |
| 104 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 105 | LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status); |
| 106 | } |
| 107 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 110 | static int64_t get_file_size(int fd, uint64_t reserve_len) { |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 111 | struct stat buf; |
| 112 | int ret = fstat(fd, &buf); |
| 113 | if (ret) return 0; |
| 114 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 115 | int64_t computed_size; |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 116 | if (S_ISREG(buf.st_mode)) { |
| 117 | computed_size = buf.st_size - reserve_len; |
| 118 | } else if (S_ISBLK(buf.st_mode)) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 119 | uint64_t block_device_size = get_block_device_size(fd); |
| 120 | if (block_device_size < reserve_len || |
| 121 | block_device_size > std::numeric_limits<int64_t>::max()) { |
| 122 | computed_size = 0; |
| 123 | } else { |
| 124 | computed_size = block_device_size - reserve_len; |
| 125 | } |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 126 | } else { |
| 127 | computed_size = 0; |
| 128 | } |
| 129 | |
| 130 | return computed_size; |
| 131 | } |
| 132 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 133 | int format_volume(const std::string& volume, const std::string& directory) { |
| 134 | const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 135 | if (v == nullptr) { |
| 136 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
| 137 | return -1; |
| 138 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 139 | if (v->fs_type == "ramdisk") { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 140 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
| 141 | return -1; |
| 142 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 143 | if (v->mount_point != volume) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 144 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
| 145 | return -1; |
| 146 | } |
| 147 | if (ensure_path_unmounted(volume) != 0) { |
| 148 | LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; |
| 149 | return -1; |
| 150 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 151 | if (v->fs_type != "ext4" && v->fs_type != "f2fs") { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 152 | LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 153 | return -1; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 156 | bool needs_casefold = false; |
| 157 | bool needs_projid = false; |
| 158 | |
| 159 | if (volume == "/data") { |
| 160 | needs_casefold = android::base::GetBoolProperty("ro.emulated_storage.casefold", false); |
| 161 | needs_projid = android::base::GetBoolProperty("ro.emulated_storage.projid", false); |
| 162 | } |
| 163 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 164 | // If there's a key_loc that looks like a path, it should be a block device for storing encryption |
| 165 | // metadata. Wipe it too. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 166 | if (!v->key_loc.empty() && v->key_loc[0] == '/') { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 167 | LOG(INFO) << "Wiping " << v->key_loc; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 168 | int fd = open(v->key_loc.c_str(), O_WRONLY | O_CREAT, 0644); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 169 | if (fd == -1) { |
| 170 | PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc; |
| 171 | return -1; |
| 172 | } |
| 173 | wipe_block_device(fd, get_file_size(fd)); |
| 174 | close(fd); |
| 175 | } |
| 176 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 177 | int64_t length = 0; |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 178 | if (v->length > 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 179 | length = v->length; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 180 | } else if (v->length < 0 || v->key_loc == "footer") { |
| 181 | android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 182 | if (fd == -1) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 183 | PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 184 | return -1; |
| 185 | } |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 186 | length = get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 187 | if (length <= 0) { |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 188 | LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | } |
| 192 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 193 | if (v->fs_type == "ext4") { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 194 | static constexpr int kBlockSize = 4096; |
| 195 | std::vector<std::string> mke2fs_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 196 | "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 199 | // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs on boot. |
| 200 | if (needs_projid) { |
| 201 | mke2fs_args.push_back("-I"); |
| 202 | mke2fs_args.push_back("512"); |
| 203 | } |
| 204 | |
Jaegeuk Kim | a8d36e1 | 2020-02-11 15:24:54 -0800 | [diff] [blame] | 205 | if (v->fs_mgr_flags.ext_meta_csum) { |
| 206 | mke2fs_args.push_back("-O"); |
| 207 | mke2fs_args.push_back("metadata_csum"); |
| 208 | mke2fs_args.push_back("-O"); |
| 209 | mke2fs_args.push_back("64bit"); |
| 210 | mke2fs_args.push_back("-O"); |
| 211 | mke2fs_args.push_back("extent"); |
| 212 | } |
| 213 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 214 | int raid_stride = v->logical_blk_size / kBlockSize; |
| 215 | int raid_stripe_width = v->erase_blk_size / kBlockSize; |
| 216 | // stride should be the max of 8KB and logical block size |
| 217 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 218 | raid_stride = 8192 / kBlockSize; |
| 219 | } |
| 220 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 221 | mke2fs_args.push_back("-E"); |
| 222 | mke2fs_args.push_back( |
| 223 | android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); |
| 224 | } |
| 225 | mke2fs_args.push_back(v->blk_device); |
| 226 | if (length != 0) { |
| 227 | mke2fs_args.push_back(std::to_string(length / kBlockSize)); |
| 228 | } |
| 229 | |
| 230 | int result = exec_cmd(mke2fs_args); |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 231 | if (result == 0 && !directory.empty()) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 232 | std::vector<std::string> e2fsdroid_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 233 | "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device, |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 234 | }; |
| 235 | result = exec_cmd(e2fsdroid_args); |
| 236 | } |
| 237 | |
| 238 | if (result != 0) { |
| 239 | PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; |
| 240 | return -1; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | // Has to be f2fs because we checked earlier. |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 246 | static constexpr int kSectorSize = 4096; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 247 | std::vector<std::string> make_f2fs_cmd = { |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 248 | "/system/bin/make_f2fs", |
| 249 | "-g", |
| 250 | "android", |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 251 | }; |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 252 | if (needs_projid) { |
| 253 | make_f2fs_cmd.push_back("-O"); |
| 254 | make_f2fs_cmd.push_back("project_quota,extra_attr"); |
| 255 | } |
| 256 | if (needs_casefold) { |
| 257 | make_f2fs_cmd.push_back("-O"); |
| 258 | make_f2fs_cmd.push_back("casefold"); |
| 259 | make_f2fs_cmd.push_back("-C"); |
| 260 | make_f2fs_cmd.push_back("utf8"); |
| 261 | } |
Jaegeuk Kim | 3210600 | 2020-01-14 11:00:37 -0800 | [diff] [blame] | 262 | if (v->fs_mgr_flags.fs_compress) { |
| 263 | make_f2fs_cmd.push_back("-O"); |
| 264 | make_f2fs_cmd.push_back("compression"); |
| 265 | make_f2fs_cmd.push_back("-O"); |
| 266 | make_f2fs_cmd.push_back("extra_attr"); |
| 267 | } |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 268 | make_f2fs_cmd.push_back(v->blk_device); |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 269 | if (length >= kSectorSize) { |
| 270 | make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 273 | if (exec_cmd(make_f2fs_cmd) != 0) { |
| 274 | PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 275 | return -1; |
| 276 | } |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 277 | if (!directory.empty()) { |
| 278 | std::vector<std::string> sload_f2fs_cmd = { |
| 279 | "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device, |
| 280 | }; |
| 281 | if (exec_cmd(sload_f2fs_cmd) != 0) { |
| 282 | PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device; |
| 283 | return -1; |
| 284 | } |
| 285 | } |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 286 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 287 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 288 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 289 | int format_volume(const std::string& volume) { |
| 290 | return format_volume(volume, ""); |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 293 | int setup_install_mounts() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 294 | if (fstab.empty()) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 295 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 296 | return -1; |
| 297 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 298 | for (const FstabEntry& entry : fstab) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 299 | // We don't want to do anything with "/". |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 300 | if (entry.mount_point == "/") { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 301 | continue; |
| 302 | } |
| 303 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 304 | if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") { |
| 305 | if (ensure_path_mounted(entry.mount_point) != 0) { |
| 306 | LOG(ERROR) << "Failed to mount " << entry.mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 307 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 308 | } |
| 309 | } else { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 310 | if (ensure_path_unmounted(entry.mount_point) != 0) { |
| 311 | LOG(ERROR) << "Failed to unmount " << entry.mount_point; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 312 | return -1; |
| 313 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 314 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 315 | } |
| 316 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 317 | } |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 318 | |
| 319 | bool HasCache() { |
| 320 | CHECK(!fstab.empty()); |
| 321 | static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 322 | return has_cache; |
| 323 | } |