5 Unix / Linux Mkfs Command Examples (Build Linux Filesystem)

After you’ve partitioned a hard disk, you can use mkfs command is used to build a Linux filesystem. This article explains 5 practical examples on how to use this command.

Please be careful while executing this command, as this will destroy all data on the device. So, use this only on your test system to understand how this command works.

1. Basic mkfs Usage

The following example will build a Linux ext2 filesystem on /dev/sdb1 partition.

# mkfs /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6168576 inodes, 24657920 blocks
1232896 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
753 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

By default, this will create ext2 filesystem as shown below.

# file -sL /dev/sdb1
/dev/sdb1: Linux rev 1.0 ext2 filesystem data (large files)

Use -V verbose option, which will give you little bit more detail while building the filesystem.

# mkfs -V /dev/sdb1

2. Specify filesystem Type

Use -v option to specify the filesystem type.

The following example, creates ext3 filesystem. As you see in the output below, it also contains a line that says “Creating journal”, which is required for ext3 filesystem.

# mkfs -t ext3 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6168576 inodes, 24657920 blocks
1232896 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
753 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Verify that it created ext3 filesystem as shown below.

# file -sL /dev/sdb1
/dev/sdb1: Linux rev 1.0 ext3 filesystem data (large files)

3. Check for Bad Blocks before Building Filesystem

As shown below, -c option will check for bad blocks before it creates the filesystem. Depending on the size of your filesystem, the “Checking for bad blocks (read-only test):” line in the following output, might take a very long time to complete.

As you see in the following example, it took me around 7 minutes, to even get to 67% of the bad blocks checking.

# mkfs -c /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6168576 inodes, 24657920 blocks
1232896 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
753 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Checking for bad blocks (read-only test): 67.30% done, 7:16 elapsed Checking for bad blocks (read-only test): done Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

When the above checking for bad blocks is happening, if you try to do the following from another terminal, it will says that the device is used by the system.

# mkfs /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
/dev/sdb1 is apparently in use by the system; will not make a filesystem here!

4. Read Bad Blocks from File During Mkfs

You can also list all the known badblocks in a file and pass that as an argument to mkfs using -l as shown below.

# mkfs -V -l badblocks.txt -v /dev/sdb1
mkfs (util-linux-ng 2.17.2)
mkfs.ext2 -l badblocks.txt -v /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
fs_types for mke2fs.conf resolution: 'ext2', 'default'
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6168576 inodes, 24657920 blocks
1232896 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
753 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

If the badblocks.txt file doesn’t exits, the above command will display the following error message:

# mkfs -V -l badblocks.txt -v /dev/sdb1
read_bad_blocks_file: No such file or directory while trying to open badblocks.txt

5. Specify a Block Count in Mkfs

You can also specify the block count to the mkfs count. In a typical situation, you don’t have to specify this, as mkfs will use all available blocks in the filesystem.

But, for some reason, if you want to specify it, you can do it as shown below.

# mkfs /dev/sdb1 10000
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
2512 inodes, 10000 blocks
500 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=10485760
2 block groups
8192 blocks per group, 8192 fragments per group
1256 inodes per group
Superblock backups stored on blocks:
        8193

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

As shown by the following output, it is using only 10000 blocks that we specified in the above mkfs command.

# tune2fs -l /dev/sdb1 | grep -i block
Block count: 10000
Reserved block count:     500
Free blocks:              9585
First block:              1
Block size:               1024
Reserved GDT blocks:      39
Blocks per group:         8192
Inode blocks per group:   157
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)

Comments on this entry are closed.

  • Robin Arora October 21, 2013, 3:30 pm

    What maximum block size we can specify for file system with mkfs command ?