blob: 62a3f1acd9b9041be7a2b7fb842ca780a35b230c [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -07001/*
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 Cross33f96c62010-12-22 18:40:14 -080017#include "make_ext4fs.h"
Colin Cross33f96c62010-12-22 18:40:14 -080018#include "ext4_utils.h"
19#include "allocate.h"
20#include "contents.h"
21#include "uuid.h"
Colin Crossdc5abee2012-04-23 23:20:48 -070022#include "wipe.h"
23
24#include <sparse/sparse.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070025
Raphael Moll4605b3f2012-02-03 23:02:33 -080026#include <assert.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070027#include <dirent.h>
Anatol Pomazau0349bd92012-01-11 15:12:27 -080028#include <fcntl.h>
Colin Crossaf072342014-01-23 13:19:27 -080029#include <inttypes.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070030#include <libgen.h>
Colin Cross881cca22010-06-20 23:57:06 -070031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
35#include <sys/stat.h>
36#include <sys/types.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070037
Raphael Moll4605b3f2012-02-03 23:02:33 -080038#ifdef USE_MINGW
39
40#include <winsock2.h>
41
42/* These match the Linux definitions of these flags.
43 L_xx is defined to avoid conflicting with the win32 versions.
44*/
45#define L_S_IRUSR 00400
46#define L_S_IWUSR 00200
47#define L_S_IXUSR 00100
48#define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
49#define S_IRGRP 00040
50#define S_IWGRP 00020
51#define S_IXGRP 00010
52#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
53#define S_IROTH 00004
54#define S_IWOTH 00002
55#define S_IXOTH 00001
56#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
57#define S_ISUID 0004000
58#define S_ISGID 0002000
59#define S_ISVTX 0001000
60
Anatol Pomazau0349bd92012-01-11 15:12:27 -080061#else
62
Colin Cross96529862013-01-23 15:38:57 -080063#include <selinux/selinux.h>
64#include <selinux/label.h>
65#include <selinux/android.h>
66
Anatol Pomazau0349bd92012-01-11 15:12:27 -080067#define O_BINARY 0
68
Raphael Moll4605b3f2012-02-03 23:02:33 -080069#endif
70
Colin Crossec0a2e82010-06-11 14:21:37 -070071/* TODO: Not implemented:
72 Allocating blocks in the same block group as the file inode
73 Hash or binary tree directories
Colin Cross7a8bee72010-06-20 14:53:14 -070074 Special files: sockets, devices, fifos
Colin Crossec0a2e82010-06-11 14:21:37 -070075 */
76
Colin Crossec0a2e82010-06-11 14:21:37 -070077static int filter_dot(const struct dirent *d)
78{
79 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
80}
81
Stephen Smalley7907ac72014-04-25 14:57:55 -040082static u32 build_default_directory_structure(const char *dir_path,
83 struct selabel_handle *sehnd)
Colin Crossec0a2e82010-06-11 14:21:37 -070084{
85 u32 inode;
86 u32 root_inode;
87 struct dentry dentries = {
88 .filename = "lost+found",
89 .file_type = EXT4_FT_DIR,
90 .mode = S_IRWXU,
91 .uid = 0,
Colin Crossde61f982010-08-04 15:06:09 -070092 .gid = 0,
93 .mtime = 0,
Colin Crossec0a2e82010-06-11 14:21:37 -070094 };
95 root_inode = make_directory(0, 1, &dentries, 1);
96 inode = make_directory(root_inode, 0, NULL, 0);
97 *dentries.inode = inode;
Ken Sumrall75249ed2010-08-13 16:04:49 -070098 inode_set_permissions(inode, dentries.mode,
99 dentries.uid, dentries.gid, dentries.mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700100
Stephen Smalley7907ac72014-04-25 14:57:55 -0400101#ifndef USE_MINGW
102 if (sehnd) {
103 char *path = NULL;
104 char *secontext = NULL;
105
106 asprintf(&path, "%slost+found", dir_path);
107 if (selabel_lookup(sehnd, &secontext, path, S_IFDIR) < 0) {
108 error("cannot lookup security context for %s", path);
109 } else {
110 inode_set_selinux(inode, secontext);
111 freecon(secontext);
112 }
113 free(path);
114 }
115#endif
116
Colin Crossec0a2e82010-06-11 14:21:37 -0700117 return root_inode;
118}
119
Raphael Moll4605b3f2012-02-03 23:02:33 -0800120#ifndef USE_MINGW
Colin Crossec0a2e82010-06-11 14:21:37 -0700121/* Read a local directory and create the same tree in the generated filesystem.
Colin Crossa4460142012-12-20 01:00:33 -0800122 Calls itself recursively with each directory in the given directory.
123 full_path is an absolute or relative path, with a trailing slash, to the
124 directory on disk that should be copied, or NULL if this is a directory
125 that does not exist on disk (e.g. lost+found).
126 dir_path is an absolute path, with trailing slash, to the same directory
127 if the image were mounted at the specified mount point */
Colin Crossec0a2e82010-06-11 14:21:37 -0700128static u32 build_directory_structure(const char *full_path, const char *dir_path,
Kenny Root2e5c5232012-03-30 20:38:32 -0700129 u32 dir_inode, fs_config_func_t fs_config_func,
Doug Zongker95266802013-12-05 15:51:28 -0800130 struct selabel_handle *sehnd, int verbose, time_t fixed_time)
Colin Crossec0a2e82010-06-11 14:21:37 -0700131{
132 int entries = 0;
133 struct dentry *dentries;
Colin Crossc18120d2012-11-20 19:41:42 -0800134 struct dirent **namelist = NULL;
Colin Crossec0a2e82010-06-11 14:21:37 -0700135 struct stat stat;
136 int ret;
137 int i;
138 u32 inode;
139 u32 entry_inode;
140 u32 dirs = 0;
Colin Crossc18120d2012-11-20 19:41:42 -0800141 bool needs_lost_and_found = false;
Colin Crossec0a2e82010-06-11 14:21:37 -0700142
Colin Crossc18120d2012-11-20 19:41:42 -0800143 if (full_path) {
144 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
145 if (entries < 0) {
Mihai Serban24309ab2015-01-07 12:44:24 +0200146#ifdef __GLIBC__
147 /* The scandir function implemented in glibc has a bug that makes it
148 erroneously fail with ENOMEM under certain circumstances.
149 As a workaround we can retry the scandir call with the same arguments.
150 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
151 if (errno == ENOMEM)
152 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
153#endif
154 if (entries < 0) {
155 error_errno("scandir");
156 return EXT4_ALLOCATE_FAILED;
157 }
Colin Crossc18120d2012-11-20 19:41:42 -0800158 }
159 }
160
161 if (dir_inode == 0) {
162 /* root directory, check if lost+found already exists */
163 for (i = 0; i < entries; i++)
164 if (strcmp(namelist[i]->d_name, "lost+found") == 0)
165 break;
166 if (i == entries)
167 needs_lost_and_found = true;
Colin Crossec0a2e82010-06-11 14:21:37 -0700168 }
169
170 dentries = calloc(entries, sizeof(struct dentry));
171 if (dentries == NULL)
172 critical_error_errno("malloc");
173
174 for (i = 0; i < entries; i++) {
175 dentries[i].filename = strdup(namelist[i]->d_name);
176 if (dentries[i].filename == NULL)
177 critical_error_errno("strdup");
178
Colin Crossa4460142012-12-20 01:00:33 -0800179 asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
180 asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
Colin Crossec0a2e82010-06-11 14:21:37 -0700181
182 free(namelist[i]);
183
184 ret = lstat(dentries[i].full_path, &stat);
185 if (ret < 0) {
186 error_errno("lstat");
187 i--;
188 entries--;
189 continue;
190 }
191
192 dentries[i].size = stat.st_size;
193 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
Doug Zongker95266802013-12-05 15:51:28 -0800194 if (fixed_time == -1) {
195 dentries[i].mtime = stat.st_mtime;
196 } else {
197 dentries[i].mtime = fixed_time;
198 }
Nick Kralevich4df62f32013-02-07 14:21:34 -0800199 uint64_t capabilities;
Kenny Root68e3dfd2012-03-29 14:43:22 -0700200 if (fs_config_func != NULL) {
Colin Crossec0a2e82010-06-11 14:21:37 -0700201#ifdef ANDROID
202 unsigned int mode = 0;
203 unsigned int uid = 0;
204 unsigned int gid = 0;
205 int dir = S_ISDIR(stat.st_mode);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800206 fs_config_func(dentries[i].path, dir, &uid, &gid, &mode, &capabilities);
Colin Crossec0a2e82010-06-11 14:21:37 -0700207 dentries[i].mode = mode;
208 dentries[i].uid = uid;
209 dentries[i].gid = gid;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800210 dentries[i].capabilities = capabilities;
Colin Crossec0a2e82010-06-11 14:21:37 -0700211#else
212 error("can't set android permissions - built without android support");
213#endif
214 }
Kenny Root723f1c72012-10-10 11:13:13 -0700215#ifndef USE_MINGW
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500216 if (sehnd) {
Colin Crossa4460142012-12-20 01:00:33 -0800217 if (selabel_lookup(sehnd, &dentries[i].secon, dentries[i].path, stat.st_mode) < 0) {
218 error("cannot lookup security context for %s", dentries[i].path);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500219 }
William Roberts20573702013-01-17 13:24:27 -0800220
221 if (dentries[i].secon && verbose)
Colin Crossa4460142012-12-20 01:00:33 -0800222 printf("Labeling %s as %s\n", dentries[i].path, dentries[i].secon);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500223 }
224#endif
Colin Crossec0a2e82010-06-11 14:21:37 -0700225
226 if (S_ISREG(stat.st_mode)) {
227 dentries[i].file_type = EXT4_FT_REG_FILE;
228 } else if (S_ISDIR(stat.st_mode)) {
229 dentries[i].file_type = EXT4_FT_DIR;
230 dirs++;
231 } else if (S_ISCHR(stat.st_mode)) {
232 dentries[i].file_type = EXT4_FT_CHRDEV;
233 } else if (S_ISBLK(stat.st_mode)) {
234 dentries[i].file_type = EXT4_FT_BLKDEV;
235 } else if (S_ISFIFO(stat.st_mode)) {
236 dentries[i].file_type = EXT4_FT_FIFO;
237 } else if (S_ISSOCK(stat.st_mode)) {
238 dentries[i].file_type = EXT4_FT_SOCK;
239 } else if (S_ISLNK(stat.st_mode)) {
240 dentries[i].file_type = EXT4_FT_SYMLINK;
241 dentries[i].link = calloc(info.block_size, 1);
242 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
243 } else {
244 error("unknown file type on %s", dentries[i].path);
245 i--;
246 entries--;
247 }
248 }
249 free(namelist);
250
Colin Crossc18120d2012-11-20 19:41:42 -0800251 if (needs_lost_and_found) {
252 /* insert a lost+found directory at the beginning of the dentries */
253 struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
254 memset(tmp, 0, sizeof(struct dentry));
255 memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
256 dentries = tmp;
257
258 dentries[0].filename = strdup("lost+found");
Colin Crossa4460142012-12-20 01:00:33 -0800259 asprintf(&dentries[0].path, "%slost+found", dir_path);
Colin Crossc18120d2012-11-20 19:41:42 -0800260 dentries[0].full_path = NULL;
261 dentries[0].size = 0;
262 dentries[0].mode = S_IRWXU;
263 dentries[0].file_type = EXT4_FT_DIR;
264 dentries[0].uid = 0;
265 dentries[0].gid = 0;
Colin Crossa532ecf2012-11-26 16:32:16 -0800266 if (sehnd) {
Colin Crossa4460142012-12-20 01:00:33 -0800267 if (selabel_lookup(sehnd, &dentries[0].secon, dentries[0].path, dentries[0].mode) < 0)
Colin Crossa532ecf2012-11-26 16:32:16 -0800268 error("cannot lookup security context for %s", dentries[0].path);
Colin Crossa532ecf2012-11-26 16:32:16 -0800269 }
Colin Crossc18120d2012-11-20 19:41:42 -0800270 entries++;
271 dirs++;
272 }
273
Colin Crossec0a2e82010-06-11 14:21:37 -0700274 inode = make_directory(dir_inode, entries, dentries, dirs);
275
276 for (i = 0; i < entries; i++) {
277 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
278 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
279 } else if (dentries[i].file_type == EXT4_FT_DIR) {
Colin Crossa4460142012-12-20 01:00:33 -0800280 char *subdir_full_path = NULL;
281 char *subdir_dir_path;
282 if (dentries[i].full_path) {
283 ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
284 if (ret < 0)
285 critical_error_errno("asprintf");
286 }
287 ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
288 if (ret < 0)
289 critical_error_errno("asprintf");
290 entry_inode = build_directory_structure(subdir_full_path,
Doug Zongker95266802013-12-05 15:51:28 -0800291 subdir_dir_path, inode, fs_config_func, sehnd, verbose, fixed_time);
Colin Crossa4460142012-12-20 01:00:33 -0800292 free(subdir_full_path);
293 free(subdir_dir_path);
Colin Crossec0a2e82010-06-11 14:21:37 -0700294 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
Nick Kralevich5446bde2013-02-19 19:05:47 -0800295 entry_inode = make_link(dentries[i].link);
Colin Crossec0a2e82010-06-11 14:21:37 -0700296 } else {
297 error("unknown file type on %s", dentries[i].path);
298 entry_inode = 0;
299 }
300 *dentries[i].inode = entry_inode;
301
302 ret = inode_set_permissions(entry_inode, dentries[i].mode,
Colin Crossde61f982010-08-04 15:06:09 -0700303 dentries[i].uid, dentries[i].gid,
304 dentries[i].mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700305 if (ret)
306 error("failed to set permissions on %s\n", dentries[i].path);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800307
308 /*
309 * It's important to call inode_set_selinux() before
310 * inode_set_capabilities(). Extended attributes need to
311 * be stored sorted order, and we guarantee this by making
312 * the calls in the proper order.
313 * Please see xattr_assert_sane() in contents.c
314 */
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500315 ret = inode_set_selinux(entry_inode, dentries[i].secon);
316 if (ret)
317 error("failed to set SELinux context on %s\n", dentries[i].path);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800318 ret = inode_set_capabilities(entry_inode, dentries[i].capabilities);
319 if (ret)
320 error("failed to set capability on %s\n", dentries[i].path);
Colin Crossec0a2e82010-06-11 14:21:37 -0700321
322 free(dentries[i].path);
323 free(dentries[i].full_path);
324 free(dentries[i].link);
325 free((void *)dentries[i].filename);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500326 free(dentries[i].secon);
Colin Crossec0a2e82010-06-11 14:21:37 -0700327 }
328
329 free(dentries);
330 return inode;
331}
Raphael Moll4605b3f2012-02-03 23:02:33 -0800332#endif
Colin Crossec0a2e82010-06-11 14:21:37 -0700333
334static u32 compute_block_size()
335{
336 return 4096;
337}
338
Colin Crosse4b5ae82010-08-03 14:10:07 -0700339static u32 compute_journal_blocks()
340{
341 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
342 if (journal_blocks < 1024)
343 journal_blocks = 1024;
344 if (journal_blocks > 32768)
345 journal_blocks = 32768;
346 return journal_blocks;
347}
348
Colin Crossec0a2e82010-06-11 14:21:37 -0700349static u32 compute_blocks_per_group()
350{
351 return info.block_size * 8;
352}
353
354static u32 compute_inodes()
355{
356 return DIV_ROUND_UP(info.len, info.block_size) / 4;
357}
358
359static u32 compute_inodes_per_group()
360{
361 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
362 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
Colin Cross96cc54a2011-04-29 16:45:48 -0700363 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
Paul Lawrence40ce87a2014-03-03 10:51:58 -0800364 inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size));
Ken Sumrall107a9f12011-06-29 20:28:30 -0700365
366 /* After properly rounding up the number of inodes/group,
367 * make sure to update the total inodes field in the info struct.
368 */
369 info.inodes = inodes * block_groups;
370
371 return inodes;
Colin Crossec0a2e82010-06-11 14:21:37 -0700372}
373
Colin Cross22742ce2010-12-22 16:01:52 -0800374static u32 compute_bg_desc_reserve_blocks()
375{
376 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
377 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
378 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
379 info.block_size);
380
381 u32 bg_desc_reserve_blocks =
382 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
383 info.block_size) - bg_desc_blocks;
384
385 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
386 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
387
388 return bg_desc_reserve_blocks;
389}
390
Doug Zongker263eefd2010-06-29 17:23:14 -0700391void reset_ext4fs_info() {
Doug Zongker95266802013-12-05 15:51:28 -0800392 // Reset all the global data structures used by make_ext4fs so it
393 // can be called again.
394 memset(&info, 0, sizeof(info));
395 memset(&aux_info, 0, sizeof(aux_info));
Colin Crossf0ee37f2012-04-24 17:48:43 -0700396
Colin Cross3843c142014-01-31 09:38:20 -0800397 if (ext4_sparse_file) {
398 sparse_file_destroy(ext4_sparse_file);
399 ext4_sparse_file = NULL;
Doug Zongker95266802013-12-05 15:51:28 -0800400 }
Colin Crossec0a2e82010-06-11 14:21:37 -0700401}
402
Colin Cross96529862013-01-23 15:38:57 -0800403int make_ext4fs_sparse_fd(int fd, long long len,
Doug Zongker95266802013-12-05 15:51:28 -0800404 const char *mountpoint, struct selabel_handle *sehnd)
Colin Cross03ad99c2013-01-23 15:25:29 -0800405{
406 reset_ext4fs_info();
407 info.len = len;
408
Doug Zongkerbec598e2014-08-12 11:35:37 -0700409 return make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 1, 0, 0, sehnd, 0, -1, NULL);
Colin Cross03ad99c2013-01-23 15:25:29 -0800410}
411
Colin Cross96529862013-01-23 15:38:57 -0800412int make_ext4fs(const char *filename, long long len,
Doug Zongker95266802013-12-05 15:51:28 -0800413 const char *mountpoint, struct selabel_handle *sehnd)
Ken Sumrall983fb192011-01-19 17:15:42 -0800414{
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800415 int fd;
416 int status;
417
418 reset_ext4fs_info();
419 info.len = len;
420
421 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
422 if (fd < 0) {
423 error_errno("open");
424 return EXIT_FAILURE;
425 }
426
Doug Zongkerbec598e2014-08-12 11:35:37 -0700427 status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, sehnd, 0, -1, NULL);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800428 close(fd);
429
430 return status;
Ken Sumrall983fb192011-01-19 17:15:42 -0800431}
432
Colin Cross5b89a4a2012-12-21 11:17:58 -0800433/* return a newly-malloc'd string that is a copy of str. The new string
434 is guaranteed to have a trailing slash. If absolute is true, the new string
435 is also guaranteed to have a leading slash.
436*/
437static char *canonicalize_slashes(const char *str, bool absolute)
438{
439 char *ret;
440 int len = strlen(str);
441 int newlen = len;
442 char *ptr;
443
Benoit Fradin7b4448d2013-08-28 14:44:32 +0200444 if (len == 0) {
445 if (absolute)
446 return strdup("/");
447 else
448 return strdup("");
Colin Cross5b89a4a2012-12-21 11:17:58 -0800449 }
450
451 if (str[0] != '/' && absolute) {
452 newlen++;
453 }
454 if (str[len - 1] != '/') {
455 newlen++;
456 }
457 ret = malloc(newlen + 1);
458 if (!ret) {
459 critical_error("malloc");
460 }
461
462 ptr = ret;
463 if (str[0] != '/' && absolute) {
464 *ptr++ = '/';
465 }
466
467 strcpy(ptr, str);
468 ptr += len;
469
470 if (str[len - 1] != '/') {
471 *ptr++ = '/';
472 }
473
474 if (ptr != ret + newlen) {
475 critical_error("assertion failed\n");
476 }
477
478 *ptr = '\0';
479
480 return ret;
481}
482
483static char *canonicalize_abs_slashes(const char *str)
484{
485 return canonicalize_slashes(str, true);
486}
487
488static char *canonicalize_rel_slashes(const char *str)
489{
490 return canonicalize_slashes(str, false);
491}
492
Colin Crossa4460142012-12-20 01:00:33 -0800493int make_ext4fs_internal(int fd, const char *_directory,
Doug Zongker95266802013-12-05 15:51:28 -0800494 const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
495 int sparse, int crc, int wipe,
Doug Zongkerbec598e2014-08-12 11:35:37 -0700496 struct selabel_handle *sehnd, int verbose, time_t fixed_time,
497 FILE* block_list_file)
Colin Crossec0a2e82010-06-11 14:21:37 -0700498{
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800499 u32 root_inode_num;
500 u16 root_mode;
Colin Crossa4460142012-12-20 01:00:33 -0800501 char *mountpoint;
502 char *directory = NULL;
Colin Crossec0a2e82010-06-11 14:21:37 -0700503
Ken Sumrall2ae76632011-03-23 22:08:53 -0700504 if (setjmp(setjmp_env))
505 return EXIT_FAILURE; /* Handle a call to longjmp() */
506
Colin Crossa4460142012-12-20 01:00:33 -0800507 if (_mountpoint == NULL) {
508 mountpoint = strdup("");
Colin Crossa4460142012-12-20 01:00:33 -0800509 } else {
Colin Cross5b89a4a2012-12-21 11:17:58 -0800510 mountpoint = canonicalize_abs_slashes(_mountpoint);
Colin Crossa4460142012-12-20 01:00:33 -0800511 }
512
513 if (_directory) {
Colin Cross5b89a4a2012-12-21 11:17:58 -0800514 directory = canonicalize_rel_slashes(_directory);
Colin Crossa4460142012-12-20 01:00:33 -0800515 }
516
Ken Sumrall435a8b62011-01-14 18:33:06 -0800517 if (info.len <= 0)
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800518 info.len = get_file_size(fd);
Colin Crossec0a2e82010-06-11 14:21:37 -0700519
520 if (info.len <= 0) {
521 fprintf(stderr, "Need size of filesystem\n");
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800522 return EXIT_FAILURE;
Colin Crossec0a2e82010-06-11 14:21:37 -0700523 }
524
Colin Crossec0a2e82010-06-11 14:21:37 -0700525 if (info.block_size <= 0)
526 info.block_size = compute_block_size();
527
Ken Sumrall88833a62011-07-13 17:27:07 -0700528 /* Round down the filesystem length to be a multiple of the block size */
529 info.len &= ~((u64)info.block_size - 1);
530
Colin Crosse4b5ae82010-08-03 14:10:07 -0700531 if (info.journal_blocks == 0)
532 info.journal_blocks = compute_journal_blocks();
533
534 if (info.no_journal == 0)
535 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
536 else
537 info.journal_blocks = 0;
538
Colin Crossec0a2e82010-06-11 14:21:37 -0700539 if (info.blocks_per_group <= 0)
540 info.blocks_per_group = compute_blocks_per_group();
541
542 if (info.inodes <= 0)
543 info.inodes = compute_inodes();
544
545 if (info.inode_size <= 0)
546 info.inode_size = 256;
547
548 if (info.label == NULL)
549 info.label = "";
550
551 info.inodes_per_group = compute_inodes_per_group();
552
553 info.feat_compat |=
Nick Kralevich4df62f32013-02-07 14:21:34 -0800554 EXT4_FEATURE_COMPAT_RESIZE_INODE |
555 EXT4_FEATURE_COMPAT_EXT_ATTR;
Colin Crossec0a2e82010-06-11 14:21:37 -0700556
557 info.feat_ro_compat |=
558 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
Colin Cross56497f22013-02-04 00:44:55 -0800559 EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
560 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
Colin Crossec0a2e82010-06-11 14:21:37 -0700561
562 info.feat_incompat |=
563 EXT4_FEATURE_INCOMPAT_EXTENTS |
564 EXT4_FEATURE_INCOMPAT_FILETYPE;
565
566
Colin Cross22742ce2010-12-22 16:01:52 -0800567 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
568
Colin Crossec0a2e82010-06-11 14:21:37 -0700569 printf("Creating filesystem with parameters:\n");
Colin Crossaf072342014-01-23 13:19:27 -0800570 printf(" Size: %"PRIu64"\n", info.len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700571 printf(" Block size: %d\n", info.block_size);
572 printf(" Blocks per group: %d\n", info.blocks_per_group);
573 printf(" Inodes per group: %d\n", info.inodes_per_group);
574 printf(" Inode size: %d\n", info.inode_size);
Colin Crosse4b5ae82010-08-03 14:10:07 -0700575 printf(" Journal blocks: %d\n", info.journal_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700576 printf(" Label: %s\n", info.label);
577
578 ext4_create_fs_aux_info();
579
Colin Crossaf072342014-01-23 13:19:27 -0800580 printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700581 printf(" Block groups: %d\n", aux_info.groups);
Colin Cross22742ce2010-12-22 16:01:52 -0800582 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700583
Colin Cross782879a2014-01-23 13:08:16 -0800584 ext4_sparse_file = sparse_file_new(info.block_size, info.len);
Colin Crossf0ee37f2012-04-24 17:48:43 -0700585
Colin Crossec0a2e82010-06-11 14:21:37 -0700586 block_allocator_init();
587
588 ext4_fill_in_sb();
589
590 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
591 error("failed to reserve first 10 inodes");
592
593 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
594 ext4_create_journal_inode();
595
596 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
597 ext4_create_resize_inode();
598
Raphael Moll4605b3f2012-02-03 23:02:33 -0800599#ifdef USE_MINGW
600 // Windows needs only 'create an empty fs image' functionality
601 assert(!directory);
Stephen Smalley7907ac72014-04-25 14:57:55 -0400602 root_inode_num = build_default_directory_structure(mountpoint, sehnd);
Raphael Moll4605b3f2012-02-03 23:02:33 -0800603#else
Colin Crossec0a2e82010-06-11 14:21:37 -0700604 if (directory)
Kenny Root2e5c5232012-03-30 20:38:32 -0700605 root_inode_num = build_directory_structure(directory, mountpoint, 0,
Doug Zongkerbec598e2014-08-12 11:35:37 -0700606 fs_config_func, sehnd, verbose, fixed_time);
Colin Crossec0a2e82010-06-11 14:21:37 -0700607 else
Stephen Smalley7907ac72014-04-25 14:57:55 -0400608 root_inode_num = build_default_directory_structure(mountpoint, sehnd);
Raphael Moll4605b3f2012-02-03 23:02:33 -0800609#endif
Doug Zongker263eefd2010-06-29 17:23:14 -0700610
Colin Crossec0a2e82010-06-11 14:21:37 -0700611 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
Colin Crossde61f982010-08-04 15:06:09 -0700612 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
Colin Crossec0a2e82010-06-11 14:21:37 -0700613
Kenny Root723f1c72012-10-10 11:13:13 -0700614#ifndef USE_MINGW
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500615 if (sehnd) {
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500616 char *secontext = NULL;
Stephen Smalley6ece7082012-02-09 14:19:24 -0500617
Colin Crossa4460142012-12-20 01:00:33 -0800618 if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) {
619 error("cannot lookup security context for %s", mountpoint);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500620 }
Robert Craig29293132013-01-30 12:54:46 -0500621 if (secontext) {
622 if (verbose) {
623 printf("Labeling %s as %s\n", mountpoint, secontext);
624 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500625 inode_set_selinux(root_inode_num, secontext);
626 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500627 freecon(secontext);
628 }
629#endif
630
Colin Crossec0a2e82010-06-11 14:21:37 -0700631 ext4_update_free();
632
Colin Crossb7813302010-12-22 18:41:13 -0800633 ext4_queue_sb();
634
Doug Zongkerbec598e2014-08-12 11:35:37 -0700635 if (block_list_file) {
636 size_t dirlen = directory ? strlen(directory) : 0;
637 struct block_allocation* p = get_saved_allocation_chain();
638 while (p) {
639 if (directory && strncmp(p->filename, directory, dirlen) == 0) {
640 // substitute mountpoint for the leading directory in the filename, in the output file
641 fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
642 } else {
643 fprintf(block_list_file, "%s", p->filename);
644 }
645 print_blocks(block_list_file, p);
646 struct block_allocation* pn = p->next;
647 free_alloc(p);
648 p = pn;
649 }
650 }
651
Colin Crossec0a2e82010-06-11 14:21:37 -0700652 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
653 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
654 aux_info.sb->s_inodes_count,
655 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
656 aux_info.sb->s_blocks_count_lo);
657
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +0200658 if (wipe && WIPE_IS_SUPPORTED) {
Colin Crossdc5abee2012-04-23 23:20:48 -0700659 wipe_block_device(fd, info.len);
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +0200660 }
Colin Crossdc5abee2012-04-23 23:20:48 -0700661
662 write_ext4_image(fd, gzip, sparse, crc);
Colin Crossec0a2e82010-06-11 14:21:37 -0700663
Colin Cross782879a2014-01-23 13:08:16 -0800664 sparse_file_destroy(ext4_sparse_file);
665 ext4_sparse_file = NULL;
Colin Crossf0ee37f2012-04-24 17:48:43 -0700666
Colin Crossa4460142012-12-20 01:00:33 -0800667 free(mountpoint);
668 free(directory);
669
Colin Crossec0a2e82010-06-11 14:21:37 -0700670 return 0;
671}