📄 mke2fs.c
字号:
noaction++; break; case 'o': creator_os = optarg; break; case 'r': param.s_rev_level = xatoi_u(optarg); if (param.s_rev_level == EXT2_GOOD_OLD_REV) { param.s_feature_incompat = 0; param.s_feature_compat = 0; param.s_feature_ro_compat = 0; } break; case 's': /* deprecated */ if (xatou(optarg)) param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; else param.s_feature_ro_compat &= ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; break;#ifdef EXT2_DYNAMIC_REV case 'I': inode_size = xatoi_u(optarg); break;#endif case 'N': num_inodes = xatoi_u(optarg); break; case 'v': quiet = 0; break; case 'q': quiet = 1; break; case 'F': force = 1; break; case 'L': volume_label = optarg; break; case 'M': mount_dir = optarg; break; case 'O': if (!strcmp(optarg, "none")) { param.s_feature_compat = 0; param.s_feature_incompat = 0; param.s_feature_ro_compat = 0; break; } if (e2p_edit_feature(optarg, ¶m.s_feature_compat, ok_features)) { bb_error_msg_and_die("Invalid filesystem option set: %s", optarg); } break; case 'E': case 'R': extended_opts = optarg; break; case 'S': super_only = 1; break; case 'T': fs_type = optarg; break; case 'V': /* Print version number and exit */ show_version_only = 1; quiet = 0; break; default: bb_show_usage(); } } if ((optind == argc) /*&& !show_version_only*/) bb_show_usage(); device_name = argv[optind++]; mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); if (show_version_only) { return 0; } /* * If there's no blocksize specified and there is a journal * device, use it to figure out the blocksize */ if (blocksize <= 0 && journal_device) { ext2_filsys jfs; io_manager io_ptr;#ifdef CONFIG_TESTIO_DEBUG io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager;#else io_ptr = unix_io_manager;#endif retval = ext2fs_open(journal_device, EXT2_FLAG_JOURNAL_DEV_OK, 0, 0, io_ptr, &jfs); mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device); if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { bb_error_msg_and_die( "Journal dev blocksize (%d) smaller than " "minimum blocksize %d\n", jfs->blocksize, -blocksize); } blocksize = jfs->blocksize; param.s_log_block_size = int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); ext2fs_close(jfs); } if (blocksize > sys_page_size) { mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)", blocksize, sys_page_size); if (!force) { proceed_question(); } bb_error_msg("Forced to continue"); } mke2fs_warning_msg(((blocksize > 4096) && (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)), "some 2.4 kernels do not support " "blocksizes greater than 4096 using ext3.\n" "Use -b 4096 if this is an issue for you\n"); if (optind < argc) { param.s_blocks_count = parse_num_blocks(argv[optind++], param.s_log_block_size); mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]); } if (optind < argc) bb_show_usage(); if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { if (!fs_type) fs_type = "journal"; reserved_ratio = 0; param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; param.s_feature_compat = 0; param.s_feature_ro_compat = 0; } if (param.s_rev_level == EXT2_GOOD_OLD_REV && (param.s_feature_compat || param.s_feature_ro_compat || param.s_feature_incompat)) param.s_rev_level = 1; /* Create a revision 1 filesystem */ check_plausibility(device_name , force); check_mount(device_name, force, "filesystem"); param.s_log_frag_size = param.s_log_block_size; if (noaction && param.s_blocks_count) { dev_size = param.s_blocks_count; retval = 0; } else { retry: retval = ext2fs_get_device_size(device_name, EXT2_BLOCK_SIZE(¶m), &dev_size); if ((retval == EFBIG) && (blocksize == 0) && (param.s_log_block_size == 0)) { param.s_log_block_size = 2; blocksize = 4096; goto retry; } } mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size"); if (!param.s_blocks_count) { if (retval == EXT2_ET_UNIMPLEMENTED) { mke2fs_error_msg_and_die(1, "determine device size; you " "must specify\nthe size of the " "filesystem"); } else { if (dev_size == 0) { bb_error_msg_and_die( "Device size reported to be zero. " "Invalid partition specified, or\n\t" "partition table wasn't reread " "after running fdisk, due to\n\t" "a modified partition being busy " "and in use. You may need to reboot\n\t" "to re-read your partition table.\n" ); } param.s_blocks_count = dev_size; if (sys_page_size > EXT2_BLOCK_SIZE(¶m)) param.s_blocks_count &= ~((sys_page_size / EXT2_BLOCK_SIZE(¶m))-1); } } else if (!force && (param.s_blocks_count > dev_size)) { bb_error_msg("Filesystem larger than apparent device size"); proceed_question(); } /* * If the user asked for HAS_JOURNAL, then make sure a journal * gets created. */ if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && !journal_size) journal_size = -1; /* Set first meta blockgroup via an environment variable */ /* (this is mostly for debugging purposes) */ if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && ((tmp = getenv("MKE2FS_FIRST_META_BG")))) param.s_first_meta_bg = atoi(tmp); /* Get the hardware sector size, if available */ retval = ext2fs_get_device_sectsize(device_name, §or_size); mke2fs_error_msg_and_die(retval, "determine hardware sector size"); if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) sector_size = atoi(tmp); set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio); blocksize = EXT2_BLOCK_SIZE(¶m); if (extended_opts) parse_extended_opts(¶m, extended_opts); /* Since sparse_super is the default, we would only have a problem * here if it was explicitly disabled. */ if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) { bb_error_msg_and_die("reserved online resize blocks not supported " "on non-sparse filesystem"); } if (param.s_blocks_per_group) { if (param.s_blocks_per_group < 256 || param.s_blocks_per_group > 8 * (unsigned) blocksize) { bb_error_msg_and_die("blocks per group count out of range"); } } if (!force && param.s_blocks_count >= (1 << 31)) { bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n" "\t (8TB using a blocksize of 4k) are currently supported."); } if (inode_size) { if (inode_size < EXT2_GOOD_OLD_INODE_SIZE || inode_size > EXT2_BLOCK_SIZE(¶m) || inode_size & (inode_size - 1)) { bb_error_msg_and_die("invalid inode size %d (min %d/max %d)", inode_size, EXT2_GOOD_OLD_INODE_SIZE, blocksize); } mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE), "%d-byte inodes not usable on most systems", inode_size); param.s_inode_size = inode_size; } /* * Calculate number of inodes based on the inode ratio */ param.s_inodes_count = num_inodes ? num_inodes : ((__u64) param.s_blocks_count * blocksize) / inode_ratio; /* * Calculate number of blocks to reserve */ param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; return 1;}static void mke2fs_clean_up(void){ if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device);}int mke2fs_main (int argc, char **argv);int mke2fs_main (int argc, char **argv){ errcode_t retval; ext2_filsys fs; badblocks_list bb_list = 0; unsigned int i; int val; io_manager io_ptr; if (ENABLE_FEATURE_CLEAN_UP) atexit(mke2fs_clean_up); if(!PRS(argc, argv)) return 0;#ifdef CONFIG_TESTIO_DEBUG io_ptr = test_io_manager; test_io_backing_manager = unix_io_manager;#else io_ptr = unix_io_manager;#endif /* * Initialize the superblock.... */ retval = ext2fs_initialize(device_name, 0, ¶m, io_ptr, &fs); mke2fs_error_msg_and_die(retval, "set up superblock"); /* * Wipe out the old on-disk superblock */ if (!noaction) zap_sector(fs, 2, 6); /* * Generate a UUID for it... */ uuid_generate(fs->super->s_uuid); /* * Initialize the directory index variables */ fs->super->s_def_hash_version = EXT2_HASH_TEA; uuid_generate((unsigned char *) fs->super->s_hash_seed); /* * Add "jitter" to the superblock's check interval so that we * don't check all the filesystems at the same time. We use a * kludgy hack of using the UUID to derive a random jitter value. */ for (i = 0, val = 0; i < sizeof(fs->super->s_uuid); i++) val += fs->super->s_uuid[i]; fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; /* * Override the creator OS, if applicable */ if (creator_os && !set_os(fs->super, creator_os)) { bb_error_msg_and_die("unknown os - %s", creator_os); } /* * For the Hurd, we will turn off filetype since it doesn't * support it. */ if (fs->super->s_creator_os == EXT2_OS_HURD) fs->super->s_feature_incompat &= ~EXT2_FEATURE_INCOMPAT_FILETYPE; /* * Set the volume label... */ if (volume_label) { snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label); } /* * Set the last mount directory */ if (mount_dir) { snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir); } if (!quiet || noaction) show_stats(fs); if (noaction) return 0; if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { create_journal_dev(fs); return (ext2fs_close(fs) ? 1 : 0); } if (bad_blocks_filename) read_bb_file(fs, &bb_list, bad_blocks_filename); if (cflag) test_disk(fs, &bb_list); handle_bad_blocks(fs, bb_list); fs->stride = fs_stride; retval = ext2fs_allocate_tables(fs); mke2fs_error_msg_and_die(retval, "allocate filesystem tables"); if (super_only) { fs->super->s_state |= EXT2_ERROR_FS; fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); } else { /* rsv must be a power of two (64kB is MD RAID sb alignment) */ unsigned int rsv = 65536 / fs->blocksize; unsigned long blocks = fs->super->s_blocks_count; unsigned long start; blk_t ret_blk;#ifdef ZAP_BOOTBLOCK zap_sector(fs, 0, 2);#endif /* * Wipe out any old MD RAID (or other) metadata at the end * of the device. This will also verify that the device is * as large as we think. Be careful with very small devices. */ start = (blocks & ~(rsv - 1)); if (start > rsv) start -= rsv; if (start > 0) retval = zero_blocks(fs, start, blocks - start, NULL, &ret_blk, NULL); mke2fs_warning_msg(retval, "cannot zero block %u at end of filesystem", ret_blk); write_inode_tables(fs); create_root_dir(fs); create_lost_and_found(fs); reserve_inodes(fs); create_bad_block_inode(fs, bb_list); if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) { retval = ext2fs_create_resize_inode(fs); mke2fs_error_msg_and_die(retval, "reserve blocks for online resize"); } } if (journal_device) { make_journal_device(journal_device, fs, quiet, force); } else if (journal_size) { make_journal_blocks(fs, journal_size, journal_flags, quiet); } mke2fs_verbose("Writing superblocks and filesystem accounting information: "); retval = ext2fs_flush(fs); mke2fs_warning_msg(retval, "had trouble writing out superblocks"); mke2fs_verbose_done(); if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG")) print_check_message(fs); val = ext2fs_close(fs); return (retval || val) ? 1 : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -