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