mkfs_ext2: make it able to create images larger than 4G
[oweals/busybox.git] / util-linux / mkfs_ext2.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mkfs_ext2: utility to create EXT2 filesystem
4  * inspired by genext2fs
5  *
6  * Busybox'ed (2009) by Vladimir Dronnikov <dronnikov@gmail.com>
7  *
8  * Licensed under GPLv2, see file LICENSE in this tarball for details.
9  */
10 #include "libbb.h"
11 #include <linux/fs.h>
12 #include <linux/ext2_fs.h>
13 #include "volume_id/volume_id_internal.h"
14
15 #define ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT 0
16 #define ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX 1
17
18 // from e2fsprogs
19 #define s_reserved_gdt_blocks s_padding1
20 #define s_mkfs_time           s_reserved[0]
21 #define s_flags               s_reserved[22]
22 #define EXT2_HASH_HALF_MD4     1
23 #define EXT2_FLAGS_SIGNED_HASH 0x0001
24
25 // whiteout: for writable overlays
26 //#define LINUX_S_IFWHT                  0160000
27 //#define EXT2_FEATURE_INCOMPAT_WHITEOUT 0x0020
28
29 // storage helper
30 void BUG_unsupported_field_size(void);
31 #define STORE_LE(field, value) \
32 do { \
33         if (sizeof(field) == 4) \
34                 field = cpu_to_le32(value); \
35         else if (sizeof(field) == 2) \
36                 field = cpu_to_le16(value); \
37         else if (sizeof(field) == 1) \
38                 field = (value); \
39         else \
40                 BUG_unsupported_field_size(); \
41 } while (0)
42
43 // All fields are little-endian
44 struct ext2_dir {
45         uint32_t inode1;
46         uint16_t rec_len1;
47         uint8_t  name_len1;
48         uint8_t  file_type1;
49         char     name1[4];
50         uint32_t inode2;
51         uint16_t rec_len2;
52         uint8_t  name_len2;
53         uint8_t  file_type2;
54         char     name2[4];
55         uint32_t inode3;
56         uint16_t rec_len3;
57         uint8_t  name_len3;
58         uint8_t  file_type3;
59         char     name3[12];
60 };
61
62 static unsigned int_log2(unsigned arg)
63 {
64         unsigned r = 0;
65         while ((arg >>= 1) != 0)
66                 r++;
67         return r;
68 }
69
70 // taken from mkfs_minix.c. libbb candidate?
71 static unsigned div_roundup(uint32_t size, uint32_t n)
72 {
73         return (size + n-1) / n;
74 }
75
76 static void allocate(uint8_t *bitmap, uint32_t blocksize, uint32_t start, uint32_t end)
77 {
78         uint32_t i;
79         memset(bitmap, 0, blocksize);
80         i = start / 8;
81         memset(bitmap, 0xFF, i);
82         bitmap[i] = 0xFF >> (8-(start&7));
83 //bb_info_msg("ALLOC: [%u][%u][%u]: [%u]:=[%x]", blocksize, start, end, blocksize - end/8 - 1, (uint8_t)(0xFF << (8-(end&7))));
84         i = end / 8;
85         bitmap[blocksize - i - 1] = 0xFF << (8 - (end & 7));
86         memset(bitmap + blocksize - i, 0xFF, i); // N.B. no overflow here!
87 }
88
89 static uint32_t has_super(uint32_t x)
90 {
91         // 0, 1 and powers of 3, 5, 7 up to 2^32 limit
92         static const uint32_t supers[] = {
93                 0, 1, 3, 5, 7, 9, 25, 27, 49, 81, 125, 243, 343, 625, 729,
94                 2187, 2401, 3125, 6561, 15625, 16807, 19683, 59049, 78125,
95                 117649, 177147, 390625, 531441, 823543, 1594323, 1953125,
96                 4782969, 5764801, 9765625, 14348907, 40353607, 43046721,
97                 48828125, 129140163, 244140625, 282475249, 387420489,
98                 1162261467, 1220703125, 1977326743, 3486784401/* >2^31 */,
99         };
100         const uint32_t *sp = supers + ARRAY_SIZE(supers);
101         while (1) {
102                 sp--;
103                 if (x == *sp)
104                         return 1;
105                 if (x > *sp)
106                         return 0;
107         }
108 }
109
110 /* Standard mke2fs 1.41.9:
111  * Usage: mke2fs [-c|-l filename] [-b block-size] [-f fragment-size]
112  *      [-i bytes-per-inode] [-I inode-size] [-J journal-options]
113  *      [-G meta group size] [-N number-of-inodes]
114  *      [-m reserved-blocks-percentage] [-o creator-os]
115  *      [-g blocks-per-group] [-L volume-label] [-M last-mounted-directory]
116  *      [-O feature[,...]] [-r fs-revision] [-E extended-option[,...]]
117  *      [-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]
118 */
119 // N.B. not commented below options are taken and silently ignored
120 enum {
121         OPT_c = 1 << 0,
122         OPT_l = 1 << 1,
123         OPT_b = 1 << 2,         // block size, in bytes
124         OPT_f = 1 << 3,
125         OPT_i = 1 << 4,         // bytes per inode
126         OPT_I = 1 << 5,
127         OPT_J = 1 << 6,
128         OPT_G = 1 << 7,
129         OPT_N = 1 << 8,
130         OPT_m = 1 << 9,         // percentage of blocks reserved for superuser
131         OPT_o = 1 << 10,
132         OPT_g = 1 << 11,
133         OPT_L = 1 << 12,        // label
134         OPT_M = 1 << 13,
135         OPT_O = 1 << 14,
136         OPT_r = 1 << 15,
137         OPT_E = 1 << 16,
138         OPT_T = 1 << 17,
139         OPT_U = 1 << 18,
140         OPT_j = 1 << 19,
141         OPT_n = 1 << 20,        // dry run: do not write anything
142         OPT_q = 1 << 21,
143         OPT_v = 1 << 22,
144         OPT_F = 1 << 23,
145         OPT_S = 1 << 24,
146         //OPT_V = 1 << 25,      // -V version. bbox applets don't support that
147 };
148
149 #define fd 3    /* predefined output descriptor */
150
151 static void PUT(uint64_t off, void *buf, uint32_t size)
152 {
153 //      bb_info_msg("PUT[%llu]:[%u]", off, size);
154         xlseek(fd, off, SEEK_SET);
155         xwrite(fd, buf, size);
156 }
157
158 int mkfs_ext2_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
159 int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv)
160 {
161         unsigned i, pos, n;
162         unsigned bs, blocksize, blocksize_log2;
163         unsigned nreserved = 5;
164         uint32_t nblocks;
165         uint32_t ngroups;
166         unsigned bytes_per_inode;
167         uint32_t nblocks_per_group;
168         uint32_t first_data_block;
169         uint32_t ninodes;
170         uint32_t ninodes_per_group;
171         uint32_t gdtsz, rgdtsz, itsz;
172         time_t timestamp;
173         unsigned opts;
174         const char *label;
175         struct stat st;
176         struct ext2_super_block *sb; // superblock
177         struct ext2_group_desc *gd; // group descriptors
178         struct ext2_inode *inode;
179         struct ext2_dir *dir;
180         uint8_t *buf;
181
182         bs = EXT2_MIN_BLOCK_SIZE;
183         opt_complementary = "-1:b+:m+:i+";
184         opts = getopt32(argv, "cl:b:f:i:I:J:G:N:m:o:g:L:M:O:r:E:T:U:jnqvFS",
185                 NULL, &bs, NULL, &bytes_per_inode, NULL, NULL, NULL, NULL,
186                 &nreserved, NULL, NULL, &label, NULL, NULL, NULL, NULL, NULL, NULL);
187         argv += optind; // argv[0] -- device
188
189         // block size minimax, block size is a multiple of minimum
190         blocksize = bs;
191         if (blocksize < EXT2_MIN_BLOCK_SIZE
192          || blocksize > EXT2_MAX_BLOCK_SIZE
193          || (blocksize & (blocksize - 1)) // not power of 2
194         ) {
195                 bb_error_msg_and_die("-%c is bad", 'b');
196         }
197
198         // reserved blocks count
199         if (nreserved > 50)
200                 bb_error_msg_and_die("-%c is bad", 'm');
201
202         // check the device is a block device
203         xstat(argv[0], &st);
204         if (!S_ISBLK(st.st_mode) && !(opts & OPT_F))
205                 bb_error_msg_and_die("not a block device");
206
207         // check if it is mounted
208         // N.B. what if we format a file? find_mount_point will return false negative since
209         // it is loop block device which mounted!
210         if (find_mount_point(argv[0], 0))
211                 bb_error_msg_and_die("can't format mounted filesystem");
212
213         // TODO: 5?/5 WE MUST NOT DEPEND ON WHETHER DEVICE IS /dev/zero 'ed OR NOT
214         // TODO: 3/5 refuse if mounted
215         // TODO: 4/5 compat options
216         // TODO: 1/5 sanity checks
217         // TODO: 0/5 more verbose error messages
218         // TODO: 0/5 info printing
219         // TODO: 2/5 bigendianness! Spot where it comes to play! sb->, gd->
220         // TODO: 2/5 reserved GDT: how to mark but not allocate?
221         // TODO: 3/5 dir_index?
222
223         // fill the superblock
224         sb = xzalloc(blocksize);
225         sb->s_rev_level = 1; // revision 1 filesystem
226         sb->s_magic = EXT2_SUPER_MAGIC;
227         sb->s_inode_size = sizeof(*inode);
228         sb->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
229         blocksize_log2 = int_log2(blocksize);
230         sb->s_log_block_size = sb->s_log_frag_size = blocksize_log2 - EXT2_MIN_BLOCK_LOG_SIZE;
231         // first 1024 bytes of the device are for boot record. If block size is 1024 bytes, then
232         // the first block available for data is 1, otherwise 0
233         first_data_block = sb->s_first_data_block = (EXT2_MIN_BLOCK_SIZE == blocksize);
234         // block and inode bitmaps occupy no more than one block, so maximum number of blocks is
235         // number of bits in one block, i.e. 8*blocksize
236         nblocks_per_group = sb->s_blocks_per_group = sb->s_frags_per_group = sb->s_inodes_per_group = 8 * blocksize;
237         timestamp = time(NULL);
238         sb->s_mkfs_time = sb->s_wtime = sb->s_lastcheck = timestamp;
239         sb->s_state = 1;
240         sb->s_creator_os = EXT2_OS_LINUX;
241         sb->s_max_mnt_count = EXT2_DFL_MAX_MNT_COUNT;
242         sb->s_checkinterval = 24*60*60 * 180; // 180 days
243         sb->s_errors = EXT2_ERRORS_DEFAULT;
244         sb->s_feature_compat = EXT2_FEATURE_COMPAT_SUPP
245                 | (EXT2_FEATURE_COMPAT_RESIZE_INO * ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT)
246                 | (EXT2_FEATURE_COMPAT_DIR_INDEX * ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX)
247                 ;
248         // e2fsprogs-1.41.9 doesn't like EXT2_FEATURE_INCOMPAT_WHITEOUT
249         sb->s_feature_incompat = EXT2_FEATURE_INCOMPAT_FILETYPE;// | EXT2_FEATURE_INCOMPAT_WHITEOUT;
250         sb->s_feature_ro_compat = EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
251         sb->s_flags = EXT2_FLAGS_SIGNED_HASH * ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX;
252         generate_uuid(sb->s_uuid);
253         if (ENABLE_FEATURE_MKFS_EXT2_DIR_INDEX) {
254                 sb->s_def_hash_version = EXT2_HASH_HALF_MD4;
255                 generate_uuid((uint8_t *)sb->s_hash_seed);
256         }
257         /*
258          * From e2fsprogs: add "jitter" to the superblock's check interval so that we
259          * don't check all the filesystems at the same time.  We use a
260          * kludgy hack of using the UUID to derive a random jitter value.
261          */
262         sb->s_max_mnt_count += sb->s_uuid[ARRAY_SIZE(sb->s_uuid)-1] % EXT2_DFL_MAX_MNT_COUNT;
263
264         // open the device, get number of blocks
265         xmove_fd(xopen3(argv[0], O_WRONLY | O_CREAT, 0666), fd);
266         if (argv[1]) {
267                 nblocks = xatou(argv[1]);
268         } else {
269                 nblocks = (uoff_t)xlseek(fd, 0, SEEK_END) >> blocksize_log2;
270         }
271         sb->s_blocks_count = nblocks;
272
273         // nblocks is the total number of blocks in the filesystem
274         if (nblocks < 8)
275                 bb_error_msg_and_die("nblocks");
276         // reserve blocks for superuser
277         sb->s_r_blocks_count = ((uint64_t) nblocks * nreserved) / 100;
278
279         // N.B. a block group can have no more than nblocks_per_group blocks
280         ngroups = div_roundup(nblocks - first_data_block, nblocks_per_group);
281         if (0 == ngroups)
282                 bb_error_msg_and_die("ngroups");
283         gdtsz = div_roundup(ngroups, EXT2_DESC_PER_BLOCK(sb));
284         /*
285          * From e2fsprogs: Calculate the number of GDT blocks to reserve for online
286          * filesystem growth.
287          * The absolute maximum number of GDT blocks we can reserve is determined by
288          * the number of block pointers that can fit into a single block.
289          */
290         /* We set it at 1024x the current filesystem size, or
291          * the upper block count limit (2^32), whichever is lower.
292          */
293 #if ENABLE_FEATURE_MKFS_EXT2_RESERVED_GDT
294         rgdtsz = 0xFFFFFFFF; // maximum block number
295         if (nblocks < rgdtsz / 1024)
296                 rgdtsz = nblocks * 1024;
297         rgdtsz = div_roundup(rgdtsz - first_data_block, nblocks_per_group);
298         rgdtsz = div_roundup(rgdtsz, EXT2_DESC_PER_BLOCK(sb)) - gdtsz;
299         if (rgdtsz > EXT2_ADDR_PER_BLOCK(sb))
300                 rgdtsz = EXT2_ADDR_PER_BLOCK(sb);
301         sb->s_reserved_gdt_blocks = rgdtsz;
302         //bb_info_msg("RSRVD[%u]", n);
303 #else
304         rgdtsz = 0;
305 #endif
306
307         // ninodes is the total number of inodes (files) in the file system
308         if (!(opts & OPT_i)) {
309                 bytes_per_inode = 16384;
310                 if (nblocks < 512*1024)
311                         bytes_per_inode = 4096;
312                 if (nblocks < 3*1024)
313                         bytes_per_inode = 8192;
314         }
315         ninodes = nblocks / (bytes_per_inode >> blocksize_log2);
316         if (ninodes < EXT2_GOOD_OLD_FIRST_INO+1)
317                 ninodes = EXT2_GOOD_OLD_FIRST_INO+1;
318         ninodes_per_group = div_roundup(ninodes, ngroups);
319         if (ninodes_per_group < 16)
320                 ninodes_per_group = 16; // minimum number because the first EXT2_GOOD_OLD_FIRST_INO-1 are reserved
321         // N.B. a block group can have no more than 8*blocksize = sb->s_inodes_per_group inodes
322         if (ninodes_per_group > sb->s_inodes_per_group)
323                 ninodes_per_group = sb->s_inodes_per_group;
324         // adjust inodes per group so they completely fill the inode table blocks in the descriptor
325         ninodes_per_group = (div_roundup(ninodes_per_group * EXT2_INODE_SIZE(sb), blocksize) << blocksize_log2) / EXT2_INODE_SIZE(sb);
326         // make sure the number of inodes per group is a multiple of 8
327         ninodes_per_group &= ~7;
328         sb->s_inodes_per_group = ninodes_per_group;// = div_roundup(ninodes_per_group * sb->s_inode_size, blocksize);
329         // total ninodes
330         ninodes = sb->s_inodes_count = ninodes_per_group * ngroups;
331
332         itsz = ninodes_per_group * sb->s_inode_size / blocksize;
333         sb->s_free_inodes_count = sb->s_inodes_count - EXT2_GOOD_OLD_FIRST_INO;
334
335         // write the label, if any
336         if (opts & OPT_L)
337                 safe_strncpy((char *)sb->s_volume_name, label, sizeof(sb->s_volume_name));
338
339 #if 1
340 /*      if (fs_param.s_blocks_count != s->s_blocks_count)
341                 fprintf(stderr, _("warning: %u blocks unused.\n\n"),
342                        fs_param.s_blocks_count - s->s_blocks_count);
343 */
344
345         printf(
346                 "Filesystem label=%s\n"
347                 "OS type: Linux\n"
348                 "Block size=%u (log=%u)\n"
349                 "Fragment size=%u (log=%u)\n"
350                 "%u inodes, %u blocks\n"
351                 "%u blocks (%u%%) reserved for the super user\n"
352                 "First data block=%u\n"
353 //              "Maximum filesystem blocks=%lu\n"
354                 "%u block groups\n"
355                 "%u blocks per group, %u fragments per group\n"
356                 "%u inodes per group"
357                 , (char *)sb->s_volume_name
358                 , blocksize, sb->s_log_block_size
359                 , blocksize, sb->s_log_block_size
360                 , sb->s_inodes_count, sb->s_blocks_count
361                 , sb->s_r_blocks_count, nreserved
362                 , first_data_block
363 //              , (rgdtsz + gdtsz) * EXT2_DESC_PER_BLOCK(sb) * nblocks_per_group
364                 , ngroups
365                 , nblocks_per_group, nblocks_per_group
366                 , ninodes_per_group
367         );
368         {
369                 const char *fmt = "\nSuperblock backups stored on blocks: %u";
370                 pos = first_data_block;
371                 for (i = 1; i < ngroups; i++) {
372                         pos += nblocks_per_group;
373                         if (has_super(i)) {
374                                 printf(fmt, (unsigned)pos);
375                                 fmt = ", %u";
376                         }
377                 }
378         }
379         bb_putchar('\n');
380 #endif
381
382         if (opts & OPT_n)
383                 return EXIT_SUCCESS;
384
385         // fill group descriptors
386         gd = xzalloc((gdtsz + rgdtsz) * blocksize);
387         sb->s_free_blocks_count = 0;
388         for (i = 0, pos = first_data_block, n = nblocks;
389                 i < ngroups;
390                 i++, pos += nblocks_per_group, n -= nblocks_per_group
391         ) {
392                 uint32_t overhead = pos + (has_super(i) ? (1/*sb*/ + gdtsz + rgdtsz) : 0);
393                 gd[i].bg_block_bitmap = overhead + 0;
394                 gd[i].bg_inode_bitmap = overhead + 1;
395                 gd[i].bg_inode_table  = overhead + 2;
396                 overhead = overhead - pos + 1/*bbmp*/ + 1/*ibmp*/ + itsz;
397                 gd[i].bg_free_inodes_count = ninodes_per_group;
398                 //gd[i].bg_used_dirs_count = 0;
399                 // N.B. both root and lost+found dirs are within the first block group, thus +2
400                 if (0 == i) {
401                         overhead += 2;
402                         gd[i].bg_used_dirs_count = 2;
403                         gd[i].bg_free_inodes_count -= EXT2_GOOD_OLD_FIRST_INO;
404                 }
405                 // N.B. the following is pure heuristics!
406                 // Likely to cope with 1024-byte blocks, when first block is for boot sectors
407                 if (ngroups-1 == i) {
408                         overhead += first_data_block;
409                 }
410                 gd[i].bg_free_blocks_count = (n < nblocks_per_group ? n : nblocks_per_group) - overhead;
411                 sb->s_free_blocks_count += gd[i].bg_free_blocks_count;
412         }
413         STORE_LE(sb->s_free_blocks_count, sb->s_free_blocks_count);
414
415         // dump filesystem skeleton structures
416 //      printf("Writing superblocks and filesystem accounting information: ");
417         buf = xmalloc(blocksize);
418         for (i = 0, pos = first_data_block; i < ngroups; i++, pos += nblocks_per_group) {
419                 uint32_t overhead = has_super(i) ? (1/*sb*/ + gdtsz + rgdtsz) : 0;
420                 uint32_t start;// = has_super(i) ? (1/*sb*/ + gdtsz + rgdtsz) : 0;
421                 uint32_t end;
422
423                 // dump superblock and group descriptors and their backups
424                 if (overhead) { // N.B. in fact, we want (has_super(i)) condition, but it is equal to (overhead != 0) and is cheaper
425                         // N.B. 1024 byte blocks are special
426                         PUT(((uint64_t)pos << blocksize_log2) + ((0 == i && 0 == first_data_block) ? 1024 : 0), sb, 1024);//blocksize);
427                         PUT(((uint64_t)pos << blocksize_log2) + blocksize, gd, (gdtsz + rgdtsz) * blocksize);
428                 }
429
430                 start = overhead + 1/*bbmp*/ + 1/*ibmp*/ + itsz;
431                 if (i == 0)
432                         start += 2; // for "/" and "/lost+found"
433                 end = nblocks_per_group - (start + gd[i].bg_free_blocks_count);
434
435                 // mark preallocated blocks as allocated
436                 allocate(buf, blocksize, start, end);
437                 // dump block bitmap
438                 PUT((uint64_t)(pos + overhead) * blocksize, buf, blocksize);
439
440                 // mark preallocated inodes as allocated
441                 allocate(buf, blocksize,
442                         ninodes_per_group - gd[i].bg_free_inodes_count,
443                         8 * blocksize - ninodes_per_group
444                 );
445                 // dump inode bitmap
446                 //PUT((uint64_t)(pos + overhead + 1) * blocksize, buf, blocksize);
447                 //but it's right after block bitmap, so we can just:
448                 xwrite(fd, buf, blocksize);
449         }
450
451         // zero boot sectors
452         memset(buf, 0, blocksize);
453         PUT(0, buf, 1024); // N.B. 1024 <= blocksize
454         // zero inode tables
455         for (i = 0; i < ngroups; ++i)
456                 for (n = 0; n < itsz; ++n)
457                         PUT((uint64_t)(gd[i].bg_inode_table + n) * blocksize, buf, blocksize);
458
459         // prepare directory inode
460         inode = (struct ext2_inode *)buf;
461         STORE_LE(inode->i_mode, S_IFDIR | S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH);
462         STORE_LE(inode->i_mtime, timestamp);
463         STORE_LE(inode->i_atime, timestamp);
464         STORE_LE(inode->i_ctime, timestamp);
465         STORE_LE(inode->i_size, blocksize);
466         // N.B. inode->i_blocks stores the number of 512 byte data blocks. Why on Earth?!
467         STORE_LE(inode->i_blocks, blocksize / 512);
468
469         // dump root dir inode
470         STORE_LE(inode->i_links_count, 3); // "/.", "/..", "/lost+found/.." point to this inode
471         STORE_LE(inode->i_block[0], gd[0].bg_inode_table + itsz);
472         PUT(((uint64_t)gd[0].bg_inode_table << blocksize_log2) + (EXT2_ROOT_INO-1) * sizeof(*inode), buf, sizeof(*inode));
473
474         // dump lost+found dir inode
475         STORE_LE(inode->i_links_count, 2); // both "/lost+found" and "/lost+found/." point to this inode
476         STORE_LE(inode->i_block[0], inode->i_block[0]+1); // use next block //= gd[0].bg_inode_table + itsz + 1;
477         PUT(((uint64_t)gd[0].bg_inode_table << blocksize_log2) + (EXT2_GOOD_OLD_FIRST_INO-1) * sizeof(*inode), buf, sizeof(*inode));
478
479         // dump directories
480         memset(buf, 0, blocksize);
481         dir = (struct ext2_dir *)buf;
482
483         // dump lost+found dir block
484         STORE_LE(dir->inode1, EXT2_GOOD_OLD_FIRST_INO);
485         STORE_LE(dir->rec_len1, 12);
486         STORE_LE(dir->name_len1, 1);
487         STORE_LE(dir->file_type1, EXT2_FT_DIR);
488         dir->name1[0] = '.';
489         STORE_LE(dir->inode2, EXT2_ROOT_INO);
490         STORE_LE(dir->rec_len2, blocksize - 12);
491         STORE_LE(dir->name_len2, 2);
492         STORE_LE(dir->file_type2, EXT2_FT_DIR);
493         dir->name2[0] = '.'; dir->name2[1] = '.';
494         PUT((uint64_t)(gd[0].bg_inode_table + itsz + 1) * blocksize, buf, blocksize);
495
496         // dump root dir block
497         STORE_LE(dir->inode1, EXT2_ROOT_INO);
498         STORE_LE(dir->rec_len2, 12);
499         STORE_LE(dir->inode3, EXT2_GOOD_OLD_FIRST_INO);
500         STORE_LE(dir->rec_len3, blocksize - 12 - 12);
501         STORE_LE(dir->name_len3, 10);
502         STORE_LE(dir->file_type3, EXT2_FT_DIR);
503         strcpy(dir->name3, "lost+found");
504         PUT((uint64_t)(gd[0].bg_inode_table + itsz + 0) * blocksize, buf, blocksize);
505
506 //      bb_info_msg("done\n"
507 //              "This filesystem will be automatically checked every %u mounts or\n"
508 //              "%u days, whichever comes first. Use tune2fs -c or -i to override.",
509 //              sb->s_max_mnt_count, sb->s_checkinterval / (3600 * 24)
510 //      );
511
512         // cleanup
513         if (ENABLE_FEATURE_CLEAN_UP) {
514                 free(buf);
515                 free(gd);
516                 free(sb);
517         }
518
519         xclose(fd);
520         return EXIT_SUCCESS;
521 }