Keeping filesystems intact

Keeping a filesystem healthy: how to see what is using space, how to check and repair, and what a journal does that makes a repair rarely necessary.

Lesson 2 of 6 in objective 104. Devices, Linux filesystems, filesystem hierarchy standard, part of LPIC-1 Exam 101-500.

Two questions about space, and why the answers disagree. df — Reads: The filesystem itself; Counts: Blocks in use, per mount; Blind to: Which directory is to blame; The flag to know: -i, when inodes run out first. du — Reads: The directory tree, by walking it; Counts: The files it can see; Blind to: A deleted file a process still holds open; The flag to know: -sh for one total df du Reads The filesystem itself The directory tree, by walking it Counts Blocks in use, per mount The files it can see Blind to Which directory is to blame A deleted file a process still holds open The flag to know -i, when inodes run out first -sh for one total
Two questions about space, and why the answers disagree.

Space, and the second thing that runs out

df reports free space per mounted filesystem — df -h in human units, df -i for inodes. That second one matters: a filesystem can be nowhere near full in bytes and still refuse to create a file, because every file needs an inode and the count was fixed when the filesystem was made. Millions of tiny files is how you get there, and df alone will not show it.

du reports usage by directory, walking the tree — du -sh DIR for one total, du -h --max-depth=1 to find which subdirectory is the problem. df and du can legitimately disagree: a deleted file still held open by a running process is gone from du's tree and still occupying space df counts, which is resolved by restarting whatever holds it.

Checking and repairing

fsck is the front end that dispatches to a filesystem-specific checker: e2fsck for the ext family, xfs_repair for XFS. The rule that matters more than any flag is that a filesystem must be UNMOUNTED, or mounted read-only, before it is checked — running a repair on a live filesystem corrupts it. Checks also run automatically at boot, driven by the mount count and interval in the superblock.

A journal is what makes most of that unnecessary. ext3, ext4 and XFS record what they are about to do before they do it, so after a power cut the filesystem replays or discards the incomplete records rather than scanning every inode. ext2 has no journal, which is precisely why an ext2 filesystem takes so long to check after an unclean shutdown.

tune2fs adjusts ext filesystem parameters after creation — the label, the reserved-blocks percentage, the check interval — and tune2fs -l prints the superblock. dumpe2fs prints far more detail. badblocks scans for physically bad sectors, and is the layer below all of this.

XFS answers with a family of separate programs rather than flags on one, and what separates them is which may run on a MOUNTED filesystem. xfs_repair checks and repairs, and demands the filesystem be unmounted, because it rewrites metadata the kernel also believes it owns. xfs_fsr reorganises the data of a filesystem that is mounted and in use — the defragmenter, and it is safe there precisely because it moves file contents through ordinary filesystem calls rather than going behind the kernel's back. xfs_db inspects metadata, xfs_info reports geometry and xfs_admin changes the label and UUID. The generalisation is worth carrying past XFS: a repair tool wants the filesystem offline, a reorganiser does not.

Which condition a filesystem must be in before it is checked, and the move that corrupts it. A column of 4 states: Mounted read-only (Still mounted, but nothing can be written to it); Mounted read-write (Normal use, with writes still going to it); Unmounted (Not mounted anywhere, so nothing can write to it); Checked and repaired (fsck dispatches: e2fsck on ext, xfs_repair on XFS). You get from Mounted read-write to Unmounted by unmounting it first; from Unmounted to Checked and repaired by fsck, or the automatic check at boot; from Mounted read-only to Checked and repaired by fsck, without unmounting: read-only is enough. One arrow is drawn crossed through, because that move does not exist: Mounted read-write to Checked and repaired — never: repairing a live read-write filesystem corrupts it. Mounted read-only Still mounted, but nothing can be written to it Mounted read-write Normal use, with writes still going to it unmounting it first Unmounted Not mounted anywhere, so nothing can write to it fsck, or the automatic check at boot Checked and repaired fsck dispatches: e2fsck on ext, xfs_repair on XFS fsck, without unmounting: read-only is enough never: repairing a live read-write filesystem corrupts it
Which condition a filesystem must be in before it is checked, and the move that corrupts it.

Worth carrying in

df -h / df -i
Free space by filesystem; -i for inodes, which run out separately.
du -sh DIR
Total usage of a directory tree.
fsck -t ext4
Check a filesystem. Unmount it first.
e2fsck -f
Force a check of an ext filesystem even if it looks clean.
tune2fs -l
Print the ext superblock. Also sets label, reserved blocks, check interval.
dumpe2fs
Full ext filesystem metadata.
xfs_repair
The XFS repair tool. Unmount first. xfs_info reports geometry, xfs_admin sets the label.
xfs_fsr
Defragment an XFS filesystem while it stays mounted and in use.
badblocks
Scan for physically bad sectors.

What the exam does with this

Objective
104. Devices, Linux filesystems, filesystem hierarchy standard
Share of the exam
25% (the whole objective)
Questions in this lesson
11
Signed for by a person
0

Partly checked. None of the 11 questions here has been read against the cited source by a person. 11 questions have been checked against their cited clause by an automated pass — which is not the same thing, and is not a signature.

Only questions a person has signed for are used in mock exams here. That is the whole difference between the two kinds of checking above.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

Drill this lesson

A lesson is one sitting: the trainer draws a short run from these questions alone and spaces the ones you get wrong.

Practise Keeping filesystems intact

Questions in this lesson

Practise Keeping filesystems intact

The rest of objective 104