// Standard mke2fs 1.41.9:
// Usage: mke2fs [-c|-l filename] [-b block-size] [-f fragment-size]
-// [-i bytes-per-inode] [-I inode-size] [-J journal-options]
-// [-G meta group size] [-N number-of-inodes]
-// [-m reserved-blocks-percentage] [-o creator-os]
-// [-g blocks-per-group] [-L volume-label] [-M last-mounted-directory]
-// [-O feature[,...]] [-r fs-revision] [-E extended-option[,...]]
-// [-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]
+// [-i bytes-per-inode] [-I inode-size] [-J journal-options]
+// [-G meta group size] [-N number-of-inodes]
+// [-m reserved-blocks-percentage] [-o creator-os]
+// [-g blocks-per-group] [-L volume-label] [-M last-mounted-directory]
+// [-O feature[,...]] [-r fs-revision] [-E extended-option[,...]]
+// [-T fs-type] [-U UUID] [-jnqvFSV] device [blocks-count]
//
// Options not commented below are taken but silently ignored:
enum {
&reserved_percent, NULL, NULL, &label, NULL, NULL, NULL, NULL, NULL, NULL);
argv += optind; // argv[0] -- device
- // check the device is a block device
+ // open the device, check the device is a block device
xmove_fd(xopen(argv[0], O_WRONLY), fd);
fstat(fd, &st);
if (!S_ISBLK(st.st_mode) && !(option_mask32 & OPT_F))
// check if it is mounted
// N.B. what if we format a file? find_mount_point will return false negative since
- // it is loop block device which mounted!
+ // it is loop block device which is mounted!
if (find_mount_point(argv[0], 0))
bb_error_msg_and_die("can't format mounted filesystem");
- // open the device, get size in kbytes
- if (argv[1]) {
- kilobytes = xatoull(argv[1]);
- // seek past end fails on block devices but works on files
- if (lseek(fd, kilobytes * 1024 - 1, SEEK_SET) != (off_t)-1) {
- if (!(option_mask32 & OPT_n))
- xwrite(fd, "", 1); // file grows if needed
- }
- //else {
- // bb_error_msg("warning, block device is smaller");
- //}
- } else {
- kilobytes = (uoff_t)xlseek(fd, 0, SEEK_END) / 1024;
- }
+ // get size in kbytes
+ kilobytes = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ !(option_mask32 & OPT_n)) / 1024;
bytes_per_inode = 16384;
if (kilobytes < 512*1024)
nreserved = (uint64_t)nblocks * reserved_percent / 100;
// N.B. killing e2fsprogs feature! Unused blocks don't account in calculations
- nblocks_full = nblocks;
+ nblocks_full = nblocks;
// If last block group is too small, nblocks may be decreased in order
// to discard it, and control returns here to recalculate some