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

by Balakrishnan Mariyappan on January 8, 2013

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.

1. View the Available Filesystem Builder Commands

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.

2. Build a Filesystem on a Specific Device

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.

3. Create a Filesystem with Journal using -j option

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 

4. Check for Bad Blocks on a Device

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

5. Force to Create a Filesystem on a Mounted Partition

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

6. Bytes per Inode Group on a Partition

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.

7. Change the Inode size 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

8. Set the Volume Label for Partition

You can create a name/label for a partition using option -L. 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" 

9. Simulate a Filesystem Creation

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

10. Create a Filesystem with specific number of Inodes

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 15 comments… read them below or add one }

1 Appreciator January 8, 2013 at 10:00 am

Thanks for the article dude… it is really helpful, thanks for the email from the website that directed me here.

2 Jalal Hajigholamali January 8, 2013 at 11:07 am

Hi,

Thanks for very usable and helpful article

3 Jalal Hajigholamali January 8, 2013 at 11:20 am

Hi,

This line is (OK) correct # tune2fs -l /dev//sda6 | grep Inode

but it is better to remove one “/” from /dev//sda6

4 cee January 9, 2013 at 12:09 am

I am always using:
mkfs.ext4 instead of mkfs -t ext4
and their corresponding versions for different file systems.
What is the difference or what version should be favored?

Thank you for any suggestion.
Best regards,
cee

5 Kuldeep January 9, 2013 at 1:40 am

Many thanks for very informative article.
Please check 8th point entitled “Set the Volume Label for Partition”, seems typo in using appropriate switch (option) for labeling.

6 Ghasem Pahlavan January 9, 2013 at 3:47 am

Thanks :D .

7 matcet January 10, 2013 at 7:57 pm

Thanx for the article, good article, now i know, that i can control my drive with linux with varian of configs. Things that we can’t do explicitly on windows.

8 Balakrishnan Mariyappan January 14, 2013 at 1:50 am

Hey Kuldeep,
Thanks for pointing out the typo and corrected the same.

9 Balakrishnan Mariyappan January 14, 2013 at 2:07 am

@cee :
Run whereis command for mkfs.ext4,

# whereis mkfs.ext4
mkfs: /sbin/mkfs.ext2 /sbin/mkfs.ntfs /sbin/mkfs.ext4 /sbin/mkfs.vfat /sbin/mkfs.ext3 /sbin/mkfs.ext4dev /sbin/mkfs.minix /sbin/mkfs /sbin/mkfs.msdos /sbin/mkfs.bfs /sbin/mkfs.cramfs /usr/share/man/man8/mkfs.8.gz

mkfs.ext4 is actually a executable file(i.e: filesystem builder) present under /sbin(this path may differ across Linux distrubutions).

when you do “mkfs -t ext4″, the search is on a standard directories list to find out the file filesystem builder(i.e: mkfs.ext4).

Hence both ways are similar and there is no major difference.

Hope I clarified you.

10 sathish January 18, 2013 at 3:36 am

Hi,

I need to know, how to setup the partition inode value as unlimited.

if ex inode limit 100.
+ i created 100 files
+ further i cant create more files. so in this case if set as unlimit it wil be helpful. (or increase dynamically)

can anyone know, pls share it.

Thanks in advance

11 Azhar Khan January 20, 2013 at 9:48 pm

Thanks Alot

12 Balakrishnan Mariyappan January 23, 2013 at 10:18 am

@sathish,
as fas as I know, there is no way of increasing inode value dynamically.
I can give you 2 suggestion on this,

1. When the filesystem is supposed to hold huge number of files, you should actually be thinking of having the partition with large disk size.

2. There is no way of increasing inode count after creating filesystem. You can actually create a softlink for another partition(i.e: partition X) when the inode count is about to go beyond its limit. And the newer files can be continued to store under the softlink. Remember that It is just a logical view(I assume that you are aware of soft/hard link in unix) and the files(under softlink) originally going to reside on partition X.

13 Balakrishnan Mariyappan January 23, 2013 at 10:22 am

@Appreciator, Jalal Hajigholamali, Kuldeep, Ghasem Pahlavan, matcet, Azhar Khan :

Thank you so much for your comments.

I am so happy that this article would have helped you in someway :) !!

14 Gaurav February 6, 2013 at 8:03 am

Thanks to ‘GeekStuff’ and ‘Balakrishnan Mariyappan’ for this artical .

15 Bengt D November 30, 2013 at 4:56 pm

Sorry. This is too complicate for a normal pc-user. Instead tell us where we can find how to make a boot floppy with cfdisk? I have just fixed a floppy with bg-tlb-2.0. But could not find cfdisk on it.
\BgD

Leave a Comment

Previous post:

Next post: