Where to start First steps track · 15 stages
Linux Labs

Filesystems: ext4, xfs, fsck

7 min Ubuntu 24.04

Filesystems: creating one with mkfs, labels and UUIDs, fsck checks, choosing between ext4 and xfs.

Cheat sheet

5 commands Printable version
  • lsblk -f ; blkid ; df -Th filesystem overview
  • sudo mkfs.ext4 -L data /dev/sdX1 create one (wipes data!)
  • sudo tune2fs -m 1 /dev/sdX1 shrink the root reserve to 1%
  • sudo umount /dev/sdX1 && sudo fsck -fy /dev/sdX1 check ext (must be unmounted!)
  • sudo xfs_repair /dev/sdX1 check xfs

Overview

A filesystem organizes data on a partition/volume. The most common ones:

  • ext4 — general-purpose, can be shrunk, checked with fsck/e2fsck;
  • xfs — scales well, can only grow (cannot be shrunk), repaired with xfs_repair.

The chain: a block device → a filesystem (mkfs) → mounting (mount) → files.

bash
lsblk -f                          # devices + filesystem type + UUID + mount point
blkid                             # UUID and type of each partition
df -Th                            # mounted filesystems with type and space

Recipes

3

Creating and labeling

bash
sudo mkfs.ext4 /dev/sdb1          # create ext4 (WIPES data!)
sudo mkfs.ext4 -L data /dev/sdb1  # with a label right away
sudo mkfs.xfs /dev/sdb1           # create xfs
sudo mkfs.xfs -f /dev/sdb1        # overwrite an existing filesystem
sudo e2label /dev/sdb1 data       # a volume label (ext)
sudo xfs_admin -L data /dev/sdb1  # a volume label (xfs)
sudo blkid /dev/sdb1              # find the UUID (for fstab)

Mounting via /etc/fstab is best done by UUID (/dev/sdX names can shift).

Tuning and info (ext4)

bash
sudo tune2fs -l /dev/sdb1         # all filesystem parameters (UUID, mount count, features)
sudo tune2fs -m 1 /dev/sdb1       # root reserve: 5%→1% (reclaim space on large disks)
sudo tune2fs -L newlabel /dev/sdb1
sudo tune2fs -U random /dev/sdb1  # change the UUID (after cloning a disk — otherwise a duplicate!)
sudo dumpe2fs -h /dev/sdb1        # a brief superblock summary
xfs_info /mnt/xfs                 # parameters of a mounted xfs

Checking (fsck)

bash
sudo umount /dev/sdb1             # the filesystem must be UNMOUNTED!
sudo fsck -f /dev/sdb1            # force-check ext (-f)
sudo fsck -y /dev/sdb1            # auto-answer "yes" to fixes
sudo e2fsck -fy /dev/sdb1         # the same, directly for ext
sudo xfs_repair /dev/sdb1         # for xfs (fsck.xfs does nothing!)
sudo xfs_repair -n /dev/sdb1      # dry-run (check only, don't fix)

Never run fsck/xfs_repair on a mounted filesystem — it can corrupt data. The root filesystem is checked from a live/recovery environment or via an automatic check on the next boot (fsck.mode=force as a kernel parameter, or tune2fs -c). More in "Danger zone".

Troubleshooting

5

An ext4 error, the filesystem went read-only

ext4 found damage and wrote EXT4-fs error to the kernel log (see dmesg). What happens next is set by the error behaviour in the superblock:

bash
sudo dumpe2fs -h /dev/sdb1 | grep -i 'errors behavior'
sudo tune2fs -e remount-ro /dev/sdb1     # continue | remount-ro | panic

A new filesystem from mkfs.ext4 gets Continue: the error is logged and work goes on. The Ubuntu root is mounted with errors=remount-ro (/etc/fstab). With remount-ro the kernel aborts the journal (Aborting journal) and writes Remounting filesystem read-only. On the Ubuntu 24.04 kernel 6.8 it looks like this:

  • findmnt still shows rw, while writes answer Input/output error;
  • mount -o remount,rw returns 0, but writes do not come back;
  • mount -o remount,ro gives cannot mount /dev/loop0 read-only, code 32.

Only a check of the unmounted filesystem fixes it:

bash
sudo umount /mnt/data
sudo dumpe2fs -h /dev/sdb1 | grep -iE 'state|error count'
sudo e2fsck -fy /dev/sdb1; echo $?       # 1 — errors were fixed
sudo mount /mnt/data

Before the check dumpe2fs shows Filesystem state: clean with errors and FS Error count: 1, after it clean. e2fsck refuses a mounted filesystem: is mounted. and Cannot continue, aborting., code 8. Repeated errors on a real disk are a sign of wear: check SMART (smartctl -a).

Can't shrink xfs

xfs cannot be shrunk at all. Only ext4 can (resize2fs to a smaller size, while unmounted); otherwise you have to recreate it and copy the data.

"Structure needs cleaning" (xfs)

Corruption: unmount it and run xfs_repair. If it won't mount, xfs_repair -L resets the journal (a last resort, risking loss of the most recent writes).

"Not enough space" on a big disk that has free space

5% is reserved for root by default. On a data disk, reclaim it: tune2fs -m 1.

Two partitions with the same UUID after cloning

Change one of them: tune2fs -U random (ext) / xfs_admin -U generate (xfs).

Danger zone

3

mkfs wipes a partition with no way back

sudo mkfs.ext4 /dev/sdX1 (and any other mkfs.*) creates a new filesystem on top of the partition's contents — all previous data becomes unreachable immediately, with no built-in undo. Double-check the device before running it (lsblk -f, blkid): /dev/sdb1 on one machine, or after reattaching a disk, can be a completely different partition.

fsck/xfs_repair on a mounted filesystem

Checking and fixing a filesystem while it's mounted and in use can corrupt data — fsck sees an inconsistent state that the kernel hasn't yet flushed to disk and "fixes" what was actually fine. Always umount before fsck; check the root filesystem from a live environment/recovery, or via an automatic check on the next boot.

xfs_repair -L (resetting the journal)

A last resort when xfs won't mount even for a normal repair. It resets the transaction journal — the most recent changes not yet flushed to disk are lost. Try xfs_repair without -L first.

Terms

3
filesystem
A way to organize files, directories, metadata and allocated space on storage. Also appears in: mkfs, blkid, Mounting and fstab, resize2fs / xfs_growfs and 37 more
filesystem block
A filesystem space-allocation unit whose size depends on format and configuration. Also appears in: resize2fs / xfs_growfs
block device
A device providing access to addressable storage blocks; it may contain a filesystem. Also appears in: file, Loop device, lsblk, iostat and pidstat and 1 more

See also

  • mkfs — create a filesystem: options, label, what gets wiped.
  • blkid — filesystem UUID, label and type for fstab.
  • fdisk-parted — partition a disk before mkfs.
  • mount — Mounting filesystems, persistent mounts via /etc/fstab, UUIDs.
  • lvm — PV → VG → LV; flexible disk management, extending and snapshots.
  • disk-usage — Where the space and inodes went — df, du, finding large files, pitfalls.
Loading…