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 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 17 | #include "roots.h" |
| 18 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/mount.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 24 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 26 | #include <ctype.h> |
Doug Zongker | f39989a | 2013-12-11 15:40:28 -0800 | [diff] [blame] | 27 | #include <fcntl.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Jin Qian | ac31808 | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 30 | #include <android-base/properties.h> |
| 31 | #include <android-base/stringprintf.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 32 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 33 | #include <fs_mgr.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 35 | #include "common.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 36 | #include "mounts.h" |
Doug Zongker | f39989a | 2013-12-11 15:40:28 -0800 | [diff] [blame] | 37 | #include "cryptfs.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 38 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 39 | static struct fstab *fstab = NULL; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 40 | |
Kenny Root | 41dda82 | 2012-03-30 20:48:34 -0700 | [diff] [blame] | 41 | extern struct selabel_handle *sehandle; |
Stephen Smalley | 779701d | 2012-02-09 14:13:23 -0500 | [diff] [blame] | 42 | |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 43 | void load_volume_table() |
| 44 | { |
| 45 | int i; |
| 46 | int ret; |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 47 | |
Bowgo Tsai | 84a0648 | 2017-03-29 15:13:58 +0800 | [diff] [blame] | 48 | fstab = fs_mgr_read_fstab_default(); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 49 | if (!fstab) { |
Bowgo Tsai | 84a0648 | 2017-03-29 15:13:58 +0800 | [diff] [blame] | 50 | LOG(ERROR) << "failed to read default fstab"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 51 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 52 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 53 | |
Sasha Levitskiy | 85ef47d | 2014-04-10 17:11:34 -0700 | [diff] [blame] | 54 | ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk"); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 55 | if (ret < 0 ) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 56 | LOG(ERROR) << "failed to add /tmp entry to fstab"; |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 57 | fs_mgr_free_fstab(fstab); |
| 58 | fstab = NULL; |
| 59 | return; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 60 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 61 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 62 | printf("recovery filesystem table\n"); |
| 63 | printf("=========================\n"); |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 64 | for (i = 0; i < fstab->num_entries; ++i) { |
| 65 | Volume* v = &fstab->recs[i]; |
| 66 | printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, |
| 67 | v->blk_device, v->length); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 68 | } |
| 69 | printf("\n"); |
| 70 | } |
| 71 | |
| 72 | Volume* volume_for_path(const char* path) { |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 73 | return fs_mgr_get_entry_for_mount_point(fstab, path); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 76 | // Mount the volume specified by path at the given mount_point. |
| 77 | int ensure_path_mounted_at(const char* path, const char* mount_point) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 78 | Volume* v = volume_for_path(path); |
| 79 | if (v == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 80 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 81 | return -1; |
| 82 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 83 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 84 | // the ramdisk is always mounted. |
| 85 | return 0; |
| 86 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 88 | if (!scan_mounted_volumes()) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 89 | LOG(ERROR) << "failed to scan mounted volumes"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 90 | return -1; |
Doug Zongker | 23ceeea | 2010-07-08 17:27:55 -0700 | [diff] [blame] | 91 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 93 | if (!mount_point) { |
| 94 | mount_point = v->mount_point; |
| 95 | } |
| 96 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 97 | MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 98 | if (mv) { |
| 99 | // volume is already mounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 100 | return 0; |
| 101 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 102 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 103 | mkdir(mount_point, 0755); // in case it doesn't already exist |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 105 | if (strcmp(v->fs_type, "ext4") == 0 || |
Mohamad Ayyash | 522ea72 | 2015-06-29 18:57:14 -0700 | [diff] [blame] | 106 | strcmp(v->fs_type, "squashfs") == 0 || |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 107 | strcmp(v->fs_type, "vfat") == 0) { |
Johan Harvyl | 29dd6b6 | 2016-08-08 12:37:56 +0200 | [diff] [blame] | 108 | int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 109 | if (result == -1 && fs_mgr_is_formattable(v)) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 110 | LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno) |
| 111 | << ") , formatting....."; |
Johan Harvyl | 29dd6b6 | 2016-08-08 12:37:56 +0200 | [diff] [blame] | 112 | bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer"); |
| 113 | if (fs_mgr_do_format(v, crypt_footer) == 0) { |
| 114 | result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options); |
| 115 | } else { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 116 | PLOG(ERROR) << "failed to format " << mount_point; |
Johan Harvyl | 29dd6b6 | 2016-08-08 12:37:56 +0200 | [diff] [blame] | 117 | return -1; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (result == -1) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 122 | PLOG(ERROR) << "failed to mount " << mount_point; |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | return 0; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 128 | LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 132 | int ensure_path_mounted(const char* path) { |
| 133 | // Mount at the default mount point. |
| 134 | return ensure_path_mounted_at(path, nullptr); |
| 135 | } |
| 136 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 137 | int ensure_path_unmounted(const char* path) { |
| 138 | Volume* v = volume_for_path(path); |
| 139 | if (v == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 140 | LOG(ERROR) << "unknown volume for path [" << path << "]"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 143 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 144 | // the ramdisk is always mounted; you can't unmount it. |
| 145 | return -1; |
| 146 | } |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 147 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 148 | if (!scan_mounted_volumes()) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 149 | LOG(ERROR) << "failed to scan mounted volumes"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 153 | MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 154 | if (mv == NULL) { |
| 155 | // volume is already unmounted |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 159 | return unmount_mounted_volume(mv); |
| 160 | } |
| 161 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 162 | static int exec_cmd(const char* path, char* const argv[]) { |
| 163 | int status; |
| 164 | pid_t child; |
| 165 | if ((child = vfork()) == 0) { |
| 166 | execv(path, argv); |
Tao Bao | 3da8801 | 2017-02-03 13:09:23 -0800 | [diff] [blame] | 167 | _exit(EXIT_FAILURE); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 168 | } |
| 169 | waitpid(child, &status, 0); |
| 170 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 171 | LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 172 | } |
| 173 | return WEXITSTATUS(status); |
| 174 | } |
| 175 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 176 | int format_volume(const char* volume, const char* directory) { |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 177 | Volume* v = volume_for_path(volume); |
| 178 | if (v == NULL) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 179 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 180 | return -1; |
| 181 | } |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 182 | if (strcmp(v->fs_type, "ramdisk") == 0) { |
| 183 | // you can't format the ramdisk. |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 184 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
Doug Zongker | c18eeb8 | 2010-09-21 16:49:26 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 187 | if (strcmp(v->mount_point, volume) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 188 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | if (ensure_path_unmounted(volume) != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 193 | LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\""; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 194 | return -1; |
| 195 | } |
| 196 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 197 | if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) { |
Doug Zongker | f39989a | 2013-12-11 15:40:28 -0800 | [diff] [blame] | 198 | // if there's a key_loc that looks like a path, it should be a |
| 199 | // block device for storing encryption metadata. wipe it too. |
| 200 | if (v->key_loc != NULL && v->key_loc[0] == '/') { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 201 | LOG(INFO) << "wiping " << v->key_loc; |
Doug Zongker | f39989a | 2013-12-11 15:40:28 -0800 | [diff] [blame] | 202 | int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644); |
| 203 | if (fd < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 204 | LOG(ERROR) << "format_volume: failed to open " << v->key_loc; |
Doug Zongker | f39989a | 2013-12-11 15:40:28 -0800 | [diff] [blame] | 205 | return -1; |
| 206 | } |
| 207 | wipe_block_device(fd, get_file_size(fd)); |
| 208 | close(fd); |
| 209 | } |
| 210 | |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 211 | ssize_t length = 0; |
| 212 | if (v->length != 0) { |
| 213 | length = v->length; |
| 214 | } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) { |
| 215 | length = -CRYPT_FOOTER_OFFSET; |
| 216 | } |
| 217 | int result; |
| 218 | if (strcmp(v->fs_type, "ext4") == 0) { |
Jin Qian | ac31808 | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 219 | static constexpr int block_size = 4096; |
| 220 | int raid_stride = v->logical_blk_size / block_size; |
| 221 | int raid_stripe_width = v->erase_blk_size / block_size; |
| 222 | |
| 223 | // stride should be the max of 8kb and logical block size |
| 224 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 225 | raid_stride = 8192 / block_size; |
| 226 | } |
| 227 | |
| 228 | const char* mke2fs_argv[] = { "/sbin/mke2fs_static", |
| 229 | "-F", |
| 230 | "-t", |
| 231 | "ext4", |
| 232 | "-b", |
| 233 | nullptr, |
| 234 | nullptr, |
| 235 | nullptr, |
| 236 | nullptr, |
| 237 | nullptr, |
| 238 | nullptr }; |
| 239 | |
| 240 | int i = 5; |
| 241 | std::string block_size_str = std::to_string(block_size); |
| 242 | mke2fs_argv[i++] = block_size_str.c_str(); |
| 243 | |
| 244 | std::string ext_args; |
| 245 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 246 | ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, |
| 247 | raid_stripe_width); |
| 248 | mke2fs_argv[i++] = "-E"; |
| 249 | mke2fs_argv[i++] = ext_args.c_str(); |
| 250 | } |
| 251 | |
| 252 | mke2fs_argv[i++] = v->blk_device; |
| 253 | |
| 254 | std::string size_str = std::to_string(length / block_size); |
| 255 | if (length != 0) { |
| 256 | mke2fs_argv[i++] = size_str.c_str(); |
| 257 | } |
| 258 | |
| 259 | result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv)); |
| 260 | if (result == 0 && directory != nullptr) { |
| 261 | const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static", |
| 262 | "-e", |
Jin Qian | ac31808 | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 263 | "-f", |
| 264 | directory, |
| 265 | "-a", |
| 266 | volume, |
| 267 | v->blk_device, |
| 268 | nullptr }; |
| 269 | |
| 270 | result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv)); |
Tao Bao | 7af933b | 2017-07-11 16:45:04 -0700 | [diff] [blame] | 271 | } |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 272 | } else { /* Has to be f2fs because we checked earlier. */ |
| 273 | if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 274 | LOG(ERROR) << "format_volume: crypt footer + negative length (" << length |
| 275 | << ") not supported on " << v->fs_type; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 276 | return -1; |
| 277 | } |
| 278 | if (length < 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 279 | LOG(ERROR) << "format_volume: negative length (" << length |
| 280 | << ") not supported on " << v->fs_type; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 281 | return -1; |
| 282 | } |
Jin Qian | adeb41a | 2017-04-28 16:15:13 -0700 | [diff] [blame] | 283 | char *num_sectors = nullptr; |
| 284 | if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 285 | LOG(ERROR) << "format_volume: failed to create " << v->fs_type |
| 286 | << " command for " << v->blk_device; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 287 | return -1; |
| 288 | } |
| 289 | const char *f2fs_path = "/sbin/mkfs.f2fs"; |
Jin Qian | adeb41a | 2017-04-28 16:15:13 -0700 | [diff] [blame] | 290 | const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr}; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 291 | |
| 292 | result = exec_cmd(f2fs_path, (char* const*)f2fs_argv); |
| 293 | free(num_sectors); |
| 294 | } |
| 295 | if (result != 0) { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 296 | PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device; |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 297 | return -1; |
| 298 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 299 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 300 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 301 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 302 | 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] | 303 | return -1; |
| 304 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 305 | |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 306 | int format_volume(const char* volume) { |
| 307 | return format_volume(volume, NULL); |
| 308 | } |
| 309 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 310 | int setup_install_mounts() { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 311 | if (fstab == nullptr) { |
| 312 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 313 | return -1; |
| 314 | } |
| 315 | for (int i = 0; i < fstab->num_entries; ++i) { |
| 316 | const Volume* v = fstab->recs + i; |
| 317 | |
| 318 | // We don't want to do anything with "/". |
| 319 | if (strcmp(v->mount_point, "/") == 0) { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) { |
| 324 | if (ensure_path_mounted(v->mount_point) != 0) { |
| 325 | LOG(ERROR) << "failed to mount " << v->mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 326 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 327 | } |
| 328 | } else { |
| 329 | if (ensure_path_unmounted(v->mount_point) != 0) { |
| 330 | LOG(ERROR) << "failed to unmount " << v->mount_point; |
| 331 | return -1; |
| 332 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 333 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 334 | } |
| 335 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 336 | } |