Creating and labeling
Mounting via /etc/fstab is best done by UUID (/dev/sdX names
can shift).
Filesystems: creating one with mkfs, labels and UUIDs, fsck checks, choosing between ext4 and xfs.
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
A filesystem organizes data on a partition/volume. The most common ones:
fsck/e2fsck;xfs_repair.The chain: a block device → a filesystem (mkfs) → mounting (mount) → files.
Mounting via /etc/fstab is best done by UUID (/dev/sdX names
can shift).
Never run
fsck/xfs_repairon 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=forceas a kernel parameter, ortune2fs -c). More in "Danger zone".
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:
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:
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).
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.
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).
5% is reserved for root by default. On a data disk, reclaim it:
tune2fs -m 1.
Change one of them: tune2fs -U random (ext) / xfs_admin -U generate (xfs).
mkfs wipes a partition with no way backsudo 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 filesystemChecking 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.
Found an inaccuracy? Report an error in the article