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.
# cd /sbin # ls mkfs* mkfs mkfs.bfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.ext4dev mkfs.minix mkfs.msdos mkfs.ntfs mkfs.vfat
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.
# mkfs -t ext3 /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1120112 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
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.
# mke2fs /dev/sda6 -j mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1120112 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
When you don’t need the journaling on a filesystem, then execute mke2fs command as shown below.
# mke2fs /dev/sda6
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.
# mke2fs -c /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1120112 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000
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.
# mke2fs -F /dev/sda6
You may want to list out the used and available inodes in the partition as shown below using df command.
# df -i /dev/sda6 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda6 1120112 11 1120101 1% /mydata
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.
# cd /mydata # touch sample.txt # ls sample.txt # df -i /dev/sda6 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda6 1120112 12 1120100 1% /mydata
You may also use the tune2fs command to view the Inode details:
# tune2fs -l /dev/sda6 | grep Inode Inode count: 1120112 Inodes per group: 8176 Inode blocks per group: 511 Inode size: 256
To change the inode-per-group of the above partition(i.e:/dev/sda6) use -i option as follows:
# mke2fs -i 8192 /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 2240224 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 16352 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
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.
# df -i /dev/sda6 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda6 2240224 11 2240213 1% /mydata # tune2fs -l /dev/sda6 | grep Inode Inode count: 2240224 Inodes per group: 16352 Inode blocks per group: 1022 Inode size: 256
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.
# mke2fs -I 128 -i 8192 /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 2240224 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 16352 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
As seen above, the size is being changed from 256 to 128 and it leads in increasing the total Inode count on a partition:
# tune2fs -l /dev//sda6 | grep Inode Inode count: 2240224 Inodes per group: 16352 Inode blocks per group: 511 Inode size: 128
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.
# mke2fs -L DATA /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label=DATA OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1120112 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
You can view the label name of the above partition by using e2label command as shown below:
# e2label /dev/sda6 DATA
Another way to view the label of a partition is using blkid command as shown below:
# blkid /dev/sda6 /dev/sda6: LABEL="DATA" UUID="0de74d35-6050-4838-99b0-46cb1d518da8" TYPE="ext2"
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 -t ext3 -n /dev/sda6 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1120112 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 8176 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000
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.
# mkfs -t ext3 -v -N 700000 /dev/sda6 mke2fs 1.42 (29-Nov-2011) fs_types for mke2fs.conf resolution: 'ext3' Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 701440 inodes, 4476416 blocks 223820 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 137 block groups 32768 blocks per group, 32768 fragments per group 5120 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
The above can be verified using both tune2fs and df command as shown below.
# tune2fs -l /dev/sda6 | grep -i inode Filesystem features: has_journal ext_attr resize_inode dir_index filetype sparse_super large_file Inode count: 701440 Free inodes: 701429 Inodes per group: 5120 Inode blocks per group: 320 First inode: 11 Inode size: 256 Journal inode: 8 Journal backup: inode blocks # df -i /dev/sda6 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda6 701440 11 701429 1% /mnt