← Full reference

mkfs, tune2fs, dumpe2fs

Create ext4 (including in an image file), set the label, reserve and inode count, inspect filesystem parameters

You create a filesystem with mkfs (for ext4 that is mkfs.ext4, also known as mke2fs -t ext4); you inspect the parameters of an existing ext2/3/4 filesystem with dumpe2fs and change them with tune2fs. All of this works not only on a disk partition but also on a plain image file — so you can practice without a real disk. All these tools come from the e2fsprogs package.

In the IzzyLab sandbox you can create and tune a filesystem in an image file, but you cannot mount the image (no mount permission and no loop devices). debugfs lets you look inside an image without mounting it.

Syntax

mkfs.ext4 [options] device|file [blocks_count]
tune2fs [options] device|file
dumpe2fs [-h] device|file
e2label device|file [new_label]

mkfs.ext4 options

OptionWhat it sets
-L labelvolume label (up to 16 characters) — for LABEL= in fstab
-m percentreserve for root, 5% by default. Data disks usually get 1 or 0
-N numberhow many inodes to create (rounded up to a multiple of the number of groups)
-i bytesone inode per this many bytes of space: smaller value — more inodes. Default 16384, and 4096 for filesystems under 512 MiB (the small profile)
-I sizesize of one inode in bytes; default 256
-b sizeblock size: 1024, 2048, 4096
-T typeprofile from /etc/mke2fs.conf: small, news (many inodes), largefile, largefile4
-U uuidset the UUID instead of a random one
-O featuresenable or (^name) disable features: ^has_journal, ^metadata_csum
-E optionsextended options: lazy_itable_init=0, root_owner=uid:gid…
-d directorycopy the contents of a directory into the new filesystem right away
-Fdon't ask for confirmation if the device already has a filesystem
-ncreate nothing, just show what would be created
-qno output

Other types: mkfs.ext2, mkfs.ext3, mkfs.xfs (package xfsprogs), mkfs.vfat (dosfstools), mkswap for a swap partition. mkfs -t type calls the matching mkfs.type.

tune2fs options

OptionWhat it changes
-lshow parameters (same as dumpe2fs -h)
-L labelvolume label
-m percent / -r blocksreserve for root in percent / in blocks
-u user / -g groupwho besides root may use the reserve
-U uuidUUID: a specific one, random or time
-c numberrun fsck every N mounts (-1 — disable)
-i intervalcheck once per interval: 30d, 2w, 6m (m — months); 0 — disable
-e behaviorwhat to do on a filesystem error: continue, remount-ro, panic
-O featuresenable/disable features: -O ^has_journal, -j — add a journal
-o optionsdefault mount options stored in the filesystem itself (acl, user_xattr)

tune2fs cannot change the inode count — it is set only at creation time.

Reading dumpe2fs -h

FieldMeaning
Filesystem volume namelabel (<none> — not set)
Filesystem UUIDUUID for UUID= in fstab
Filesystem featuresenabled features: has_journal = ext3/4 with a journal, extent = ext4
Filesystem stateclean or not clean — needs an e2fsck check
Errors behaviorreaction to errors (-e)
Inode count / Free inodestotal and free inodes — the limit on the number of files
Block count / Free blocks / Block sizefilesystem size: blocks × block size
Reserved block countroot reserve in blocks
Inode sizeinode size
Mount count / Maximum mount countmounts since the last check / threshold (-1 — no threshold)
Last checked / Check intervallast check / check interval

Without -h, dumpe2fs also prints a description of every block group — long and rarely needed.

Examples

# a 100 MiB image: a sparse file, disk space is allocated as data is written
truncate -s 100M disk.img

# ext4 with a label, 1% reserve and 50,000 inodes
mkfs.ext4 -L data -m 1 -N 50000 disk.img

# key parameters
dumpe2fs -h disk.img 2>/dev/null | grep -E 'volume name|Inode count|Reserved block count|Block size'
tune2fs -l disk.img | grep -i uuid

# label and UUID via blkid; change the label
blkid disk.img
e2label disk.img backup
tune2fs -L backup disk.img          # same thing

# remove the root reserve on a data disk
tune2fs -m 0 disk.img

# switch to read-only on a filesystem error
tune2fs -e remount-ro disk.img

# filesystem for many small files: set the inode count explicitly
mkfs.ext4 -F -N 100000 disk.img

# create a filesystem with a directory's contents and look inside without mounting
mkfs.ext4 -F -q -d ./site disk.img
debugfs -R 'ls -l /' disk.img
debugfs -R 'cat /index.html' disk.img

# check the filesystem (only when NOT mounted!); -n — fix nothing
e2fsck -f -n disk.img

# grow the image and the filesystem in it
truncate -s 200M disk.img
resize2fs disk.img

Pitfalls

  • mkfs destroys data with no "trash can". Mix up /dev/sdb and /dev/sda and you lose the system. Check with lsblk -f and blkid before running it.
  • Confirmation is asked only in a terminal. If the device already has a filesystem, mkfs.ext4 in an interactive shell asks "Proceed anyway?", but in a script or through a pipe it silently overwrites it. -F suppresses the question in a terminal too.
  • Out of inodes — "No space left on device" with free space left. A million small files eat up inodes before blocks. Check df -i; the only fix is to recreate the filesystem with a smaller -i or a larger -N.
  • A 5% reserve on a large data disk is tens of gigabytes available only to root. That's why Avail in df is less than Size − Used, and a regular user gets "No space left on device" while root can still write. Reduce it with tune2fs -m 1.
  • The label is truncated to 16 characters with a "label too long" warning.
  • e2fsck on a mounted filesystem can corrupt it. Check it unmounted or with -n.
  • -I 128 gives inodes that cannot store dates after 2038 — mkfs warns about this. Keep 256.
  • A small image gets few inodes and a small journal: a 10 MiB image fits only 2560 files by default.

See also

mount · df-du · truncate · dd · system-info