The mkfs Command

The mkfs (i.e., make filesystem) command is used to create a filesystem (i.e., a system for organizing a hierarchy of directories, subdirectories and files) on a formatted storage device or media, usually a partition on a hard disk drive (HDD),

A partition is a logically independent section of a HDD. A formatted partition or disk is one to which a low level format, also called a physical format, has been applied. It consists of dividing the disk's tracks into a specified number of sectors and filling the data area of each sector with dummy byte values or with a pattern of test values. This results in the destruction of any existing data on the disk. Formatting of new HDD and floppy disks is done at the factory. It is rarely necessary to perform a low level format on a HDD. Low level formats are more commonly performed on floppy disks (using the fdformat command) to prepare them for use with a different type of filesystem and operating system.

The creation of a filesystem is also referred to as high level formatting or logical formatting. It includes creation of a table of contents for the partition or disk, and it does not destroy data already on the partition or disk.

mkfs is actually just a front-end for the various specific filesystem creation programs that are available in Linux, such as mke2fs, mkfs.ext3 and mkfs.vfat. The specific program is searched for first in several standard directories such as /sbin, /sbin/fs, /sbin/fs.d and /etc/fs (the precise list is defined when mkfs is compiled) and then in the directories listed in the PATH environment variable (which lists the directories that the system searches for an executable program).

mkfs's syntax is

mkfs [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]

The items in square brackets are optional, and the only mandatory argument (i.e., input) is filesys. filesys is the name of a device file (i.e., a file that the system uses to implement access to a physical device), such as /dev/hda3, the third partition on the first hard disk, or /dev/fd0, the first floppy disk drive. It can also be the mount point (i.e., the point where it is attached to the system) for the new filesystem, such as the root directory, /usr or /home.

The most commonly used option is -t, which is followed by the type of filesystem to be created. If this option is not used, the default is ext2 (second extended filesystem). Among the other types of filesystems that can be created are ext3, minix, msdos, vfat and xfs.

As an example, the following would create an ext2 filesystem on a formatted floppy disk that has been inserted into the first floppy drive:

mkfs /dev/fd0

The following would be used to create a vfat (i.e., Microsoft Windows-compatible) filesystem on the second partition of the first HDD:

mkfs -t vfat /dev/hda2

The -V option produces verbose output, including all filesystem-specific commands that are executed. Specifying this option more than once prevents the execution of any filesystem-specific commands.

fs-options are filesystem-specific options that are to be passed to the real filesystem creation program (i.e., the program for which mkfs is serving just as a front end). Although not guaranteed, they are supported by most such programs. They are -c, which checks the device for bad blocks (i.e., defective areas on the storage medium) before building the filesystem, -l, which reads the bad blocks list from a file whose name follows it, and -v, which produces verbose output.

All generic options must precede and not be combined with filesystem-specific options. Some filesystem-specific programs do not support the -V option nor return meaningful exit codes (which indicate whether the command was successful or failed). Also, some filesystem-specific programs do not automatically detect the device size and thus require a blocks argument to be provided.

Although it is stated in the man built-in manual that mkfs is used to build a Linux file system on a device, the semantics might be a bit confusing if one thinks of the definition of a filesystem as being a hierarchy of directories. That is, mkfs does not actually create a hierarchy of directories, but rather it prepares a disk or a partition thereof so that a user can create a specific type of filesystem on it. Thus, it might be more appropriate to think of a filesystem as being a way of organizing directories and files rather than the hierarchy itself.

Created June 14, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.