| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | * |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 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 | 018ef1b | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 17 | #include "ext4_utils/make_ext4fs.h" |
| Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame] | 18 | |
| Tao Bao | 018ef1b | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 19 | #ifndef _GNU_SOURCE |
| 20 | #define _GNU_SOURCE |
| 21 | #endif |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 22 | |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 23 | #include <assert.h> |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 24 | #include <dirent.h> |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 27 | #include <libgen.h> |
| Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <sys/types.h> |
| Tao Bao | 018ef1b | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | |
| 35 | #include <sparse/sparse.h> |
| 36 | |
| 37 | #include "allocate.h" |
| 38 | #include "contents.h" |
| 39 | #include "ext4_utils/ext4_utils.h" |
| 40 | #include "ext4_utils/wipe.h" |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 41 | |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 42 | #ifdef _WIN32 |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 43 | |
| 44 | #include <winsock2.h> |
| 45 | |
| 46 | /* These match the Linux definitions of these flags. |
| 47 | L_xx is defined to avoid conflicting with the win32 versions. |
| 48 | */ |
| Dan Albert | 8797850 | 2016-02-02 15:35:33 -0800 | [diff] [blame] | 49 | #undef S_IRWXU |
| 50 | #undef S_IRGRP |
| 51 | #undef S_IWGRP |
| 52 | #undef S_IXGRP |
| 53 | #undef S_IRWXG |
| 54 | #undef S_IROTH |
| 55 | #undef S_IWOTH |
| 56 | #undef S_IXOTH |
| 57 | #undef S_IRWXO |
| 58 | #undef S_ISUID |
| 59 | #undef S_ISGID |
| 60 | #undef S_ISVTX |
| 61 | |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 62 | #define L_S_IRUSR 00400 |
| 63 | #define L_S_IWUSR 00200 |
| 64 | #define L_S_IXUSR 00100 |
| 65 | #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR) |
| 66 | #define S_IRGRP 00040 |
| 67 | #define S_IWGRP 00020 |
| 68 | #define S_IXGRP 00010 |
| 69 | #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP) |
| 70 | #define S_IROTH 00004 |
| 71 | #define S_IWOTH 00002 |
| 72 | #define S_IXOTH 00001 |
| 73 | #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH) |
| 74 | #define S_ISUID 0004000 |
| 75 | #define S_ISGID 0002000 |
| 76 | #define S_ISVTX 0001000 |
| 77 | |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 78 | #else |
| 79 | |
| Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 80 | #include <selinux/selinux.h> |
| 81 | #include <selinux/label.h> |
| Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 82 | |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 83 | #define O_BINARY 0 |
| 84 | |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 85 | #endif |
| 86 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 87 | #define MAX_PATH 4096 |
| 88 | #define MAX_BLK_MAPPING_STR 1000 |
| 89 | |
| 90 | const int blk_file_major_ver = 1; |
| 91 | const int blk_file_minor_ver = 0; |
| 92 | const char *blk_file_header_fmt = "Base EXT4 version %d.%d"; |
| 93 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 94 | /* TODO: Not implemented: |
| 95 | Allocating blocks in the same block group as the file inode |
| 96 | Hash or binary tree directories |
| Colin Cross | 7a8bee7 | 2010-06-20 14:53:14 -0700 | [diff] [blame] | 97 | Special files: sockets, devices, fifos |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 98 | */ |
| 99 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 100 | static int filter_dot(const struct dirent *d) |
| 101 | { |
| 102 | return (strcmp(d->d_name, "..") && strcmp(d->d_name, ".")); |
| 103 | } |
| 104 | |
| Stephen Smalley | 7907ac7 | 2014-04-25 14:57:55 -0400 | [diff] [blame] | 105 | static u32 build_default_directory_structure(const char *dir_path, |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 106 | struct selabel_handle *sehnd) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 107 | { |
| 108 | u32 inode; |
| 109 | u32 root_inode; |
| 110 | struct dentry dentries = { |
| 111 | .filename = "lost+found", |
| 112 | .file_type = EXT4_FT_DIR, |
| 113 | .mode = S_IRWXU, |
| 114 | .uid = 0, |
| Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 115 | .gid = 0, |
| 116 | .mtime = 0, |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 117 | }; |
| 118 | root_inode = make_directory(0, 1, &dentries, 1); |
| 119 | inode = make_directory(root_inode, 0, NULL, 0); |
| 120 | *dentries.inode = inode; |
| Ken Sumrall | 75249ed | 2010-08-13 16:04:49 -0700 | [diff] [blame] | 121 | inode_set_permissions(inode, dentries.mode, |
| 122 | dentries.uid, dentries.gid, dentries.mtime); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 123 | |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 124 | #ifndef _WIN32 |
| Stephen Smalley | 7907ac7 | 2014-04-25 14:57:55 -0400 | [diff] [blame] | 125 | if (sehnd) { |
| 126 | char *path = NULL; |
| 127 | char *secontext = NULL; |
| 128 | |
| 129 | asprintf(&path, "%slost+found", dir_path); |
| 130 | if (selabel_lookup(sehnd, &secontext, path, S_IFDIR) < 0) { |
| 131 | error("cannot lookup security context for %s", path); |
| 132 | } else { |
| 133 | inode_set_selinux(inode, secontext); |
| 134 | freecon(secontext); |
| 135 | } |
| 136 | free(path); |
| 137 | } |
| 138 | #endif |
| 139 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 140 | return root_inode; |
| 141 | } |
| 142 | |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 143 | #ifndef _WIN32 |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 144 | /* Read a local directory and create the same tree in the generated filesystem. |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 145 | Calls itself recursively with each directory in the given directory. |
| 146 | full_path is an absolute or relative path, with a trailing slash, to the |
| 147 | directory on disk that should be copied, or NULL if this is a directory |
| 148 | that does not exist on disk (e.g. lost+found). |
| 149 | dir_path is an absolute path, with trailing slash, to the same directory |
| 150 | if the image were mounted at the specified mount point */ |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 151 | static u32 build_directory_structure(const char *full_path, const char *dir_path, const char *target_out_path, |
| Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 152 | u32 dir_inode, fs_config_func_t fs_config_func, |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 153 | struct selabel_handle *sehnd, int verbose, time_t fixed_time) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 154 | { |
| 155 | int entries = 0; |
| 156 | struct dentry *dentries; |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 157 | struct dirent **namelist = NULL; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 158 | struct stat stat; |
| 159 | int ret; |
| 160 | int i; |
| 161 | u32 inode; |
| 162 | u32 entry_inode; |
| 163 | u32 dirs = 0; |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 164 | bool needs_lost_and_found = false; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 165 | |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 166 | if (full_path) { |
| 167 | entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); |
| 168 | if (entries < 0) { |
| Mihai Serban | 04f4839 | 2015-01-07 12:44:24 +0200 | [diff] [blame] | 169 | #ifdef __GLIBC__ |
| 170 | /* The scandir function implemented in glibc has a bug that makes it |
| 171 | erroneously fail with ENOMEM under certain circumstances. |
| 172 | As a workaround we can retry the scandir call with the same arguments. |
| 173 | GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */ |
| 174 | if (errno == ENOMEM) |
| 175 | entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); |
| 176 | #endif |
| 177 | if (entries < 0) { |
| 178 | error_errno("scandir"); |
| 179 | return EXT4_ALLOCATE_FAILED; |
| 180 | } |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | if (dir_inode == 0) { |
| 185 | /* root directory, check if lost+found already exists */ |
| 186 | for (i = 0; i < entries; i++) |
| 187 | if (strcmp(namelist[i]->d_name, "lost+found") == 0) |
| 188 | break; |
| 189 | if (i == entries) |
| 190 | needs_lost_and_found = true; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | dentries = calloc(entries, sizeof(struct dentry)); |
| 194 | if (dentries == NULL) |
| 195 | critical_error_errno("malloc"); |
| 196 | |
| 197 | for (i = 0; i < entries; i++) { |
| 198 | dentries[i].filename = strdup(namelist[i]->d_name); |
| 199 | if (dentries[i].filename == NULL) |
| 200 | critical_error_errno("strdup"); |
| 201 | |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 202 | asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name); |
| 203 | asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 204 | |
| 205 | free(namelist[i]); |
| 206 | |
| 207 | ret = lstat(dentries[i].full_path, &stat); |
| 208 | if (ret < 0) { |
| 209 | error_errno("lstat"); |
| 210 | i--; |
| 211 | entries--; |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | dentries[i].size = stat.st_size; |
| 216 | dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 217 | if (fixed_time == -1) { |
| 218 | dentries[i].mtime = stat.st_mtime; |
| 219 | } else { |
| 220 | dentries[i].mtime = fixed_time; |
| 221 | } |
| Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 222 | uint64_t capabilities; |
| Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 223 | if (fs_config_func != NULL) { |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 224 | #ifdef ANDROID |
| 225 | unsigned int mode = 0; |
| 226 | unsigned int uid = 0; |
| 227 | unsigned int gid = 0; |
| 228 | int dir = S_ISDIR(stat.st_mode); |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 229 | fs_config_func(dentries[i].path, dir, target_out_path, &uid, &gid, &mode, &capabilities); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 230 | dentries[i].mode = mode; |
| 231 | dentries[i].uid = uid; |
| 232 | dentries[i].gid = gid; |
| Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 233 | dentries[i].capabilities = capabilities; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 234 | #else |
| 235 | error("can't set android permissions - built without android support"); |
| 236 | #endif |
| 237 | } |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 238 | #ifndef _WIN32 |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 239 | if (sehnd) { |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 240 | if (selabel_lookup(sehnd, &dentries[i].secon, dentries[i].path, stat.st_mode) < 0) { |
| 241 | error("cannot lookup security context for %s", dentries[i].path); |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 242 | } |
| William Roberts | 2057370 | 2013-01-17 13:24:27 -0800 | [diff] [blame] | 243 | |
| 244 | if (dentries[i].secon && verbose) |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 245 | printf("Labeling %s as %s\n", dentries[i].path, dentries[i].secon); |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 246 | } |
| 247 | #endif |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 248 | |
| 249 | if (S_ISREG(stat.st_mode)) { |
| 250 | dentries[i].file_type = EXT4_FT_REG_FILE; |
| 251 | } else if (S_ISDIR(stat.st_mode)) { |
| 252 | dentries[i].file_type = EXT4_FT_DIR; |
| 253 | dirs++; |
| 254 | } else if (S_ISCHR(stat.st_mode)) { |
| 255 | dentries[i].file_type = EXT4_FT_CHRDEV; |
| 256 | } else if (S_ISBLK(stat.st_mode)) { |
| 257 | dentries[i].file_type = EXT4_FT_BLKDEV; |
| 258 | } else if (S_ISFIFO(stat.st_mode)) { |
| 259 | dentries[i].file_type = EXT4_FT_FIFO; |
| 260 | } else if (S_ISSOCK(stat.st_mode)) { |
| 261 | dentries[i].file_type = EXT4_FT_SOCK; |
| 262 | } else if (S_ISLNK(stat.st_mode)) { |
| 263 | dentries[i].file_type = EXT4_FT_SYMLINK; |
| 264 | dentries[i].link = calloc(info.block_size, 1); |
| 265 | readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1); |
| 266 | } else { |
| 267 | error("unknown file type on %s", dentries[i].path); |
| 268 | i--; |
| 269 | entries--; |
| 270 | } |
| 271 | } |
| 272 | free(namelist); |
| 273 | |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 274 | if (needs_lost_and_found) { |
| 275 | /* insert a lost+found directory at the beginning of the dentries */ |
| 276 | struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry)); |
| 277 | memset(tmp, 0, sizeof(struct dentry)); |
| 278 | memcpy(tmp + 1, dentries, entries * sizeof(struct dentry)); |
| 279 | dentries = tmp; |
| 280 | |
| 281 | dentries[0].filename = strdup("lost+found"); |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 282 | asprintf(&dentries[0].path, "%slost+found", dir_path); |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 283 | dentries[0].full_path = NULL; |
| 284 | dentries[0].size = 0; |
| 285 | dentries[0].mode = S_IRWXU; |
| 286 | dentries[0].file_type = EXT4_FT_DIR; |
| 287 | dentries[0].uid = 0; |
| 288 | dentries[0].gid = 0; |
| Colin Cross | a532ecf | 2012-11-26 16:32:16 -0800 | [diff] [blame] | 289 | if (sehnd) { |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 290 | if (selabel_lookup(sehnd, &dentries[0].secon, dentries[0].path, dentries[0].mode) < 0) |
| Colin Cross | a532ecf | 2012-11-26 16:32:16 -0800 | [diff] [blame] | 291 | error("cannot lookup security context for %s", dentries[0].path); |
| Colin Cross | a532ecf | 2012-11-26 16:32:16 -0800 | [diff] [blame] | 292 | } |
| Colin Cross | c18120d | 2012-11-20 19:41:42 -0800 | [diff] [blame] | 293 | entries++; |
| 294 | dirs++; |
| 295 | } |
| 296 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 297 | inode = make_directory(dir_inode, entries, dentries, dirs); |
| 298 | |
| 299 | for (i = 0; i < entries; i++) { |
| 300 | if (dentries[i].file_type == EXT4_FT_REG_FILE) { |
| 301 | entry_inode = make_file(dentries[i].full_path, dentries[i].size); |
| 302 | } else if (dentries[i].file_type == EXT4_FT_DIR) { |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 303 | char *subdir_full_path = NULL; |
| 304 | char *subdir_dir_path; |
| 305 | if (dentries[i].full_path) { |
| 306 | ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path); |
| 307 | if (ret < 0) |
| 308 | critical_error_errno("asprintf"); |
| 309 | } |
| 310 | ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path); |
| 311 | if (ret < 0) |
| 312 | critical_error_errno("asprintf"); |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 313 | entry_inode = build_directory_structure(subdir_full_path, subdir_dir_path, target_out_path, |
| 314 | inode, fs_config_func, sehnd, verbose, fixed_time); |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 315 | free(subdir_full_path); |
| 316 | free(subdir_dir_path); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 317 | } else if (dentries[i].file_type == EXT4_FT_SYMLINK) { |
| Nick Kralevich | 5446bde | 2013-02-19 19:05:47 -0800 | [diff] [blame] | 318 | entry_inode = make_link(dentries[i].link); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 319 | } else { |
| 320 | error("unknown file type on %s", dentries[i].path); |
| 321 | entry_inode = 0; |
| 322 | } |
| 323 | *dentries[i].inode = entry_inode; |
| 324 | |
| 325 | ret = inode_set_permissions(entry_inode, dentries[i].mode, |
| Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 326 | dentries[i].uid, dentries[i].gid, |
| 327 | dentries[i].mtime); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 328 | if (ret) |
| 329 | error("failed to set permissions on %s\n", dentries[i].path); |
| Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * It's important to call inode_set_selinux() before |
| 333 | * inode_set_capabilities(). Extended attributes need to |
| 334 | * be stored sorted order, and we guarantee this by making |
| 335 | * the calls in the proper order. |
| 336 | * Please see xattr_assert_sane() in contents.c |
| 337 | */ |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 338 | ret = inode_set_selinux(entry_inode, dentries[i].secon); |
| 339 | if (ret) |
| 340 | error("failed to set SELinux context on %s\n", dentries[i].path); |
| Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 341 | ret = inode_set_capabilities(entry_inode, dentries[i].capabilities); |
| 342 | if (ret) |
| 343 | error("failed to set capability on %s\n", dentries[i].path); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 344 | |
| 345 | free(dentries[i].path); |
| 346 | free(dentries[i].full_path); |
| 347 | free(dentries[i].link); |
| 348 | free((void *)dentries[i].filename); |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 349 | free(dentries[i].secon); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | free(dentries); |
| 353 | return inode; |
| 354 | } |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 355 | #endif |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 356 | |
| 357 | static u32 compute_block_size() |
| 358 | { |
| 359 | return 4096; |
| 360 | } |
| 361 | |
| Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 362 | static u32 compute_journal_blocks() |
| 363 | { |
| 364 | u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64; |
| 365 | if (journal_blocks < 1024) |
| 366 | journal_blocks = 1024; |
| 367 | if (journal_blocks > 32768) |
| 368 | journal_blocks = 32768; |
| 369 | return journal_blocks; |
| 370 | } |
| 371 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 372 | static u32 compute_blocks_per_group() |
| 373 | { |
| 374 | return info.block_size * 8; |
| 375 | } |
| 376 | |
| 377 | static u32 compute_inodes() |
| 378 | { |
| 379 | return DIV_ROUND_UP(info.len, info.block_size) / 4; |
| 380 | } |
| 381 | |
| 382 | static u32 compute_inodes_per_group() |
| 383 | { |
| 384 | u32 blocks = DIV_ROUND_UP(info.len, info.block_size); |
| 385 | u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group); |
| Colin Cross | 96cc54a | 2011-04-29 16:45:48 -0700 | [diff] [blame] | 386 | u32 inodes = DIV_ROUND_UP(info.inodes, block_groups); |
| Paul Lawrence | 40ce87a | 2014-03-03 10:51:58 -0800 | [diff] [blame] | 387 | inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size)); |
| Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 388 | |
| 389 | /* After properly rounding up the number of inodes/group, |
| 390 | * make sure to update the total inodes field in the info struct. |
| 391 | */ |
| 392 | info.inodes = inodes * block_groups; |
| 393 | |
| 394 | return inodes; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 397 | static u32 compute_bg_desc_reserve_blocks() |
| 398 | { |
| 399 | u32 blocks = DIV_ROUND_UP(info.len, info.block_size); |
| 400 | u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group); |
| 401 | u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc), |
| 402 | info.block_size); |
| 403 | |
| 404 | u32 bg_desc_reserve_blocks = |
| 405 | DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc), |
| 406 | info.block_size) - bg_desc_blocks; |
| 407 | |
| 408 | if (bg_desc_reserve_blocks > info.block_size / sizeof(u32)) |
| 409 | bg_desc_reserve_blocks = info.block_size / sizeof(u32); |
| 410 | |
| 411 | return bg_desc_reserve_blocks; |
| 412 | } |
| 413 | |
| Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 414 | void reset_ext4fs_info() { |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 415 | // Reset all the global data structures used by make_ext4fs so it |
| 416 | // can be called again. |
| 417 | memset(&info, 0, sizeof(info)); |
| 418 | memset(&aux_info, 0, sizeof(aux_info)); |
| Colin Cross | f0ee37f | 2012-04-24 17:48:43 -0700 | [diff] [blame] | 419 | |
| Colin Cross | 3843c14 | 2014-01-31 09:38:20 -0800 | [diff] [blame] | 420 | if (ext4_sparse_file) { |
| 421 | sparse_file_destroy(ext4_sparse_file); |
| 422 | ext4_sparse_file = NULL; |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 423 | } |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 426 | int make_ext4fs_sparse_fd(int fd, long long len, |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 427 | const char *mountpoint, struct selabel_handle *sehnd) |
| Colin Cross | 03ad99c | 2013-01-23 15:25:29 -0800 | [diff] [blame] | 428 | { |
| Paul Crowley | d7a3fb4 | 2015-11-27 09:05:39 +0000 | [diff] [blame] | 429 | return make_ext4fs_sparse_fd_directory(fd, len, mountpoint, sehnd, NULL); |
| 430 | } |
| 431 | |
| 432 | int make_ext4fs_sparse_fd_directory(int fd, long long len, |
| 433 | const char *mountpoint, struct selabel_handle *sehnd, |
| 434 | const char *directory) |
| 435 | { |
| Colin Cross | 03ad99c | 2013-01-23 15:25:29 -0800 | [diff] [blame] | 436 | reset_ext4fs_info(); |
| 437 | info.len = len; |
| 438 | |
| Paul Crowley | d7a3fb4 | 2015-11-27 09:05:39 +0000 | [diff] [blame] | 439 | return make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL, |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 440 | 0, 1, 0, 0, 0, |
| 441 | sehnd, 0, -1, NULL, NULL, NULL); |
| Colin Cross | 03ad99c | 2013-01-23 15:25:29 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 444 | int make_ext4fs(const char *filename, long long len, |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 445 | const char *mountpoint, struct selabel_handle *sehnd) |
| Ken Sumrall | 983fb19 | 2011-01-19 17:15:42 -0800 | [diff] [blame] | 446 | { |
| Paul Lawrence | 01c43f2 | 2015-11-05 13:37:43 -0800 | [diff] [blame] | 447 | return make_ext4fs_directory(filename, len, mountpoint, sehnd, NULL); |
| 448 | } |
| 449 | |
| 450 | int make_ext4fs_directory(const char *filename, long long len, |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 451 | const char *mountpoint, struct selabel_handle *sehnd, |
| 452 | const char *directory) |
| Paul Lawrence | 01c43f2 | 2015-11-05 13:37:43 -0800 | [diff] [blame] | 453 | { |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 454 | int fd; |
| 455 | int status; |
| 456 | |
| 457 | reset_ext4fs_info(); |
| 458 | info.len = len; |
| 459 | |
| 460 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
| 461 | if (fd < 0) { |
| 462 | error_errno("open"); |
| 463 | return EXIT_FAILURE; |
| 464 | } |
| 465 | |
| Paul Crowley | d7a3fb4 | 2015-11-27 09:05:39 +0000 | [diff] [blame] | 466 | status = make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL, |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 467 | 0, 0, 0, 1, 0, |
| 468 | sehnd, 0, -1, NULL, NULL, NULL); |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 469 | close(fd); |
| 470 | |
| 471 | return status; |
| Ken Sumrall | 983fb19 | 2011-01-19 17:15:42 -0800 | [diff] [blame] | 472 | } |
| 473 | |
| Colin Cross | 5b89a4a | 2012-12-21 11:17:58 -0800 | [diff] [blame] | 474 | /* return a newly-malloc'd string that is a copy of str. The new string |
| 475 | is guaranteed to have a trailing slash. If absolute is true, the new string |
| 476 | is also guaranteed to have a leading slash. |
| 477 | */ |
| 478 | static char *canonicalize_slashes(const char *str, bool absolute) |
| 479 | { |
| 480 | char *ret; |
| 481 | int len = strlen(str); |
| 482 | int newlen = len; |
| 483 | char *ptr; |
| 484 | |
| Benoit Fradin | 7b4448d | 2013-08-28 14:44:32 +0200 | [diff] [blame] | 485 | if (len == 0) { |
| 486 | if (absolute) |
| 487 | return strdup("/"); |
| 488 | else |
| 489 | return strdup(""); |
| Colin Cross | 5b89a4a | 2012-12-21 11:17:58 -0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | if (str[0] != '/' && absolute) { |
| 493 | newlen++; |
| 494 | } |
| 495 | if (str[len - 1] != '/') { |
| 496 | newlen++; |
| 497 | } |
| 498 | ret = malloc(newlen + 1); |
| 499 | if (!ret) { |
| 500 | critical_error("malloc"); |
| 501 | } |
| 502 | |
| 503 | ptr = ret; |
| 504 | if (str[0] != '/' && absolute) { |
| 505 | *ptr++ = '/'; |
| 506 | } |
| 507 | |
| 508 | strcpy(ptr, str); |
| 509 | ptr += len; |
| 510 | |
| 511 | if (str[len - 1] != '/') { |
| 512 | *ptr++ = '/'; |
| 513 | } |
| 514 | |
| 515 | if (ptr != ret + newlen) { |
| 516 | critical_error("assertion failed\n"); |
| 517 | } |
| 518 | |
| 519 | *ptr = '\0'; |
| 520 | |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | static char *canonicalize_abs_slashes(const char *str) |
| 525 | { |
| 526 | return canonicalize_slashes(str, true); |
| 527 | } |
| 528 | |
| 529 | static char *canonicalize_rel_slashes(const char *str) |
| 530 | { |
| 531 | return canonicalize_slashes(str, false); |
| 532 | } |
| 533 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 534 | static int compare_chunks(const void* chunk1, const void* chunk2) { |
| 535 | struct region* c1 = (struct region*) chunk1; |
| 536 | struct region* c2 = (struct region*) chunk2; |
| 537 | return c1->block - c2->block; |
| 538 | } |
| 539 | |
| 540 | static int get_block_group(u32 block) { |
| Dmitry Shmidt | cd20530 | 2016-11-18 15:38:47 -0800 | [diff] [blame] | 541 | unsigned int i, group = 0; |
| 542 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 543 | for(i = 0; i < aux_info.groups; i++) { |
| 544 | if (block >= aux_info.bgs[i].first_block) |
| 545 | group = i; |
| 546 | else |
| 547 | break; |
| 548 | } |
| 549 | return group; |
| 550 | } |
| 551 | |
| 552 | static void extract_base_fs_allocations(const char *directory, const char *mountpoint, |
| 553 | FILE* base_alloc_file_in) { |
| 554 | #define err_msg "base file badly formatted" |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 555 | #ifndef _WIN32 |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 556 | // FORMAT Version 1.0: filename blk_mapping |
| 557 | const char *base_alloc_file_in_format = "%s %s"; |
| 558 | const int base_file_format_param_count = 2; |
| 559 | |
| 560 | char stored_file_name[MAX_PATH], real_file_name[MAX_PATH], file_map[MAX_BLK_MAPPING_STR]; |
| 561 | struct block_allocation *fs_alloc; |
| 562 | struct block_group_info *bgs = aux_info.bgs; |
| Dmitry Shmidt | cd20530 | 2016-11-18 15:38:47 -0800 | [diff] [blame] | 563 | int major_version = 0, minor_version = 0; |
| 564 | unsigned int i; |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 565 | char *base_file_line = NULL; |
| 566 | size_t base_file_line_len = 0; |
| 567 | |
| 568 | printf("[v%d.%d] Generating an Incremental EXT4 image\n", |
| 569 | blk_file_major_ver, blk_file_minor_ver); |
| 570 | if (base_fs_allocations == NULL) |
| 571 | base_fs_allocations = create_allocation(); |
| 572 | fs_alloc = base_fs_allocations; |
| 573 | |
| 574 | fscanf(base_alloc_file_in, blk_file_header_fmt, &major_version, &minor_version); |
| 575 | if (major_version == 0) { |
| 576 | critical_error("Invalid base file"); |
| 577 | } |
| 578 | |
| 579 | if (major_version != blk_file_major_ver) { |
| 580 | critical_error("Incompatible base file: version required is %d.X", |
| 581 | blk_file_major_ver); |
| 582 | } |
| 583 | |
| 584 | if (minor_version < blk_file_minor_ver) { |
| 585 | critical_error("Incompatible base file: version required is %d.%d or above", |
| 586 | blk_file_major_ver, blk_file_minor_ver); |
| 587 | } |
| 588 | |
| 589 | while (getline(&base_file_line, &base_file_line_len, base_alloc_file_in) != -1) { |
| 590 | if (sscanf(base_file_line, base_alloc_file_in_format, &stored_file_name, &file_map) |
| 591 | != base_file_format_param_count) { |
| 592 | continue; |
| 593 | } |
| 594 | if (strlen(stored_file_name) < strlen(mountpoint)) { |
| 595 | continue; |
| 596 | } |
| 597 | snprintf(real_file_name, MAX_PATH, "%s%s", directory, stored_file_name + strlen(mountpoint)); |
| 598 | if (!access(real_file_name, R_OK)) { |
| 599 | char *block_range, *end_string; |
| 600 | int real_file_fd; |
| Dmitry Shmidt | cd20530 | 2016-11-18 15:38:47 -0800 | [diff] [blame] | 601 | int start_block, end_block; |
| 602 | u32 block_file_size; |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 603 | u32 real_file_block_size; |
| 604 | |
| 605 | real_file_fd = open(real_file_name, O_RDONLY); |
| 606 | if (real_file_fd == -1) { |
| 607 | critical_error(err_msg); |
| 608 | } |
| 609 | real_file_block_size = get_file_size(real_file_fd); |
| 610 | close(real_file_fd); |
| 611 | real_file_block_size = DIV_ROUND_UP(real_file_block_size, info.block_size); |
| 612 | fs_alloc->filename = strdup(real_file_name); |
| 613 | block_range = strtok_r(file_map, ",", &end_string); |
| 614 | while (block_range && real_file_block_size) { |
| 615 | int block_group; |
| 616 | char *range, *end_token = NULL; |
| 617 | range = strtok_r(block_range, "-", &end_token); |
| 618 | if (!range) { |
| 619 | critical_error(err_msg); |
| 620 | } |
| 621 | start_block = parse_num(range); |
| 622 | range = strtok_r(NULL, "-", &end_token); |
| 623 | if (!range) { |
| 624 | end_block = start_block; |
| 625 | } else { |
| 626 | end_block = parse_num(range); |
| 627 | } |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 628 | // Assummption is that allocations are within the same block group |
| 629 | block_group = get_block_group(start_block); |
| 630 | if (block_group != get_block_group(end_block)) { |
| 631 | critical_error("base file allocation's end block is in a different " |
| 632 | "block group than start block. did you change fs params?"); |
| 633 | } |
| 634 | block_range = strtok_r(NULL, ",", &end_string); |
| Mohamad Ayyash | d2ed02a | 2016-04-28 21:49:01 -0700 | [diff] [blame] | 635 | int bg_first_block = bgs[block_group].first_block; |
| Colin Cross | afb5297 | 2016-09-30 01:01:50 +0000 | [diff] [blame] | 636 | int min_bg_bound = bgs[block_group].chunks[0].block + bgs[block_group].chunks[0].len; |
| 637 | int max_bg_bound = bgs[block_group].chunks[bgs[block_group].chunk_count - 1].block; |
| Mohamad Ayyash | d2ed02a | 2016-04-28 21:49:01 -0700 | [diff] [blame] | 638 | |
| 639 | if (min_bg_bound >= start_block - bg_first_block || |
| 640 | max_bg_bound <= end_block - bg_first_block) { |
| 641 | continue; |
| 642 | } |
| 643 | block_file_size = end_block - start_block + 1; |
| 644 | if (block_file_size > real_file_block_size) { |
| 645 | block_file_size = real_file_block_size; |
| 646 | } |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 647 | append_region(fs_alloc, start_block, block_file_size, block_group); |
| 648 | reserve_bg_chunk(block_group, start_block - bgs[block_group].first_block, block_file_size); |
| 649 | real_file_block_size -= block_file_size; |
| 650 | } |
| 651 | if (reserve_blocks_for_allocation(fs_alloc) < 0) |
| 652 | critical_error("failed to reserve base fs allocation"); |
| 653 | fs_alloc->next = create_allocation(); |
| 654 | fs_alloc = fs_alloc->next; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | for (i = 0; i < aux_info.groups; i++) { |
| 659 | qsort(bgs[i].chunks, bgs[i].chunk_count, sizeof(struct region), compare_chunks); |
| 660 | } |
| 661 | |
| 662 | free(base_file_line); |
| 663 | |
| Mohamad Ayyash | ef24424 | 2016-02-23 20:13:02 -0800 | [diff] [blame] | 664 | #else |
| 665 | return; |
| 666 | #endif |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 667 | #undef err_msg |
| 668 | } |
| 669 | |
| 670 | void generate_base_alloc_file_out(FILE* base_alloc_file_out, char* dir, char* mountpoint, |
| 671 | struct block_allocation* p) |
| 672 | { |
| 673 | size_t dirlen = dir ? strlen(dir) : 0; |
| 674 | fprintf(base_alloc_file_out, blk_file_header_fmt, blk_file_major_ver, blk_file_minor_ver); |
| 675 | fputc('\n', base_alloc_file_out); |
| 676 | while (p) { |
| 677 | if (dir && strncmp(p->filename, dir, dirlen) == 0) { |
| 678 | // substitute mountpoint for the leading directory in the filename, in the output file |
| 679 | fprintf(base_alloc_file_out, "%s%s", mountpoint, p->filename + dirlen); |
| 680 | } else { |
| 681 | fprintf(base_alloc_file_out, "%s", p->filename); |
| 682 | } |
| 683 | print_blocks(base_alloc_file_out, p, ','); |
| 684 | struct block_allocation* pn = p->next; |
| 685 | p = pn; |
| 686 | } |
| 687 | } |
| 688 | |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 689 | int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out_directory, |
| Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 690 | const char *_mountpoint, fs_config_func_t fs_config_func, int gzip, |
| Jeff Sharkey | f965968 | 2015-04-06 22:29:04 -0700 | [diff] [blame] | 691 | int sparse, int crc, int wipe, int real_uuid, |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 692 | struct selabel_handle *sehnd, int verbose, time_t fixed_time, |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 693 | FILE* block_list_file, FILE* base_alloc_file_in, FILE* base_alloc_file_out) |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 694 | { |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 695 | u32 root_inode_num; |
| 696 | u16 root_mode; |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 697 | char *mountpoint; |
| 698 | char *directory = NULL; |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 699 | char *target_out_directory = NULL; |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 700 | struct block_allocation* p; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 701 | |
| Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 702 | if (setjmp(setjmp_env)) |
| 703 | return EXIT_FAILURE; /* Handle a call to longjmp() */ |
| 704 | |
| Eric Miao | aaf32ea | 2015-04-16 15:00:11 -0700 | [diff] [blame] | 705 | info.block_device = is_block_device_fd(fd); |
| 706 | |
| 707 | if (info.block_device && (sparse || gzip || crc)) { |
| 708 | fprintf(stderr, "No sparse/gzip/crc allowed for block device\n"); |
| 709 | return EXIT_FAILURE; |
| 710 | } |
| 711 | |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 712 | if (_mountpoint == NULL) { |
| 713 | mountpoint = strdup(""); |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 714 | } else { |
| Colin Cross | 5b89a4a | 2012-12-21 11:17:58 -0800 | [diff] [blame] | 715 | mountpoint = canonicalize_abs_slashes(_mountpoint); |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | if (_directory) { |
| Colin Cross | 5b89a4a | 2012-12-21 11:17:58 -0800 | [diff] [blame] | 719 | directory = canonicalize_rel_slashes(_directory); |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 720 | } |
| 721 | |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 722 | if (_target_out_directory) { |
| 723 | target_out_directory = canonicalize_rel_slashes(_target_out_directory); |
| 724 | } |
| 725 | |
| Ken Sumrall | 435a8b6 | 2011-01-14 18:33:06 -0800 | [diff] [blame] | 726 | if (info.len <= 0) |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 727 | info.len = get_file_size(fd); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 728 | |
| 729 | if (info.len <= 0) { |
| 730 | fprintf(stderr, "Need size of filesystem\n"); |
| Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 731 | return EXIT_FAILURE; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 734 | if (info.block_size <= 0) |
| 735 | info.block_size = compute_block_size(); |
| 736 | |
| Ken Sumrall | 88833a6 | 2011-07-13 17:27:07 -0700 | [diff] [blame] | 737 | /* Round down the filesystem length to be a multiple of the block size */ |
| 738 | info.len &= ~((u64)info.block_size - 1); |
| 739 | |
| Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 740 | if (info.journal_blocks == 0) |
| 741 | info.journal_blocks = compute_journal_blocks(); |
| 742 | |
| 743 | if (info.no_journal == 0) |
| 744 | info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL; |
| 745 | else |
| 746 | info.journal_blocks = 0; |
| 747 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 748 | if (info.blocks_per_group <= 0) |
| 749 | info.blocks_per_group = compute_blocks_per_group(); |
| 750 | |
| 751 | if (info.inodes <= 0) |
| 752 | info.inodes = compute_inodes(); |
| 753 | |
| 754 | if (info.inode_size <= 0) |
| 755 | info.inode_size = 256; |
| 756 | |
| 757 | if (info.label == NULL) |
| 758 | info.label = ""; |
| 759 | |
| 760 | info.inodes_per_group = compute_inodes_per_group(); |
| 761 | |
| 762 | info.feat_compat |= |
| Nick Kralevich | 4df62f3 | 2013-02-07 14:21:34 -0800 | [diff] [blame] | 763 | EXT4_FEATURE_COMPAT_RESIZE_INODE | |
| 764 | EXT4_FEATURE_COMPAT_EXT_ATTR; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 765 | |
| 766 | info.feat_ro_compat |= |
| 767 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | |
| Mohamad Ayyash | dedf8f9 | 2016-04-14 19:43:31 -0700 | [diff] [blame] | 768 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE | |
| 769 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM; |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 770 | |
| 771 | info.feat_incompat |= |
| 772 | EXT4_FEATURE_INCOMPAT_EXTENTS | |
| 773 | EXT4_FEATURE_INCOMPAT_FILETYPE; |
| 774 | |
| 775 | |
| Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 776 | info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(); |
| 777 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 778 | printf("Creating filesystem with parameters:\n"); |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 779 | printf(" Size: %"PRIu64"\n", info.len); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 780 | printf(" Block size: %d\n", info.block_size); |
| 781 | printf(" Blocks per group: %d\n", info.blocks_per_group); |
| 782 | printf(" Inodes per group: %d\n", info.inodes_per_group); |
| 783 | printf(" Inode size: %d\n", info.inode_size); |
| Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 784 | printf(" Journal blocks: %d\n", info.journal_blocks); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 785 | printf(" Label: %s\n", info.label); |
| 786 | |
| 787 | ext4_create_fs_aux_info(); |
| 788 | |
| Colin Cross | af07234 | 2014-01-23 13:19:27 -0800 | [diff] [blame] | 789 | printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 790 | printf(" Block groups: %d\n", aux_info.groups); |
| Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 791 | printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 792 | |
| Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 793 | ext4_sparse_file = sparse_file_new(info.block_size, info.len); |
| Colin Cross | f0ee37f | 2012-04-24 17:48:43 -0700 | [diff] [blame] | 794 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 795 | block_allocator_init(); |
| 796 | |
| Jeff Sharkey | f965968 | 2015-04-06 22:29:04 -0700 | [diff] [blame] | 797 | ext4_fill_in_sb(real_uuid); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 798 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 799 | if (base_alloc_file_in) { |
| 800 | extract_base_fs_allocations(directory, mountpoint, base_alloc_file_in); |
| 801 | } |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 802 | if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED) |
| 803 | error("failed to reserve first 10 inodes"); |
| 804 | |
| 805 | if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) |
| 806 | ext4_create_journal_inode(); |
| 807 | |
| 808 | if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE) |
| 809 | ext4_create_resize_inode(); |
| 810 | |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 811 | #ifdef _WIN32 |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 812 | // Windows needs only 'create an empty fs image' functionality |
| 813 | assert(!directory); |
| Stephen Smalley | 7907ac7 | 2014-04-25 14:57:55 -0400 | [diff] [blame] | 814 | root_inode_num = build_default_directory_structure(mountpoint, sehnd); |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 815 | #else |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 816 | if (directory) |
| Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 817 | root_inode_num = build_directory_structure(directory, mountpoint, target_out_directory, 0, |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 818 | fs_config_func, sehnd, verbose, fixed_time); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 819 | else |
| Stephen Smalley | 7907ac7 | 2014-04-25 14:57:55 -0400 | [diff] [blame] | 820 | root_inode_num = build_default_directory_structure(mountpoint, sehnd); |
| Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 821 | #endif |
| Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 822 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 823 | root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 824 | inode_set_permissions(root_inode_num, root_mode, 0, 0, 0); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 825 | |
| Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 826 | #ifndef _WIN32 |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 827 | if (sehnd) { |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 828 | char *secontext = NULL; |
| Stephen Smalley | 6ece708 | 2012-02-09 14:19:24 -0500 | [diff] [blame] | 829 | |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 830 | if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) { |
| 831 | error("cannot lookup security context for %s", mountpoint); |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 832 | } |
| Robert Craig | 2929313 | 2013-01-30 12:54:46 -0500 | [diff] [blame] | 833 | if (secontext) { |
| 834 | if (verbose) { |
| 835 | printf("Labeling %s as %s\n", mountpoint, secontext); |
| 836 | } |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 837 | inode_set_selinux(root_inode_num, secontext); |
| 838 | } |
| Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 839 | freecon(secontext); |
| 840 | } |
| 841 | #endif |
| 842 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 843 | ext4_update_free(); |
| 844 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 845 | // TODO: Consider migrating the OTA tools to the new base alloc file format |
| 846 | // used for generating incremental images (see go/incremental-ext4) |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 847 | if (block_list_file) { |
| Mohamad Ayyash | 18785a8 | 2016-02-19 21:16:34 +0000 | [diff] [blame] | 848 | size_t dirlen = directory ? strlen(directory) : 0; |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 849 | struct block_allocation* p = get_saved_allocation_chain(); |
| Mohamad Ayyash | 18785a8 | 2016-02-19 21:16:34 +0000 | [diff] [blame] | 850 | while (p) { |
| 851 | if (directory && strncmp(p->filename, directory, dirlen) == 0) { |
| 852 | // substitute mountpoint for the leading directory in the filename, in the output file |
| 853 | fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen); |
| 854 | } else { |
| 855 | fprintf(block_list_file, "%s", p->filename); |
| 856 | } |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 857 | print_blocks(block_list_file, p, ' '); |
| Mohamad Ayyash | 18785a8 | 2016-02-19 21:16:34 +0000 | [diff] [blame] | 858 | struct block_allocation* pn = p->next; |
| Mohamad Ayyash | 18785a8 | 2016-02-19 21:16:34 +0000 | [diff] [blame] | 859 | p = pn; |
| 860 | } |
| Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 863 | if (base_alloc_file_out) { |
| 864 | struct block_allocation* p = get_saved_allocation_chain(); |
| 865 | generate_base_alloc_file_out(base_alloc_file_out, directory, mountpoint, p); |
| 866 | } |
| 867 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 868 | printf("Created filesystem with %d/%d inodes and %d/%d blocks\n", |
| 869 | aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, |
| 870 | aux_info.sb->s_inodes_count, |
| 871 | aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, |
| 872 | aux_info.sb->s_blocks_count_lo); |
| 873 | |
| David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 874 | if (wipe && WIPE_IS_SUPPORTED) { |
| Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame] | 875 | wipe_block_device(fd, info.len); |
| David 'Digit' Turner | eb5fcc3 | 2014-06-12 20:54:50 +0200 | [diff] [blame] | 876 | } |
| Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame] | 877 | |
| 878 | write_ext4_image(fd, gzip, sparse, crc); |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 879 | |
| Colin Cross | 782879a | 2014-01-23 13:08:16 -0800 | [diff] [blame] | 880 | sparse_file_destroy(ext4_sparse_file); |
| 881 | ext4_sparse_file = NULL; |
| Colin Cross | f0ee37f | 2012-04-24 17:48:43 -0700 | [diff] [blame] | 882 | |
| Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 883 | p = get_saved_allocation_chain(); |
| 884 | while (p) { |
| 885 | struct block_allocation* pn = p->next; |
| 886 | free_alloc(p); |
| 887 | p = pn; |
| 888 | } |
| 889 | |
| Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 890 | free(mountpoint); |
| 891 | free(directory); |
| 892 | |
| Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 893 | return 0; |
| 894 | } |