blob: 687f18567d7364c465ed5fda2b5081877b5206c8 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger6447ac02005-06-11 05:29:40 +00002/*
3 * mke2fs.c - Make a ext2fs filesystem.
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00004 *
Mike Frysinger6447ac02005-06-11 05:29:40 +00005 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00006 * 2003, 2004, 2005 by Theodore Ts'o.
Mike Frysinger6447ac02005-06-11 05:29:40 +00007 *
Mike Frysinger6447ac02005-06-11 05:29:40 +00008 * This file may be redistributed under the terms of the GNU Public
9 * License.
Mike Frysinger6447ac02005-06-11 05:29:40 +000010 */
11
12/* Usage: mke2fs [options] device
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000013 *
Mike Frysinger6447ac02005-06-11 05:29:40 +000014 * 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 +000015 * enforced (but it's not much fun on a character device :-).
Mike Frysinger6447ac02005-06-11 05:29:40 +000016 */
17
18#include <stdio.h>
19#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
22#include <time.h>
Mike Frysinger6447ac02005-06-11 05:29:40 +000023#include <getopt.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <errno.h>
27#include <mntent.h>
28#include <sys/ioctl.h>
29#include <sys/types.h>
30
31#include "e2fsbb.h"
32#include "ext2fs/ext2_fs.h"
33#include "uuid/uuid.h"
34#include "e2p/e2p.h"
35#include "ext2fs/ext2fs.h"
36#include "util.h"
37
38#define STRIDE_LENGTH 8
39
40#ifndef __sparc__
41#define ZAP_BOOTBLOCK
42#endif
43
Mike Frysinger7f782da2005-10-02 08:10:31 +000044static const char * device_name;
Mike Frysinger6447ac02005-06-11 05:29:40 +000045
46/* Command line options */
47static int cflag;
Mike Frysinger6447ac02005-06-11 05:29:40 +000048static int quiet;
49static int super_only;
50static int force;
51static int noaction;
52static int journal_size;
53static int journal_flags;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000054static const char *bad_blocks_filename;
Mike Frysinger6447ac02005-06-11 05:29:40 +000055static __u32 fs_stride;
56
57static struct ext2_super_block param;
58static char *creator_os;
59static char *volume_label;
60static char *mount_dir;
Mike Frysinger7f782da2005-10-02 08:10:31 +000061static char *journal_device = NULL;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000062static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */
Mike Frysinger6447ac02005-06-11 05:29:40 +000063
64static int sys_page_size = 4096;
65static int linux_version_code = 0;
66
67static int int_log2(int arg)
68{
69 int l = 0;
70
71 arg >>= 1;
72 while (arg) {
73 l++;
74 arg >>= 1;
75 }
76 return l;
77}
78
79static int int_log10(unsigned int arg)
80{
81 int l;
82
83 for (l=0; arg ; l++)
84 arg = arg / 10;
85 return l;
86}
87
Mike Frysinger6447ac02005-06-11 05:29:40 +000088/*
89 * This function sets the default parameters for a filesystem
90 *
91 * The type is specified by the user. The size is the maximum size
92 * (in megabytes) for which a set of parameters applies, with a size
93 * of zero meaning that it is the default parameter for the type.
94 * Note that order is important in the table below.
95 */
96#define DEF_MAX_BLOCKSIZE -1
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +000097static const char default_str[] = "default";
Mike Frysinger6447ac02005-06-11 05:29:40 +000098struct mke2fs_defaults {
99 const char *type;
100 int size;
101 int blocksize;
102 int inode_ratio;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000103};
104
105static const struct mke2fs_defaults settings[] = {
106 { default_str, 0, 4096, 8192 },
Mike Frysinger6447ac02005-06-11 05:29:40 +0000107 { default_str, 512, 1024, 4096 },
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000108 { default_str, 3, 1024, 8192 },
109 { "journal", 0, 4096, 8192 },
110 { "news", 0, 4096, 4096 },
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000111 { "largefile", 0, 4096, 1024 * 1024 },
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000112 { "largefile4", 0, 4096, 4096 * 1024 },
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000113 { 0, 0, 0, 0},
Mike Frysinger6447ac02005-06-11 05:29:40 +0000114};
115
116static void set_fs_defaults(const char *fs_type,
117 struct ext2_super_block *super,
118 int blocksize, int sector_size,
119 int *inode_ratio)
120{
121 int megs;
122 int ratio = 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000123 const struct mke2fs_defaults *p;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000124 int use_bsize = 1024;
125
126 megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024;
127 if (inode_ratio)
128 ratio = *inode_ratio;
129 if (!fs_type)
130 fs_type = default_str;
131 for (p = settings; p->type; p++) {
132 if ((strcmp(p->type, fs_type) != 0) &&
133 (strcmp(p->type, default_str) != 0))
134 continue;
135 if ((p->size != 0) && (megs > p->size))
136 continue;
137 if (ratio == 0)
138 *inode_ratio = p->inode_ratio < blocksize ?
139 blocksize : p->inode_ratio;
140 use_bsize = p->blocksize;
141 }
142 if (blocksize <= 0) {
143 if (use_bsize == DEF_MAX_BLOCKSIZE) {
144 use_bsize = sys_page_size;
145 if ((linux_version_code < (2*65536 + 6*256)) &&
146 (use_bsize > 4096))
147 use_bsize = 4096;
148 }
149 if (sector_size && use_bsize < sector_size)
150 use_bsize = sector_size;
151 if ((blocksize < 0) && (use_bsize < (-blocksize)))
152 use_bsize = -blocksize;
153 blocksize = use_bsize;
154 super->s_blocks_count /= blocksize / 1024;
155 }
156 super->s_log_frag_size = super->s_log_block_size =
157 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
158}
159
160
161/*
162 * Helper function for read_bb_file and test_disk
163 */
164static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk)
165{
166 bb_error_msg("Bad block %u out of range; ignored", blk);
167 return;
168}
169
170/*
Mike Frysinger7f782da2005-10-02 08:10:31 +0000171 * Busybox stuff
172 */
173static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3)));
174static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)
175{
176 va_list ap;
177
178 if (retval) {
179 va_start(ap, fmt);
180 bb_fprintf(stderr,"\nCould not ");
181 bb_vfprintf(stderr, fmt, ap);
182 bb_fprintf(stderr, "\n");
183 va_end(ap);
184 exit(EXIT_FAILURE);
185 }
186}
187
188static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2)));
189static void mke2fs_verbose(const char *fmt, ...)
190{
191 va_list ap;
192
193 if (!quiet) {
194 va_start(ap, fmt);
195 bb_vfprintf(stdout, fmt, ap);
196 fflush(stdout);
197 va_end(ap);
198 }
199}
200
201static void mke2fs_verbose_done(void)
202{
203 mke2fs_verbose("done\n");
204}
205
206static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3)));
207static void mke2fs_warning_msg(int retval, char *fmt, ... )
208{
209 va_list ap;
210
211 if (retval) {
212 va_start(ap, fmt);
213 bb_fprintf(stderr,"\nWarning: ");
214 bb_vfprintf(stderr, fmt, ap);
215 bb_fprintf(stderr, "\n");
216 va_end(ap);
217 }
218}
219
220/*
Mike Frysinger6447ac02005-06-11 05:29:40 +0000221 * Reads the bad blocks list from a file
222 */
223static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
224 const char *bad_blocks_file)
225{
226 FILE *f;
227 errcode_t retval;
228
Rob Landleyd921b2e2006-08-03 15:41:12 +0000229 f = xfopen(bad_blocks_file, "r");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000230 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
231 fclose (f);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000232 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000233}
234
235/*
236 * Runs the badblocks program to test the disk
237 */
238static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
239{
240 FILE *f;
241 errcode_t retval;
242 char buf[1024];
243
244 sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize,
245 quiet ? "" : "-s ", (cflag > 1) ? "-w " : "",
246 fs->device_name, fs->super->s_blocks_count);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000247 mke2fs_verbose("Running command: %s\n", buf);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000248 f = popen(buf, "r");
249 if (!f) {
250 bb_perror_msg_and_die("Could not run '%s'", buf);
251 }
252 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
253 pclose(f);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000254 mke2fs_error_msg_and_die(retval, "read bad blocks from program");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000255}
256
257static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
258{
259 dgrp_t i;
260 blk_t j;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000261 unsigned must_be_good;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000262 blk_t blk;
263 badblocks_iterate bb_iter;
264 errcode_t retval;
265 blk_t group_block;
266 int group;
267 int group_bad;
268
269 if (!bb_list)
270 return;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000271
Mike Frysinger6447ac02005-06-11 05:29:40 +0000272 /*
273 * The primary superblock and group descriptors *must* be
274 * good; if not, abort.
275 */
276 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
277 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
278 if (ext2fs_badblocks_list_test(bb_list, i)) {
279 bb_error_msg_and_die(
280 "Block %d in primary superblock/group descriptor area bad\n"
281 "Blocks %d through %d must be good in order to build a filesystem\n"
282 "Aborting ...", i, fs->super->s_first_data_block, must_be_good);
283 }
284 }
285
286 /*
287 * See if any of the bad blocks are showing up in the backup
288 * superblocks and/or group descriptors. If so, issue a
289 * warning and adjust the block counts appropriately.
290 */
291 group_block = fs->super->s_first_data_block +
292 fs->super->s_blocks_per_group;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000293
Mike Frysinger6447ac02005-06-11 05:29:40 +0000294 for (i = 1; i < fs->group_desc_count; i++) {
295 group_bad = 0;
296 for (j=0; j < fs->desc_blocks+1; j++) {
297 if (ext2fs_badblocks_list_test(bb_list,
298 group_block + j)) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000299 mke2fs_warning_msg(!group_bad,
300 "the backup superblock/group descriptors at block %d contain\n"
301 "bad blocks\n", group_block);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000302 group_bad++;
303 group = ext2fs_group_of_blk(fs, group_block+j);
304 fs->group_desc[group].bg_free_blocks_count++;
305 fs->super->s_free_blocks_count++;
306 }
307 }
308 group_block += fs->super->s_blocks_per_group;
309 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000310
Mike Frysinger6447ac02005-06-11 05:29:40 +0000311 /*
312 * Mark all the bad blocks as used...
313 */
314 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000315 mke2fs_error_msg_and_die(retval, "mark bad blocks as used");
316
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000317 while (ext2fs_badblocks_list_iterate(bb_iter, &blk))
Mike Frysinger6447ac02005-06-11 05:29:40 +0000318 ext2fs_mark_block_bitmap(fs->block_map, blk);
319 ext2fs_badblocks_list_iterate_end(bb_iter);
320}
321
322/*
323 * These functions implement a generalized progress meter.
324 */
325struct progress_struct {
326 char format[20];
327 char backup[80];
328 __u32 max;
329 int skip_progress;
330};
331
332static void progress_init(struct progress_struct *progress,
333 const char *label,__u32 max)
334{
335 int i;
336
337 memset(progress, 0, sizeof(struct progress_struct));
338 if (quiet)
339 return;
340
341 /*
342 * Figure out how many digits we need
343 */
344 i = int_log10(max);
345 sprintf(progress->format, "%%%dd/%%%dld", i, i);
346 memset(progress->backup, '\b', sizeof(progress->backup)-1);
347 progress->backup[sizeof(progress->backup)-1] = 0;
348 if ((2*i)+1 < (int) sizeof(progress->backup))
349 progress->backup[(2*i)+1] = 0;
350 progress->max = max;
351
352 progress->skip_progress = 0;
353 if (getenv("MKE2FS_SKIP_PROGRESS"))
354 progress->skip_progress++;
355
356 fputs(label, stdout);
357 fflush(stdout);
358}
359
360static void progress_update(struct progress_struct *progress, __u32 val)
361{
362 if ((progress->format[0] == 0) || progress->skip_progress)
363 return;
364 printf(progress->format, val, progress->max);
365 fputs(progress->backup, stdout);
366}
367
368static void progress_close(struct progress_struct *progress)
369{
370 if (progress->format[0] == 0)
371 return;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000372 printf("%-28s\n", "done");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000373}
374
375
376/*
377 * Helper function which zeros out _num_ blocks starting at _blk_. In
378 * case of an error, the details of the error is returned via _ret_blk_
379 * and _ret_count_ if they are non-NULL pointers. Returns 0 on
380 * success, and an error code on an error.
381 *
382 * As a special case, if the first argument is NULL, then it will
383 * attempt to free the static zeroizing buffer. (This is to keep
384 * programs that check for memory leaks happy.)
385 */
386static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num,
387 struct progress_struct *progress,
388 blk_t *ret_blk, int *ret_count)
389{
390 int j, count, next_update, next_update_incr;
391 static char *buf;
392 errcode_t retval;
393
394 /* If fs is null, clean up the static buffer and return */
395 if (!fs) {
396 if (buf) {
397 free(buf);
398 buf = 0;
399 }
400 return 0;
401 }
402 /* Allocate the zeroizing buffer if necessary */
403 if (!buf) {
Rob Landley081e3842006-08-03 20:07:35 +0000404 buf = xzalloc(fs->blocksize * STRIDE_LENGTH);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000405 }
406 /* OK, do the write loop */
407 next_update = 0;
408 next_update_incr = num / 100;
409 if (next_update_incr < 1)
410 next_update_incr = 1;
411 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
412 count = num - j;
413 if (count > STRIDE_LENGTH)
414 count = STRIDE_LENGTH;
415 retval = io_channel_write_blk(fs->io, blk, count, buf);
416 if (retval) {
417 if (ret_count)
418 *ret_count = count;
419 if (ret_blk)
420 *ret_blk = blk;
421 return retval;
422 }
423 if (progress && j > next_update) {
424 next_update += num / 100;
425 progress_update(progress, blk);
426 }
427 }
428 return 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000429}
Mike Frysinger6447ac02005-06-11 05:29:40 +0000430
431static void write_inode_tables(ext2_filsys fs)
432{
433 errcode_t retval;
434 blk_t blk;
435 dgrp_t i;
436 int num;
437 struct progress_struct progress;
438
439 if (quiet)
440 memset(&progress, 0, sizeof(progress));
441 else
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000442 progress_init(&progress, "Writing inode tables: ",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000443 fs->group_desc_count);
444
445 for (i = 0; i < fs->group_desc_count; i++) {
446 progress_update(&progress, i);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000447
Mike Frysinger6447ac02005-06-11 05:29:40 +0000448 blk = fs->group_desc[i].bg_inode_table;
449 num = fs->inode_blocks_per_group;
450
451 retval = zero_blocks(fs, blk, num, 0, &blk, &num);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000452 mke2fs_error_msg_and_die(retval,
Mike Frysinger7f782da2005-10-02 08:10:31 +0000453 "write %d blocks in inode table starting at %d.",
454 num, blk);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000455 if (sync_kludge) {
456 if (sync_kludge == 1)
457 sync();
458 else if ((i % sync_kludge) == 0)
459 sync();
460 }
461 }
462 zero_blocks(0, 0, 0, 0, 0, 0);
463 progress_close(&progress);
464}
465
466static void create_root_dir(ext2_filsys fs)
467{
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000468 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000469 struct ext2_inode inode;
470
471 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000472 mke2fs_error_msg_and_die(retval, "create root dir");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000473 if (geteuid()) {
474 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000475 mke2fs_error_msg_and_die(retval, "read root inode");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000476 inode.i_uid = getuid();
477 if (inode.i_uid)
478 inode.i_gid = getgid();
479 retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000480 mke2fs_error_msg_and_die(retval, "set root inode ownership");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000481 }
482}
483
484static void create_lost_and_found(ext2_filsys fs)
485{
486 errcode_t retval;
487 ext2_ino_t ino;
488 const char *name = "lost+found";
Mike Frysinger7f782da2005-10-02 08:10:31 +0000489 int i = 1;
490 char *msg = "create";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000491 int lpf_size = 0;
492
493 fs->umask = 077;
494 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
495 if (retval) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000496 goto CREATE_LOST_AND_FOUND_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000497 }
498
499 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
500 if (retval) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000501 msg = "lookup";
502 goto CREATE_LOST_AND_FOUND_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000503 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000504
Mike Frysinger7f782da2005-10-02 08:10:31 +0000505 for (; i < EXT2_NDIR_BLOCKS; i++) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000506 if ((lpf_size += fs->blocksize) >= 16*1024)
507 break;
508 retval = ext2fs_expand_dir(fs, ino);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000509 msg = "expand";
510CREATE_LOST_AND_FOUND_ERROR:
511 mke2fs_error_msg_and_die(retval, "%s %s", msg, name);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000512 }
513}
514
515static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
516{
517 errcode_t retval;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000518
Mike Frysinger6447ac02005-06-11 05:29:40 +0000519 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
520 fs->group_desc[0].bg_free_inodes_count--;
521 fs->super->s_free_inodes_count--;
522 retval = ext2fs_update_bb_inode(fs, bb_list);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000523 mke2fs_error_msg_and_die(retval, "set bad block inode");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000524}
525
526static void reserve_inodes(ext2_filsys fs)
527{
528 ext2_ino_t i;
529 int group;
530
531 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
532 ext2fs_mark_inode_bitmap(fs->inode_map, i);
533 group = ext2fs_group_of_ino(fs, i);
534 fs->group_desc[group].bg_free_inodes_count--;
535 fs->super->s_free_inodes_count--;
536 }
537 ext2fs_mark_ib_dirty(fs);
538}
539
540#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
541#define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */
542#define BSD_LABEL_OFFSET 64
543
544static void zap_sector(ext2_filsys fs, int sect, int nsect)
545{
546 char *buf;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000547 char *fmt = "could not %s %d";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000548 int retval;
549 unsigned int *magic;
550
Mike Frysinger2401ce52005-06-11 22:24:15 +0000551 buf = xmalloc(512*nsect);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000552
553 if (sect == 0) {
554 /* Check for a BSD disklabel, and don't erase it if so */
555 retval = io_channel_read_blk(fs->io, 0, -512, buf);
556 if (retval)
Mike Frysinger7f782da2005-10-02 08:10:31 +0000557 mke2fs_warning_msg(retval, fmt, "read block", 0);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000558 else {
559 magic = (unsigned int *) (buf + BSD_LABEL_OFFSET);
560 if ((*magic == BSD_DISKMAGIC) ||
561 (*magic == BSD_MAGICDISK))
562 return;
563 }
564 }
565
566 memset(buf, 0, 512*nsect);
567 io_channel_set_blksize(fs->io, 512);
568 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf);
569 io_channel_set_blksize(fs->io, fs->blocksize);
570 free(buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000571 mke2fs_warning_msg(retval, fmt, "erase sector", sect);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000572}
573
574static void create_journal_dev(ext2_filsys fs)
575{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000576 struct progress_struct progress;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000577 errcode_t retval;
578 char *buf;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000579 char *fmt = "%s journal superblock";
Mike Frysinger6447ac02005-06-11 05:29:40 +0000580 blk_t blk;
581 int count;
582
583 retval = ext2fs_create_journal_superblock(fs,
584 fs->super->s_blocks_count, 0, &buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000585 mke2fs_error_msg_and_die(retval, fmt, "init");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000586 if (quiet)
587 memset(&progress, 0, sizeof(progress));
588 else
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000589 progress_init(&progress, "Zeroing journal device: ",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000590 fs->super->s_blocks_count);
591
592 retval = zero_blocks(fs, 0, fs->super->s_blocks_count,
593 &progress, &blk, &count);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000594 mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000595 blk, count);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000596 zero_blocks(0, 0, 0, 0, 0, 0);
597
598 retval = io_channel_write_blk(fs->io,
599 fs->super->s_first_data_block+1,
600 1, buf);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000601 mke2fs_error_msg_and_die(retval, fmt, "write");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000602 progress_close(&progress);
603}
604
605static void show_stats(ext2_filsys fs)
606{
607 struct ext2_super_block *s = fs->super;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000608 char *os;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000609 blk_t group_block;
610 dgrp_t i;
611 int need, col_left;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000612
Mike Frysinger7f782da2005-10-02 08:10:31 +0000613 mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count),
614 "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000615 os = e2p_os2string(fs->super->s_creator_os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000616 printf( "Filesystem label=%.*s\n"
617 "OS type: %s\n"
618 "Block size=%u (log=%u)\n"
619 "Fragment size=%u (log=%u)\n"
620 "%u inodes, %u blocks\n"
621 "%u blocks (%2.2f%%) reserved for the super user\n"
622 "First data block=%u\n",
623 (int) sizeof(s->s_volume_name),
624 s->s_volume_name,
625 os,
626 fs->blocksize, s->s_log_block_size,
627 fs->fragsize, s->s_log_frag_size,
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000628 s->s_inodes_count, s->s_blocks_count,
Mike Frysinger7f782da2005-10-02 08:10:31 +0000629 s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count,
630 s->s_first_data_block);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000631 free(os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000632 if (s->s_reserved_gdt_blocks) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000633 printf("Maximum filesystem blocks=%lu\n",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000634 (s->s_reserved_gdt_blocks + fs->desc_blocks) *
635 (fs->blocksize / sizeof(struct ext2_group_desc)) *
636 s->s_blocks_per_group);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000637 }
638 printf( "%u block group%s\n"
639 "%u blocks per group, %u fragments per group\n"
640 "%u inodes per group\n",
641 fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "",
642 s->s_blocks_per_group, s->s_frags_per_group,
643 s->s_inodes_per_group);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000644 if (fs->group_desc_count == 1) {
Mike Frysinger7f782da2005-10-02 08:10:31 +0000645 puts("");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000646 return;
647 }
648
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000649 printf("Superblock backups stored on blocks: ");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000650 group_block = s->s_first_data_block;
651 col_left = 0;
652 for (i = 1; i < fs->group_desc_count; i++) {
653 group_block += s->s_blocks_per_group;
654 if (!ext2fs_bg_has_super(fs, i))
655 continue;
656 if (i != 1)
657 printf(", ");
658 need = int_log10(group_block) + 2;
659 if (need > col_left) {
660 printf("\n\t");
661 col_left = 72;
662 }
663 col_left -= need;
664 printf("%u", group_block);
665 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000666 puts("\n");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000667}
668
669/*
670 * Set the S_CREATOR_OS field. Return true if OS is known,
671 * otherwise, 0.
672 */
673static int set_os(struct ext2_super_block *sb, char *os)
674{
Mike Frysinger7f782da2005-10-02 08:10:31 +0000675 if (isdigit (*os)) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000676 sb->s_creator_os = atoi (os);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000677 return 1;
678 }
Mike Frysinger6447ac02005-06-11 05:29:40 +0000679
Mike Frysinger7f782da2005-10-02 08:10:31 +0000680 if((sb->s_creator_os = e2p_string2os(os)) >= 0) {
681 return 1;
682 } else if (!strcasecmp("GNU", os)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000683 sb->s_creator_os = EXT2_OS_HURD;
Mike Frysinger7f782da2005-10-02 08:10:31 +0000684 return 1;
685 }
686 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000687}
688
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000689static void parse_extended_opts(struct ext2_super_block *sb_param,
Mike Frysinger6447ac02005-06-11 05:29:40 +0000690 const char *opts)
691{
692 char *buf, *token, *next, *p, *arg;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000693 int r_usage = 0;
694
Rob Landleyd921b2e2006-08-03 15:41:12 +0000695 buf = xstrdup(opts);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000696 for (token = buf; token && *token; token = next) {
697 p = strchr(token, ',');
698 next = 0;
699 if (p) {
700 *p = 0;
701 next = p+1;
702 }
703 arg = strchr(token, '=');
704 if (arg) {
705 *arg = 0;
706 arg++;
707 }
708 if (strcmp(token, "stride") == 0) {
709 if (!arg) {
710 r_usage++;
711 continue;
712 }
713 fs_stride = strtoul(arg, &p, 0);
714 if (*p || (fs_stride == 0)) {
Mike Frysinger874af852006-03-08 07:03:27 +0000715 bb_error_msg("Invalid stride parameter: %s", arg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000716 r_usage++;
717 continue;
718 }
719 } else if (!strcmp(token, "resize")) {
720 unsigned long resize, bpg, rsv_groups;
721 unsigned long group_desc_count, desc_blocks;
722 unsigned int gdpb, blocksize;
723 int rsv_gdb;
724
725 if (!arg) {
726 r_usage++;
727 continue;
728 }
729
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000730 resize = parse_num_blocks(arg,
Mike Frysinger6447ac02005-06-11 05:29:40 +0000731 sb_param->s_log_block_size);
732
733 if (resize == 0) {
734 bb_error_msg("Invalid resize parameter: %s", arg);
735 r_usage++;
736 continue;
737 }
738 if (resize <= sb_param->s_blocks_count) {
Mike Frysinger874af852006-03-08 07:03:27 +0000739 bb_error_msg("The resize maximum must be greater "
740 "than the filesystem size");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000741 r_usage++;
742 continue;
743 }
744
745 blocksize = EXT2_BLOCK_SIZE(sb_param);
746 bpg = sb_param->s_blocks_per_group;
747 if (!bpg)
748 bpg = blocksize * 8;
749 gdpb = blocksize / sizeof(struct ext2_group_desc);
750 group_desc_count = (sb_param->s_blocks_count +
751 bpg - 1) / bpg;
752 desc_blocks = (group_desc_count +
753 gdpb - 1) / gdpb;
754 rsv_groups = (resize + bpg - 1) / bpg;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000755 rsv_gdb = (rsv_groups + gdpb - 1) / gdpb -
Mike Frysinger6447ac02005-06-11 05:29:40 +0000756 desc_blocks;
757 if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param))
758 rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param);
759
760 if (rsv_gdb > 0) {
761 sb_param->s_feature_compat |=
762 EXT2_FEATURE_COMPAT_RESIZE_INODE;
763
764 sb_param->s_reserved_gdt_blocks = rsv_gdb;
765 }
766 } else
767 r_usage++;
768 }
769 if (r_usage) {
770 bb_error_msg_and_die(
771 "\nBad options specified.\n\n"
Mike Frysinger874af852006-03-08 07:03:27 +0000772 "Extended options are separated by commas, "
Mike Frysinger6447ac02005-06-11 05:29:40 +0000773 "and may take an argument which\n"
774 "\tis set off by an equals ('=') sign.\n\n"
Mike Frysinger874af852006-03-08 07:03:27 +0000775 "Valid extended options are:\n"
Mike Frysinger6447ac02005-06-11 05:29:40 +0000776 "\tstride=<stride length in blocks>\n"
777 "\tresize=<resize maximum size in blocks>\n");
778 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000779}
Mike Frysinger6447ac02005-06-11 05:29:40 +0000780
781static __u32 ok_features[3] = {
782 EXT3_FEATURE_COMPAT_HAS_JOURNAL |
783 EXT2_FEATURE_COMPAT_RESIZE_INODE |
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000784 EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */
785 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */
Mike Frysinger6447ac02005-06-11 05:29:40 +0000786 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV|
787 EXT2_FEATURE_INCOMPAT_META_BG,
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000788 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
Mike Frysinger6447ac02005-06-11 05:29:40 +0000789};
790
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000791static int PRS(int argc, char *argv[])
Mike Frysinger6447ac02005-06-11 05:29:40 +0000792{
793 int b, c;
794 int size;
795 char * tmp;
796 int blocksize = 0;
797 int inode_ratio = 0;
798 int inode_size = 0;
799 int reserved_ratio = 5;
800 int sector_size = 0;
801 int show_version_only = 0;
802 ext2_ino_t num_inodes = 0;
803 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000804 char * extended_opts = 0;
805 const char * fs_type = 0;
806 blk_t dev_size;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000807 long sysval;
808
809 /* Update our PATH to include /sbin */
"Vladimir N. Oleynik"d20cfbd2005-10-12 16:22:19 +0000810 e2fs_set_sbin_path();
Mike Frysinger6447ac02005-06-11 05:29:40 +0000811
812 tmp = getenv("MKE2FS_SYNC");
813 if (tmp)
814 sync_kludge = atoi(tmp);
815
816 /* Determine the system page size if possible */
817#if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE))
818#define _SC_PAGESIZE _SC_PAGE_SIZE
819#endif
820#ifdef _SC_PAGESIZE
821 sysval = sysconf(_SC_PAGESIZE);
822 if (sysval > 0)
823 sys_page_size = sysval;
824#endif /* _SC_PAGESIZE */
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000825
Mike Frysinger6447ac02005-06-11 05:29:40 +0000826 setbuf(stdout, NULL);
827 setbuf(stderr, NULL);
828 memset(&param, 0, sizeof(struct ext2_super_block));
829 param.s_rev_level = 1; /* Create revision 1 filesystems now */
830 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE;
831 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000832
833#ifdef __linux__
Bernhard Reutner-Fischere2e56c72006-05-19 11:54:02 +0000834 linux_version_code = get_linux_version_code();
835 if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000836 param.s_rev_level = 0;
837 param.s_feature_incompat = 0;
838 param.s_feature_compat = 0;
839 param.s_feature_ro_compat = 0;
840 }
841#endif
842
Mike Frysinger7f782da2005-10-02 08:10:31 +0000843 /* If called as mkfs.ext3, create a journal inode */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000844 if (last_char_is(bb_applet_name, '3'))
Mike Frysinger7f782da2005-10-02 08:10:31 +0000845 journal_size = -1;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000846
847 while ((c = getopt (argc, argv,
848 "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) {
849 switch (c) {
850 case 'b':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000851 if (safe_strtoi(optarg, &blocksize))
852 goto BLOCKSIZE_ERROR;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000853 b = (blocksize > 0) ? blocksize : -blocksize;
854 if (b < EXT2_MIN_BLOCK_SIZE ||
Mike Frysinger7f782da2005-10-02 08:10:31 +0000855 b > EXT2_MAX_BLOCK_SIZE) {
856BLOCKSIZE_ERROR:
Mike Frysinger874af852006-03-08 07:03:27 +0000857 bb_error_msg_and_die("invalid block size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000858 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000859 mke2fs_warning_msg((blocksize > 4096),
860 "blocksize %d not usable on most systems",
861 blocksize);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000862 if (blocksize > 0)
Mike Frysinger6447ac02005-06-11 05:29:40 +0000863 param.s_log_block_size =
864 int_log2(blocksize >>
865 EXT2_MIN_BLOCK_LOG_SIZE);
866 break;
867 case 'c': /* Check for bad blocks */
868 case 't': /* deprecated */
869 cflag++;
870 break;
871 case 'f':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000872 if (safe_strtoi(optarg, &size) || size < EXT2_MIN_BLOCK_SIZE || size > EXT2_MAX_BLOCK_SIZE ){
Mike Frysinger874af852006-03-08 07:03:27 +0000873 bb_error_msg_and_die("invalid fragment size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000874 }
875 param.s_log_frag_size =
876 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000877 mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option");
Mike Frysinger6447ac02005-06-11 05:29:40 +0000878 break;
879 case 'g':
Eric Andersen3496fdc2006-01-30 23:09:20 +0000880 {
881 int foo;
882 if (safe_strtoi(optarg, &foo)) {
Mike Frysinger6447ac02005-06-11 05:29:40 +0000883 bb_error_msg_and_die("Illegal number for blocks per group");
Eric Andersen3496fdc2006-01-30 23:09:20 +0000884 }
885 param.s_blocks_per_group = foo;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000886 }
887 if ((param.s_blocks_per_group % 8) != 0) {
888 bb_error_msg_and_die("blocks per group must be multiple of 8");
889 }
890 break;
891 case 'i':
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000892 if (safe_strtoi(optarg, &inode_ratio)
893 || inode_ratio < EXT2_MIN_BLOCK_SIZE
Mike Frysinger7f782da2005-10-02 08:10:31 +0000894 || inode_ratio > EXT2_MAX_BLOCK_SIZE * 1024) {
Mike Frysinger874af852006-03-08 07:03:27 +0000895 bb_error_msg_and_die("invalid inode ratio %s (min %d/max %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +0000896 optarg, EXT2_MIN_BLOCK_SIZE,
897 EXT2_MAX_BLOCK_SIZE);
Mike Frysinger7f782da2005-10-02 08:10:31 +0000898 }
Mike Frysinger6447ac02005-06-11 05:29:40 +0000899 break;
900 case 'J':
901 parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg);
902 break;
903 case 'j':
904 param.s_feature_compat |=
905 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
906 if (!journal_size)
907 journal_size = -1;
908 break;
909 case 'l':
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000910 bad_blocks_filename = optarg;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000911 break;
912 case 'm':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000913 if (safe_strtoi(optarg, &reserved_ratio) || reserved_ratio > 50 ) {
Mike Frysinger874af852006-03-08 07:03:27 +0000914 bb_error_msg_and_die("invalid reserved blocks percent - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000915 }
916 break;
917 case 'n':
918 noaction++;
919 break;
920 case 'o':
921 creator_os = optarg;
922 break;
923 case 'r':
924 param.s_rev_level = atoi(optarg);
925 if (param.s_rev_level == EXT2_GOOD_OLD_REV) {
926 param.s_feature_incompat = 0;
927 param.s_feature_compat = 0;
928 param.s_feature_ro_compat = 0;
929 }
930 break;
931 case 's': /* deprecated */
932 if (atoi(optarg))
933 param.s_feature_ro_compat |=
934 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +0000935 else
Mike Frysinger6447ac02005-06-11 05:29:40 +0000936 param.s_feature_ro_compat &=
937 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
938 break;
939#ifdef EXT2_DYNAMIC_REV
940 case 'I':
Mike Frysinger7f782da2005-10-02 08:10:31 +0000941 if (safe_strtoi(optarg, &inode_size)) {
Mike Frysinger874af852006-03-08 07:03:27 +0000942 bb_error_msg_and_die("invalid inode size - %s", optarg);
Mike Frysinger6447ac02005-06-11 05:29:40 +0000943 }
944 break;
945#endif
946 case 'N':
947 num_inodes = atoi(optarg);
948 break;
949 case 'v':
Mike Frysingerdf1eda82005-06-17 02:13:57 +0000950 quiet = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000951 break;
952 case 'q':
953 quiet = 1;
954 break;
955 case 'F':
956 force = 1;
957 break;
958 case 'L':
959 volume_label = optarg;
960 break;
961 case 'M':
962 mount_dir = optarg;
963 break;
964 case 'O':
965 if (!strcmp(optarg, "none")) {
966 param.s_feature_compat = 0;
967 param.s_feature_incompat = 0;
968 param.s_feature_ro_compat = 0;
969 break;
970 }
971 if (e2p_edit_feature(optarg,
972 &param.s_feature_compat,
973 ok_features)) {
974 bb_error_msg_and_die("Invalid filesystem option set: %s", optarg);
975 }
976 break;
977 case 'E':
978 case 'R':
979 extended_opts = optarg;
980 break;
981 case 'S':
982 super_only = 1;
983 break;
984 case 'T':
985 fs_type = optarg;
986 break;
987 case 'V':
988 /* Print version number and exit */
Mike Frysinger7f782da2005-10-02 08:10:31 +0000989 show_version_only = 1;
990 quiet = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +0000991 break;
992 default:
993 bb_show_usage();
994 }
995 }
Mike Frysinger7f782da2005-10-02 08:10:31 +0000996 if ((optind == argc) /*&& !show_version_only*/)
Mike Frysinger6447ac02005-06-11 05:29:40 +0000997 bb_show_usage();
998 device_name = argv[optind++];
999
Mike Frysinger7f782da2005-10-02 08:10:31 +00001000 mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001001
1002 if (show_version_only) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001003 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001004 }
1005
1006 /*
1007 * If there's no blocksize specified and there is a journal
1008 * device, use it to figure out the blocksize
1009 */
1010 if (blocksize <= 0 && journal_device) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001011 ext2_filsys jfs;
1012 io_manager io_ptr;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001013
1014#ifdef CONFIG_TESTIO_DEBUG
1015 io_ptr = test_io_manager;
1016 test_io_backing_manager = unix_io_manager;
1017#else
1018 io_ptr = unix_io_manager;
1019#endif
1020 retval = ext2fs_open(journal_device,
1021 EXT2_FLAG_JOURNAL_DEV_OK, 0,
1022 0, io_ptr, &jfs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001023 mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001024 if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) {
1025 bb_error_msg_and_die(
1026 "Journal dev blocksize (%d) smaller than "
1027 "minimum blocksize %d\n", jfs->blocksize,
1028 -blocksize);
1029 }
1030 blocksize = jfs->blocksize;
1031 param.s_log_block_size =
1032 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
1033 ext2fs_close(jfs);
1034 }
1035
1036 if (blocksize > sys_page_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001037 mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)",
1038 blocksize, sys_page_size);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001039 if (!force) {
Mike Frysinger6447ac02005-06-11 05:29:40 +00001040 proceed_question();
1041 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001042 bb_error_msg("Forced to continue");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001043 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001044 mke2fs_warning_msg(((blocksize > 4096) &&
1045 (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)),
1046 "some 2.4 kernels do not support "
1047 "blocksizes greater than 4096 using ext3.\n"
1048 "Use -b 4096 if this is an issue for you\n");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001049
1050 if (optind < argc) {
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001051 param.s_blocks_count = parse_num_blocks(argv[optind++],
Mike Frysinger6447ac02005-06-11 05:29:40 +00001052 param.s_log_block_size);
Mike Frysinger874af852006-03-08 07:03:27 +00001053 mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001054 }
1055 if (optind < argc)
1056 bb_show_usage();
1057
1058 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1059 if (!fs_type)
1060 fs_type = "journal";
1061 reserved_ratio = 0;
1062 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV;
1063 param.s_feature_compat = 0;
1064 param.s_feature_ro_compat = 0;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001065 }
Mike Frysinger6447ac02005-06-11 05:29:40 +00001066 if (param.s_rev_level == EXT2_GOOD_OLD_REV &&
1067 (param.s_feature_compat || param.s_feature_ro_compat ||
1068 param.s_feature_incompat))
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001069 param.s_rev_level = 1; /* Create a revision 1 filesystem */
Mike Frysinger6447ac02005-06-11 05:29:40 +00001070
Mike Frysinger7f782da2005-10-02 08:10:31 +00001071 check_plausibility(device_name , force);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001072 check_mount(device_name, force, "filesystem");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001073
1074 param.s_log_frag_size = param.s_log_block_size;
1075
1076 if (noaction && param.s_blocks_count) {
1077 dev_size = param.s_blocks_count;
1078 retval = 0;
1079 } else {
1080 retry:
1081 retval = ext2fs_get_device_size(device_name,
1082 EXT2_BLOCK_SIZE(&param),
1083 &dev_size);
1084 if ((retval == EFBIG) &&
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001085 (blocksize == 0) &&
Mike Frysinger6447ac02005-06-11 05:29:40 +00001086 (param.s_log_block_size == 0)) {
1087 param.s_log_block_size = 2;
1088 blocksize = 4096;
1089 goto retry;
1090 }
1091 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001092
Mike Frysinger7f782da2005-10-02 08:10:31 +00001093 mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size");
1094
Mike Frysinger6447ac02005-06-11 05:29:40 +00001095 if (!param.s_blocks_count) {
1096 if (retval == EXT2_ET_UNIMPLEMENTED) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001097 mke2fs_error_msg_and_die(1,
1098 "determine device size; you "
Mike Frysinger6447ac02005-06-11 05:29:40 +00001099 "must specify\nthe size of the "
1100 "filesystem");
1101 } else {
1102 if (dev_size == 0) {
1103 bb_error_msg_and_die(
1104 "Device size reported to be zero. "
1105 "Invalid partition specified, or\n\t"
1106 "partition table wasn't reread "
1107 "after running fdisk, due to\n\t"
1108 "a modified partition being busy "
1109 "and in use. You may need to reboot\n\t"
1110 "to re-read your partition table.\n"
1111 );
1112 }
1113 param.s_blocks_count = dev_size;
1114 if (sys_page_size > EXT2_BLOCK_SIZE(&param))
1115 param.s_blocks_count &= ~((sys_page_size /
1116 EXT2_BLOCK_SIZE(&param))-1);
1117 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001118
Mike Frysinger6447ac02005-06-11 05:29:40 +00001119 } else if (!force && (param.s_blocks_count > dev_size)) {
1120 bb_error_msg("Filesystem larger than apparent device size");
1121 proceed_question();
1122 }
1123
1124 /*
1125 * If the user asked for HAS_JOURNAL, then make sure a journal
1126 * gets created.
1127 */
1128 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
1129 !journal_size)
1130 journal_size = -1;
1131
1132 /* Set first meta blockgroup via an environment variable */
1133 /* (this is mostly for debugging purposes) */
1134 if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) &&
1135 ((tmp = getenv("MKE2FS_FIRST_META_BG"))))
1136 param.s_first_meta_bg = atoi(tmp);
1137
1138 /* Get the hardware sector size, if available */
1139 retval = ext2fs_get_device_sectsize(device_name, &sector_size);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001140 mke2fs_error_msg_and_die(retval, "determine hardware sector size");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001141
1142 if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL)
1143 sector_size = atoi(tmp);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001144
Mike Frysinger6447ac02005-06-11 05:29:40 +00001145 set_fs_defaults(fs_type, &param, blocksize, sector_size, &inode_ratio);
1146 blocksize = EXT2_BLOCK_SIZE(&param);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001147
Mike Frysinger6447ac02005-06-11 05:29:40 +00001148 if (extended_opts)
1149 parse_extended_opts(&param, extended_opts);
1150
1151 /* Since sparse_super is the default, we would only have a problem
1152 * here if it was explicitly disabled.
1153 */
1154 if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) &&
1155 !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) {
1156 bb_error_msg_and_die("reserved online resize blocks not supported "
1157 "on non-sparse filesystem");
1158 }
1159
1160 if (param.s_blocks_per_group) {
1161 if (param.s_blocks_per_group < 256 ||
1162 param.s_blocks_per_group > 8 * (unsigned) blocksize) {
1163 bb_error_msg_and_die("blocks per group count out of range");
1164 }
1165 }
1166
Mike Frysinger874af852006-03-08 07:03:27 +00001167 if (!force && param.s_blocks_count >= (1 << 31)) {
1168 bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n"
1169 "\t (8TB using a blocksize of 4k) are currently supported.");
1170 }
1171
Mike Frysinger6447ac02005-06-11 05:29:40 +00001172 if (inode_size) {
1173 if (inode_size < EXT2_GOOD_OLD_INODE_SIZE ||
1174 inode_size > EXT2_BLOCK_SIZE(&param) ||
1175 inode_size & (inode_size - 1)) {
Mike Frysinger874af852006-03-08 07:03:27 +00001176 bb_error_msg_and_die("invalid inode size %d (min %d/max %d)",
Mike Frysinger6447ac02005-06-11 05:29:40 +00001177 inode_size, EXT2_GOOD_OLD_INODE_SIZE,
1178 blocksize);
1179 }
Mike Frysinger7f782da2005-10-02 08:10:31 +00001180 mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE),
1181 "%d-byte inodes not usable on most systems",
1182 inode_size);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001183 param.s_inode_size = inode_size;
1184 }
1185
1186 /*
1187 * Calculate number of inodes based on the inode ratio
1188 */
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001189 param.s_inodes_count = num_inodes ? num_inodes :
Mike Frysinger6447ac02005-06-11 05:29:40 +00001190 ((__u64) param.s_blocks_count * blocksize)
1191 / inode_ratio;
1192
1193 /*
1194 * Calculate number of blocks to reserve
1195 */
1196 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001197 return 1;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001198}
1199
Bernhard Reutner-Fischer1f6dc6d2006-03-30 13:54:30 +00001200static void mke2fs_clean_up(void)
Mike Frysinger7f782da2005-10-02 08:10:31 +00001201{
1202 if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);
1203}
1204
Mike Frysinger6447ac02005-06-11 05:29:40 +00001205int mke2fs_main (int argc, char *argv[])
1206{
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001207 errcode_t retval;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001208 ext2_filsys fs;
1209 badblocks_list bb_list = 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001210 unsigned int i;
1211 int val;
1212 io_manager io_ptr;
1213
Mike Frysinger7f782da2005-10-02 08:10:31 +00001214 if (ENABLE_FEATURE_CLEAN_UP)
Bernhard Reutner-Fischer1f6dc6d2006-03-30 13:54:30 +00001215 atexit(mke2fs_clean_up);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001216 if(!PRS(argc, argv))
1217 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001218
1219#ifdef CONFIG_TESTIO_DEBUG
1220 io_ptr = test_io_manager;
1221 test_io_backing_manager = unix_io_manager;
1222#else
1223 io_ptr = unix_io_manager;
1224#endif
1225
1226 /*
1227 * Initialize the superblock....
1228 */
1229 retval = ext2fs_initialize(device_name, 0, &param,
1230 io_ptr, &fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001231 mke2fs_error_msg_and_die(retval, "set up superblock");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001232
1233 /*
1234 * Wipe out the old on-disk superblock
1235 */
1236 if (!noaction)
1237 zap_sector(fs, 2, 6);
1238
1239 /*
1240 * Generate a UUID for it...
1241 */
1242 uuid_generate(fs->super->s_uuid);
1243
1244 /*
1245 * Initialize the directory index variables
1246 */
1247 fs->super->s_def_hash_version = EXT2_HASH_TEA;
1248 uuid_generate((unsigned char *) fs->super->s_hash_seed);
1249
1250 /*
1251 * Add "jitter" to the superblock's check interval so that we
1252 * don't check all the filesystems at the same time. We use a
1253 * kludgy hack of using the UUID to derive a random jitter value.
1254 */
1255 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++)
1256 val += fs->super->s_uuid[i];
1257 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT;
1258
1259 /*
1260 * Override the creator OS, if applicable
1261 */
1262 if (creator_os && !set_os(fs->super, creator_os)) {
1263 bb_error_msg_and_die("unknown os - %s", creator_os);
1264 }
1265
1266 /*
1267 * For the Hurd, we will turn off filetype since it doesn't
1268 * support it.
1269 */
1270 if (fs->super->s_creator_os == EXT2_OS_HURD)
1271 fs->super->s_feature_incompat &=
1272 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
1273
1274 /*
1275 * Set the volume label...
1276 */
1277 if (volume_label) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001278 snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001279 }
1280
1281 /*
1282 * Set the last mount directory
1283 */
1284 if (mount_dir) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001285 snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001286 }
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001287
Mike Frysinger6447ac02005-06-11 05:29:40 +00001288 if (!quiet || noaction)
1289 show_stats(fs);
1290
1291 if (noaction)
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001292 return 0;
Mike Frysinger6447ac02005-06-11 05:29:40 +00001293
1294 if (fs->super->s_feature_incompat &
1295 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
1296 create_journal_dev(fs);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001297 return (ext2fs_close(fs) ? 1 : 0);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001298 }
1299
1300 if (bad_blocks_filename)
1301 read_bb_file(fs, &bb_list, bad_blocks_filename);
1302 if (cflag)
1303 test_disk(fs, &bb_list);
1304
1305 handle_bad_blocks(fs, bb_list);
1306 fs->stride = fs_stride;
1307 retval = ext2fs_allocate_tables(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001308 mke2fs_error_msg_and_die(retval, "allocate filesystem tables");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001309 if (super_only) {
1310 fs->super->s_state |= EXT2_ERROR_FS;
1311 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1312 } else {
1313 /* rsv must be a power of two (64kB is MD RAID sb alignment) */
1314 unsigned int rsv = 65536 / fs->blocksize;
1315 unsigned long blocks = fs->super->s_blocks_count;
1316 unsigned long start;
1317 blk_t ret_blk;
1318
1319#ifdef ZAP_BOOTBLOCK
1320 zap_sector(fs, 0, 2);
1321#endif
1322
1323 /*
1324 * Wipe out any old MD RAID (or other) metadata at the end
1325 * of the device. This will also verify that the device is
1326 * as large as we think. Be careful with very small devices.
1327 */
1328 start = (blocks & ~(rsv - 1));
1329 if (start > rsv)
1330 start -= rsv;
1331 if (start > 0)
1332 retval = zero_blocks(fs, start, blocks - start,
1333 NULL, &ret_blk, NULL);
1334
Mike Frysinger7f782da2005-10-02 08:10:31 +00001335 mke2fs_warning_msg(retval, "could not zero block %u at end of filesystem", ret_blk);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001336 write_inode_tables(fs);
1337 create_root_dir(fs);
1338 create_lost_and_found(fs);
1339 reserve_inodes(fs);
1340 create_bad_block_inode(fs, bb_list);
"Vladimir N. Oleynik"b71e6022005-09-19 13:48:39 +00001341 if (fs->super->s_feature_compat &
Mike Frysinger6447ac02005-06-11 05:29:40 +00001342 EXT2_FEATURE_COMPAT_RESIZE_INODE) {
1343 retval = ext2fs_create_resize_inode(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001344 mke2fs_error_msg_and_die(retval, "reserve blocks for online resize");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001345 }
1346 }
1347
1348 if (journal_device) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001349 make_journal_device(journal_device, fs, quiet, force);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001350 } else if (journal_size) {
Mike Frysinger7f782da2005-10-02 08:10:31 +00001351 make_journal_blocks(fs, journal_size, journal_flags, quiet);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001352 }
Mike Frysinger6447ac02005-06-11 05:29:40 +00001353
Mike Frysinger7f782da2005-10-02 08:10:31 +00001354 mke2fs_verbose("Writing superblocks and filesystem accounting information: ");
Mike Frysinger6447ac02005-06-11 05:29:40 +00001355 retval = ext2fs_flush(fs);
Mike Frysinger7f782da2005-10-02 08:10:31 +00001356 mke2fs_warning_msg(retval, "had trouble writing out superblocks");
1357 mke2fs_verbose_done();
1358 if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG"))
1359 print_check_message(fs);
Mike Frysinger6447ac02005-06-11 05:29:40 +00001360 val = ext2fs_close(fs);
1361 return (retval || val) ? 1 : 0;
1362}