Where to start First steps track · 15 stages
Linux Labs

mkfs — create a filesystem

7 min Ubuntu 24.04 · e2fsprogs 1.47 · xfsprogs 6.6

The mkfs command creates an empty ext4 or xfs filesystem on a partition; the old data on it becomes unreachable.

Cheat sheet

7 commands Printable version
  • lsblk -f /dev/vdb what is on the disk now
  • sudo mkfs.ext4 /dev/vdb1 ext4 on a partition
  • sudo mkfs.ext4 -L data /dev/vdb1 with the label data
  • sudo mkfs -t ext4 /dev/vdb1 the same through mkfs -t
  • sudo mkfs.xfs -L logs /dev/vdb2 xfs with a label
  • sudo mkfs.ext4 -n /dev/vdb1 show parameters, create nothing
  • sudo blkid /dev/vdb1 the new filesystem's UUID for fstab

Overview

mkfs (make filesystem) writes the filesystem's own structures to a partition: the superblock with its parameters, inode tables, the journal. After that the partition can be mounted and filled with files. mkfs.ext4 and mkfs.xfs are separate programs, one per type; mkfs -t type just calls the right one.

Preparing a new disk goes: table and partition (fdisk-parted) → filesystem (mkfs) → mount (mount).

text
student@ubuntu:~$ sudo mkfs.ext4 -L data /dev/loop0p1
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 130560 4k blocks and 130560 inodes
Filesystem UUID: 4db8b2fe-ff93-4036-b20c-8175523f9825
Superblock backups stored on blocks:
        32768, 98304

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

On creation the filesystem gets a UUID and, with -L, a label. blkid prints both, and they are what goes into /etc/fstab. Running mkfs again on the same partition gives a new UUID, and the old fstab line no longer finds the partition.

How ext4 and xfs differ and how to check and repair them is in Filesystems.

Options

10

Below are the options of mkfs.ext4 (that is mke2fs) and mkfs.xfs. The full lists are in man mke2fs and man mkfs.xfs.

Option What it does Example
-L LABEL Filesystem label — the name for LABEL= in fstab and lsblk -f. sudo mkfs.ext4 -L data /dev/vdb1
-t TYPE Filesystem type for mkfs — ext4, xfs, vfat. sudo mkfs -t ext4 /dev/vdb1
-m PERCENT Percentage of space reserved for root; 5 by default. sudo mkfs.ext4 -m 1 /dev/vdb1
-n Show what would be created without writing anything. sudo mkfs.ext4 -n /dev/vdb1
-q Quiet — no progress output, for scripts. sudo mkfs.ext4 -q /dev/vdb1
-L LABEL xfs label, up to 12 characters. sudo mkfs.xfs -L logs /dev/vdb2
-F Force — do not ask even if the device already holds a filesystem. dangerous sudo mkfs.ext4 -F /dev/vdb1
-i BYTES One inode per this many bytes; a smaller number gives more inodes. sudo mkfs.ext4 -i 4096 /dev/vdb1
-U UUID Set the UUID instead of a random one. sudo mkfs.ext4 -U 3f6c1a2e-9b7d-4c55-8e21-6d0b9a4f7c13 /dev/vdb1
-f Overwrite an existing filesystem. dangerous sudo mkfs.xfs -f /dev/vdb2

Nothing found. Try a shorter query.

Recipes

5

A new data partition

bash
lsblk -f /dev/vdb                       # vdb1 has an empty FSTYPE
sudo mkfs.ext4 -L data /dev/vdb1
sudo mkdir -p /srv/data
sudo mount /dev/vdb1 /srv/data
df -h /srv/data

Find a partition by its label

bash
sudo blkid -L data             # /dev/vdb1
sudo mount LABEL=data /srv/data

Less root-only space on a big disk

bash
sudo mkfs.ext4 -m 1 -L backup /dev/vdb1   # reserve 1% instead of 5%

By default ext4 keeps 5% of the space for root only. On a data disk that is wasted gigabytes; on an existing filesystem tune2fs -m changes the reserve.

Lots of small files

bash
sudo mkfs.ext4 -i 4096 /dev/vdb1   # one inode per 4 KiB

The number of inodes is fixed at creation. If there are millions of files and the inodes run out before the space does, the filesystem has to be recreated — see disk-usage.

A filesystem inside a file

bash
truncate -s 256M disk.img
mkfs.ext4 -q disk.img              # no sudo: it is a regular file
sudo mount disk.img /mnt           # mount sets up the loop itself

More in Loop device.

Troubleshooting

6

contains a ext4 file system … Proceed anyway? (y,N)

The partition already has a filesystem, and mkfs.ext4 asks again. Answering y wipes it. If you need the partition, answer N and check the name with lsblk -f.

/dev/vdb1 is mounted; will not make a filesystem here!

The partition is mounted and in use. If it really has to be recreated, unmount it first (sudo umount /dev/vdb1) and remove its fstab line.

appears to contain an existing filesystem (mkfs.xfs)

mkfs.xfs found another filesystem and refused. -f overwrites it, once you are sure the partition is not needed. On a mounted partition it answers contains a mounted filesystem and prints its usage — run umount first.

The file /dev/vdb1 does not exist and no size was specified

There is no such partition: a typo, or the kernel has not re-read the table. Compare with lsblk; after partitioning run sudo partprobe /dev/vdb.

After mkfs the partition does not mount from fstab

mkfs produced a new UUID, and fstab still has the old one. Take the new one from sudo blkid /dev/vdb1 and fix the line — see mount.

mkfs on the whole disk instead of a partition

mkfs.ext4 /dev/vdb (no digit) puts the filesystem on the whole disk, over the partition table. That works, but the partitions are gone. If you need a partition, run fdisk or parted first, then mkfs on /dev/vdb1.

Danger zone

2

mkfs wipes a partition with no undo

The new filesystem is laid over the old one, and the old files are unreachable at once. mkfs.ext4 asks for confirmation only in a terminal: in a script or a pipeline it silently writes over an existing filesystem. Check the device before you run it: lsblk -f shows its type, label and mount point. /dev/vdb1 can be a different partition on another machine or after a reboot.

mkfs to "repair" a partition

If a partition does not mount because of a wrong fstab line, mkfs does not repair anything — it wipes the data. Compare the fstab line with blkid output first.

See also

  • fdisk-parted — create the partition before mkfs.
  • blkid — the new filesystem's UUID, label and type.
  • mount — mount the filesystem and add it to fstab.
  • filesystems — ext4 and xfs, fsck checks, tune2fs.
  • lsblk — the disks and their filesystems.
  • loop-device — practise on a file instead of a disk.
Loading…