blob: e6a1392c9fad373340ef3acafe171382c57a58c3 [file] [log] [blame]
Mike Frysinger6447ac02005-06-11 05:29:40 +00001/*
2 * mke2fs.c - Make a ext2fs filesystem.
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00003 *
Mike Frysinger6447ac02005-06-11 05:29:40 +00004 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00005 * 2003, 2004, 2005 by Theodore Ts'o.
Mike Frysinger6447ac02005-06-11 05:29:40 +00006 *
Mike Frysinger6447ac02005-06-11 05:29:40 +00007 * This file may be redistributed under the terms of the GNU Public
8 * License.
Mike Frysinger6447ac02005-06-11 05:29:40 +00009 */
10
11/* Usage: mke2fs [options] device
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000012 *
Mike Frysinger6447ac02005-06-11 05:29:40 +000013 * The device may be a block device or a image of one, but this isn't
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000014 * enforced (but it's not much fun on a character device :-).
Mike Frysinger6447ac02005-06-11 05:29:40 +000015 */
16
17#include <stdio.h>
18#include <string.h>
19#include <fcntl.h>
20#include <ctype.h>
21#include <time.h>
Mike Frysinger6447ac02005-06-11 05:29:40 +000022#include <getopt.h>
23#include <unistd.h>
24#include <stdlib.h>
25#include <errno.h>
26#include <mntent.h>
27#include <sys/ioctl.h>
28#include <sys/types.h>
29
30#include "e2fsbb.h"
31#include "ext2fs/ext2_fs.h"
32#include "uuid/uuid.h"
33#include "e2p/e2p.h"
34#include "ext2fs/ext2fs.h"
35#include "util.h"
36
37#define STRIDE_LENGTH 8
38
39#ifndef __sparc__
40#define ZAP_BOOTBLOCK
41#endif
42
Mike Frysinger7f782da2005-10-02 08:10:31 +000043static const char * device_name;
Mike Frysinger6447ac02005-06-11 05:29:40 +000044
45/* Command line options */
46static int cflag;
Mike Frysinger6447ac02005-06-11 05:29:40 +000047static int quiet;
48static int super_only;
49static int force;
50static int noaction;
51static int journal_size;
52static int journal_flags;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000053static const char *bad_blocks_filename;
Mike Frysinger6447ac02005-06-11 05:29:40 +000054static __u32 fs_stride;
55
56static struct ext2_super_block param;
57static char *creator_os;
58static char *volume_label;
59static char *mount_dir;
Mike Frysinger7f782da2005-10-02 08:10:31 +000060static char *journal_device = NULL;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000061static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Mike Frysinger6447ac02005-06-11 05:29:40 +000062
63static int sys_page_size = 4096;
64static int linux_version_code = 0;
65
66static int int_log2(int arg)
67{
68 int l = 0;
69
70 arg >>= 1;
71 while (arg) {
72 l++;
73 arg >>= 1;
74 }
75 return l;
76}
77
78static int int_log10(unsigned int arg)
79{
80 int l;
81
82 for (l=0; arg ; l++)
83 arg = arg / 10;
84 return l;
85}
86
Mike Frysinger6447ac02005-06-11 05:29:40 +000087/*
88 * This function sets the default parameters for a filesystem
89 *
90 * The type is specified by the user. The size is the maximum size
91 * (in megabytes) for which a set of parameters applies, with a size
92 * of zero meaning that it is the default parameter for the type.
93 * Note that order is important in the table below.
94 */
95#define DEF_MAX_BLOCKSIZE -1
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000096static const char default_str[] = "default";
Mike Frysinger6447ac02005-06-11 05:29:40 +000097struct mke2fs_defaults {
98 const char *type;
99 int size;
100 int blocksize;
101 int inode_ratio;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000102};
103
104static const struct mke2fs_defaults settings[] = {
105 { default_str, 0, 4096, 8192 },
Mike Frysinger6447ac02005-06-11 05:29:40 +0000106 { default_str, 512, 1024, 4096 },
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000107 { default_str, 3, 1024, 8192 },
108 { "journal", 0, 4096, 8192 },
109 { "news", 0, 4096, 4096 },
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000110 { "largefile", 0, 4096, 1024 * 1024 },
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000111 { "largefile4", 0, 4096, 4096 * 1024 },
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000112 { 0, 0, 0, 0},
Mike Frysinger6447ac02005-06-11 05:29:40 +0000113};
114
115static void set_fs_defaults(const char *fs_type,
116 struct ext2_super_block *super,
117 int blocksize, int sector_size,
118 int *inode_ratio)
119{
120 int megs;
121 int ratio = 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000122 const struct mke2fs_defaults *p;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000123 int use_bsize = 1024;
124
125 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
126 if (inode_ratio)
127 ratio = *inode_ratio;
128 if (!fs_type)
129 fs_type = default_str;
130 for (p = settings; p->type; p++) {
131 if ((strcmp(p->type, fs_type) != 0) &&
132 (strcmp(p->type, default_str) != 0))
133 continue;
134 if ((p->size != 0) && (megs > p->size))
135 continue;
136 if (ratio == 0)
137 *inode_ratio = p->inode_ratio < blocksize ?
138 blocksize : p->inode_ratio;
139 use_bsize = p->blocksize;
140 }
141 if (blocksize <= 0) {
142 if (use_bsize == DEF_MAX_BLOCKSIZE) {
143 use_bsize = sys_page_size;
144 if ((linux_version_code < (2*65536 + 6*256)) &&
145 (use_bsize > 4096))
146 use_bsize = 4096;
147 }
148 if (sector_size && use_bsize < sector_size)
149 use_bsize = sector_size;
150 if ((blocksize < 0) && (use_bsize < (-blocksize)))
151 use_bsize = -blocksize;
152 blocksize = use_bsize;
153 super->s_blocks_count /= blocksize / 1024;
154 }
155 super->s_log_frag_size = super->s_log_block_size =
156 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
157}
158
159
160/*
161 * Helper function for read_bb_file and test_disk
162 */
163static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
164{
165 bb_error_msg("Bad block %u out of range; ignored", blk);
166 return;
167}
168
169/*
Mike Frysinger7f782da2005-10-02 08:10:31 +0000170 * Busybox stuff
171 */
172static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3)));
173static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
174{
175 va_list ap;
176
177 if (retval) {
178 va_start(ap, fmt);
179 bb_fprintf(stderr,"\nCould not ");
180 bb_vfprintf(stderr, fmt, ap);
181 bb_fprintf(stderr, "\n");
182 va_end(ap);
183 exit(EXIT_FAILURE);
184 }
185}
186
187static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2)));
188static void mke2fs_verbose(const char *fmt, ...)
189{
190 va_list ap;
191
192 if (!quiet) {
193 va_start(ap, fmt);
194 bb_vfprintf(stdout, fmt, ap);
195 fflush(stdout);
196 va_end(ap);
197 }
198}
199
200static void mke2fs_verbose_done(void)
201{
202 mke2fs_verbose("done\n");
203}
204
205static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3)));
206static void mke2fs_warning_msg(int retval, char *fmt, ... )
207{
208 va_list ap;
209
210 if (retval) {
211 va_start(ap, fmt);
212 bb_fprintf(stderr,"\nWarning: ");
213 bb_vfprintf(stderr, fmt, ap);
214 bb_fprintf(stderr, "\n");
215 va_end(ap);
216 }
217}
218
219/*
Mike Frysinger6447ac02005-06-11 05:29:40 +0000220 * Reads the bad blocks list from a file
221 */
222static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
223 const char *bad_blocks_file)
224{
225 FILE *f;
226 errcode_t retval;
227
Mike Frysinger7f782da2005-10-02 08:10:31 +0000228 f = bb_xfopen(bad_blocks_file, "r");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
230 fclose (f);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000231 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000232}
233
234/*
235 * Runs the badblocks program to test the disk
236 */
237static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
238{
239 FILE *f;
240 errcode_t retval;
241 char buf[1024];
242
243 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
244 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
245 fs->device_name, fs->super->s_blocks_count);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000246 mke2fs_verbose("Running command: %s\n", buf);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000247 f = popen(buf, "r");
248 if (!f) {
249 bb_perror_msg_and_die("Could not run '%s'", buf);
250 }
251 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
252 pclose(f);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000253 mke2fs_error_msg_and_die(retval, "read bad blocks from program");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000254}
255
256static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
257{
258 dgrp_t i;
259 blk_t j;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000260 unsigned must_be_good;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000261 blk_t blk;
262 badblocks_iterate bb_iter;
263 errcode_t retval;
264 blk_t group_block;
265 int group;
266 int group_bad;
267
268 if (!bb_list)
269 return;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000270
Mike Frysinger6447ac02005-06-11 05:29:40 +0000271 /*
272 * The primary superblock and group descriptors *must* be
273 * good; if not, abort.
274 */
275 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
276 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
277 if (ext2fs_badblocks_list_test(bb_list, i)) {
278 bb_error_msg_and_die(
279 "Block %d in primary superblock/group descriptor area bad\n"
280 "Blocks %d through %d must be good in order to build a filesystem\n"
281 "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
282 }
283 }
284
285 /*
286 * See if any of the bad blocks are showing up in the backup
287 * superblocks and/or group descriptors. If so, issue a
288 * warning and adjust the block counts appropriately.
289 */
290 group_block = fs->super->s_first_data_block +
291 fs->super->s_blocks_per_group;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000292
Mike Frysinger6447ac02005-06-11 05:29:40 +0000293 for (i = 1; i < fs->group_desc_count; i++) {
294 group_bad = 0;
295 for (j=0; j < fs->desc_blocks+1; j++) {
296 if (ext2fs_badblocks_list_test(bb_list,
297 group_block + j)) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000298 mke2fs_warning_msg(!group_bad,
299 "the backup superblock/group descriptors at block %d contain\n"
300 "bad blocks\n", group_block);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000301 group_bad++;
302 group = ext2fs_group_of_blk(fs, group_block+j);
303 fs->group_desc[group].bg_free_blocks_count++;
304 fs->super->s_free_blocks_count++;
305 }
306 }
307 group_block += fs->super->s_blocks_per_group;
308 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000309
Mike Frysinger6447ac02005-06-11 05:29:40 +0000310 /*
311 * Mark all the bad blocks as used...
312 */
313 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000314 mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
315
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000316 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Mike Frysinger6447ac02005-06-11 05:29:40 +0000317 ext2fs_mark_block_bitmap(fs->block_map, blk);
318 ext2fs_badblocks_list_iterate_end(bb_iter);
319}
320
321/*
322 * These functions implement a generalized progress meter.
323 */
324struct progress_struct {
325 char format[20];
326 char backup[80];
327 __u32 max;
328 int skip_progress;
329};
330
331static void progress_init(struct progress_struct *progress,
332 const char *label,__u32 max)
333{
334 int i;
335
336 memset(progress, 0, sizeof(struct progress_struct));
337 if (quiet)
338 return;
339
340 /*
341 * Figure out how many digits we need
342 */
343 i = int_log10(max);
344 sprintf(progress->format, "%%%dd/%%%dld", i, i);
345 memset(progress->backup, '\b', sizeof(progress->backup)-1);
346 progress->backup[sizeof(progress->backup)-1] = 0;
347 if ((2*i)+1 < (int) sizeof(progress->backup))
348 progress->backup[(2*i)+1] = 0;
349 progress->max = max;
350
351 progress->skip_progress = 0;
352 if (getenv("MKE2FS_SKIP_PROGRESS"))
353 progress->skip_progress++;
354
355 fputs(label, stdout);
356 fflush(stdout);
357}
358
359static void progress_update(struct progress_struct *progress, __u32 val)
360{
361 if ((progress->format[0] == 0) || progress->skip_progress)
362 return;
363 printf(progress->format, val, progress->max);
364 fputs(progress->backup, stdout);
365}
366
367static void progress_close(struct progress_struct *progress)
368{
369 if (progress->format[0] == 0)
370 return;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000371 printf("%-28s\n", "done");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000372}
373
374
375/*
376 * Helper function which zeros out _num_ blocks starting at _blk_. In
377 * case of an error, the details of the error is returned via _ret_blk_
378 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
379 * success, and an error code on an error.
380 *
381 * As a special case, if the first argument is NULL, then it will
382 * attempt to free the static zeroizing buffer. (This is to keep
383 * programs that check for memory leaks happy.)
384 */
385static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
386 struct progress_struct *progress,
387 blk_t *ret_blk, int *ret_count)
388{
389 int j, count, next_update, next_update_incr;
390 static char *buf;
391 errcode_t retval;
392
393 /* If fs is null, clean up the static buffer and return */
394 if (!fs) {
395 if (buf) {
396 free(buf);
397 buf = 0;
398 }
399 return 0;
400 }
401 /* Allocate the zeroizing buffer if necessary */
402 if (!buf) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000403 buf = xcalloc(fs->blocksize, STRIDE_LENGTH);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000404 }
405 /* OK, do the write loop */
406 next_update = 0;
407 next_update_incr = num / 100;
408 if (next_update_incr < 1)
409 next_update_incr = 1;
410 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
411 count = num - j;
412 if (count > STRIDE_LENGTH)
413 count = STRIDE_LENGTH;
414 retval = io_channel_write_blk(fs->io, blk, count, buf);
415 if (retval) {
416 if (ret_count)
417 *ret_count = count;
418 if (ret_blk)
419 *ret_blk = blk;
420 return retval;
421 }
422 if (progress && j > next_update) {
423 next_update += num / 100;
424 progress_update(progress, blk);
425 }
426 }
427 return 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000428}
Mike Frysinger6447ac02005-06-11 05:29:40 +0000429
430static void write_inode_tables(ext2_filsys fs)
431{
432 errcode_t retval;
433 blk_t blk;
434 dgrp_t i;
435 int num;
436 struct progress_struct progress;
437
438 if (quiet)
439 memset(&progress, 0, sizeof(progress));
440 else
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000441 progress_init(&progress, "Writing inode tables: ",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000442 fs->group_desc_count);
443
444 for (i = 0; i < fs->group_desc_count; i++) {
445 progress_update(&progress, i);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000446
Mike Frysinger6447ac02005-06-11 05:29:40 +0000447 blk = fs->group_desc[i].bg_inode_table;
448 num = fs->inode_blocks_per_group;
449
450 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000451 mke2fs_error_msg_and_die(retval,
Mike Frysinger7f782da2005-10-02 08:10:31 +0000452 "write %d blocks in inode table starting at %d.",
453 num, blk);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000454 if (sync_kludge) {
455 if (sync_kludge == 1)
456 sync();
457 else if ((i % sync_kludge) == 0)
458 sync();
459 }
460 }
461 zero_blocks(0, 0, 0, 0, 0, 0);
462 progress_close(&progress);
463}
464
465static void create_root_dir(ext2_filsys fs)
466{
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000467 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000468 struct ext2_inode inode;
469
470 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000471 mke2fs_error_msg_and_die(retval, "create root dir");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000472 if (geteuid()) {
473 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000474 mke2fs_error_msg_and_die(retval, "read root inode");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000475 inode.i_uid = getuid();
476 if (inode.i_uid)
477 inode.i_gid = getgid();
478 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000479 mke2fs_error_msg_and_die(retval, "set root inode ownership");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000480 }
481}
482
483static void create_lost_and_found(ext2_filsys fs)
484{
485 errcode_t retval;
486 ext2_ino_t ino;
487 const char *name = "lost+found";
Mike Frysinger7f782da2005-10-02 08:10:31 +0000488 int i = 1;
489 char *msg = "create";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000490 int lpf_size = 0;
491
492 fs->umask = 077;
493 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
494 if (retval) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000495 goto CREATE_LOST_AND_FOUND_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000496 }
497
498 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
499 if (retval) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000500 msg = "lookup";
501 goto CREATE_LOST_AND_FOUND_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000502 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000503
Mike Frysinger7f782da2005-10-02 08:10:31 +0000504 for (; i < EXT2_NDIR_BLOCKS; i++) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000505 if ((lpf_size += fs->blocksize) >= 16*1024)
506 break;
507 retval = ext2fs_expand_dir(fs, ino);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000508 msg = "expand";
509CREATE_LOST_AND_FOUND_ERROR:
510 mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000511 }
512}
513
514static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
515{
516 errcode_t retval;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000517
Mike Frysinger6447ac02005-06-11 05:29:40 +0000518 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
519 fs->group_desc[0].bg_free_inodes_count--;
520 fs->super->s_free_inodes_count--;
521 retval = ext2fs_update_bb_inode(fs, bb_list);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000522 mke2fs_error_msg_and_die(retval, "set bad block inode");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000523}
524
525static void reserve_inodes(ext2_filsys fs)
526{
527 ext2_ino_t i;
528 int group;
529
530 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
531 ext2fs_mark_inode_bitmap(fs->inode_map, i);
532 group = ext2fs_group_of_ino(fs, i);
533 fs->group_desc[group].bg_free_inodes_count--;
534 fs->super->s_free_inodes_count--;
535 }
536 ext2fs_mark_ib_dirty(fs);
537}
538
539#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
540#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
541#define BSD_LABEL_OFFSET 64
542
543static void zap_sector(ext2_filsys fs, int sect, int nsect)
544{
545 char *buf;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000546 char *fmt = "could not %s %d";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000547 int retval;
548 unsigned int *magic;
549
Mike Frysinger2401ce52005-06-11 22:24:15 +0000550 buf = xmalloc(512*nsect);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000551
552 if (sect == 0) {
553 /* Check for a BSD disklabel, and don't erase it if so */
554 retval = io_channel_read_blk(fs->io, 0, -512, buf);
555 if (retval)
Mike Frysinger7f782da2005-10-02 08:10:31 +0000556 mke2fs_warning_msg(retval, fmt, "read block", 0);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000557 else {
558 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
559 if ((*magic == BSD_DISKMAGIC) ||
560 (*magic == BSD_MAGICDISK))
561 return;
562 }
563 }
564
565 memset(buf, 0, 512*nsect);
566 io_channel_set_blksize(fs->io, 512);
567 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
568 io_channel_set_blksize(fs->io, fs->blocksize);
569 free(buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000570 mke2fs_warning_msg(retval, fmt, "erase sector", sect);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000571}
572
573static void create_journal_dev(ext2_filsys fs)
574{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000575 struct progress_struct progress;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000576 errcode_t retval;
577 char *buf;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000578 char *fmt = "%s journal superblock";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000579 blk_t blk;
580 int count;
581
582 retval = ext2fs_create_journal_superblock(fs,
583 fs->super->s_blocks_count, 0, &buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000584 mke2fs_error_msg_and_die(retval, fmt, "init");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000585 if (quiet)
586 memset(&progress, 0, sizeof(progress));
587 else
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000588 progress_init(&progress, "Zeroing journal device: ",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000589 fs->super->s_blocks_count);
590
591 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
592 &progress, &blk, &count);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000593 mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000594 blk, count);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000595 zero_blocks(0, 0, 0, 0, 0, 0);
596
597 retval = io_channel_write_blk(fs->io,
598 fs->super->s_first_data_block+1,
599 1, buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000600 mke2fs_error_msg_and_die(retval, fmt, "write");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000601 progress_close(&progress);
602}
603
604static void show_stats(ext2_filsys fs)
605{
606 struct ext2_super_block *s = fs->super;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000607 char *os;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000608 blk_t group_block;
609 dgrp_t i;
610 int need, col_left;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000611
Mike Frysinger7f782da2005-10-02 08:10:31 +0000612 mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
613 "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000614 os = e2p_os2string(fs->super->s_creator_os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000615 printf( "Filesystem label=%.*s\n"
616 "OS type: %s\n"
617 "Block size=%u (log=%u)\n"
618 "Fragment size=%u (log=%u)\n"
619 "%u inodes, %u blocks\n"
620 "%u blocks (%2.2f%%) reserved for the super user\n"
621 "First data block=%u\n",
622 (int) sizeof(s->s_volume_name),
623 s->s_volume_name,
624 os,
625 fs->blocksize, s->s_log_block_size,
626 fs->fragsize, s->s_log_frag_size,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000627 s->s_inodes_count, s->s_blocks_count,
Mike Frysinger7f782da2005-10-02 08:10:31 +0000628 s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
629 s->s_first_data_block);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000630 free(os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000631 if (s->s_reserved_gdt_blocks) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000632 printf("Maximum filesystem blocks=%lu\n",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000633 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
634 (fs->blocksize / sizeof(struct ext2_group_desc)) *
635 s->s_blocks_per_group);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000636 }
637 printf( "%u block group%s\n"
638 "%u blocks per group, %u fragments per group\n"
639 "%u inodes per group\n",
640 fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
641 s->s_blocks_per_group, s->s_frags_per_group,
642 s->s_inodes_per_group);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000643 if (fs->group_desc_count == 1) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000644 puts("");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000645 return;
646 }
647
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000648 printf("Superblock backups stored on blocks: ");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000649 group_block = s->s_first_data_block;
650 col_left = 0;
651 for (i = 1; i < fs->group_desc_count; i++) {
652 group_block += s->s_blocks_per_group;
653 if (!ext2fs_bg_has_super(fs, i))
654 continue;
655 if (i != 1)
656 printf(", ");
657 need = int_log10(group_block) + 2;
658 if (need > col_left) {
659 printf("\n\t");
660 col_left = 72;
661 }
662 col_left -= need;
663 printf("%u", group_block);
664 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000665 puts("\n");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000666}
667
668/*
669 * Set the S_CREATOR_OS field. Return true if OS is known,
670 * otherwise, 0.
671 */
672static int set_os(struct ext2_super_block *sb, char *os)
673{
Mike Frysinger7f782da2005-10-02 08:10:31 +0000674 if (isdigit (*os)) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000675 sb->s_creator_os = atoi (os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000676 return 1;
677 }
Mike Frysinger6447ac02005-06-11 05:29:40 +0000678
Mike Frysinger7f782da2005-10-02 08:10:31 +0000679 if((sb->s_creator_os = e2p_string2os(os)) >= 0) {
680 return 1;
681 } else if (!strcasecmp("GNU", os)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000682 sb->s_creator_os = EXT2_OS_HURD;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000683 return 1;
684 }
685 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000686}
687
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000688static void parse_extended_opts(struct ext2_super_block *sb_param,
Mike Frysinger6447ac02005-06-11 05:29:40 +0000689 const char *opts)
690{
691 char *buf, *token, *next, *p, *arg;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000692 int r_usage = 0;
693
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000694 buf = bb_xstrdup(opts);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000695 for (token = buf; token && *token; token = next) {
696 p = strchr(token, ',');
697 next = 0;
698 if (p) {
699 *p = 0;
700 next = p+1;
701 }
702 arg = strchr(token, '=');
703 if (arg) {
704 *arg = 0;
705 arg++;
706 }
707 if (strcmp(token, "stride") == 0) {
708 if (!arg) {
709 r_usage++;
710 continue;
711 }
712 fs_stride = strtoul(arg, &p, 0);
713 if (*p || (fs_stride == 0)) {
Mike Frysinger874af852006-03-08 07:03:27 +0000714 bb_error_msg("Invalid stride parameter: %s", arg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000715 r_usage++;
716 continue;
717 }
718 } else if (!strcmp(token, "resize")) {
719 unsigned long resize, bpg, rsv_groups;
720 unsigned long group_desc_count, desc_blocks;
721 unsigned int gdpb, blocksize;
722 int rsv_gdb;
723
724 if (!arg) {
725 r_usage++;
726 continue;
727 }
728
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000729 resize = parse_num_blocks(arg,
Mike Frysinger6447ac02005-06-11 05:29:40 +0000730 sb_param->s_log_block_size);
731
732 if (resize == 0) {
733 bb_error_msg("Invalid resize parameter: %s", arg);
734 r_usage++;
735 continue;
736 }
737 if (resize <= sb_param->s_blocks_count) {
Mike Frysinger874af852006-03-08 07:03:27 +0000738 bb_error_msg("The resize maximum must be greater "
739 "than the filesystem size");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000740 r_usage++;
741 continue;
742 }
743
744 blocksize = EXT2_BLOCK_SIZE(sb_param);
745 bpg = sb_param->s_blocks_per_group;
746 if (!bpg)
747 bpg = blocksize * 8;
748 gdpb = blocksize / sizeof(struct ext2_group_desc);
749 group_desc_count = (sb_param->s_blocks_count +
750 bpg - 1) / bpg;
751 desc_blocks = (group_desc_count +
752 gdpb - 1) / gdpb;
753 rsv_groups = (resize + bpg - 1) / bpg;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000754 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
Mike Frysinger6447ac02005-06-11 05:29:40 +0000755 desc_blocks;
756 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
757 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
758
759 if (rsv_gdb > 0) {
760 sb_param->s_feature_compat |=
761 EXT2_FEATURE_COMPAT_RESIZE_INODE;
762
763 sb_param->s_reserved_gdt_blocks = rsv_gdb;
764 }
765 } else
766 r_usage++;
767 }
768 if (r_usage) {
769 bb_error_msg_and_die(
770 "\nBad options specified.\n\n"
Mike Frysinger874af852006-03-08 07:03:27 +0000771 "Extended options are separated by commas, "
Mike Frysinger6447ac02005-06-11 05:29:40 +0000772 "and may take an argument which\n"
773 "\tis set off by an equals ('=') sign.\n\n"
Mike Frysinger874af852006-03-08 07:03:27 +0000774 "Valid extended options are:\n"
Mike Frysinger6447ac02005-06-11 05:29:40 +0000775 "\tstride=<stride length in blocks>\n"
776 "\tresize=<resize maximum size in blocks>\n");
777 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000778}
Mike Frysinger6447ac02005-06-11 05:29:40 +0000779
780static __u32 ok_features[3] = {
781 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
782 EXT2_FEATURE_COMPAT_RESIZE_INODE |
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000783 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
784 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Mike Frysinger6447ac02005-06-11 05:29:40 +0000785 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
786 EXT2_FEATURE_INCOMPAT_META_BG,
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000787 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
Mike Frysinger6447ac02005-06-11 05:29:40 +0000788};
789
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000790static int PRS(int argc, char *argv[])
Mike Frysinger6447ac02005-06-11 05:29:40 +0000791{
792 int b, c;
793 int size;
794 char * tmp;
795 int blocksize = 0;
796 int inode_ratio = 0;
797 int inode_size = 0;
798 int reserved_ratio = 5;
799 int sector_size = 0;
800 int show_version_only = 0;
801 ext2_ino_t num_inodes = 0;
802 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000803 char * extended_opts = 0;
804 const char * fs_type = 0;
805 blk_t dev_size;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000806 long sysval;
807
808 /* Update our PATH to include /sbin */
"Vladimir N. Oleynik"d20cfbd2005-10-12 16:22:19 +0000809 e2fs_set_sbin_path();
Mike Frysinger6447ac02005-06-11 05:29:40 +0000810
811 tmp = getenv("MKE2FS_SYNC");
812 if (tmp)
813 sync_kludge = atoi(tmp);
814
815 /* Determine the system page size if possible */
816#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
817#define _SC_PAGESIZE _SC_PAGE_SIZE
818#endif
819#ifdef _SC_PAGESIZE
820 sysval = sysconf(_SC_PAGESIZE);
821 if (sysval > 0)
822 sys_page_size = sysval;
823#endif /* _SC_PAGESIZE */
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000824
Mike Frysinger6447ac02005-06-11 05:29:40 +0000825 setbuf(stdout, NULL);
826 setbuf(stderr, NULL);
827 memset(&param, 0, sizeof(struct ext2_super_block));
828 param.s_rev_level = 1; /* Create revision 1 filesystems now */
829 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
830 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
831#if 0
832 param.s_feature_compat |= EXT2_FEATURE_COMPAT_DIR_INDEX;
833#endif
834
835#ifdef __linux__
Mike Frysingerd0615ae2005-06-17 01:35:52 +0000836 linux_version_code = get_kernel_revision();
Mike Frysinger6447ac02005-06-11 05:29:40 +0000837 if (linux_version_code && linux_version_code < (2*65536 + 2*256)) {
838 param.s_rev_level = 0;
839 param.s_feature_incompat = 0;
840 param.s_feature_compat = 0;
841 param.s_feature_ro_compat = 0;
842 }
843#endif
844
Mike Frysinger7f782da2005-10-02 08:10:31 +0000845 /* If called as mkfs.ext3, create a journal inode */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000846 if (last_char_is(bb_applet_name, '3'))
Mike Frysinger7f782da2005-10-02 08:10:31 +0000847 journal_size = -1;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000848
849 while ((c = getopt (argc, argv,
850 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
851 switch (c) {
852 case 'b':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000853 if (safe_strtoi(optarg, &blocksize))
854 goto BLOCKSIZE_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000855 b = (blocksize > 0) ? blocksize : -blocksize;
856 if (b < EXT2_MIN_BLOCK_SIZE ||
Mike Frysinger7f782da2005-10-02 08:10:31 +0000857 b > EXT2_MAX_BLOCK_SIZE) {
858BLOCKSIZE_ERROR:
Mike Frysinger874af852006-03-08 07:03:27 +0000859 bb_error_msg_and_die("invalid block size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000860 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000861 mke2fs_warning_msg((blocksize > 4096),
862 "blocksize %d not usable on most systems",
863 blocksize);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000864 if (blocksize > 0)
Mike Frysinger6447ac02005-06-11 05:29:40 +0000865 param.s_log_block_size =
866 int_log2(blocksize >>
867 EXT2_MIN_BLOCK_LOG_SIZE);
868 break;
869 case 'c': /* Check for bad blocks */
870 case 't': /* deprecated */
871 cflag++;
872 break;
873 case 'f':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000874 if (safe_strtoi(optarg, &size) || size < EXT2_MIN_BLOCK_SIZE || size > EXT2_MAX_BLOCK_SIZE ){
Mike Frysinger874af852006-03-08 07:03:27 +0000875 bb_error_msg_and_die("invalid fragment size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000876 }
877 param.s_log_frag_size =
878 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000879 mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000880 break;
881 case 'g':
Eric Andersen3496fdc2006-01-30 23:09:20 +0000882 {
883 int foo;
884 if (safe_strtoi(optarg, &foo)) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000885 bb_error_msg_and_die("Illegal number for blocks per group");
Eric Andersen3496fdc2006-01-30 23:09:20 +0000886 }
887 param.s_blocks_per_group = foo;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000888 }
889 if ((param.s_blocks_per_group % 8) != 0) {
890 bb_error_msg_and_die("blocks per group must be multiple of 8");
891 }
892 break;
893 case 'i':
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000894 if (safe_strtoi(optarg, &inode_ratio)
895 || inode_ratio < EXT2_MIN_BLOCK_SIZE
Mike Frysinger7f782da2005-10-02 08:10:31 +0000896 || inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) {
Mike Frysinger874af852006-03-08 07:03:27 +0000897 bb_error_msg_and_die("invalid inode ratio %s (min %d/max %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000898 optarg, EXT2_MIN_BLOCK_SIZE,
899 EXT2_MAX_BLOCK_SIZE);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000900 }
Mike Frysinger6447ac02005-06-11 05:29:40 +0000901 break;
902 case 'J':
903 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
904 break;
905 case 'j':
906 param.s_feature_compat |=
907 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
908 if (!journal_size)
909 journal_size = -1;
910 break;
911 case 'l':
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000912 bad_blocks_filename = optarg;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000913 break;
914 case 'm':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000915 if (safe_strtoi(optarg, &reserved_ratio) || reserved_ratio > 50 ) {
Mike Frysinger874af852006-03-08 07:03:27 +0000916 bb_error_msg_and_die("invalid reserved blocks percent - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000917 }
918 break;
919 case 'n':
920 noaction++;
921 break;
922 case 'o':
923 creator_os = optarg;
924 break;
925 case 'r':
926 param.s_rev_level = atoi(optarg);
927 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
928 param.s_feature_incompat = 0;
929 param.s_feature_compat = 0;
930 param.s_feature_ro_compat = 0;
931 }
932 break;
933 case 's': /* deprecated */
934 if (atoi(optarg))
935 param.s_feature_ro_compat |=
936 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000937 else
Mike Frysinger6447ac02005-06-11 05:29:40 +0000938 param.s_feature_ro_compat &=
939 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
940 break;
941#ifdef EXT2_DYNAMIC_REV
942 case 'I':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000943 if (safe_strtoi(optarg, &inode_size)) {
Mike Frysinger874af852006-03-08 07:03:27 +0000944 bb_error_msg_and_die("invalid inode size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000945 }
946 break;
947#endif
948 case 'N':
949 num_inodes = atoi(optarg);
950 break;
951 case 'v':
Mike Frysingerdf1eda82005-06-17 02:13:57 +0000952 quiet = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000953 break;
954 case 'q':
955 quiet = 1;
956 break;
957 case 'F':
958 force = 1;
959 break;
960 case 'L':
961 volume_label = optarg;
962 break;
963 case 'M':
964 mount_dir = optarg;
965 break;
966 case 'O':
967 if (!strcmp(optarg, "none")) {
968 param.s_feature_compat = 0;
969 param.s_feature_incompat = 0;
970 param.s_feature_ro_compat = 0;
971 break;
972 }
973 if (e2p_edit_feature(optarg,
974 &param.s_feature_compat,
975 ok_features)) {
976 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
977 }
978 break;
979 case 'E':
980 case 'R':
981 extended_opts = optarg;
982 break;
983 case 'S':
984 super_only = 1;
985 break;
986 case 'T':
987 fs_type = optarg;
988 break;
989 case 'V':
990 /* Print version number and exit */
Mike Frysinger7f782da2005-10-02 08:10:31 +0000991 show_version_only = 1;
992 quiet = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000993 break;
994 default:
995 bb_show_usage();
996 }
997 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000998 if ((optind == argc) /*&& !show_version_only*/)
Mike Frysinger6447ac02005-06-11 05:29:40 +0000999 bb_show_usage();
1000 device_name = argv[optind++];
1001
Mike Frysinger7f782da2005-10-02 08:10:31 +00001002 mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001003
1004 if (show_version_only) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001005 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001006 }
1007
1008 /*
1009 * If there's no blocksize specified and there is a journal
1010 * device, use it to figure out the blocksize
1011 */
1012 if (blocksize <= 0 && journal_device) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001013 ext2_filsys jfs;
1014 io_manager io_ptr;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001015
1016#ifdef CONFIG_TESTIO_DEBUG
1017 io_ptr = test_io_manager;
1018 test_io_backing_manager = unix_io_manager;
1019#else
1020 io_ptr = unix_io_manager;
1021#endif
1022 retval = ext2fs_open(journal_device,
1023 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1024 0, io_ptr, &jfs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001025 mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001026 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1027 bb_error_msg_and_die(
1028 "Journal dev blocksize (%d) smaller than "
1029 "minimum blocksize %d\n", jfs->blocksize,
1030 -blocksize);
1031 }
1032 blocksize = jfs->blocksize;
1033 param.s_log_block_size =
1034 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1035 ext2fs_close(jfs);
1036 }
1037
1038 if (blocksize > sys_page_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001039 mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
1040 blocksize, sys_page_size);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001041 if (!force) {
Mike Frysinger6447ac02005-06-11 05:29:40 +00001042 proceed_question();
1043 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001044 bb_error_msg("Forced to continue");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001045 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001046 mke2fs_warning_msg(((blocksize > 4096) &&
1047 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
1048 "some 2.4 kernels do not support "
1049 "blocksizes greater than 4096 using ext3.\n"
1050 "Use -b 4096 if this is an issue for you\n");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001051
1052 if (optind < argc) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001053 param.s_blocks_count = parse_num_blocks(argv[optind++],
Mike Frysinger6447ac02005-06-11 05:29:40 +00001054 param.s_log_block_size);
Mike Frysinger874af852006-03-08 07:03:27 +00001055 mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001056 }
1057 if (optind < argc)
1058 bb_show_usage();
1059
1060 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1061 if (!fs_type)
1062 fs_type = "journal";
1063 reserved_ratio = 0;
1064 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1065 param.s_feature_compat = 0;
1066 param.s_feature_ro_compat = 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001067 }
Mike Frysinger6447ac02005-06-11 05:29:40 +00001068 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1069 (param.s_feature_compat || param.s_feature_ro_compat ||
1070 param.s_feature_incompat))
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001071 param.s_rev_level = 1; /* Create a revision 1 filesystem */
Mike Frysinger6447ac02005-06-11 05:29:40 +00001072
Mike Frysinger7f782da2005-10-02 08:10:31 +00001073 check_plausibility(device_name , force);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001074 check_mount(device_name, force, "filesystem");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001075
1076 param.s_log_frag_size = param.s_log_block_size;
1077
1078 if (noaction && param.s_blocks_count) {
1079 dev_size = param.s_blocks_count;
1080 retval = 0;
1081 } else {
1082 retry:
1083 retval = ext2fs_get_device_size(device_name,
1084 EXT2_BLOCK_SIZE(&param),
1085 &dev_size);
1086 if ((retval == EFBIG) &&
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001087 (blocksize == 0) &&
Mike Frysinger6447ac02005-06-11 05:29:40 +00001088 (param.s_log_block_size == 0)) {
1089 param.s_log_block_size = 2;
1090 blocksize = 4096;
1091 goto retry;
1092 }
1093 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001094
Mike Frysinger7f782da2005-10-02 08:10:31 +00001095 mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
1096
Mike Frysinger6447ac02005-06-11 05:29:40 +00001097 if (!param.s_blocks_count) {
1098 if (retval == EXT2_ET_UNIMPLEMENTED) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001099 mke2fs_error_msg_and_die(1,
1100 "determine device size; you "
Mike Frysinger6447ac02005-06-11 05:29:40 +00001101 "must specify\nthe size of the "
1102 "filesystem");
1103 } else {
1104 if (dev_size == 0) {
1105 bb_error_msg_and_die(
1106 "Device size reported to be zero. "
1107 "Invalid partition specified, or\n\t"
1108 "partition table wasn't reread "
1109 "after running fdisk, due to\n\t"
1110 "a modified partition being busy "
1111 "and in use. You may need to reboot\n\t"
1112 "to re-read your partition table.\n"
1113 );
1114 }
1115 param.s_blocks_count = dev_size;
1116 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1117 param.s_blocks_count &= ~((sys_page_size /
1118 EXT2_BLOCK_SIZE(&param))-1);
1119 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001120
Mike Frysinger6447ac02005-06-11 05:29:40 +00001121 } else if (!force && (param.s_blocks_count > dev_size)) {
1122 bb_error_msg("Filesystem larger than apparent device size");
1123 proceed_question();
1124 }
1125
1126 /*
1127 * If the user asked for HAS_JOURNAL, then make sure a journal
1128 * gets created.
1129 */
1130 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1131 !journal_size)
1132 journal_size = -1;
1133
1134 /* Set first meta blockgroup via an environment variable */
1135 /* (this is mostly for debugging purposes) */
1136 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1137 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1138 param.s_first_meta_bg = atoi(tmp);
1139
1140 /* Get the hardware sector size, if available */
1141 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001142 mke2fs_error_msg_and_die(retval, "determine hardware sector size");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001143
1144 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1145 sector_size = atoi(tmp);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001146
Mike Frysinger6447ac02005-06-11 05:29:40 +00001147 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
1148 blocksize = EXT2_BLOCK_SIZE(&param);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001149
Mike Frysinger6447ac02005-06-11 05:29:40 +00001150 if (extended_opts)
1151 parse_extended_opts(&param, extended_opts);
1152
1153 /* Since sparse_super is the default, we would only have a problem
1154 * here if it was explicitly disabled.
1155 */
1156 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1157 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1158 bb_error_msg_and_die("reserved online resize blocks not supported "
1159 "on non-sparse filesystem");
1160 }
1161
1162 if (param.s_blocks_per_group) {
1163 if (param.s_blocks_per_group < 256 ||
1164 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1165 bb_error_msg_and_die("blocks per group count out of range");
1166 }
1167 }
1168
Mike Frysinger874af852006-03-08 07:03:27 +00001169 if (!force && param.s_blocks_count >= (1 << 31)) {
1170 bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
1171 "\t (8TB using a blocksize of 4k) are currently supported.");
1172 }
1173
Mike Frysinger6447ac02005-06-11 05:29:40 +00001174 if (inode_size) {
1175 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1176 inode_size > EXT2_BLOCK_SIZE(&param) ||
1177 inode_size & (inode_size - 1)) {
Mike Frysinger874af852006-03-08 07:03:27 +00001178 bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +00001179 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1180 blocksize);
1181 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001182 mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
1183 "%d-byte inodes not usable on most systems",
1184 inode_size);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001185 param.s_inode_size = inode_size;
1186 }
1187
1188 /*
1189 * Calculate number of inodes based on the inode ratio
1190 */
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001191 param.s_inodes_count = num_inodes ? num_inodes :
Mike Frysinger6447ac02005-06-11 05:29:40 +00001192 ((__u64) param.s_blocks_count * blocksize)
1193 / inode_ratio;
1194
1195 /*
1196 * Calculate number of blocks to reserve
1197 */
1198 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001199 return 1;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001200}
1201
Mike Frysinger7f782da2005-10-02 08:10:31 +00001202static void clean_up(void)
1203{
1204 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
1205}
1206
Mike Frysinger6447ac02005-06-11 05:29:40 +00001207int mke2fs_main (int argc, char *argv[])
1208{
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001209 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001210 ext2_filsys fs;
1211 badblocks_list bb_list = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001212 unsigned int i;
1213 int val;
1214 io_manager io_ptr;
1215
Mike Frysinger7f782da2005-10-02 08:10:31 +00001216 if (ENABLE_FEATURE_CLEAN_UP)
1217 atexit(clean_up);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001218 if(!PRS(argc, argv))
1219 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001220
1221#ifdef CONFIG_TESTIO_DEBUG
1222 io_ptr = test_io_manager;
1223 test_io_backing_manager = unix_io_manager;
1224#else
1225 io_ptr = unix_io_manager;
1226#endif
1227
1228 /*
1229 * Initialize the superblock....
1230 */
1231 retval = ext2fs_initialize(device_name, 0, &param,
1232 io_ptr, &fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001233 mke2fs_error_msg_and_die(retval, "set up superblock");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001234
1235 /*
1236 * Wipe out the old on-disk superblock
1237 */
1238 if (!noaction)
1239 zap_sector(fs, 2, 6);
1240
1241 /*
1242 * Generate a UUID for it...
1243 */
1244 uuid_generate(fs->super->s_uuid);
1245
1246 /*
1247 * Initialize the directory index variables
1248 */
1249 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1250 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1251
1252 /*
1253 * Add "jitter" to the superblock's check interval so that we
1254 * don't check all the filesystems at the same time. We use a
1255 * kludgy hack of using the UUID to derive a random jitter value.
1256 */
1257 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1258 val += fs->super->s_uuid[i];
1259 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1260
1261 /*
1262 * Override the creator OS, if applicable
1263 */
1264 if (creator_os && !set_os(fs->super, creator_os)) {
1265 bb_error_msg_and_die("unknown os - %s", creator_os);
1266 }
1267
1268 /*
1269 * For the Hurd, we will turn off filetype since it doesn't
1270 * support it.
1271 */
1272 if (fs->super->s_creator_os == EXT2_OS_HURD)
1273 fs->super->s_feature_incompat &=
1274 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1275
1276 /*
1277 * Set the volume label...
1278 */
1279 if (volume_label) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001280 snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001281 }
1282
1283 /*
1284 * Set the last mount directory
1285 */
1286 if (mount_dir) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001287 snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001288 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001289
Mike Frysinger6447ac02005-06-11 05:29:40 +00001290 if (!quiet || noaction)
1291 show_stats(fs);
1292
1293 if (noaction)
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001294 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001295
1296 if (fs->super->s_feature_incompat &
1297 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1298 create_journal_dev(fs);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001299 return (ext2fs_close(fs) ? 1 : 0);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001300 }
1301
1302 if (bad_blocks_filename)
1303 read_bb_file(fs, &bb_list, bad_blocks_filename);
1304 if (cflag)
1305 test_disk(fs, &bb_list);
1306
1307 handle_bad_blocks(fs, bb_list);
1308 fs->stride = fs_stride;
1309 retval = ext2fs_allocate_tables(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001310 mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001311 if (super_only) {
1312 fs->super->s_state |= EXT2_ERROR_FS;
1313 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1314 } else {
1315 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1316 unsigned int rsv = 65536 / fs->blocksize;
1317 unsigned long blocks = fs->super->s_blocks_count;
1318 unsigned long start;
1319 blk_t ret_blk;
1320
1321#ifdef ZAP_BOOTBLOCK
1322 zap_sector(fs, 0, 2);
1323#endif
1324
1325 /*
1326 * Wipe out any old MD RAID (or other) metadata at the end
1327 * of the device. This will also verify that the device is
1328 * as large as we think. Be careful with very small devices.
1329 */
1330 start = (blocks & ~(rsv - 1));
1331 if (start > rsv)
1332 start -= rsv;
1333 if (start > 0)
1334 retval = zero_blocks(fs, start, blocks - start,
1335 NULL, &ret_blk, NULL);
1336
Mike Frysinger7f782da2005-10-02 08:10:31 +00001337 mke2fs_warning_msg(retval, "could not zero block %u at end of filesystem", ret_blk);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001338 write_inode_tables(fs);
1339 create_root_dir(fs);
1340 create_lost_and_found(fs);
1341 reserve_inodes(fs);
1342 create_bad_block_inode(fs, bb_list);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001343 if (fs->super->s_feature_compat &
Mike Frysinger6447ac02005-06-11 05:29:40 +00001344 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1345 retval = ext2fs_create_resize_inode(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001346 mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001347 }
1348 }
1349
1350 if (journal_device) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001351 make_journal_device(journal_device, fs, quiet, force);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001352 } else if (journal_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001353 make_journal_blocks(fs, journal_size, journal_flags, quiet);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001354 }
Mike Frysinger6447ac02005-06-11 05:29:40 +00001355
Mike Frysinger7f782da2005-10-02 08:10:31 +00001356 mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001357 retval = ext2fs_flush(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001358 mke2fs_warning_msg(retval, "had trouble writing out superblocks");
1359 mke2fs_verbose_done();
1360 if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
1361 print_check_message(fs);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001362 val = ext2fs_close(fs);
1363 return (retval || val) ? 1 : 0;
1364}