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> |
Simon Shields | dbc4d66 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 27 | #include <sys/mount.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 29 | #include <iostream> |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 34 | #include <android-base/properties.h> |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 37 | #include <blkid/blkid.h> |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 38 | #include <cryptfs.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 39 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 40 | #include <fs_mgr.h> |
Yifan Hong | 0f339e2 | 2018-12-03 13:44:01 -0800 | [diff] [blame] | 41 | #include <fs_mgr/roots.h> |
Erfan Abdi | a24643e | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 42 | #include <fs_mgr_dm_linear.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 43 | |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 44 | #include "otautil/sysutil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 45 | |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 46 | using android::fs_mgr::Fstab; |
| 47 | using android::fs_mgr::FstabEntry; |
| 48 | using android::fs_mgr::ReadDefaultFstab; |
aleasto | b4eca16 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 49 | using android::dm::DeviceMapper; |
| 50 | using android::dm::DmDeviceState; |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 51 | |
Steve Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 52 | static 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 Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 62 | static Fstab fstab; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 64 | constexpr const char* CACHE_ROOT = "/cache"; |
| 65 | |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 66 | FstabEntry* 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 Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 85 | void load_volume_table() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 86 | if (!ReadDefaultFstab(&fstab)) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 87 | LOG(ERROR) << "Failed to read default fstab"; |
| 88 | return; |
| 89 | } |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 90 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 91 | fstab.emplace_back(FstabEntry{ |
Nick Desaulniers | 1cb510d | 2019-10-10 16:33:58 -0700 | [diff] [blame] | 92 | .blk_device = "ramdisk", |
| 93 | .mount_point = "/tmp", |
| 94 | .fs_type = "ramdisk", |
| 95 | .length = 0, |
| 96 | }); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 97 | |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 98 | Fstab fake_fstab; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 99 | 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 Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 105 | |
| 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 Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 113 | } |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 114 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 115 | std::cout << std::endl; |
Steve Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 116 | |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 117 | // Create a boring /etc/fstab so tools like Busybox work |
| 118 | FILE* file = fopen("/etc/fstab", "w"); |
Steve Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 119 | if (file) { |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 120 | for (auto& entry : fake_fstab) { |
| 121 | write_fstab_entry(entry, file); |
| 122 | } |
Steve Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 123 | fclose(file); |
Alessandro Astone | 7a3ce38 | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 124 | } else { |
| 125 | LOG(ERROR) << "Unable to create /etc/fstab"; |
Steve Kondik | bed2b29 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 126 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 129 | Volume* volume_for_mount_point(const std::string& mount_point) { |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 130 | return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point); |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 133 | // Mount the volume specified by path at the given mount_point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 134 | int 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 138 | int ensure_path_mounted(const std::string& path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 139 | // Mount at the default mount point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 140 | return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1; |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 143 | int ensure_path_unmounted(const std::string& path) { |
| 144 | return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Simon Shields | dbc4d66 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 147 | int 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 Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 168 | static int exec_cmd(const std::vector<std::string>& args) { |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 169 | CHECK(!args.empty()); |
| 170 | auto argv = StringVectorToNullTerminatedArray(args); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 171 | |
| 172 | pid_t child; |
George Burgess IV | 1cfb361 | 2018-02-17 17:48:45 -0800 | [diff] [blame] | 173 | if ((child = fork()) == 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 174 | 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 Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 186 | static int64_t get_file_size(int fd, uint64_t reserve_len) { |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 187 | struct stat buf; |
| 188 | int ret = fstat(fd, &buf); |
| 189 | if (ret) return 0; |
| 190 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 191 | int64_t computed_size; |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 192 | if (S_ISREG(buf.st_mode)) { |
| 193 | computed_size = buf.st_size - reserve_len; |
| 194 | } else if (S_ISBLK(buf.st_mode)) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 195 | 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 Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 202 | } else { |
| 203 | computed_size = 0; |
| 204 | } |
| 205 | |
| 206 | return computed_size; |
| 207 | } |
| 208 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 209 | int format_volume(const std::string& volume, const std::string& directory) { |
| 210 | const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 211 | if (v == nullptr) { |
| 212 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
| 213 | return -1; |
| 214 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 215 | if (v->fs_type == "ramdisk") { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 216 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
| 217 | return -1; |
| 218 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 219 | if (v->mount_point != volume) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 220 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
| 221 | return -1; |
| 222 | } |
Alessandro Astone | 606b3a7 | 2020-03-30 00:01:21 +0200 | [diff] [blame] | 223 | if (ensure_volume_unmounted(v->blk_device) != 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 224 | LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; |
| 225 | return -1; |
| 226 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 227 | if (v->fs_type != "ext4" && v->fs_type != "f2fs") { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 228 | 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] | 229 | return -1; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 232 | bool needs_casefold = false; |
| 233 | bool needs_projid = false; |
| 234 | |
| 235 | if (volume == "/data") { |
Martijn Coenen | 5a4a7ff | 2020-04-15 11:52:21 +0200 | [diff] [blame] | 236 | needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
| 237 | needs_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false); |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 240 | // If there's a key_loc that looks like a path, it should be a block device for storing encryption |
| 241 | // metadata. Wipe it too. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 242 | if (!v->key_loc.empty() && v->key_loc[0] == '/') { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 243 | LOG(INFO) << "Wiping " << v->key_loc; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 244 | 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] | 245 | if (fd == -1) { |
| 246 | PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc; |
| 247 | return -1; |
| 248 | } |
| 249 | wipe_block_device(fd, get_file_size(fd)); |
| 250 | close(fd); |
| 251 | } |
| 252 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 253 | int64_t length = 0; |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 254 | if (v->length > 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 255 | length = v->length; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 256 | } else if (v->length < 0 || v->key_loc == "footer") { |
| 257 | 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] | 258 | if (fd == -1) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 259 | PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 260 | return -1; |
| 261 | } |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 262 | 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] | 263 | if (length <= 0) { |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 264 | LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 265 | return -1; |
| 266 | } |
| 267 | } |
| 268 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 269 | if (v->fs_type == "ext4") { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 270 | static constexpr int kBlockSize = 4096; |
| 271 | std::vector<std::string> mke2fs_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 272 | "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 275 | // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs on boot. |
| 276 | if (needs_projid) { |
| 277 | mke2fs_args.push_back("-I"); |
| 278 | mke2fs_args.push_back("512"); |
| 279 | } |
| 280 | |
Jaegeuk Kim | a8d36e1 | 2020-02-11 15:24:54 -0800 | [diff] [blame] | 281 | if (v->fs_mgr_flags.ext_meta_csum) { |
| 282 | mke2fs_args.push_back("-O"); |
| 283 | mke2fs_args.push_back("metadata_csum"); |
| 284 | mke2fs_args.push_back("-O"); |
| 285 | mke2fs_args.push_back("64bit"); |
| 286 | mke2fs_args.push_back("-O"); |
| 287 | mke2fs_args.push_back("extent"); |
| 288 | } |
| 289 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 290 | int raid_stride = v->logical_blk_size / kBlockSize; |
| 291 | int raid_stripe_width = v->erase_blk_size / kBlockSize; |
| 292 | // stride should be the max of 8KB and logical block size |
| 293 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 294 | raid_stride = 8192 / kBlockSize; |
| 295 | } |
| 296 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 297 | mke2fs_args.push_back("-E"); |
| 298 | mke2fs_args.push_back( |
| 299 | android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); |
| 300 | } |
| 301 | mke2fs_args.push_back(v->blk_device); |
| 302 | if (length != 0) { |
| 303 | mke2fs_args.push_back(std::to_string(length / kBlockSize)); |
| 304 | } |
| 305 | |
| 306 | int result = exec_cmd(mke2fs_args); |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 307 | if (result == 0 && !directory.empty()) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 308 | std::vector<std::string> e2fsdroid_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 309 | "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device, |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 310 | }; |
| 311 | result = exec_cmd(e2fsdroid_args); |
| 312 | } |
| 313 | |
| 314 | if (result != 0) { |
| 315 | PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; |
| 316 | return -1; |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | // Has to be f2fs because we checked earlier. |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 322 | static constexpr int kSectorSize = 4096; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 323 | std::vector<std::string> make_f2fs_cmd = { |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 324 | "/system/bin/make_f2fs", |
| 325 | "-g", |
| 326 | "android", |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 327 | }; |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 328 | if (needs_projid) { |
| 329 | make_f2fs_cmd.push_back("-O"); |
| 330 | make_f2fs_cmd.push_back("project_quota,extra_attr"); |
| 331 | } |
| 332 | if (needs_casefold) { |
| 333 | make_f2fs_cmd.push_back("-O"); |
| 334 | make_f2fs_cmd.push_back("casefold"); |
| 335 | make_f2fs_cmd.push_back("-C"); |
| 336 | make_f2fs_cmd.push_back("utf8"); |
| 337 | } |
Jaegeuk Kim | 3210600 | 2020-01-14 11:00:37 -0800 | [diff] [blame] | 338 | if (v->fs_mgr_flags.fs_compress) { |
| 339 | make_f2fs_cmd.push_back("-O"); |
| 340 | make_f2fs_cmd.push_back("compression"); |
| 341 | make_f2fs_cmd.push_back("-O"); |
| 342 | make_f2fs_cmd.push_back("extra_attr"); |
| 343 | } |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 344 | make_f2fs_cmd.push_back(v->blk_device); |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 345 | if (length >= kSectorSize) { |
| 346 | make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 349 | if (exec_cmd(make_f2fs_cmd) != 0) { |
| 350 | PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 351 | return -1; |
| 352 | } |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 353 | if (!directory.empty()) { |
| 354 | std::vector<std::string> sload_f2fs_cmd = { |
| 355 | "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device, |
| 356 | }; |
| 357 | if (exec_cmd(sload_f2fs_cmd) != 0) { |
| 358 | PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device; |
| 359 | return -1; |
| 360 | } |
| 361 | } |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 362 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 363 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 364 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 365 | int format_volume(const std::string& volume) { |
| 366 | return format_volume(volume, ""); |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 369 | int setup_install_mounts() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 370 | if (fstab.empty()) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 371 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 372 | return -1; |
| 373 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 374 | for (const FstabEntry& entry : fstab) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 375 | // We don't want to do anything with "/". |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 376 | if (entry.mount_point == "/") { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 377 | continue; |
| 378 | } |
| 379 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 380 | if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") { |
| 381 | if (ensure_path_mounted(entry.mount_point) != 0) { |
| 382 | LOG(ERROR) << "Failed to mount " << entry.mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 383 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 384 | } |
| 385 | } else { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 386 | if (ensure_path_unmounted(entry.mount_point) != 0) { |
| 387 | LOG(ERROR) << "Failed to unmount " << entry.mount_point; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 388 | return -1; |
| 389 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 390 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 391 | } |
| 392 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 393 | } |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 394 | |
| 395 | bool HasCache() { |
| 396 | CHECK(!fstab.empty()); |
| 397 | static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 398 | return has_cache; |
| 399 | } |
Erfan Abdi | a24643e | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 400 | |
aleasto | b4eca16 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 401 | static bool logical_partitions_auto_mapped = false; |
| 402 | |
| 403 | void map_logical_partitions() { |
| 404 | if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) && |
| 405 | !logical_partitions_mapped()) { |
| 406 | std::string super_name = fs_mgr_get_super_partition_name(); |
| 407 | if (!android::fs_mgr::CreateLogicalPartitions("/dev/block/by-name/" + super_name)) { |
| 408 | LOG(ERROR) << "Failed to map logical partitions"; |
| 409 | } else { |
| 410 | logical_partitions_auto_mapped = true; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | bool dm_find_system() { |
| 416 | auto rec = GetEntryForPath(&fstab, android::fs_mgr::GetSystemRoot()); |
| 417 | if (!rec->fs_mgr_flags.logical) { |
| 418 | return false; |
| 419 | } |
| 420 | // If the fstab entry for system it's a path instead of a name, then it was already mapped |
| 421 | if (rec->blk_device[0] != '/') { |
| 422 | if (DeviceMapper::Instance().GetState(rec->blk_device) == DmDeviceState::INVALID) { |
| 423 | return false; |
| 424 | } |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
Erfan Abdi | a24643e | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 429 | bool logical_partitions_mapped() { |
aleasto | b4eca16 | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 430 | return android::fs_mgr::LogicalPartitionsMapped() || logical_partitions_auto_mapped || |
| 431 | dm_find_system(); |
Erfan Abdi | a24643e | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 432 | } |