8 #include <ext2fs/ext2fs.h>
12 #include "block_list.h"
13 #include "basefs_allocator.h"
14 #include "create_inode.h"
16 #ifndef UID_GID_MAP_MAX_EXTENTS
18 * The value is defined in linux/user_namspace.h.
19 * The value is (arbitrarily) 5 in 4.14 and earlier, or 340 in 4.15 and later.
20 * Here, the bigger value is taken. See also man user_namespace(7).
22 #define UID_GID_MAP_MAX_EXTENTS 340
25 static char *prog_name = "e2fsdroid";
27 static char *block_list;
28 static char *basefs_out;
29 static char *basefs_in;
30 static char *mountpoint = "";
31 static time_t fixed_time = -1;
32 static char *fs_config_file;
33 static struct selinux_opt seopt_file[8];
34 static int max_nr_opt = (int)sizeof(seopt_file) / sizeof(seopt_file[0]);
35 static char *product_out;
37 static int android_configure;
38 static int android_sparse_file = 1;
40 static void usage(int ret)
42 fprintf(stderr, "%s [-B block_list] [-D basefs_out] [-T timestamp]\n"
43 "\t[-C fs_config] [-S file_contexts] [-p product_out]\n"
44 "\t[-a mountpoint] [-d basefs_in] [-f src_dir] [-e] [-s]\n"
45 "\t[-u uid-mapping] [-g gid-mapping] image\n",
50 static char *absolute_path(const char *file)
56 if (getcwd(cwd, PATH_MAX) == NULL) {
57 fprintf(stderr, "Failed to getcwd\n");
60 ret = malloc(strlen(cwd) + 1 + strlen(file) + 1);
62 sprintf(ret, "%s/%s", cwd, file);
68 static int parse_ugid_map_entry(char* line, struct ugid_map_entry* result)
70 char *token, *token_saveptr;
72 unsigned int *parsed[] = {&result->child_id,
75 for (token = strtok_r(line, " ", &token_saveptr), num_tokens = 0;
76 token && num_tokens < 3;
77 token = strtok_r(NULL, " ", &token_saveptr), ++num_tokens) {
79 *parsed[num_tokens] = strtoul(token, &endptr, 10);
80 if ((*parsed[num_tokens] == ULONG_MAX && errno) || *endptr) {
81 fprintf(stderr, "Malformed u/gid mapping line\n");
85 if (num_tokens < 3 || strtok_r(NULL, " ", &token_saveptr) != NULL) {
86 fprintf(stderr, "Malformed u/gid mapping line\n");
89 if (result->child_id + result->length < result->child_id ||
90 result->parent_id + result->length < result->parent_id) {
91 fprintf(stderr, "u/gid mapping overflow\n");
98 * Returns 1 if [begin1, begin1+length1) and [begin2, begin2+length2) have
99 * overlapping range. Otherwise 0.
101 static int is_overlapping(unsigned int begin1, unsigned int length1,
102 unsigned int begin2, unsigned int length2)
104 unsigned int end1 = begin1 + length1;
105 unsigned int end2 = begin2 + length2;
106 return !(end1 <= begin2 || end2 <= begin1);
110 * Verifies if the given mapping works.
111 * - Checks if the number of entries is less than or equals to
112 * UID_GID_MAP_MAX_EXTENTS.
113 * - Checks if there is no overlapped ranges.
114 * Returns 1 if valid, otherwise 0.
116 static int is_valid_ugid_map(const struct ugid_map* mapping)
120 if (mapping->size > UID_GID_MAP_MAX_EXTENTS) {
121 fprintf(stderr, "too many u/gid mapping entries\n");
125 for (i = 0; i < mapping->size; ++i) {
126 const struct ugid_map_entry *entry1 = &mapping->entries[i];
127 for (j = i + 1; j < mapping->size; ++j) {
128 const struct ugid_map_entry *entry2 =
129 &mapping->entries[j];
130 if (is_overlapping(entry1->child_id, entry1->length,
131 entry2->child_id, entry2->length)) {
133 "Overlapping child u/gid: [%d %d %d],"
135 entry1->child_id, entry1->parent_id,
136 entry1->length, entry2->child_id,
137 entry2->parent_id, entry2->length);
140 if (is_overlapping(entry1->parent_id, entry1->length,
141 entry2->parent_id, entry2->length)) {
143 "Overlapping parent u/gid: [%d %d %d],"
145 entry1->child_id, entry1->parent_id,
146 entry1->length, entry2->child_id,
147 entry2->parent_id, entry2->length);
156 * Parses the UID/GID mapping argument. The argument could be a multi-line
157 * string (separated by '\n', no trailing '\n' is allowed). Each line must
158 * contain exact three integer tokens; the first token is |child_id|,
159 * the second is |parent_id|, and the last is |length| of the mapping range.
160 * See also user_namespace(7) man page.
161 * On success, the parsed entries are stored in |result|, and it returns 1.
162 * Otherwise, returns 0.
164 static int parse_ugid_map(char* arg, struct ugid_map* result)
167 char *line, *line_saveptr;
168 size_t current_index;
170 /* Count the number of lines. */
172 for (i = 0; arg[i]; ++i) {
177 /* Allocate memory for entries. */
178 result->entries = malloc(sizeof(struct ugid_map_entry) * result->size);
179 if (!result->entries) {
184 /* Parse each line */
185 for (line = strtok_r(arg, "\n", &line_saveptr), current_index = 0;
187 line = strtok_r(NULL, "\n", &line_saveptr), ++current_index) {
188 if (!parse_ugid_map_entry(
189 line, &result->entries[current_index])) {
194 return is_valid_ugid_map(result);
197 int main(int argc, char *argv[])
201 int flags = EXT2_FLAG_RW;
204 ext2_filsys fs = NULL;
205 struct fs_ops_callbacks fs_callbacks = { NULL, NULL };
208 ext2_ino_t inodes_count;
209 ext2_ino_t free_inodes_count;
210 blk64_t blocks_count;
211 blk64_t free_blocks_count;
212 struct ugid_map uid_map = { 0, NULL }, gid_map = { 0, NULL };
214 add_error_table(&et_ext2_error_table);
216 while ((c = getopt (argc, argv, "T:C:S:p:a:D:d:B:f:esu:g:")) != EOF) {
219 fixed_time = strtoul(optarg, &p, 0);
220 android_configure = 1;
223 fs_config_file = absolute_path(optarg);
224 android_configure = 1;
227 token = strtok(optarg, ",");
229 if (nr_opt == max_nr_opt) {
230 fprintf(stderr, "Expected at most %d selinux opts\n",
234 seopt_file[nr_opt].type = SELABEL_OPT_PATH;
235 seopt_file[nr_opt].value = absolute_path(token);
237 token = strtok(NULL, ",");
239 android_configure = 1;
242 product_out = absolute_path(optarg);
245 mountpoint = strdup(optarg);
248 basefs_out = absolute_path(optarg);
251 basefs_in = absolute_path(optarg);
254 block_list = absolute_path(optarg);
257 src_dir = absolute_path(optarg);
260 android_sparse_file = 0;
263 flags |= EXT2_FLAG_SHARE_DUP;
266 if (!parse_ugid_map(optarg, &uid_map))
268 android_configure = 1;
271 if (!parse_ugid_map(optarg, &gid_map))
273 android_configure = 1;
279 if (optind >= argc) {
280 fprintf(stderr, "Expected filename after options\n");
284 if (android_sparse_file) {
285 io_mgr = sparse_io_manager;
286 if (asprintf(&in_file, "(%s)", argv[optind]) == -1) {
287 fprintf(stderr, "Failed to allocate file name\n");
291 io_mgr = unix_io_manager;
292 in_file = strdup(argv[optind]);
294 retval = ext2fs_open(in_file, flags, 0, 0, io_mgr, &fs);
296 com_err(prog_name, retval, "while opening file %s\n", in_file);
301 ext2fs_read_bitmaps(fs);
303 retval = base_fs_alloc_load(fs, basefs_in, mountpoint);
305 com_err(prog_name, retval, "%s",
306 "while reading base_fs file");
309 fs_callbacks.create_new_inode =
310 base_fs_alloc_set_target;
311 fs_callbacks.end_create_new_inode =
312 base_fs_alloc_unset_target;
314 retval = populate_fs2(fs, EXT2_ROOT_INO, src_dir,
315 EXT2_ROOT_INO, &fs_callbacks);
317 com_err(prog_name, retval, "%s",
318 "while populating file system");
322 base_fs_alloc_cleanup(fs);
325 if (android_configure) {
326 retval = android_configure_fs(
327 fs, src_dir, product_out, mountpoint, seopt_file,
328 nr_opt, fs_config_file, fixed_time, &uid_map, &gid_map);
330 com_err(prog_name, retval, "%s",
331 "while configuring the file system");
337 retval = fsmap_iter_filsys(fs, &block_list_format, block_list,
340 com_err(prog_name, retval, "%s",
341 "while creating the block_list");
347 retval = fsmap_iter_filsys(fs, &base_fs_format,
348 basefs_out, mountpoint);
350 com_err(prog_name, retval, "%s",
351 "while creating the basefs file");
356 inodes_count = fs->super->s_inodes_count;
357 free_inodes_count = fs->super->s_free_inodes_count;
358 blocks_count = ext2fs_blocks_count(fs->super);
359 free_blocks_count = ext2fs_free_blocks_count(fs->super);
361 retval = ext2fs_close_free(&fs);
363 com_err(prog_name, retval, "%s",
364 "while writing superblocks");
368 printf("Created filesystem with %u/%u inodes and %llu/%llu blocks\n",
369 inodes_count - free_inodes_count, inodes_count,
370 blocks_count - free_blocks_count, blocks_count);
372 remove_error_table(&et_ext2_error_table);