天天看點

Linux mkfs, mke2fs Command Examples (Create ext2, ext3, ext4 Filesystem)

mkfs utility is used to create filesystem (ext2, ext3, ext4, etc) on your Linux system. You should specify the device name to mkfs on which the filesystem to be created.

WARNING: Executing these commands will destroy all the data on your filesystem. So, try these commands only on a test system where you don’t care about losing your data.

The filesystem builders (mkfs* commands) are usually searched in directories like /sbin/, /sbin/fs, /sbin/fs.d, /etc/fs and /etc. If not found, finally it searches the directories found in the PATH variable.

The following list shows the available mkfs* commands in a system.

If you are new to filesystem, read about Ext2 vs Ext3 vs Ext4.

In order to build the filesystem using mkfs command, the required arguments are device-filename and filesystem-type as shown below. The following example creates ext3 filesystem on /dev/sda6 partition.

Please note that the default filesystem type for mkfs command is ext2. If you don’t specify “-t” option, it will create ext2 filesystem.

Also, you can use the method we discussed earlier to identify whether you have ext2 or ext3 or ext4 file system.

The example given below creates a file system with journaling.

When you don’t need the journaling on a filesystem, then execute mke2fs command as shown below.

Before creating a filesystem, you can check to make sure there are no bad blocks on the system using -c option as shown below. This will do a read-only test on the filesystem to make sure there are no bad blocks. You can also perform a read-write test to verify there are no bad blocks using “-c -c” option. Please note that this will be slower than the “-c” option.

It is recommended not to do any filesystem operations on a mounted partition. But still you can force it to create a filesystem on in-use partition or mounted partition with option -F as shown below.

You may want to list out the used and available inodes in the partition as shown below using df command.

As seen above, on /dev/sda6 the IUsed is 11 out of 1120112. After the new file gets created on it, the value of IUsed gets changes accordingly as shown below.

You may also use the tune2fs command to view the Inode details:

To change the inode-per-group of the above partition(i.e:/dev/sda6) use -i option as follows:

As seen above, the bytes-per-inode is changed to 8192 from the default value of 16384. Now the inode limit of the partition got increased as shown below.

Changing the value of inodes-per-group just nothing but impacting the number of files to be kept on the partition. Reducing this value is for increasing the number of files on a partition.

It is possible to change the size of an Inode on a parition using the option I and i as shown below.

As seen above, the size is being changed from 256 to 128 and it leads in increasing the total Inode count on a partition:

You can create a name/label for a partition using option -M. In the following example, we are assigning DATA as the label for /dev/sda6 partition.

You can view the label name of the above partition by using e2label command as shown below:

Another way to view the label of a partition is using blkid command as shown below:

When executing mkfs command, it displays the contents like what it would do on stdout. You can even display the those messages on stdout without actually creating a filesystem using -n option as shown below:

mkfs provides the facility to create the filesystem with the number of desired inodes. It allows you to override the default number of inodes per filesystem based on the bytes-per-inode ratio as shown below.

The above can be verified using both tune2fs and df command as shown below.

下一篇: XXE

繼續閱讀