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 | * |
| 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 | |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 17 | #include "make_ext4fs.h" |
Colin Cross | 33f96c6 | 2010-12-22 18:40:14 -0800 | [diff] [blame] | 18 | #include "ext4_utils.h" |
| 19 | #include "allocate.h" |
| 20 | #include "contents.h" |
| 21 | #include "uuid.h" |
Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame^] | 22 | #include "wipe.h" |
| 23 | |
| 24 | #include <sparse/sparse.h> |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 25 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 26 | #include <assert.h> |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 27 | #include <dirent.h> |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 28 | #include <fcntl.h> |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 29 | #include <libgen.h> |
Colin Cross | 881cca2 | 2010-06-20 23:57:06 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/types.h> |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 36 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 37 | #ifdef USE_MINGW |
| 38 | |
| 39 | #include <winsock2.h> |
| 40 | |
| 41 | /* These match the Linux definitions of these flags. |
| 42 | L_xx is defined to avoid conflicting with the win32 versions. |
| 43 | */ |
| 44 | #define L_S_IRUSR 00400 |
| 45 | #define L_S_IWUSR 00200 |
| 46 | #define L_S_IXUSR 00100 |
| 47 | #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR) |
| 48 | #define S_IRGRP 00040 |
| 49 | #define S_IWGRP 00020 |
| 50 | #define S_IXGRP 00010 |
| 51 | #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP) |
| 52 | #define S_IROTH 00004 |
| 53 | #define S_IWOTH 00002 |
| 54 | #define S_IXOTH 00001 |
| 55 | #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH) |
| 56 | #define S_ISUID 0004000 |
| 57 | #define S_ISGID 0002000 |
| 58 | #define S_ISVTX 0001000 |
| 59 | |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 60 | #else |
| 61 | |
| 62 | #define O_BINARY 0 |
| 63 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 64 | #endif |
| 65 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 66 | /* TODO: Not implemented: |
| 67 | Allocating blocks in the same block group as the file inode |
| 68 | Hash or binary tree directories |
Colin Cross | 7a8bee7 | 2010-06-20 14:53:14 -0700 | [diff] [blame] | 69 | Special files: sockets, devices, fifos |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 70 | */ |
| 71 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 72 | static int filter_dot(const struct dirent *d) |
| 73 | { |
| 74 | return (strcmp(d->d_name, "..") && strcmp(d->d_name, ".")); |
| 75 | } |
| 76 | |
| 77 | static u32 build_default_directory_structure() |
| 78 | { |
| 79 | u32 inode; |
| 80 | u32 root_inode; |
| 81 | struct dentry dentries = { |
| 82 | .filename = "lost+found", |
| 83 | .file_type = EXT4_FT_DIR, |
| 84 | .mode = S_IRWXU, |
| 85 | .uid = 0, |
Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 86 | .gid = 0, |
| 87 | .mtime = 0, |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 88 | }; |
| 89 | root_inode = make_directory(0, 1, &dentries, 1); |
| 90 | inode = make_directory(root_inode, 0, NULL, 0); |
| 91 | *dentries.inode = inode; |
Ken Sumrall | 75249ed | 2010-08-13 16:04:49 -0700 | [diff] [blame] | 92 | inode_set_permissions(inode, dentries.mode, |
| 93 | dentries.uid, dentries.gid, dentries.mtime); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 94 | |
| 95 | return root_inode; |
| 96 | } |
| 97 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 98 | #ifndef USE_MINGW |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 99 | /* Read a local directory and create the same tree in the generated filesystem. |
| 100 | Calls itself recursively with each directory in the given directory */ |
| 101 | static u32 build_directory_structure(const char *full_path, const char *dir_path, |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 102 | u32 dir_inode, fs_config_func_t fs_config_func, |
| 103 | struct selabel_handle *sehnd) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 104 | { |
| 105 | int entries = 0; |
| 106 | struct dentry *dentries; |
| 107 | struct dirent **namelist; |
| 108 | struct stat stat; |
| 109 | int ret; |
| 110 | int i; |
| 111 | u32 inode; |
| 112 | u32 entry_inode; |
| 113 | u32 dirs = 0; |
| 114 | |
Colin Cross | 8aef66d | 2010-06-20 23:22:12 -0700 | [diff] [blame] | 115 | entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 116 | if (entries < 0) { |
| 117 | error_errno("scandir"); |
| 118 | return EXT4_ALLOCATE_FAILED; |
| 119 | } |
| 120 | |
| 121 | dentries = calloc(entries, sizeof(struct dentry)); |
| 122 | if (dentries == NULL) |
| 123 | critical_error_errno("malloc"); |
| 124 | |
| 125 | for (i = 0; i < entries; i++) { |
| 126 | dentries[i].filename = strdup(namelist[i]->d_name); |
| 127 | if (dentries[i].filename == NULL) |
| 128 | critical_error_errno("strdup"); |
| 129 | |
| 130 | asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name); |
| 131 | asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name); |
| 132 | |
| 133 | free(namelist[i]); |
| 134 | |
| 135 | ret = lstat(dentries[i].full_path, &stat); |
| 136 | if (ret < 0) { |
| 137 | error_errno("lstat"); |
| 138 | i--; |
| 139 | entries--; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | dentries[i].size = stat.st_size; |
| 144 | dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); |
Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 145 | dentries[i].mtime = stat.st_mtime; |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 146 | if (fs_config_func != NULL) { |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 147 | #ifdef ANDROID |
| 148 | unsigned int mode = 0; |
| 149 | unsigned int uid = 0; |
| 150 | unsigned int gid = 0; |
| 151 | int dir = S_ISDIR(stat.st_mode); |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 152 | fs_config_func(dentries[i].path, dir, &uid, &gid, &mode); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 153 | dentries[i].mode = mode; |
| 154 | dentries[i].uid = uid; |
| 155 | dentries[i].gid = gid; |
| 156 | #else |
| 157 | error("can't set android permissions - built without android support"); |
| 158 | #endif |
| 159 | } |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 160 | #ifdef HAVE_SELINUX |
| 161 | if (sehnd) { |
| 162 | char *sepath = NULL; |
| 163 | asprintf(&sepath, "/%s", dentries[i].path); |
| 164 | if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) { |
| 165 | error("cannot lookup security context for %s", sepath); |
| 166 | } |
| 167 | if (dentries[i].secon) |
| 168 | printf("Labeling %s as %s\n", sepath, dentries[i].secon); |
| 169 | free(sepath); |
| 170 | } |
| 171 | #endif |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 172 | |
| 173 | if (S_ISREG(stat.st_mode)) { |
| 174 | dentries[i].file_type = EXT4_FT_REG_FILE; |
| 175 | } else if (S_ISDIR(stat.st_mode)) { |
| 176 | dentries[i].file_type = EXT4_FT_DIR; |
| 177 | dirs++; |
| 178 | } else if (S_ISCHR(stat.st_mode)) { |
| 179 | dentries[i].file_type = EXT4_FT_CHRDEV; |
| 180 | } else if (S_ISBLK(stat.st_mode)) { |
| 181 | dentries[i].file_type = EXT4_FT_BLKDEV; |
| 182 | } else if (S_ISFIFO(stat.st_mode)) { |
| 183 | dentries[i].file_type = EXT4_FT_FIFO; |
| 184 | } else if (S_ISSOCK(stat.st_mode)) { |
| 185 | dentries[i].file_type = EXT4_FT_SOCK; |
| 186 | } else if (S_ISLNK(stat.st_mode)) { |
| 187 | dentries[i].file_type = EXT4_FT_SYMLINK; |
| 188 | dentries[i].link = calloc(info.block_size, 1); |
| 189 | readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1); |
| 190 | } else { |
| 191 | error("unknown file type on %s", dentries[i].path); |
| 192 | i--; |
| 193 | entries--; |
| 194 | } |
| 195 | } |
| 196 | free(namelist); |
| 197 | |
| 198 | inode = make_directory(dir_inode, entries, dentries, dirs); |
| 199 | |
| 200 | for (i = 0; i < entries; i++) { |
| 201 | if (dentries[i].file_type == EXT4_FT_REG_FILE) { |
| 202 | entry_inode = make_file(dentries[i].full_path, dentries[i].size); |
| 203 | } else if (dentries[i].file_type == EXT4_FT_DIR) { |
| 204 | entry_inode = build_directory_structure(dentries[i].full_path, |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 205 | dentries[i].path, inode, fs_config_func, sehnd); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 206 | } else if (dentries[i].file_type == EXT4_FT_SYMLINK) { |
| 207 | entry_inode = make_link(dentries[i].full_path, dentries[i].link); |
| 208 | } else { |
| 209 | error("unknown file type on %s", dentries[i].path); |
| 210 | entry_inode = 0; |
| 211 | } |
| 212 | *dentries[i].inode = entry_inode; |
| 213 | |
| 214 | ret = inode_set_permissions(entry_inode, dentries[i].mode, |
Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 215 | dentries[i].uid, dentries[i].gid, |
| 216 | dentries[i].mtime); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 217 | if (ret) |
| 218 | error("failed to set permissions on %s\n", dentries[i].path); |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 219 | ret = inode_set_selinux(entry_inode, dentries[i].secon); |
| 220 | if (ret) |
| 221 | error("failed to set SELinux context on %s\n", dentries[i].path); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 222 | |
| 223 | free(dentries[i].path); |
| 224 | free(dentries[i].full_path); |
| 225 | free(dentries[i].link); |
| 226 | free((void *)dentries[i].filename); |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 227 | free(dentries[i].secon); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | free(dentries); |
| 231 | return inode; |
| 232 | } |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 233 | #endif |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 234 | |
| 235 | static u32 compute_block_size() |
| 236 | { |
| 237 | return 4096; |
| 238 | } |
| 239 | |
Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 240 | static u32 compute_journal_blocks() |
| 241 | { |
| 242 | u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64; |
| 243 | if (journal_blocks < 1024) |
| 244 | journal_blocks = 1024; |
| 245 | if (journal_blocks > 32768) |
| 246 | journal_blocks = 32768; |
| 247 | return journal_blocks; |
| 248 | } |
| 249 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 250 | static u32 compute_blocks_per_group() |
| 251 | { |
| 252 | return info.block_size * 8; |
| 253 | } |
| 254 | |
| 255 | static u32 compute_inodes() |
| 256 | { |
| 257 | return DIV_ROUND_UP(info.len, info.block_size) / 4; |
| 258 | } |
| 259 | |
| 260 | static u32 compute_inodes_per_group() |
| 261 | { |
| 262 | u32 blocks = DIV_ROUND_UP(info.len, info.block_size); |
| 263 | u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group); |
Colin Cross | 96cc54a | 2011-04-29 16:45:48 -0700 | [diff] [blame] | 264 | u32 inodes = DIV_ROUND_UP(info.inodes, block_groups); |
Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 265 | inodes = ALIGN(inodes, (info.block_size / info.inode_size)); |
| 266 | |
| 267 | /* After properly rounding up the number of inodes/group, |
| 268 | * make sure to update the total inodes field in the info struct. |
| 269 | */ |
| 270 | info.inodes = inodes * block_groups; |
| 271 | |
| 272 | return inodes; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 275 | static u32 compute_bg_desc_reserve_blocks() |
| 276 | { |
| 277 | u32 blocks = DIV_ROUND_UP(info.len, info.block_size); |
| 278 | u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group); |
| 279 | u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc), |
| 280 | info.block_size); |
| 281 | |
| 282 | u32 bg_desc_reserve_blocks = |
| 283 | DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc), |
| 284 | info.block_size) - bg_desc_blocks; |
| 285 | |
| 286 | if (bg_desc_reserve_blocks > info.block_size / sizeof(u32)) |
| 287 | bg_desc_reserve_blocks = info.block_size / sizeof(u32); |
| 288 | |
| 289 | return bg_desc_reserve_blocks; |
| 290 | } |
| 291 | |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 292 | void reset_ext4fs_info() { |
| 293 | // Reset all the global data structures used by make_ext4fs so it |
| 294 | // can be called again. |
| 295 | memset(&info, 0, sizeof(info)); |
| 296 | memset(&aux_info, 0, sizeof(aux_info)); |
| 297 | free_data_blocks(); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Stephen Smalley | 6ece708 | 2012-02-09 14:19:24 -0500 | [diff] [blame] | 300 | int make_ext4fs(const char *filename, s64 len, |
| 301 | const char *mountpoint, struct selabel_handle *sehnd) |
Ken Sumrall | 983fb19 | 2011-01-19 17:15:42 -0800 | [diff] [blame] | 302 | { |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 303 | int fd; |
| 304 | int status; |
| 305 | |
| 306 | reset_ext4fs_info(); |
| 307 | info.len = len; |
| 308 | |
| 309 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
| 310 | if (fd < 0) { |
| 311 | error_errno("open"); |
| 312 | return EXIT_FAILURE; |
| 313 | } |
| 314 | |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 315 | status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, 0, sehnd); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 316 | close(fd); |
| 317 | |
| 318 | return status; |
Ken Sumrall | 983fb19 | 2011-01-19 17:15:42 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 321 | int make_ext4fs_internal(int fd, const char *directory, |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 322 | char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse, |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 323 | int crc, int wipe, int init_itabs, struct selabel_handle *sehnd) |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 324 | { |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 325 | u32 root_inode_num; |
| 326 | u16 root_mode; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 327 | |
Ken Sumrall | 2ae7663 | 2011-03-23 22:08:53 -0700 | [diff] [blame] | 328 | if (setjmp(setjmp_env)) |
| 329 | return EXIT_FAILURE; /* Handle a call to longjmp() */ |
| 330 | |
Ken Sumrall | 435a8b6 | 2011-01-14 18:33:06 -0800 | [diff] [blame] | 331 | if (info.len <= 0) |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 332 | info.len = get_file_size(fd); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 333 | |
| 334 | if (info.len <= 0) { |
| 335 | fprintf(stderr, "Need size of filesystem\n"); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 336 | return EXIT_FAILURE; |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 339 | if (info.block_size <= 0) |
| 340 | info.block_size = compute_block_size(); |
| 341 | |
Ken Sumrall | 88833a6 | 2011-07-13 17:27:07 -0700 | [diff] [blame] | 342 | /* Round down the filesystem length to be a multiple of the block size */ |
| 343 | info.len &= ~((u64)info.block_size - 1); |
| 344 | |
Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 345 | if (info.journal_blocks == 0) |
| 346 | info.journal_blocks = compute_journal_blocks(); |
| 347 | |
| 348 | if (info.no_journal == 0) |
| 349 | info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL; |
| 350 | else |
| 351 | info.journal_blocks = 0; |
| 352 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 353 | if (info.blocks_per_group <= 0) |
| 354 | info.blocks_per_group = compute_blocks_per_group(); |
| 355 | |
| 356 | if (info.inodes <= 0) |
| 357 | info.inodes = compute_inodes(); |
| 358 | |
| 359 | if (info.inode_size <= 0) |
| 360 | info.inode_size = 256; |
| 361 | |
| 362 | if (info.label == NULL) |
| 363 | info.label = ""; |
| 364 | |
| 365 | info.inodes_per_group = compute_inodes_per_group(); |
| 366 | |
| 367 | info.feat_compat |= |
| 368 | EXT4_FEATURE_COMPAT_RESIZE_INODE; |
| 369 | |
| 370 | info.feat_ro_compat |= |
| 371 | EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER | |
| 372 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE; |
| 373 | |
| 374 | info.feat_incompat |= |
| 375 | EXT4_FEATURE_INCOMPAT_EXTENTS | |
| 376 | EXT4_FEATURE_INCOMPAT_FILETYPE; |
| 377 | |
| 378 | |
Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 379 | info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks(); |
| 380 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 381 | printf("Creating filesystem with parameters:\n"); |
| 382 | printf(" Size: %llu\n", info.len); |
| 383 | printf(" Block size: %d\n", info.block_size); |
| 384 | printf(" Blocks per group: %d\n", info.blocks_per_group); |
| 385 | printf(" Inodes per group: %d\n", info.inodes_per_group); |
| 386 | printf(" Inode size: %d\n", info.inode_size); |
Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 387 | printf(" Journal blocks: %d\n", info.journal_blocks); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 388 | printf(" Label: %s\n", info.label); |
| 389 | |
| 390 | ext4_create_fs_aux_info(); |
| 391 | |
| 392 | printf(" Blocks: %llu\n", aux_info.len_blocks); |
| 393 | printf(" Block groups: %d\n", aux_info.groups); |
Colin Cross | 22742ce | 2010-12-22 16:01:52 -0800 | [diff] [blame] | 394 | printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 395 | |
| 396 | block_allocator_init(); |
| 397 | |
| 398 | ext4_fill_in_sb(); |
| 399 | |
| 400 | if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED) |
| 401 | error("failed to reserve first 10 inodes"); |
| 402 | |
| 403 | if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL) |
| 404 | ext4_create_journal_inode(); |
| 405 | |
| 406 | if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE) |
| 407 | ext4_create_resize_inode(); |
| 408 | |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 409 | #ifdef USE_MINGW |
| 410 | // Windows needs only 'create an empty fs image' functionality |
| 411 | assert(!directory); |
| 412 | root_inode_num = build_default_directory_structure(); |
| 413 | #else |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 414 | if (directory) |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 415 | root_inode_num = build_directory_structure(directory, mountpoint, 0, |
| 416 | fs_config_func, sehnd); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 417 | else |
| 418 | root_inode_num = build_default_directory_structure(); |
Raphael Moll | 4605b3f | 2012-02-03 23:02:33 -0800 | [diff] [blame] | 419 | #endif |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 420 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 421 | root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
Colin Cross | de61f98 | 2010-08-04 15:06:09 -0700 | [diff] [blame] | 422 | inode_set_permissions(root_inode_num, root_mode, 0, 0, 0); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 423 | |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 424 | #ifdef HAVE_SELINUX |
| 425 | if (sehnd) { |
| 426 | char *sepath = NULL; |
| 427 | char *secontext = NULL; |
Stephen Smalley | 6ece708 | 2012-02-09 14:19:24 -0500 | [diff] [blame] | 428 | |
| 429 | if (mountpoint[0] == '/') |
| 430 | sepath = strdup(mountpoint); |
| 431 | else |
| 432 | asprintf(&sepath, "/%s", mountpoint); |
| 433 | if (!sepath) |
| 434 | critical_error_errno("malloc"); |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 435 | if (selabel_lookup(sehnd, &secontext, sepath, S_IFDIR) < 0) { |
| 436 | error("cannot lookup security context for %s", sepath); |
| 437 | } |
| 438 | if (secontext) { |
| 439 | printf("Labeling %s as %s\n", sepath, secontext); |
| 440 | inode_set_selinux(root_inode_num, secontext); |
| 441 | } |
| 442 | free(sepath); |
| 443 | freecon(secontext); |
| 444 | } |
| 445 | #endif |
| 446 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 447 | ext4_update_free(); |
| 448 | |
Ken Sumrall | 107a9f1 | 2011-06-29 20:28:30 -0700 | [diff] [blame] | 449 | if (init_itabs) |
| 450 | init_unused_inode_tables(); |
| 451 | |
Colin Cross | b781330 | 2010-12-22 18:41:13 -0800 | [diff] [blame] | 452 | ext4_queue_sb(); |
| 453 | |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 454 | printf("Created filesystem with %d/%d inodes and %d/%d blocks\n", |
| 455 | aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count, |
| 456 | aux_info.sb->s_inodes_count, |
| 457 | aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo, |
| 458 | aux_info.sb->s_blocks_count_lo); |
| 459 | |
Colin Cross | dc5abee | 2012-04-23 23:20:48 -0700 | [diff] [blame^] | 460 | if (wipe) |
| 461 | wipe_block_device(fd, info.len); |
| 462 | |
| 463 | write_ext4_image(fd, gzip, sparse, crc); |
Colin Cross | ec0a2e8 | 2010-06-11 14:21:37 -0700 | [diff] [blame] | 464 | |
| 465 | return 0; |
| 466 | } |