mke2fs.c revision 3f85f65c98183c3871e24f285e5b3e1e97afd9f5
1/* 2 * mke2fs.c - Make a ext2fs filesystem. 3 * 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% 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 18#include <stdio.h> 19#include <string.h> 20#include <fcntl.h> 21#include <ctype.h> 22#include <time.h> 23#ifdef linux 24#include <sys/utsname.h> 25#endif 26#ifdef HAVE_GETOPT_H 27#include <getopt.h> 28#else 29extern char *optarg; 30extern int optind; 31#endif 32#ifdef HAVE_UNISTD_H 33#include <unistd.h> 34#endif 35#ifdef HAVE_STDLIB_H 36#include <stdlib.h> 37#endif 38#ifdef HAVE_ERRNO_H 39#include <errno.h> 40#endif 41#ifdef HAVE_MNTENT_H 42#include <mntent.h> 43#endif 44#include <sys/ioctl.h> 45#include <sys/types.h> 46 47#include "ext2fs/ext2_fs.h" 48#include "et/com_err.h" 49#include "uuid/uuid.h" 50#include "e2p/e2p.h" 51#include "ext2fs/ext2fs.h" 52#include "util.h" 53#include "../version.h" 54#include "nls-enable.h" 55 56#define STRIDE_LENGTH 8 57 58#ifndef __sparc__ 59#define ZAP_BOOTBLOCK 60#endif 61 62extern int isatty(int); 63extern FILE *fpopen(const char *cmd, const char *mode); 64 65const char * program_name = "mke2fs"; 66const char * device_name /* = NULL */; 67 68/* Command line options */ 69int cflag; 70int verbose; 71int quiet; 72int super_only; 73int force; 74int noaction; 75int journal_size; 76int journal_flags; 77char *bad_blocks_filename; 78__u32 fs_stride; 79 80struct ext2_super_block param; 81char *creator_os; 82char *volume_label; 83char *mount_dir; 84char *journal_device; 85int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ 86 87static void usage(void) 88{ 89 fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] " 90 "[-f fragment-size]\n\t[-i bytes-per-inode] [-j] [-J journal-options]" 91 " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] " 92 "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] " 93 "[-M last-mounted-directory] [-O feature[,...]]\n\t" 94 "[-r fs-revision] [-R raid_opts] [-qvSV] device [blocks-count]\n"), 95 program_name); 96 exit(1); 97} 98 99static int int_log2(int arg) 100{ 101 int l = 0; 102 103 arg >>= 1; 104 while (arg) { 105 l++; 106 arg >>= 1; 107 } 108 return l; 109} 110 111static int int_log10(unsigned int arg) 112{ 113 int l; 114 115 for (l=0; arg ; l++) 116 arg = arg / 10; 117 return l; 118} 119 120/* 121 * This function sets the default parameters for a filesystem 122 * 123 * The type is specified by the user. The size is the maximum size 124 * (in megabytes) for which a set of parameters applies, with a size 125 * of zero meaning that it is the default parameter for the type. 126 * Note that order is important in the table below. 127 */ 128static char default_str[] = "default"; 129struct mke2fs_defaults { 130 const char *type; 131 int size; 132 int blocksize; 133 int inode_ratio; 134} settings[] = { 135 { default_str, 0, 4096, 8192 }, 136 { default_str, 512, 1024, 4096 }, 137 { default_str, 3, 1024, 8192 }, 138 { "journal", 0, 4096, 8192 }, 139 { "news", 0, 4096, 4096 }, 140 { "largefile", 0, 4096, 1024 * 1024 }, 141 { "largefile4", 0, 4096, 4096 * 1024 }, 142 { 0, 0, 0, 0}, 143}; 144 145static void set_fs_defaults(char *fs_type, struct ext2_super_block *super, 146 int blocksize, int *inode_ratio) 147{ 148 int megs; 149 int ratio = 0; 150 struct mke2fs_defaults *p; 151 152 megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 153 1024); 154 if (inode_ratio) 155 ratio = *inode_ratio; 156 if (!fs_type) 157 fs_type = default_str; 158 for (p = settings; p->type; p++) { 159 if ((strcmp(p->type, fs_type) != 0) && 160 (strcmp(p->type, default_str) != 0)) 161 continue; 162 if ((p->size != 0) && 163 (megs > p->size)) 164 continue; 165 if (ratio == 0) 166 *inode_ratio = p->inode_ratio; 167 if (blocksize == 0) { 168 super->s_log_frag_size = super->s_log_block_size = 169 int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); 170 } 171 } 172 if (blocksize == 0) 173 super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024; 174} 175 176/* 177 * Helper function for read_bb_file and test_disk 178 */ 179static void invalid_block(ext2_filsys fs, blk_t blk) 180{ 181 printf(_("Bad block %u out of range; ignored.\n"), blk); 182 return; 183} 184 185/* 186 * Reads the bad blocks list from a file 187 */ 188static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, 189 const char *bad_blocks_file) 190{ 191 FILE *f; 192 errcode_t retval; 193 194 f = fopen(bad_blocks_file, "r"); 195 if (!f) { 196 com_err("read_bad_blocks_file", errno, 197 _("while trying to open %s"), bad_blocks_file); 198 exit(1); 199 } 200 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 201 fclose (f); 202 if (retval) { 203 com_err("ext2fs_read_bb_FILE", retval, 204 _("while reading in list of bad blocks from file")); 205 exit(1); 206 } 207} 208 209/* 210 * Runs the badblocks program to test the disk 211 */ 212static void test_disk(ext2_filsys fs, badblocks_list *bb_list) 213{ 214 FILE *f; 215 errcode_t retval; 216 char buf[1024]; 217 218 sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize, 219 quiet ? "" : "-s ", fs->device_name, 220 fs->super->s_blocks_count); 221 if (verbose) 222 printf(_("Running command: %s\n"), buf); 223 f = popen(buf, "r"); 224 if (!f) { 225 com_err("popen", errno, 226 _("while trying run '%s'"), buf); 227 exit(1); 228 } 229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 230 pclose(f); 231 if (retval) { 232 com_err("ext2fs_read_bb_FILE", retval, 233 _("while processing list of bad blocks from program")); 234 exit(1); 235 } 236} 237 238static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) 239{ 240 int i, j; 241 int must_be_good; 242 blk_t blk; 243 badblocks_iterate bb_iter; 244 errcode_t retval; 245 blk_t group_block; 246 int group; 247 int group_bad; 248 249 if (!bb_list) 250 return; 251 252 /* 253 * The primary superblock and group descriptors *must* be 254 * good; if not, abort. 255 */ 256 must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; 257 for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { 258 if (ext2fs_badblocks_list_test(bb_list, i)) { 259 fprintf(stderr, _("Block %d in primary " 260 "superblock/group descriptor area bad.\n"), i); 261 fprintf(stderr, _("Blocks %d through %d must be good " 262 "in order to build a filesystem.\n"), 263 fs->super->s_first_data_block, must_be_good); 264 fprintf(stderr, _("Aborting....\n")); 265 exit(1); 266 } 267 } 268 269 /* 270 * See if any of the bad blocks are showing up in the backup 271 * superblocks and/or group descriptors. If so, issue a 272 * warning and adjust the block counts appropriately. 273 */ 274 group_block = fs->super->s_first_data_block + 275 fs->super->s_blocks_per_group; 276 277 for (i = 1; i < fs->group_desc_count; i++) { 278 group_bad = 0; 279 for (j=0; j < fs->desc_blocks+1; j++) { 280 if (ext2fs_badblocks_list_test(bb_list, 281 group_block + j)) { 282 if (!group_bad) 283 fprintf(stderr, 284_("Warning: the backup superblock/group descriptors at block %d contain\n" 285" bad blocks.\n\n"), 286 group_block); 287 group_bad++; 288 group = ext2fs_group_of_blk(fs, group_block+j); 289 fs->group_desc[group].bg_free_blocks_count++; 290 fs->super->s_free_blocks_count++; 291 } 292 } 293 group_block += fs->super->s_blocks_per_group; 294 } 295 296 /* 297 * Mark all the bad blocks as used... 298 */ 299 retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); 300 if (retval) { 301 com_err("ext2fs_badblocks_list_iterate_begin", retval, 302 _("while marking bad blocks as used")); 303 exit(1); 304 } 305 while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) 306 ext2fs_mark_block_bitmap(fs->block_map, blk); 307 ext2fs_badblocks_list_iterate_end(bb_iter); 308} 309 310/* 311 * These functions implement a generalized progress meter. 312 */ 313struct progress_struct { 314 char format[20]; 315 char backup[80]; 316 __u32 max; 317}; 318 319static void progress_init(struct progress_struct *progress, 320 char *label,__u32 max) 321{ 322 int i; 323 324 memset(progress, 0, sizeof(struct progress_struct)); 325 if (quiet) 326 return; 327 328 /* 329 * Figure out how many digits we need 330 */ 331 i = int_log10(max); 332 sprintf(progress->format, "%%%dd/%%%dld", i, i); 333 memset(progress->backup, '\b', sizeof(progress->backup)-1); 334 progress->backup[sizeof(progress->backup)-1] = 0; 335 if ((2*i)+1 < sizeof(progress->backup)) 336 progress->backup[(2*i)+1] = 0; 337 progress->max = max; 338 339 fputs(label, stdout); 340 fflush(stdout); 341} 342 343static void progress_update(struct progress_struct *progress, __u32 val) 344{ 345 if (progress->format[0] == 0) 346 return; 347 printf(progress->format, val, progress->max); 348 fputs(progress->backup, stdout); 349} 350 351static void progress_close(struct progress_struct *progress) 352{ 353 if (progress->format[0] == 0) 354 return; 355 fputs(_("done \n"), stdout); 356} 357 358 359/* 360 * Helper function which zeros out _num_ blocks starting at _blk_. In 361 * case of an error, the details of the error is returned via _ret_blk_ 362 * and _ret_count_ if they are non-NULL pointers. Returns 0 on 363 * success, and an error code on an error. 364 * 365 * As a special case, if the first argument is NULL, then it will 366 * attempt to free the static zeroizing buffer. (This is to keep 367 * programs that check for memory leaks happy.) 368 */ 369static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num, 370 struct progress_struct *progress, 371 blk_t *ret_blk, int *ret_count) 372{ 373 int j, count, next_update, next_update_incr; 374 static char *buf; 375 errcode_t retval; 376 377 /* If fs is null, clean up the static buffer and return */ 378 if (!fs) { 379 if (buf) { 380 free(buf); 381 buf = 0; 382 } 383 return 0; 384 } 385 /* Allocate the zeroizing buffer if necessary */ 386 if (!buf) { 387 buf = malloc(fs->blocksize * STRIDE_LENGTH); 388 if (!buf) { 389 com_err("malloc", ENOMEM, 390 _("while allocating zeroizing buffer")); 391 exit(1); 392 } 393 memset(buf, 0, fs->blocksize * STRIDE_LENGTH); 394 } 395 /* OK, do the write loop */ 396 next_update = 0; 397 next_update_incr = num / 100; 398 if (next_update_incr < 1) 399 next_update_incr = 1; 400 for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { 401 if (num-j > STRIDE_LENGTH) 402 count = STRIDE_LENGTH; 403 else 404 count = num - j; 405 retval = io_channel_write_blk(fs->io, blk, count, buf); 406 if (retval) { 407 if (ret_count) 408 *ret_count = count; 409 if (ret_blk) 410 *ret_blk = blk; 411 return retval; 412 } 413 if (progress && j > next_update) { 414 next_update += num / 100; 415 progress_update(progress, blk); 416 } 417 } 418 return 0; 419} 420 421static void write_inode_tables(ext2_filsys fs) 422{ 423 errcode_t retval; 424 blk_t blk; 425 int i, num; 426 struct progress_struct progress; 427 428 if (quiet) 429 memset(&progress, 0, sizeof(progress)); 430 else 431 progress_init(&progress, _("Writing inode tables: "), 432 fs->group_desc_count); 433 434 for (i = 0; i < fs->group_desc_count; i++) { 435 progress_update(&progress, i); 436 437 blk = fs->group_desc[i].bg_inode_table; 438 num = fs->inode_blocks_per_group; 439 440 retval = zero_blocks(fs, blk, num, 0, &blk, &num); 441 if (retval) { 442 printf(_("\nCould not write %d blocks " 443 "in inode table starting at %d: %s\n"), 444 num, blk, error_message(retval)); 445 exit(1); 446 } 447 if (sync_kludge) { 448 if (sync_kludge == 1) 449 sync(); 450 else if ((i % sync_kludge) == 0) 451 sync(); 452 } 453 } 454 zero_blocks(0, 0, 0, 0, 0, 0); 455 progress_close(&progress); 456} 457 458static void create_root_dir(ext2_filsys fs) 459{ 460 errcode_t retval; 461 struct ext2_inode inode; 462 463 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); 464 if (retval) { 465 com_err("ext2fs_mkdir", retval, _("while creating root dir")); 466 exit(1); 467 } 468 if (geteuid()) { 469 retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); 470 if (retval) { 471 com_err("ext2fs_read_inode", retval, 472 _("while reading root inode")); 473 exit(1); 474 } 475 inode.i_uid = getuid(); 476 if (inode.i_uid) 477 inode.i_gid = getgid(); 478 retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); 479 if (retval) { 480 com_err("ext2fs_write_inode", retval, 481 _("while setting root inode ownership")); 482 exit(1); 483 } 484 } 485} 486 487static void create_lost_and_found(ext2_filsys fs) 488{ 489 errcode_t retval; 490 ext2_ino_t ino; 491 const char *name = "lost+found"; 492 int i; 493 int lpf_size = 0; 494 495 retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); 496 if (retval) { 497 com_err("ext2fs_mkdir", retval, 498 _("while creating /lost+found")); 499 exit(1); 500 } 501 502 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); 503 if (retval) { 504 com_err("ext2_lookup", retval, 505 _("while looking up /lost+found")); 506 exit(1); 507 } 508 509 for (i=1; i < EXT2_NDIR_BLOCKS; i++) { 510 if ((lpf_size += fs->blocksize) >= 16*1024) 511 break; 512 retval = ext2fs_expand_dir(fs, ino); 513 if (retval) { 514 com_err("ext2fs_expand_dir", retval, 515 _("while expanding /lost+found")); 516 exit(1); 517 } 518 } 519} 520 521static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) 522{ 523 errcode_t retval; 524 525 ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); 526 fs->group_desc[0].bg_free_inodes_count--; 527 fs->super->s_free_inodes_count--; 528 retval = ext2fs_update_bb_inode(fs, bb_list); 529 if (retval) { 530 com_err("ext2fs_update_bb_inode", retval, 531 _("while setting bad block inode")); 532 exit(1); 533 } 534 535} 536 537static void reserve_inodes(ext2_filsys fs) 538{ 539 ext2_ino_t i; 540 int group; 541 542 for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { 543 ext2fs_mark_inode_bitmap(fs->inode_map, i); 544 group = ext2fs_group_of_ino(fs, i); 545 fs->group_desc[group].bg_free_inodes_count--; 546 fs->super->s_free_inodes_count--; 547 } 548 ext2fs_mark_ib_dirty(fs); 549} 550 551static void zap_sector(ext2_filsys fs, int sect, int nsect) 552{ 553 char *buf; 554 int retval; 555 556 buf = malloc(512*nsect); 557 memset(buf, 0, 512*nsect); 558 559 io_channel_set_blksize(fs->io, 512); 560 retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf); 561 io_channel_set_blksize(fs->io, fs->blocksize); 562 free(buf); 563 if (retval) 564 printf(_("Warning: could not erase sector %d: %s\n"), sect, 565 error_message(retval)); 566} 567 568static void create_journal_dev(ext2_filsys fs) 569{ 570 struct progress_struct progress; 571 errcode_t retval; 572 char *buf; 573 blk_t blk; 574 int count; 575 576 retval = ext2fs_create_journal_superblock(fs, 577 fs->super->s_blocks_count, 0, &buf); 578 if (retval) { 579 com_err("create_journal_dev", retval, 580 _("while initializing journal superblock")); 581 exit(1); 582 } 583 if (quiet) 584 memset(&progress, 0, sizeof(progress)); 585 else 586 progress_init(&progress, _("Zeroing journal device: "), 587 fs->super->s_blocks_count); 588 589 retval = zero_blocks(fs, 0, fs->super->s_blocks_count, 590 &progress, &blk, &count); 591 if (retval) { 592 com_err("create_journal_dev", retval, 593 _("while zeroing journal device (block %u, count %d)"), 594 blk, count); 595 exit(1); 596 } 597 zero_blocks(0, 0, 0, 0, 0, 0); 598 599 retval = io_channel_write_blk(fs->io, 600 fs->super->s_first_data_block+1, 601 1, buf); 602 if (retval) { 603 com_err("create_journal_dev", retval, 604 _("while writing journal superblock")); 605 exit(1); 606 } 607 progress_close(&progress); 608} 609 610static void show_stats(ext2_filsys fs) 611{ 612 struct ext2_super_block *s = fs->super; 613 char buf[80]; 614 blk_t group_block; 615 int i, need, col_left; 616 617 if (param.s_blocks_count != s->s_blocks_count) 618 printf(_("warning: %d blocks unused.\n\n"), 619 param.s_blocks_count - s->s_blocks_count); 620 621 memset(buf, 0, sizeof(buf)); 622 strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); 623 printf(_("Filesystem label=%s\n"), buf); 624 printf(_("OS type: ")); 625 switch (fs->super->s_creator_os) { 626 case EXT2_OS_LINUX: printf ("Linux"); break; 627 case EXT2_OS_HURD: printf ("GNU/Hurd"); break; 628 case EXT2_OS_MASIX: printf ("Masix"); break; 629 default: printf (_("(unknown os)")); 630 } 631 printf("\n"); 632 printf(_("Block size=%u (log=%u)\n"), fs->blocksize, 633 s->s_log_block_size); 634 printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize, 635 s->s_log_frag_size); 636 printf(_("%u inodes, %u blocks\n"), s->s_inodes_count, 637 s->s_blocks_count); 638 printf(_("%u blocks (%2.2f%%) reserved for the super user\n"), 639 s->s_r_blocks_count, 640 100.0 * s->s_r_blocks_count / s->s_blocks_count); 641 printf(_("First data block=%u\n"), s->s_first_data_block); 642 if (fs->group_desc_count > 1) 643 printf(_("%u block groups\n"), fs->group_desc_count); 644 else 645 printf(_("%u block group\n"), fs->group_desc_count); 646 printf(_("%u blocks per group, %u fragments per group\n"), 647 s->s_blocks_per_group, s->s_frags_per_group); 648 printf(_("%u inodes per group\n"), s->s_inodes_per_group); 649 650 if (fs->group_desc_count == 1) { 651 printf("\n"); 652 return; 653 } 654 655 printf(_("Superblock backups stored on blocks: ")); 656 group_block = s->s_first_data_block; 657 col_left = 0; 658 for (i = 1; i < fs->group_desc_count; i++) { 659 group_block += s->s_blocks_per_group; 660 if (!ext2fs_bg_has_super(fs, i)) 661 continue; 662 if (i != 1) 663 printf(", "); 664 need = int_log10(group_block) + 2; 665 if (need > col_left) { 666 printf("\n\t"); 667 col_left = 72; 668 } 669 col_left -= need; 670 printf("%u", group_block); 671 } 672 printf("\n\n"); 673} 674 675/* 676 * Set the S_CREATOR_OS field. Return true if OS is known, 677 * otherwise, 0. 678 */ 679static int set_os(struct ext2_super_block *sb, char *os) 680{ 681 if (isdigit (*os)) 682 sb->s_creator_os = atoi (os); 683 else if (strcasecmp(os, "linux") == 0) 684 sb->s_creator_os = EXT2_OS_LINUX; 685 else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) 686 sb->s_creator_os = EXT2_OS_HURD; 687 else if (strcasecmp(os, "masix") == 0) 688 sb->s_creator_os = EXT2_OS_MASIX; 689 else 690 return 0; 691 return 1; 692} 693 694#define PATH_SET "PATH=/sbin" 695 696static void parse_raid_opts(const char *opts) 697{ 698 char *buf, *token, *next, *p, *arg; 699 int len; 700 int raid_usage = 0; 701 702 len = strlen(opts); 703 buf = malloc(len+1); 704 if (!buf) { 705 fprintf(stderr, _("Couldn't allocate memory to parse " 706 "raid options!\n")); 707 exit(1); 708 } 709 strcpy(buf, opts); 710 for (token = buf; token && *token; token = next) { 711 p = strchr(token, ','); 712 next = 0; 713 if (p) { 714 *p = 0; 715 next = p+1; 716 } 717 arg = strchr(token, '='); 718 if (arg) { 719 *arg = 0; 720 arg++; 721 } 722 if (strcmp(token, "stride") == 0) { 723 if (!arg) { 724 raid_usage++; 725 continue; 726 } 727 fs_stride = strtoul(arg, &p, 0); 728 if (*p || (fs_stride == 0)) { 729 fprintf(stderr, 730 _("Invalid stride parameter.\n")); 731 raid_usage++; 732 continue; 733 } 734 } else 735 raid_usage++; 736 } 737 if (raid_usage) { 738 fprintf(stderr, _("\nBad raid options specified.\n\n" 739 "Raid options are separated by commas, " 740 "and may take an argument which\n" 741 "\tis set off by an equals ('=') sign.\n\n" 742 "Valid raid options are:\n" 743 "\tstride=<stride length in blocks>\n\n")); 744 exit(1); 745 } 746} 747 748static __u32 ok_features[3] = { 749 EXT3_FEATURE_COMPAT_HAS_JOURNAL, /* Compat */ 750 EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */ 751 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV, 752 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ 753}; 754 755 756static void PRS(int argc, char *argv[]) 757{ 758 int c; 759 int size; 760 char * tmp; 761 blk_t group_blk_max = 8192; 762 int blocksize = 0; 763 int inode_ratio = 0; 764 int reserved_ratio = 5; 765 ext2_ino_t num_inodes = 0; 766 errcode_t retval; 767 char * oldpath = getenv("PATH"); 768 char * raid_opts = 0; 769 char * fs_type = 0; 770 int default_features = 1; 771 blk_t dev_size; 772#ifdef linux 773 struct utsname ut; 774#endif 775 776 /* Update our PATH to include /sbin */ 777 if (oldpath) { 778 char *newpath; 779 780 newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); 781 strcpy (newpath, PATH_SET); 782 strcat (newpath, ":"); 783 strcat (newpath, oldpath); 784 putenv (newpath); 785 } else 786 putenv (PATH_SET); 787 788 tmp = getenv("MKE2FS_SYNC"); 789 if (tmp) 790 sync_kludge = atoi(tmp); 791 792 setbuf(stdout, NULL); 793 setbuf(stderr, NULL); 794 initialize_ext2_error_table(); 795 memset(¶m, 0, sizeof(struct ext2_super_block)); 796 param.s_rev_level = 1; /* Create revision 1 filesystems now */ 797 param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE; 798 param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 799 800#ifdef linux 801 if (uname(&ut)) { 802 perror("uname"); 803 exit(1); 804 } 805 if ((ut.release[0] == '1') || 806 (ut.release[0] == '2' && ut.release[1] == '.' && 807 ut.release[2] < '2' && ut.release[3] == '.')) { 808 param.s_rev_level = 0; 809 param.s_feature_incompat = 0; 810 param.s_feature_compat = 0; 811 param.s_feature_ro_compat = 0; 812 } 813#endif 814 fprintf (stderr, "mke2fs %s (%s)\n", 815 E2FSPROGS_VERSION, E2FSPROGS_DATE); 816 if (argc && *argv) 817 program_name = *argv; 818 while ((c = getopt (argc, argv, 819 "b:cf:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) 820 switch (c) { 821 case 'b': 822 blocksize = strtoul(optarg, &tmp, 0); 823 if (blocksize < 1024 || blocksize > 4096 || *tmp) { 824 com_err(program_name, 0, 825 _("bad block size - %s"), optarg); 826 exit(1); 827 } 828 param.s_log_block_size = 829 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); 830 group_blk_max = blocksize * 8; 831 break; 832 case 'c': /* Check for bad blocks */ 833 case 't': /* deprecated */ 834 cflag = 1; 835 break; 836 case 'f': 837 size = strtoul(optarg, &tmp, 0); 838 if (size < 1024 || size > 4096 || *tmp) { 839 com_err(program_name, 0, 840 _("bad fragment size - %s"), 841 optarg); 842 exit(1); 843 } 844 param.s_log_frag_size = 845 int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); 846 printf(_("Warning: fragments not supported. " 847 "Ignoring -f option\n")); 848 break; 849 case 'g': 850 param.s_blocks_per_group = strtoul(optarg, &tmp, 0); 851 if (*tmp) { 852 com_err(program_name, 0, 853 _("Illegal number for blocks per group")); 854 exit(1); 855 } 856 if ((param.s_blocks_per_group % 8) != 0) { 857 com_err(program_name, 0, 858 _("blocks per group must be multiple of 8")); 859 exit(1); 860 } 861 break; 862 case 'i': 863 inode_ratio = strtoul(optarg, &tmp, 0); 864 if (inode_ratio < 1024 || inode_ratio > 4096 * 1024 || 865 *tmp) { 866 com_err(program_name, 0, 867 _("bad inode ratio - %s"), optarg); 868 exit(1); 869 } 870 break; 871 case 'J': 872 parse_journal_opts(optarg); 873 break; 874 case 'j': 875 param.s_feature_compat |= 876 EXT3_FEATURE_COMPAT_HAS_JOURNAL; 877 if (!journal_size) 878 journal_size = -1; 879 break; 880 case 'l': 881 bad_blocks_filename = malloc(strlen(optarg)+1); 882 if (!bad_blocks_filename) { 883 com_err(program_name, ENOMEM, 884 _("in malloc for bad_blocks_filename")); 885 exit(1); 886 } 887 strcpy(bad_blocks_filename, optarg); 888 break; 889 case 'm': 890 reserved_ratio = strtoul(optarg, &tmp, 0); 891 if (reserved_ratio > 50 || *tmp) { 892 com_err(program_name, 0, 893 _("bad reserved blocks percent - %s"), 894 optarg); 895 exit(1); 896 } 897 break; 898 case 'n': 899 noaction++; 900 break; 901 case 'o': 902 creator_os = optarg; 903 break; 904 case 'r': 905 param.s_rev_level = atoi(optarg); 906 if (param.s_rev_level == EXT2_GOOD_OLD_REV) { 907 param.s_feature_incompat = 0; 908 param.s_feature_compat = 0; 909 param.s_feature_ro_compat = 0; 910 } 911 break; 912 case 's': /* deprecated */ 913 if (atoi(optarg)) 914 param.s_feature_ro_compat |= 915 EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 916 else 917 param.s_feature_ro_compat &= 918 ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; 919 break; 920#ifdef EXT2_DYNAMIC_REV 921 case 'I': 922 param.s_inode_size = atoi(optarg); 923 break; 924#endif 925 case 'N': 926 num_inodes = atoi(optarg); 927 break; 928 case 'v': 929 verbose = 1; 930 break; 931 case 'q': 932 quiet = 1; 933 break; 934 case 'F': 935 force = 1; 936 break; 937 case 'L': 938 volume_label = optarg; 939 break; 940 case 'M': 941 mount_dir = optarg; 942 break; 943 case 'O': 944 if (!strcmp(optarg, "none") || default_features) { 945 param.s_feature_compat = 0; 946 param.s_feature_incompat = 0; 947 param.s_feature_ro_compat = 0; 948 default_features = 0; 949 } 950 if (!strcmp(optarg, "none")) 951 break; 952 if (e2p_edit_feature(optarg, 953 ¶m.s_feature_compat, 954 ok_features)) { 955 fprintf(stderr, 956 _("Invalid filesystem option set: %s\n"), optarg); 957 exit(1); 958 } 959 break; 960 case 'R': 961 raid_opts = optarg; 962 break; 963 case 'S': 964 super_only = 1; 965 break; 966 case 'T': 967 fs_type = optarg; 968 break; 969 case 'V': 970 /* Print version number and exit */ 971 fprintf(stderr, _("\tUsing %s\n"), 972 error_message(EXT2_ET_BASE)); 973 exit(0); 974 default: 975 usage(); 976 } 977 if (optind == argc) 978 usage(); 979 device_name = argv[optind]; 980 optind++; 981 if (optind < argc) { 982 unsigned long tmp2 = strtoul(argv[optind++], &tmp, 0); 983 984 if ((*tmp) || (tmp2 > 0xfffffffful)) { 985 com_err(program_name, 0, _("bad blocks count - %s"), 986 argv[optind - 1]); 987 exit(1); 988 } 989 param.s_blocks_count = tmp2; 990 } 991 if (optind < argc) 992 usage(); 993 994 if (raid_opts) 995 parse_raid_opts(raid_opts); 996 997 /* 998 * If there's no blocksize specified and there is a journal 999 * device, use it to figure out the blocksize 1000 */ 1001 if (blocksize == 0 && journal_device) { 1002 ext2_filsys jfs; 1003 1004 retval = ext2fs_open(journal_device, 1005 EXT2_FLAG_JOURNAL_DEV_OK, 0, 1006 0, unix_io_manager, &jfs); 1007 if (retval) { 1008 com_err(program_name, retval, 1009 _("while trying to open journal device %s\n"), 1010 journal_device); 1011 exit(1); 1012 } 1013 blocksize = jfs->blocksize; 1014 param.s_log_block_size = 1015 int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); 1016 ext2fs_close(jfs); 1017 } 1018 1019 if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 1020 if (!fs_type) 1021 fs_type = "journal"; 1022 reserved_ratio = 0; 1023 param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; 1024 param.s_feature_compat = 0; 1025 param.s_feature_ro_compat = 0; 1026 } 1027 if (param.s_rev_level == EXT2_GOOD_OLD_REV && 1028 (param.s_feature_compat || param.s_feature_ro_compat || 1029 param.s_feature_incompat)) 1030 param.s_rev_level = 1; /* Create a revision 1 filesystem */ 1031 1032 if (!force) 1033 check_plausibility(device_name); 1034 check_mount(device_name, force, _("filesystem")); 1035 1036 param.s_log_frag_size = param.s_log_block_size; 1037 1038 if (noaction && param.s_blocks_count) { 1039 dev_size = param.s_blocks_count; 1040 retval = 0; 1041 } else 1042 retval = ext2fs_get_device_size(device_name, 1043 EXT2_BLOCK_SIZE(¶m), 1044 &dev_size); 1045 if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { 1046 com_err(program_name, retval, 1047 _("while trying to determine filesystem size")); 1048 exit(1); 1049 } 1050 if (!param.s_blocks_count) { 1051 if (retval == EXT2_ET_UNIMPLEMENTED) { 1052 com_err(program_name, 0, 1053 _("Couldn't determine device size; you " 1054 "must specify\nthe size of the " 1055 "filesystem\n")); 1056 exit(1); 1057 } else { 1058 if (dev_size == 0) { 1059 com_err(program_name, 0, 1060 _("Device size reported to be zero. " 1061 "Invalid partition specified, or\n\t" 1062 "partition table wasn't reread " 1063 "after running fdisk, due to\n\t" 1064 "a modified partition being busy " 1065 "and in use. You may need to reboot\n\t" 1066 "to re-read your partition table.\n" 1067 )); 1068 exit(1); 1069 } 1070 param.s_blocks_count = dev_size; 1071 } 1072 1073 } else if (!force && (param.s_blocks_count > dev_size)) { 1074 com_err(program_name, 0, 1075 _("Filesystem larger than apparent filesystem size.")); 1076 proceed_question(); 1077 } 1078 1079 /* 1080 * If the user asked for HAS_JOURNAL, then make sure a journal 1081 * gets created. 1082 */ 1083 if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 1084 !journal_size) 1085 journal_size = -1; 1086 1087 set_fs_defaults(fs_type, ¶m, blocksize, &inode_ratio); 1088 1089 if (param.s_blocks_per_group) { 1090 if (param.s_blocks_per_group < 256 || 1091 param.s_blocks_per_group > group_blk_max || *tmp) { 1092 com_err(program_name, 0, 1093 _("blocks per group count out of range")); 1094 exit(1); 1095 } 1096 } 1097 1098 /* 1099 * Calculate number of inodes based on the inode ratio 1100 */ 1101 param.s_inodes_count = num_inodes ? num_inodes : 1102 ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m)) 1103 / inode_ratio; 1104 1105 /* 1106 * Calculate number of blocks to reserve 1107 */ 1108 param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; 1109 1110} 1111 1112int main (int argc, char *argv[]) 1113{ 1114 errcode_t retval = 0; 1115 ext2_filsys fs; 1116 badblocks_list bb_list = 0; 1117 int journal_blocks; 1118 int i, val; 1119 1120#ifdef ENABLE_NLS 1121 setlocale(LC_MESSAGES, ""); 1122 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 1123 textdomain(NLS_CAT_NAME); 1124#endif 1125 PRS(argc, argv); 1126 1127 /* 1128 * Initialize the superblock.... 1129 */ 1130 retval = ext2fs_initialize(device_name, 0, ¶m, 1131 unix_io_manager, &fs); 1132 if (retval) { 1133 com_err(device_name, retval, _("while setting up superblock")); 1134 exit(1); 1135 } 1136 1137 /* 1138 * Wipe out the old on-disk superblock 1139 */ 1140 if (!noaction) 1141 zap_sector(fs, 2, 6); 1142 1143 /* 1144 * Generate a UUID for it... 1145 */ 1146 uuid_generate(fs->super->s_uuid); 1147 1148 /* 1149 * Add "jitter" to the superblock's check interval so that we 1150 * don't check all the filesystems at the same time. We use a 1151 * kludgy hack of using the UUID to derive a random jitter value. 1152 */ 1153 for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++) 1154 val += fs->super->s_uuid[i]; 1155 fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; 1156 1157 /* 1158 * Override the creator OS, if applicable 1159 */ 1160 if (creator_os && !set_os(fs->super, creator_os)) { 1161 com_err (program_name, 0, _("unknown os - %s"), creator_os); 1162 exit(1); 1163 } 1164 1165 /* 1166 * For the Hurd, we will turn off filetype since it doesn't 1167 * support it. 1168 */ 1169 if (fs->super->s_creator_os == EXT2_OS_HURD) 1170 fs->super->s_feature_incompat &= 1171 ~EXT2_FEATURE_INCOMPAT_FILETYPE; 1172 1173 /* 1174 * Set the volume label... 1175 */ 1176 if (volume_label) { 1177 memset(fs->super->s_volume_name, 0, 1178 sizeof(fs->super->s_volume_name)); 1179 strncpy(fs->super->s_volume_name, volume_label, 1180 sizeof(fs->super->s_volume_name)); 1181 } 1182 1183 /* 1184 * Set the last mount directory 1185 */ 1186 if (mount_dir) { 1187 memset(fs->super->s_last_mounted, 0, 1188 sizeof(fs->super->s_last_mounted)); 1189 strncpy(fs->super->s_last_mounted, mount_dir, 1190 sizeof(fs->super->s_last_mounted)); 1191 } 1192 1193 if (!quiet || noaction) 1194 show_stats(fs); 1195 1196 if (noaction) 1197 exit(0); 1198 1199 if (fs->super->s_feature_incompat & 1200 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 1201 create_journal_dev(fs); 1202 ext2fs_close(fs); 1203 exit(0); 1204 } 1205 1206 if (bad_blocks_filename) 1207 read_bb_file(fs, &bb_list, bad_blocks_filename); 1208 if (cflag) 1209 test_disk(fs, &bb_list); 1210 1211 handle_bad_blocks(fs, bb_list); 1212 fs->stride = fs_stride; 1213 retval = ext2fs_allocate_tables(fs); 1214 if (retval) { 1215 com_err(program_name, retval, 1216 _("while trying to allocate filesystem tables")); 1217 exit(1); 1218 } 1219 if (super_only) { 1220 fs->super->s_state |= EXT2_ERROR_FS; 1221 fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); 1222 } else { 1223 /* rsv must be a power of two (64kB is MD RAID sb alignment) */ 1224 int rsv = 65536 / fs->blocksize; 1225 unsigned long blocks = fs->super->s_blocks_count; 1226 unsigned long start; 1227 blk_t ret_blk; 1228 1229#ifdef ZAP_BOOTBLOCK 1230 zap_sector(fs, 0, 2); 1231#endif 1232 1233 /* 1234 * Wipe out any old MD RAID (or other) metadata at the end 1235 * of the device. This will also verify that the device is 1236 * as large as we think. Be careful with very small devices. 1237 */ 1238 start = (blocks & ~(rsv - 1)); 1239 if (start > rsv) 1240 start -= rsv; 1241 if (start > 0) 1242 retval = zero_blocks(fs, start, blocks - start, 1243 NULL, &ret_blk, NULL); 1244 1245 if (retval) { 1246 com_err(program_name, retval, 1247 _("zeroing block %u at end of filesystem"), 1248 ret_blk); 1249 exit(1); 1250 } 1251 write_inode_tables(fs); 1252 create_root_dir(fs); 1253 create_lost_and_found(fs); 1254 reserve_inodes(fs); 1255 create_bad_block_inode(fs, bb_list); 1256 } 1257 1258 if (journal_device) { 1259 ext2_filsys jfs; 1260 1261 if (!force) 1262 check_plausibility(journal_device); 1263 check_mount(journal_device, force, _("journal")); 1264 1265 retval = ext2fs_open(journal_device, EXT2_FLAG_RW| 1266 EXT2_FLAG_JOURNAL_DEV_OK, 0, 1267 fs->blocksize, unix_io_manager, &jfs); 1268 if (retval) { 1269 com_err(program_name, retval, 1270 _("while trying to open journal device %s\n"), 1271 journal_device); 1272 exit(1); 1273 } 1274 if (!quiet) { 1275 printf(_("Adding journal to device %s: "), 1276 journal_device); 1277 fflush(stdout); 1278 } 1279 retval = ext2fs_add_journal_device(fs, jfs); 1280 if(retval) { 1281 com_err (program_name, retval, 1282 _("\n\twhile trying to add journal to device %s"), 1283 journal_device); 1284 exit(1); 1285 } 1286 if (!quiet) 1287 printf(_("done\n")); 1288 ext2fs_close(jfs); 1289 free(journal_device); 1290 } else if (journal_size) { 1291 journal_blocks = figure_journal_size(journal_size, fs); 1292 1293 if (!journal_blocks) { 1294 fs->super->s_feature_compat &= 1295 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; 1296 goto no_journal; 1297 } 1298 if (!quiet) { 1299 printf(_("Creating journal (%d blocks): "), 1300 journal_blocks); 1301 fflush(stdout); 1302 } 1303 retval = ext2fs_add_journal_inode(fs, journal_blocks, 1304 journal_flags); 1305 if (retval) { 1306 com_err (program_name, retval, 1307 _("\n\twhile trying to create journal")); 1308 exit(1); 1309 } 1310 if (!quiet) 1311 printf(_("done\n")); 1312 } 1313no_journal: 1314 1315 if (!quiet) 1316 printf(_("Writing superblocks and " 1317 "filesystem accounting information: ")); 1318 retval = ext2fs_flush(fs); 1319 if (retval) { 1320 printf(_("\nWarning, had trouble writing out superblocks.")); 1321 } 1322 if (!quiet) { 1323 printf(_("done\n\n")); 1324 print_check_message(fs); 1325 } 1326 ext2fs_close(fs); 1327 return 0; 1328} 1329