Skip to content

mkfs command on Linux (format disk)

The mkfs command on Linux formats the partition created by fdisk/gdisk/parted with the file system.

The file system type is defined by the —t option and are the native formats ext2, ext3, ext4, fat, vfat, minix, msdos, and xfs.

The mke2fs and mkdosfs commands are variations of mkfs.

mkfs has some variations that allow you to choose the type of file system directly:

$ ls -l /usr/sbin/mk * <br></br>lrwxrwxrwx 1 root 8 Aug 29 22:48 /usr/sbin/mkdosfs -> mkfs.fat <br></br>-rwxr-xr-x 4 root root 96328 Jul 26 2018 /usr/sbin/mke2fs <br></br>-rwxr-xr-x 1 root 11432 Jul 26 2018 /usr/sbin/mkfs -rwxr-xr-x 1 root 11432 Jul 26 2018 /usr/sbin/mkfs <br></br>-rwxr-xr-x r-x 4 root root & nbsp; 96328 July 26 2018 /usr/sbin/mkfs.ext2 <br></br>-rwxr-xr-x 4 root root 96328 July 26 2018 /usr/sbin/mkfs.ext3 <br></br>-rwxr-xr-x 4 root root 96328 Jul 26 2018 /usr/sbin/mkfs.ext4 -rwxr-xr-x 1 root 28512 Aug 2 2018 /usr/sbin/mkfs.ext4 <br></br>-rwxr-xr-x 1 root 28512 Aug 2 2018 /usr/sbin/mkfs.ext4 mkfs.fat <br></br>-rwxr-xr-x 1 root root 83824 Jul 26 2018 /usr/sbin/mkfs.minix

lrwxrwx 1 root 8 Aug 29 22:48 /usr/sbin/mkfs.msdos -> mkfs.fat
lrwxrwxrwx 1 root August 8 29 22:48 /usr/sbin/mkfs.vfat -> mkfs.fat -rwxr-xr-x 1 root 37r/24 Jan 24 2019 /usr/sbin/mkfs.vfat -> mkfs.fat
-rwxr-xr-x 1 root 37r/24 Jan 24 2019 /usr/sbin/mkfs.vfat -> mkfs.fat -rwxr-xr-x 1 root 37r/24 Jan 24 2019 sbin/mkfs.xfs

Depending on the variation you choose, mkfs may have more or fewer options.

In general, the possible mkfs command options are:

  • -t: Indicates the type of format the partition should be
  • -c: Check the device for bad blocks (defects); -L: name Configure the device
  • name; -n: name Configure the device
  • name; -n: name Configure the device name to the msdos format;
  • -q: Make mkfs work with a minimum of possible video output;
  • -v: Make mkfs work with the maximum possible video output;
  • -m: Percentage of reserved disk

An interesting mkfs option is the “-m percentage”, which allows you to reserve disk space as a percentage to avoid crashing the system in the event of a full disk. The idea is for the operating system to warn that the disk is full for ordinary users, BEFORE the disk is actually completely full. This allows the system to continue working for a while until the administrator can take some action to prevent a really full disk from stopping the system from functioning or causing data loss.

Example:

Format with ext3 file system:

# mkfs.ext3 /dev/xvdb1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux
Block size=4096 (log=2) <br></br>Fragment size=4096 (log=2) <br></br>Stride=0 blocks, Stripe width=0 blocks <br></br>524288 inodes, 2096891 blocks <br></br>104844 blocks (5.00%) reserved for the super user <br></br>First data block=0 <br></br>Maximum filesystem blocks=2147483648 <br></br>64 block groups <br></br>32768 blocks for group, 32768 fragments per group <br></br>8192 inodes per group <br></br>Superblock backups stored on blocks: <br></br>32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 <br></br>Allocating group tables: done <br></br>Writing inode tables: done

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

Format the disk with xfs:

# mkfs.xfs /dev/xvdb1 <br></br>meta-data=/dev/xvdb1 isize=512 agcount=4, agsize=524223 blks <br></br>= sectsz=512& nbsp; attr=2, projid32bit=1 <br></br>= crc=1 finobt=1, sparse=0 <br></br>date = bsize=4096 blocks=2096891, imaxpct=25 <br></br>= sunit=0 swidth=0 blks <br></br>naming =version 2 bsize=4096 ascii -ci=0 ftype=1 <br></br>log =internal log bsize=4096 blocks=2560, version=2 <br></br>= sectsz=512 sunit=0 blks, lazy-count=1 <br></br> realtime =none extsz=4096 blocks=0, rtextents=0

File Systems

Once the partitions are already defined, you can create the File System that will be used. Linux supports several file systems and your choice depends largely on the usefulness that will be given to the system and the type of disk.

Disks formatted with ext2, ext3, and ext4 have a wide range of tools. Since they use a fixed block size, they are not good file systems for small files, as they can consume a lot of disk unnecessarily.

For example, ext4 uses 4K blocks as standard. If you have a lot of files as small as 1k, each file will use an entire 4k block, even if its size is smaller than that.

XFS, in turn, is excellent at performing parallel input/output (I/O) operations using several physical disks, since it was designed for high performance graphics stations. It is ideal for applications that edit images and videos.

BTRFS, on the other hand, seems to be the right choice, since it allows snapshots, compression, defragmentation and so many other advantages, but it is not always available in all distributions.

The following table will help you choose the file system:

**File System Type****Suggested Use****Advantages Disadvantages****Disadvantages**
ext2Used in /boot and USB sticksSimple and fastDoesn't have journaling
ext3General use on LinuxAdd Journaling to ex2 without having to formatNot as fast and reliable as ext4
ext4General use on LinuxBetter performance and reliability than ext3 Doesn't allow a series of features that BTRFS has
XFSGeneral use on LinuxBetter performance than ext3. Ideal for videos.It does not allow a number of features that BTRFS has
ReiserFSIt is no longer usedEfficient for small filesHigh CPU consumption
VFATMemory cards and pendrivesEasy portability with WindowsAll :-)
BTRFSGeneral use on LinuxAll :-)Virtually none