]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - misc/mke2fs.c
Many files:
[thirdparty/e2fsprogs.git] / misc / mke2fs.c
CommitLineData
3839e657
TT
1/*
2 * mke2fs.c - Make a ext2fs filesystem.
3 *
19c78dc0
TT
4 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
3839e657
TT
10 */
11
12/* Usage: mke2fs [options] device
13 *
14 * The device may be a block device or a image of one, but this isn't
15 * enforced (but it's not much fun on a character device :-).
16 */
17
a418d3ad 18#include <stdio.h>
3839e657
TT
19#include <string.h>
20#include <fcntl.h>
21#include <ctype.h>
3839e657 22#include <time.h>
2740156b
TT
23#ifdef linux
24#include <sys/utsname.h>
25#endif
a418d3ad 26#ifdef HAVE_GETOPT_H
3839e657 27#include <getopt.h>
373b8337
TT
28#else
29extern char *optarg;
30extern int optind;
a418d3ad
TT
31#endif
32#ifdef HAVE_UNISTD_H
3839e657 33#include <unistd.h>
a418d3ad
TT
34#endif
35#ifdef HAVE_STDLIB_H
3839e657 36#include <stdlib.h>
a418d3ad
TT
37#endif
38#ifdef HAVE_ERRNO_H
39#include <errno.h>
40#endif
41#ifdef HAVE_MNTENT_H
3839e657 42#include <mntent.h>
a418d3ad 43#endif
3839e657 44#include <sys/ioctl.h>
f3db3566
TT
45#include <sys/types.h>
46
f3db3566 47#include <linux/ext2_fs.h>
3839e657
TT
48
49#include "et/com_err.h"
1e3472c5 50#include "uuid/uuid.h"
896938d5 51#include "e2p/e2p.h"
3839e657 52#include "ext2fs/ext2fs.h"
63985320 53#include "util.h"
3839e657 54#include "../version.h"
d9c56d3c 55#include "nls-enable.h"
3839e657
TT
56
57#define STRIDE_LENGTH 8
58
6733c2fd 59#ifndef __sparc__
1e3472c5
TT
60#define ZAP_BOOTBLOCK
61#endif
62
3839e657 63extern int isatty(int);
f3db3566 64extern FILE *fpopen(const char *cmd, const char *mode);
3839e657
TT
65
66const char * program_name = "mke2fs";
d48755e9 67const char * device_name /* = NULL */;
3839e657
TT
68
69/* Command line options */
d48755e9
TT
70int cflag /* = 0 */ ;
71int verbose /* = 0 */ ;
72int quiet /* = 0 */ ;
73int super_only /* = 0 */ ;
74int force /* = 0 */ ;
75int noaction /* = 0 */ ;
76int journal_size /* = 0 */ ;
77int journal_flags /* = 0 */ ;
78char *bad_blocks_filename /* = 0 */ ;
79__u32 fs_stride /* = 0 */ ;
3839e657
TT
80
81struct ext2_super_block param;
d48755e9
TT
82char *creator_os /* = NULL */ ;
83char *volume_label /* = NULL */ ;
84char *mount_dir /* = NULL */ ;
85char *journal_device /* = NULL */ ;
3839e657 86
63985320 87static void usage(void)
3839e657 88{
d9c56d3c 89 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] "
16ad3334 90 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j journal-options]"
5515e6b4
TT
91 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] "
92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] "
18160d26
TT
93 "[-M last-mounted-directory] [-O feature[,...]]\n\t"
94 "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t"
d9c56d3c 95 "[-qvSV] device [blocks-count]\n"),
3839e657
TT
96 program_name);
97 exit(1);
98}
99
6733c2fd 100static int int_log2(int arg)
3839e657
TT
101{
102 int l = 0;
103
104 arg >>= 1;
105 while (arg) {
106 l++;
107 arg >>= 1;
108 }
109 return l;
110}
111
6733c2fd 112static int int_log10(unsigned int arg)
1dde43f0
TT
113{
114 int l;
115
116 for (l=0; arg ; l++)
117 arg = arg / 10;
118 return l;
119}
120
50787ea2
TT
121/*
122 * This function sets the default parameters for a filesystem
123 *
2740156b
TT
124 * The type is specified by the user. The size is the maximum size
125 * (in megabytes) for which a set of parameters applies, with a size
126 * of zero meaning that it is the default parameter for the type.
127 * Note that order is important in the table below.
50787ea2
TT
128 */
129static char default_str[] = "default";
130struct mke2fs_defaults {
4a600566
TT
131 const char *type;
132 int size;
133 int blocksize;
134 int inode_ratio;
50787ea2
TT
135} settings[] = {
136 { default_str, 0, 4096, 8192 },
137 { default_str, 512, 1024, 4096 },
138 { default_str, 3, 1024, 8192 },
139 { "news", 0, 4096, 4096 },
140 { 0, 0, 0, 0},
141};
142
ef9abe5f 143static void set_fs_defaults(char *fs_type, struct ext2_super_block *super,
50787ea2
TT
144 int blocksize, int *inode_ratio)
145{
146 int megs;
147 int ratio = 0;
148 struct mke2fs_defaults *p;
149
9094f284 150 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) /
50787ea2
TT
151 1024);
152 if (inode_ratio)
153 ratio = *inode_ratio;
154 if (!fs_type)
155 fs_type = default_str;
156 for (p = settings; p->type; p++) {
157 if ((strcmp(p->type, fs_type) != 0) &&
158 (strcmp(p->type, default_str) != 0))
159 continue;
160 if ((p->size != 0) &&
161 (megs > p->size))
162 continue;
163 if (ratio == 0)
164 *inode_ratio = p->inode_ratio;
165 if (blocksize == 0) {
9094f284 166 super->s_log_frag_size = super->s_log_block_size =
6733c2fd 167 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
50787ea2
TT
168 }
169 }
170 if (blocksize == 0)
9094f284 171 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024;
50787ea2
TT
172}
173
3839e657
TT
174/*
175 * Helper function for read_bb_file and test_disk
176 */
177static void invalid_block(ext2_filsys fs, blk_t blk)
178{
d9c56d3c 179 printf(_("Bad block %u out of range; ignored.\n"), blk);
3839e657
TT
180 return;
181}
182
183/*
184 * Reads the bad blocks list from a file
185 */
186static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
187 const char *bad_blocks_file)
188{
189 FILE *f;
190 errcode_t retval;
191
192 f = fopen(bad_blocks_file, "r");
193 if (!f) {
194 com_err("read_bad_blocks_file", errno,
d9c56d3c 195 _("while trying to open %s"), bad_blocks_file);
3839e657
TT
196 exit(1);
197 }
198 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
199 fclose (f);
200 if (retval) {
201 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 202 _("while reading in list of bad blocks from file"));
3839e657
TT
203 exit(1);
204 }
205}
206
207/*
208 * Runs the badblocks program to test the disk
209 */
210static void test_disk(ext2_filsys fs, badblocks_list *bb_list)
211{
212 FILE *f;
213 errcode_t retval;
214 char buf[1024];
215
f635d7f6
TT
216 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize,
217 quiet ? "" : "-s ", fs->device_name,
3839e657
TT
218 fs->super->s_blocks_count);
219 if (verbose)
d9c56d3c 220 printf(_("Running command: %s\n"), buf);
3839e657
TT
221 f = popen(buf, "r");
222 if (!f) {
223 com_err("popen", errno,
d9c56d3c 224 _("while trying run '%s'"), buf);
3839e657
TT
225 exit(1);
226 }
227 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
f3db3566 228 pclose(f);
3839e657
TT
229 if (retval) {
230 com_err("ext2fs_read_bb_FILE", retval,
d9c56d3c 231 _("while processing list of bad blocks from program"));
3839e657
TT
232 exit(1);
233 }
234}
235
236static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list)
237{
f3db3566 238 int i, j;
3839e657
TT
239 int must_be_good;
240 blk_t blk;
241 badblocks_iterate bb_iter;
242 errcode_t retval;
f3db3566
TT
243 blk_t group_block;
244 int group;
245 int group_bad;
3839e657
TT
246
247 if (!bb_list)
248 return;
249
250 /*
251 * The primary superblock and group descriptors *must* be
252 * good; if not, abort.
253 */
254 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks;
255 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) {
256 if (badblocks_list_test(bb_list, i)) {
d9c56d3c
TT
257 fprintf(stderr, _("Block %d in primary "
258 "superblock/group descriptor area bad.\n"), i);
259 fprintf(stderr, _("Blocks %d through %d must be good "
260 "in order to build a filesystem.\n"),
3839e657 261 fs->super->s_first_data_block, must_be_good);
d9c56d3c 262 fprintf(stderr, _("Aborting....\n"));
3839e657
TT
263 exit(1);
264 }
265 }
f3db3566
TT
266
267 /*
268 * See if any of the bad blocks are showing up in the backup
269 * superblocks and/or group descriptors. If so, issue a
270 * warning and adjust the block counts appropriately.
271 */
272 group_block = fs->super->s_first_data_block +
273 fs->super->s_blocks_per_group;
f3db3566
TT
274
275 for (i = 1; i < fs->group_desc_count; i++) {
92bcc595 276 group_bad = 0;
f3db3566
TT
277 for (j=0; j < fs->desc_blocks+1; j++) {
278 if (badblocks_list_test(bb_list, group_block +
279 j)) {
280 if (!group_bad)
281 fprintf(stderr,
d9c56d3c
TT
282_("Warning: the backup superblock/group descriptors at block %d contain\n"
283" bad blocks.\n\n"),
f3db3566
TT
284 group_block);
285 group_bad++;
286 group = ext2fs_group_of_blk(fs, group_block+j);
287 fs->group_desc[group].bg_free_blocks_count++;
288 fs->super->s_free_blocks_count++;
289 }
290 }
291 group_block += fs->super->s_blocks_per_group;
292 }
3839e657
TT
293
294 /*
295 * Mark all the bad blocks as used...
296 */
297 retval = badblocks_list_iterate_begin(bb_list, &bb_iter);
298 if (retval) {
299 com_err("badblocks_list_iterate_begin", retval,
d9c56d3c 300 _("while marking bad blocks as used"));
3839e657
TT
301 exit(1);
302 }
303 while (badblocks_list_iterate(bb_iter, &blk))
f3db3566 304 ext2fs_mark_block_bitmap(fs->block_map, blk);
3839e657
TT
305 badblocks_list_iterate_end(bb_iter);
306}
307
19c78dc0 308static void write_inode_tables(ext2_filsys fs)
3839e657
TT
309{
310 errcode_t retval;
311 blk_t blk;
19c78dc0
TT
312 int i, j, num, count;
313 char *buf;
1dde43f0 314 char format[20], backup[80];
7d5633cf
TT
315 int sync_kludge = 0;
316 char *mke2fs_sync;
317
318 mke2fs_sync = getenv("MKE2FS_SYNC");
319 if (mke2fs_sync)
320 sync_kludge = atoi(mke2fs_sync);
3839e657
TT
321
322 buf = malloc(fs->blocksize * STRIDE_LENGTH);
323 if (!buf) {
d9c56d3c
TT
324 com_err("malloc", ENOMEM,
325 _("while allocating zeroizing buffer"));
3839e657
TT
326 exit(1);
327 }
328 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
1dde43f0
TT
329
330 /*
331 * Figure out how many digits we need
332 */
6733c2fd 333 i = int_log10(fs->group_desc_count);
1dde43f0
TT
334 sprintf(format, "%%%dd/%%%dld", i, i);
335 memset(backup, '\b', sizeof(backup)-1);
336 backup[sizeof(backup)-1] = 0;
337 if ((2*i)+1 < sizeof(backup))
338 backup[(2*i)+1] = 0;
339
3839e657 340 if (!quiet)
d9c56d3c 341 printf(_("Writing inode tables: "));
3839e657
TT
342 for (i = 0; i < fs->group_desc_count; i++) {
343 if (!quiet)
1dde43f0 344 printf(format, i, fs->group_desc_count);
3839e657 345
19c78dc0
TT
346 blk = fs->group_desc[i].bg_inode_table;
347 num = fs->inode_blocks_per_group;
348
349 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) {
350 if (num-j > STRIDE_LENGTH)
351 count = STRIDE_LENGTH;
352 else
353 count = num - j;
354 retval = io_channel_write_blk(fs->io, blk, count, buf);
355 if (retval)
d9c56d3c
TT
356 printf(_("Warning: could not write %d blocks "
357 "in inode table starting at %d: %s\n"),
19c78dc0
TT
358 count, blk, error_message(retval));
359 }
3839e657 360 if (!quiet)
1dde43f0 361 fputs(backup, stdout);
7d5633cf
TT
362 if (sync_kludge) {
363 if (sync_kludge == 1)
364 sync();
365 else if ((i % sync_kludge) == 0)
366 sync();
367 }
3839e657 368 }
19c78dc0 369 free(buf);
3839e657 370 if (!quiet)
d9c56d3c 371 fputs(_("done \n"), stdout);
3839e657
TT
372}
373
374static void create_root_dir(ext2_filsys fs)
375{
376 errcode_t retval;
f3db3566 377 struct ext2_inode inode;
3839e657
TT
378
379 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0);
380 if (retval) {
d9c56d3c 381 com_err("ext2fs_mkdir", retval, _("while creating root dir"));
3839e657
TT
382 exit(1);
383 }
f3db3566
TT
384 if (geteuid()) {
385 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode);
386 if (retval) {
387 com_err("ext2fs_read_inode", retval,
d9c56d3c 388 _("while reading root inode"));
f3db3566
TT
389 exit(1);
390 }
19c78dc0
TT
391 inode.i_uid = getuid();
392 if (inode.i_uid)
393 inode.i_gid = getgid();
f3db3566
TT
394 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode);
395 if (retval) {
396 com_err("ext2fs_write_inode", retval,
d9c56d3c 397 _("while setting root inode ownership"));
f3db3566
TT
398 exit(1);
399 }
400 }
3839e657
TT
401}
402
403static void create_lost_and_found(ext2_filsys fs)
404{
405 errcode_t retval;
406 ino_t ino;
407 const char *name = "lost+found";
408 int i;
50787ea2 409 int lpf_size = 0;
3839e657
TT
410
411 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name);
412 if (retval) {
d9c56d3c
TT
413 com_err("ext2fs_mkdir", retval,
414 _("while creating /lost+found"));
3839e657
TT
415 exit(1);
416 }
417
418 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino);
419 if (retval) {
d9c56d3c
TT
420 com_err("ext2_lookup", retval,
421 _("while looking up /lost+found"));
3839e657
TT
422 exit(1);
423 }
424
425 for (i=1; i < EXT2_NDIR_BLOCKS; i++) {
50787ea2
TT
426 if ((lpf_size += fs->blocksize) >= 16*1024)
427 break;
3839e657
TT
428 retval = ext2fs_expand_dir(fs, ino);
429 if (retval) {
430 com_err("ext2fs_expand_dir", retval,
d9c56d3c 431 _("while expanding /lost+found"));
3839e657
TT
432 exit(1);
433 }
434 }
435}
436
437static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list)
438{
439 errcode_t retval;
440
f3db3566 441 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO);
3839e657
TT
442 fs->group_desc[0].bg_free_inodes_count--;
443 fs->super->s_free_inodes_count--;
444 retval = ext2fs_update_bb_inode(fs, bb_list);
445 if (retval) {
446 com_err("ext2fs_update_bb_inode", retval,
d9c56d3c 447 _("while setting bad block inode"));
3839e657
TT
448 exit(1);
449 }
450
451}
452
453static void reserve_inodes(ext2_filsys fs)
454{
455 ino_t i;
456 int group;
457
7f88b043 458 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) {
f3db3566 459 ext2fs_mark_inode_bitmap(fs->inode_map, i);
3839e657
TT
460 group = ext2fs_group_of_ino(fs, i);
461 fs->group_desc[group].bg_free_inodes_count--;
462 fs->super->s_free_inodes_count--;
463 }
464 ext2fs_mark_ib_dirty(fs);
465}
466
e41784eb 467static void zap_sector(ext2_filsys fs, int sect)
f3db3566
TT
468{
469 char buf[512];
470 int retval;
471
472 memset(buf, 0, 512);
473
e41784eb
TT
474 io_channel_set_blksize(fs->io, 512);
475 retval = io_channel_write_blk(fs->io, sect, -512, buf);
476 io_channel_set_blksize(fs->io, fs->blocksize);
f3db3566 477 if (retval)
e294cf2f 478 printf(_("Warning: could not erase sector %d: %s\n"), sect,
f3db3566
TT
479 error_message(retval));
480}
481
482
3839e657
TT
483static void show_stats(ext2_filsys fs)
484{
ef9abe5f 485 struct ext2_super_block *s = fs->super;
1e3472c5 486 char buf[80];
3839e657 487 blk_t group_block;
1dde43f0 488 int i, need, col_left;
3839e657
TT
489
490 if (param.s_blocks_count != s->s_blocks_count)
d9c56d3c 491 printf(_("warning: %d blocks unused.\n\n"),
3839e657 492 param.s_blocks_count - s->s_blocks_count);
50787ea2
TT
493
494 memset(buf, 0, sizeof(buf));
495 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name));
d9c56d3c
TT
496 printf(_("Filesystem label=%s\n"), buf);
497 printf(_("OS type: "));
1e3472c5
TT
498 switch (fs->super->s_creator_os) {
499 case EXT2_OS_LINUX: printf ("Linux"); break;
ad6783df 500 case EXT2_OS_HURD: printf ("GNU/Hurd"); break;
1e3472c5 501 case EXT2_OS_MASIX: printf ("Masix"); break;
d9c56d3c 502 default: printf (_("(unknown os)"));
1e3472c5 503 }
50787ea2 504 printf("\n");
d9c56d3c 505 printf(_("Block size=%u (log=%u)\n"), fs->blocksize,
50787ea2 506 s->s_log_block_size);
d9c56d3c 507 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize,
50787ea2 508 s->s_log_frag_size);
d9c56d3c 509 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count,
3839e657 510 s->s_blocks_count);
d9c56d3c 511 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"),
3839e657
TT
512 s->s_r_blocks_count,
513 100.0 * s->s_r_blocks_count / s->s_blocks_count);
d9c56d3c
TT
514 printf(_("First data block=%u\n"), s->s_first_data_block);
515 if (fs->group_desc_count > 1)
516 printf(_("%lu block groups\n"), fs->group_desc_count);
517 else
518 printf(_("%lu block group\n"), fs->group_desc_count);
519 printf(_("%u blocks per group, %u fragments per group\n"),
3839e657 520 s->s_blocks_per_group, s->s_frags_per_group);
d9c56d3c 521 printf(_("%u inodes per group\n"), s->s_inodes_per_group);
3839e657
TT
522
523 if (fs->group_desc_count == 1) {
524 printf("\n");
525 return;
526 }
527
d9c56d3c 528 printf(_("Superblock backups stored on blocks: "));
3839e657
TT
529 group_block = s->s_first_data_block;
530 col_left = 0;
531 for (i = 1; i < fs->group_desc_count; i++) {
532 group_block += s->s_blocks_per_group;
521e3685
TT
533 if (!ext2fs_bg_has_super(fs, i))
534 continue;
7671433a
TT
535 if (i != 1)
536 printf(", ");
6733c2fd 537 need = int_log10(group_block) + 2;
1dde43f0 538 if (need > col_left) {
3839e657 539 printf("\n\t");
1dde43f0 540 col_left = 72;
3839e657 541 }
1dde43f0 542 col_left -= need;
a418d3ad 543 printf("%u", group_block);
3839e657
TT
544 }
545 printf("\n\n");
546}
547
1e3472c5
TT
548/*
549 * Set the S_CREATOR_OS field. Return true if OS is known,
550 * otherwise, 0.
551 */
552static int set_os(struct ext2_super_block *sb, char *os)
553{
554 if (isdigit (*os))
555 sb->s_creator_os = atoi (os);
556 else if (strcasecmp(os, "linux") == 0)
557 sb->s_creator_os = EXT2_OS_LINUX;
558 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0)
559 sb->s_creator_os = EXT2_OS_HURD;
560 else if (strcasecmp(os, "masix") == 0)
561 sb->s_creator_os = EXT2_OS_MASIX;
562 else
563 return 0;
564 return 1;
565}
566
a418d3ad
TT
567#define PATH_SET "PATH=/sbin"
568
d163b094 569static void parse_raid_opts(const char *opts)
a29f4d30
TT
570{
571 char *buf, *token, *next, *p, *arg;
572 int len;
573 int raid_usage = 0;
574
575 len = strlen(opts);
576 buf = malloc(len+1);
577 if (!buf) {
d9c56d3c
TT
578 fprintf(stderr, _("Couldn't allocate memory to parse "
579 "raid options!\n"));
a29f4d30
TT
580 exit(1);
581 }
582 strcpy(buf, opts);
583 for (token = buf; token && *token; token = next) {
584 p = strchr(token, ',');
585 next = 0;
586 if (p) {
587 *p = 0;
588 next = p+1;
589 }
590 arg = strchr(token, '=');
591 if (arg) {
592 *arg = 0;
593 arg++;
594 }
595 if (strcmp(token, "stride") == 0) {
596 if (!arg) {
597 raid_usage++;
598 continue;
599 }
600 fs_stride = strtoul(arg, &p, 0);
601 if (*p || (fs_stride == 0)) {
d9c56d3c
TT
602 fprintf(stderr,
603 _("Invalid stride parameter.\n"));
a29f4d30
TT
604 raid_usage++;
605 continue;
606 }
607 } else
608 raid_usage++;
609 }
610 if (raid_usage) {
d9c56d3c 611 fprintf(stderr, _("\nBad raid options specified.\n\n"
a29f4d30
TT
612 "Raid options are separated by commas, "
613 "and may take an argument which\n"
614 "\tis set off by an equals ('=') sign.\n\n"
615 "Valid raid options are:\n"
d9c56d3c 616 "\tstride=<stride length in blocks>\n\n"));
a29f4d30
TT
617 exit(1);
618 }
619}
620
896938d5
TT
621static __u32 ok_features[3] = {
622 0, /* Compat */
623 EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */
624 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */
625};
a29f4d30
TT
626
627
3839e657
TT
628static void PRS(int argc, char *argv[])
629{
4a600566
TT
630 int c;
631 int size;
632 char * tmp;
80c22c90 633 blk_t group_blk_max = 8192;
4a600566
TT
634 int blocksize = 0;
635 int inode_ratio = 0;
636 int reserved_ratio = 5;
637 ino_t num_inodes = 0;
a418d3ad 638 errcode_t retval;
f6f65832 639 int sparse_option = 1;
4a600566 640 char * oldpath = getenv("PATH");
ef9abe5f 641 struct ext2_super_block *param_ext2 = &param;
4a600566 642 char * raid_opts = 0;
8ddaa66b 643 char * journal_opts = 0;
4a600566 644 char * fs_type = 0;
f6f65832 645 const char * feature_set = "filetype";
4a600566 646 blk_t dev_size;
2740156b 647#ifdef linux
4a600566 648 struct utsname ut;
2740156b
TT
649
650 if (uname(&ut)) {
651 perror("uname");
652 exit(1);
653 }
896938d5
TT
654 if ((ut.release[0] == '1') ||
655 (ut.release[0] == '2' && ut.release[1] == '.' &&
656 ut.release[2] < '2' && ut.release[3] == '.'))
80c22c90 657 feature_set = NULL;
2740156b 658#endif
3839e657 659 /* Update our PATH to include /sbin */
a418d3ad
TT
660 if (oldpath) {
661 char *newpath;
662
663 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath));
664 strcpy (newpath, PATH_SET);
665 strcat (newpath, ":");
666 strcat (newpath, oldpath);
667 putenv (newpath);
668 } else
669 putenv (PATH_SET);
3839e657
TT
670
671 setbuf(stdout, NULL);
672 setbuf(stderr, NULL);
673 initialize_ext2_error_table();
674 memset(&param, 0, sizeof(struct ext2_super_block));
50787ea2 675 param.s_rev_level = 1; /* Create revision 1 filesystems now */
3839e657 676
d9c56d3c 677 fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"),
3839e657
TT
678 E2FSPROGS_VERSION, E2FSPROGS_DATE,
679 EXT2FS_VERSION, EXT2FS_DATE);
680 if (argc && *argv)
681 program_name = *argv;
1e3472c5 682 while ((c = getopt (argc, argv,
85ef4ae8 683 "b:cf:g:i:j:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF)
3839e657
TT
684 switch (c) {
685 case 'b':
50787ea2
TT
686 blocksize = strtoul(optarg, &tmp, 0);
687 if (blocksize < 1024 || blocksize > 4096 || *tmp) {
d9c56d3c
TT
688 com_err(program_name, 0,
689 _("bad block size - %s"), optarg);
3839e657
TT
690 exit(1);
691 }
692 param.s_log_block_size =
6733c2fd 693 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE);
80c22c90 694 group_blk_max = blocksize * 8;
3839e657
TT
695 break;
696 case 'c':
697 case 't': /* Check for bad blocks */
698 cflag = 1;
699 break;
700 case 'f':
701 size = strtoul(optarg, &tmp, 0);
702 if (size < 1024 || size > 4096 || *tmp) {
d9c56d3c
TT
703 com_err(program_name, 0,
704 _("bad fragment size - %s"),
3839e657
TT
705 optarg);
706 exit(1);
707 }
708 param.s_log_frag_size =
6733c2fd 709 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE);
d9c56d3c
TT
710 printf(_("Warning: fragments not supported. "
711 "Ignoring -f option\n"));
3839e657
TT
712 break;
713 case 'g':
714 param.s_blocks_per_group = strtoul(optarg, &tmp, 0);
f3db3566
TT
715 if (*tmp) {
716 com_err(program_name, 0,
d9c56d3c 717 _("Illegal number for blocks per group"));
f3db3566
TT
718 exit(1);
719 }
f3db3566
TT
720 if ((param.s_blocks_per_group % 8) != 0) {
721 com_err(program_name, 0,
d9c56d3c 722 _("blocks per group must be multiple of 8"));
3839e657
TT
723 exit(1);
724 }
725 break;
726 case 'i':
727 inode_ratio = strtoul(optarg, &tmp, 0);
728 if (inode_ratio < 1024 || inode_ratio > 256 * 1024 ||
729 *tmp) {
d9c56d3c
TT
730 com_err(program_name, 0,
731 _("bad inode ratio - %s"), optarg);
3839e657
TT
732 exit(1);
733 }
734 break;
85ef4ae8 735 case 'j':
8ddaa66b
TT
736 journal_opts = optarg;
737 break;
3839e657 738 case 'l':
f3db3566
TT
739 bad_blocks_filename = malloc(strlen(optarg)+1);
740 if (!bad_blocks_filename) {
741 com_err(program_name, ENOMEM,
d9c56d3c 742 _("in malloc for bad_blocks_filename"));
f3db3566
TT
743 exit(1);
744 }
745 strcpy(bad_blocks_filename, optarg);
3839e657
TT
746 break;
747 case 'm':
748 reserved_ratio = strtoul(optarg, &tmp, 0);
749 if (reserved_ratio > 50 || *tmp) {
750 com_err(program_name, 0,
d9c56d3c 751 _("bad reserved blocks percent - %s"),
3839e657
TT
752 optarg);
753 exit(1);
754 }
755 break;
50787ea2
TT
756 case 'n':
757 noaction++;
758 break;
1e3472c5
TT
759 case 'o':
760 creator_os = optarg;
761 break;
7f88b043
TT
762 case 'r':
763 param.s_rev_level = atoi(optarg);
764 break;
521e3685
TT
765 case 's':
766 sparse_option = atoi(optarg);
767 break;
7f88b043
TT
768#ifdef EXT2_DYNAMIC_REV
769 case 'I':
770 param.s_inode_size = atoi(optarg);
771 break;
772#endif
5515e6b4
TT
773 case 'N':
774 num_inodes = atoi(optarg);
775 break;
3839e657
TT
776 case 'v':
777 verbose = 1;
778 break;
779 case 'q':
780 quiet = 1;
781 break;
74becf3c
TT
782 case 'F':
783 force = 1;
784 break;
1e3472c5
TT
785 case 'L':
786 volume_label = optarg;
787 break;
788 case 'M':
789 mount_dir = optarg;
790 break;
896938d5
TT
791 case 'O':
792 feature_set = optarg;
793 break;
a29f4d30
TT
794 case 'R':
795 raid_opts = optarg;
796 break;
f3db3566
TT
797 case 'S':
798 super_only = 1;
799 break;
50787ea2
TT
800 case 'T':
801 fs_type = optarg;
802 break;
818180cd
TT
803 case 'V':
804 /* Print version number and exit */
d9c56d3c 805 fprintf(stderr, _("\tUsing %s\n"),
818180cd
TT
806 error_message(EXT2_ET_BASE));
807 exit(0);
3839e657
TT
808 default:
809 usage();
810 }
811 if (optind == argc)
812 usage();
813 device_name = argv[optind];
814 optind++;
815 if (optind < argc) {
e1a0a3e3
TT
816 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0);
817
818 if ((*tmp) || (tmp2 > 0xfffffffful)) {
d9c56d3c 819 com_err(program_name, 0, _("bad blocks count - %s"),
3839e657
TT
820 argv[optind - 1]);
821 exit(1);
822 }
e1a0a3e3 823 param.s_blocks_count = tmp2;
3839e657
TT
824 }
825 if (optind < argc)
826 usage();
a418d3ad 827
a29f4d30
TT
828 if (raid_opts)
829 parse_raid_opts(raid_opts);
830
8ddaa66b
TT
831 if (journal_opts)
832 parse_journal_opts(journal_opts);
833
74becf3c 834 if (!force)
8ddaa66b 835 check_plausibility(device_name);
63985320 836 check_mount(device_name, force, _("filesystem"));
a418d3ad 837
3839e657
TT
838 param.s_log_frag_size = param.s_log_block_size;
839
50787ea2
TT
840 if (noaction && param.s_blocks_count) {
841 dev_size = param.s_blocks_count;
842 retval = 0;
843 } else
844 retval = ext2fs_get_device_size(device_name,
845 EXT2_BLOCK_SIZE(&param),
846 &dev_size);
a789d840
TT
847 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
848 com_err(program_name, retval,
d9c56d3c 849 _("while trying to determine filesystem size"));
a789d840
TT
850 exit(1);
851 }
a418d3ad 852 if (!param.s_blocks_count) {
a789d840
TT
853 if (retval == EXT2_ET_UNIMPLEMENTED) {
854 com_err(program_name, 0,
d9c56d3c 855 _("Couldn't determine device size; you "
a789d840 856 "must specify\nthe size of the "
d9c56d3c 857 "filesystem\n"));
a418d3ad 858 exit(1);
26ab5315
TT
859 } else {
860 if (dev_size == 0) {
861 com_err(program_name, 0,
862 _("Device size reported to be zero. "
863 "Invalid partition specified, or\n\t"
864 "partition table wasn't reread "
865 "after running fdisk, due to\n\t"
866 "a modified partition being busy "
867 "and in use. You may need to reboot\n\t"
868 "to re-read your partition table.\n"
869 ));
870 exit(1);
871 }
a789d840 872 param.s_blocks_count = dev_size;
26ab5315
TT
873 }
874
a789d840
TT
875 } else if (!force && (param.s_blocks_count > dev_size)) {
876 com_err(program_name, 0,
d9c56d3c 877 _("Filesystem larger than apparent filesystem size."));
a789d840 878 proceed_question();
a418d3ad 879 }
3839e657 880
50787ea2
TT
881 set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio);
882
521e3685
TT
883 if (param.s_blocks_per_group) {
884 if (param.s_blocks_per_group < 256 ||
80c22c90 885 param.s_blocks_per_group > group_blk_max || *tmp) {
521e3685 886 com_err(program_name, 0,
d9c56d3c 887 _("blocks per group count out of range"));
521e3685
TT
888 exit(1);
889 }
890 }
891
3839e657
TT
892 /*
893 * Calculate number of inodes based on the inode ratio
894 */
5515e6b4 895 param.s_inodes_count = num_inodes ? num_inodes :
e6597048 896 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(&param))
f3db3566 897 / inode_ratio;
3839e657
TT
898
899 /*
900 * Calculate number of blocks to reserve
901 */
902 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100;
521e3685 903
f6f65832
TT
904 /* Turn off features not supported by the earlier filesystem version */
905 if (param.s_rev_level == 0) {
906 sparse_option = 0;
907 feature_set = NULL;
908 }
2740156b 909 if (sparse_option)
521e3685
TT
910 param_ext2->s_feature_ro_compat |=
911 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER;
f6f65832 912
896938d5 913 if (feature_set && !strncasecmp(feature_set, "none", 4))
80c22c90 914 feature_set = NULL;
896938d5
TT
915 if (feature_set && e2p_edit_feature(feature_set,
916 &param_ext2->s_feature_compat,
917 ok_features)) {
d9c56d3c 918 fprintf(stderr, _("Invalid filesystem option set: %s\n"),
896938d5
TT
919 feature_set);
920 exit(1);
921 }
3839e657
TT
922}
923
924int main (int argc, char *argv[])
925{
926 errcode_t retval = 0;
927 ext2_filsys fs;
928 badblocks_list bb_list = 0;
85ef4ae8 929 int journal_blocks;
d9c56d3c
TT
930
931#ifdef ENABLE_NLS
932 setlocale(LC_MESSAGES, "");
933 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
934 textdomain(NLS_CAT_NAME);
935#endif
3839e657
TT
936 PRS(argc, argv);
937
3839e657
TT
938 /*
939 * Initialize the superblock....
940 */
941 retval = ext2fs_initialize(device_name, 0, &param,
942 unix_io_manager, &fs);
943 if (retval) {
d9c56d3c 944 com_err(device_name, retval, _("while setting up superblock"));
3839e657
TT
945 exit(1);
946 }
947
e41784eb
TT
948 /*
949 * Wipe out the old on-disk superblock
950 */
951 zap_sector(fs, 2);
952
1e3472c5
TT
953 /*
954 * Generate a UUID for it...
955 */
ef9abe5f 956 uuid_generate(fs->super->s_uuid);
1e3472c5
TT
957
958 /*
959 * Override the creator OS, if applicable
960 */
961 if (creator_os && !set_os(fs->super, creator_os)) {
d9c56d3c 962 com_err (program_name, 0, _("unknown os - %s"), creator_os);
1e3472c5
TT
963 exit(1);
964 }
965
4ea0a110
TT
966 /*
967 * For the Hurd, we will turn off filetype since it doesn't
968 * support it.
969 */
970 if (fs->super->s_creator_os == EXT2_OS_HURD)
971 fs->super->s_feature_incompat &=
972 ~EXT2_FEATURE_INCOMPAT_FILETYPE;
973
1e3472c5
TT
974 /*
975 * Set the volume label...
976 */
977 if (volume_label) {
ef9abe5f
TT
978 memset(fs->super->s_volume_name, 0,
979 sizeof(fs->super->s_volume_name));
980 strncpy(fs->super->s_volume_name, volume_label,
981 sizeof(fs->super->s_volume_name));
1e3472c5
TT
982 }
983
984 /*
985 * Set the last mount directory
986 */
987 if (mount_dir) {
ef9abe5f
TT
988 memset(fs->super->s_last_mounted, 0,
989 sizeof(fs->super->s_last_mounted));
990 strncpy(fs->super->s_last_mounted, mount_dir,
991 sizeof(fs->super->s_last_mounted));
1e3472c5
TT
992 }
993
50787ea2 994 if (!quiet || noaction)
3839e657
TT
995 show_stats(fs);
996
50787ea2
TT
997 if (noaction)
998 exit(0);
999
3839e657
TT
1000 if (bad_blocks_filename)
1001 read_bb_file(fs, &bb_list, bad_blocks_filename);
1002 if (cflag)
1003 test_disk(fs, &bb_list);
1004
1005 handle_bad_blocks(fs, bb_list);
a29f4d30 1006 fs->stride = fs_stride;
19c78dc0
TT
1007 retval = ext2fs_allocate_tables(fs);
1008 if (retval) {
1009 com_err(program_name, retval,
d9c56d3c 1010 _("while trying to allocate filesystem tables"));
19c78dc0
TT
1011 exit(1);
1012 }
f3db3566
TT
1013 if (super_only) {
1014 fs->super->s_state |= EXT2_ERROR_FS;
1015 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY);
1016 } else {
19c78dc0 1017 write_inode_tables(fs);
f3db3566
TT
1018 create_root_dir(fs);
1019 create_lost_and_found(fs);
1020 reserve_inodes(fs);
1021 create_bad_block_inode(fs, bb_list);
1e3472c5 1022#ifdef ZAP_BOOTBLOCK
e41784eb 1023 zap_sector(fs, 0);
1e3472c5 1024#endif
f3db3566 1025 }
8ddaa66b 1026
d48755e9 1027 journal_blocks = journal_size * 1024 / (fs->blocksize / 1024);
8ddaa66b
TT
1028 if (journal_device) {
1029 if (!force)
1030 check_plausibility(journal_device);
63985320 1031 check_mount(journal_device, force, _("journal"));
8ddaa66b
TT
1032
1033 if (!quiet)
1034 printf(_("Creating journal on device %s: "),
1035 journal_device);
d48755e9
TT
1036 retval = ext2fs_add_journal_device(fs, journal_device,
1037 journal_blocks,
1038 journal_flags);
8ddaa66b
TT
1039 if(retval) {
1040 com_err (program_name, retval,
1041 _("while trying to create journal on device %s"),
1042 journal_device);
1043 exit(1);
1044 }
1045 if (!quiet)
1046 printf(_("done\n"));
1047 } else if (journal_size) {
85ef4ae8 1048 if (!quiet)
16ad3334
TT
1049 printf(_("Creating journal (%d blocks): "),
1050 journal_blocks);
63985320
TT
1051 retval = ext2fs_add_journal_inode(fs, journal_blocks,
1052 journal_flags);
85ef4ae8
TT
1053 if (retval) {
1054 com_err (program_name, retval,
1055 _("while trying to create journal"));
1056 exit(1);
1057 }
1058 if (!quiet)
1059 printf(_("done\n"));
1060 }
3839e657
TT
1061
1062 if (!quiet)
d9c56d3c
TT
1063 printf(_("Writing superblocks and "
1064 "filesystem accounting information: "));
5d45d803
TT
1065 retval = ext2fs_flush(fs);
1066 if (retval) {
d9c56d3c 1067 printf(_("\nWarning, had trouble writing out superblocks."));
5d45d803 1068 }
3839e657 1069 if (!quiet)
d9c56d3c 1070 printf(_("done\n"));
5d45d803 1071 ext2fs_close(fs);
3839e657
TT
1072 return 0;
1073}