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
| Option | What it sets |
|---|---|
-L label | volume label (up to 16 characters) — for LABEL= in fstab |
-m percent | reserve for root, 5% by default. Data disks usually get 1 or 0 |
-N number | how many inodes to create (rounded up to a multiple of the number of groups) |
-i bytes | one inode per this many bytes of space: smaller value — more inodes. Default 16384, and 4096 for filesystems under 512 MiB (the small profile) |
-I size | size of one inode in bytes; default 256 |
-b size | block size: 1024, 2048, 4096 |
-T type | profile from /etc/mke2fs.conf: small, news (many inodes), largefile, largefile4 |
-U uuid | set the UUID instead of a random one |
-O features | enable or (^name) disable features: ^has_journal, ^metadata_csum |
-E options | extended options: lazy_itable_init=0, root_owner=uid:gid… |
-d directory | copy the contents of a directory into the new filesystem right away |
-F | don't ask for confirmation if the device already has a filesystem |
-n | create nothing, just show what would be created |
-q | no 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
| Option | What it changes |
|---|---|
-l | show parameters (same as dumpe2fs -h) |
-L label | volume label |
-m percent / -r blocks | reserve for root in percent / in blocks |
-u user / -g group | who besides root may use the reserve |
-U uuid | UUID: a specific one, random or time |
-c number | run fsck every N mounts (-1 — disable) |
-i interval | check once per interval: 30d, 2w, 6m (m — months); 0 — disable |
-e behavior | what to do on a filesystem error: continue, remount-ro, panic |
-O features | enable/disable features: -O ^has_journal, -j — add a journal |
-o options | default 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
| Field | Meaning |
|---|---|
Filesystem volume name | label (<none> — not set) |
Filesystem UUID | UUID for UUID= in fstab |
Filesystem features | enabled features: has_journal = ext3/4 with a journal, extent = ext4 |
Filesystem state | clean or not clean — needs an e2fsck check |
Errors behavior | reaction to errors (-e) |
Inode count / Free inodes | total and free inodes — the limit on the number of files |
Block count / Free blocks / Block size | filesystem size: blocks × block size |
Reserved block count | root reserve in blocks |
Inode size | inode size |
Mount count / Maximum mount count | mounts since the last check / threshold (-1 — no threshold) |
Last checked / Check interval | last 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
mkfsdestroys data with no "trash can". Mix up/dev/sdband/dev/sdaand you lose the system. Check withlsblk -fandblkidbefore running it.- Confirmation is asked only in a terminal. If the device already has a filesystem,
mkfs.ext4in an interactive shell asks "Proceed anyway?", but in a script or through a pipe it silently overwrites it.-Fsuppresses 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-ior a larger-N. - A 5% reserve on a large data disk is tens of gigabytes available only to root. That's why
Availindfis less thanSize − Used, and a regular user gets "No space left on device" while root can still write. Reduce it withtune2fs -m 1. - The label is truncated to 16 characters with a "label too long" warning.
e2fsckon a mounted filesystem can corrupt it. Check it unmounted or with-n.-I 128gives inodes that cannot store dates after 2038 —mkfswarns 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