Open In App

Basics of mke2fs command in Linux with examples

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

mke2fs command is used to create a filesystem usually in form of a disk partition or file directly from the Linux terminal, it is a part of the e2fsprogs package.  

The e2fsprogs package provides the following tools :

  • mke2fs
  • tune2fs
  • dumpe2fs
  • debugfs

Through mke2fs we can create ext2, ext3, or ext4 filesystems

Relation between mkfs and mke2fs :

We should keep in consideration that mkfs is part of the util-linux package and allows you to create filesystems of many different varieties, whereas mke2fs being a part of the e2fsprogs package used only to create ext2/3/4 filesystems.

Also, we should keep in consideration that mkfs calls mke2fs when it is requested to create an ext2/3/4 filesystem.

The basic syntax of mke2fs command : 

mke2fs [ -c | -l filename ] [ -b block-size ] [ -f fragment-size ] [ -g blocks-per-group ] [ -G number-of-groups ] [ -i bytes-per-inode ] [ -I inode-size ] [ -j ] [ -J journal-options ] [ -K ] [ -N number-of-inodes ] [ -n ] [ -m reserved-blocks-percentage ] [ -o creator-os ] [ -O feature[,…] ] [ -q ] [ -r fs-revision-level ] [ -E extended-options ] [ -v ] [ -F ] [ -L volume-label ] [ -M last-mounted-directory ] [ -S ] [ -t fs-type ] [ -T usage-type ] [ -U UUID ] [ -V ] device [ blocks-count ]

mke2fs -O journal_dev [ -b block-size ] [ -L volume-label ] [ -n ] [ -q ] [ -v ] external-journal [ blocks-count ]

Note:  mke2fs command is destructive. mke2fs places a new, formatted filesystem onto the partition. typing the wrong partition name will overwrite all existing data!!

Some Basic Commands :

To avoid significant loss of data, it is recommended to use these commands after you have made a backup of your data. 

For safe operation try executing these commands after making an image file and creating file systems within that. We can explore and experiment with file systems without a need for spare hardware. We’ll use the dd command to create our image file.

dd if=/dev/zero of=~/abcx.img bs=1M count=250

 

  • bs: block size (1MB)
  • count: number of blocks(250)
  • the total size of our image file: 250 MB 

 1. To get mke2fs version: 

mke2fs -V

 

 2. To get the basic syntax for mke2fs along with the tags: 

mke2fs

 

 3. To find bad blocks and create an ext2 filesystem: 

sudo mke2fs -c ~/abcx.img

     

 

Note: Specifying -c twice leads to a slower read-write test instead of a fast read-only when the -c is specified only once

 4. To create a filesystem and quickly check for bad blocks: 

sudo mke2fs -t fs_type -c ~/abcx.img

 In the command above -t is used for specifying the filesystem type we want to create followed by fs_type which can be replaced by ext2/3/4.

 

 5. To create an ext4 filesystem with a BACKUP volume label:

sudo mke2fs -t ext4 -L BACKUP ~/abcx.img

 

6. To create an ext4 filesystem with 8290 bytes per inode, with a volume label BACKUP:

  sudo mke2fs -t ext4 -L BACKUP -i 8290 ~/abcx.img

 

7. To find the number of bytes per inode: 

df -i /dev/sdb

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads