Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 4 · Operations and troubleshooting · Lesson 08 · 50 minutes

Troubleshoot a storage problem in the right order

After this lesson you can
  • Tell a full disk, a lack of inodes, a deleted file that is still open, a wrong owner and a read-only mount apart
  • Run the storage checks in a fixed order and read what each one proves
  • Repair each fault safely and prove that the volume works again
Lab files of this lesson (21)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

The ticket queue has five messages about one application volume: "no space left", "cannot create new files although there is space", "the disk is full but I deleted everything", "the application cannot write" and "the volume is read-only". They look the same from far away: something fails to write. Your lead wants the cause of each, found in a fixed order and not by guessing. A script plants the five faults on a small practice volume, one at a time.

Recall

df says that a volume is 100% full, but du adds up to far less. What is a likely cause?

A file that was deleted while a process still holds it open. Its blocks stay in use, but it has no name, so du cannot count it (lesson 3 of Part 2).

Which command shows the owner, the group and the mode of a folder and not its contents?

ls -ld FOLDER (lessons 1 and 10 of Part 1). The option -d lists the directory itself.

The idea

A write can fail for five reasons that live in different places. The checks are cheap, so run them in this order, and stop at the first one that explains the symptom.

The order of the storage checks
Space left?Inodes left?Where is it?Deleted but open?Owner and mode?Read-only mount?df -hdf -idu -ah | sort -rhlsof -nP +aL1 VOLUMEls -ld, id, findmntSpace left?Inodes left?Where is it?Deleted but open?Owner and mode?Read-only mount?12345
  1. Space left? → Inodes left?: df -h
  2. Inodes left? → Where is it?: df -i
  3. Where is it? → Deleted but open?: du -ah | sort -rh
  4. Deleted but open? → Owner and mode?: lsof -nP +aL1 VOLUME
  5. Owner and mode? → Read-only mount?: ls -ld, id, findmnt
SymptomCheck that shows itCauseRepair
No space left on device, Use% 100df -hThe blocks are usedFind the big files, free or move them
Same message, df -h shows free spacedf -i, IUse% 100No free inodes: too many small filesDelete the files in the directory that holds them
df full, du smalllsof +aL1A deleted file is still openEnd or restart the process that holds it
Permission deniedls -ld, idWrong owner, group or modechown or chmod the one folder
Read-only file systemfindmnt shows roMounted read-only, by hand or after an errorFind out why, then remount read-write

An inode is the record that holds a file's owner, mode, size and the place of its data. A file system has a fixed number of them. lsof lists open files; +aL1 keeps those with a link count below one (deleted), and the volume name keeps only that volume. -n and -P stop it from looking up host names and port names, which is faster and easier to read.

Worked example

The lab needs administrator rights, because it mounts a volume and makes a test user, kr_app. The volume is a real ext4 file system of 8 MB inside an image file, /srv/krvol.img, so no real disk is at risk. truncate -s 8M makes the empty file, mkfs.ext4 writes a file system into it (-N sets the number of inodes, -m 0 reserves no blocks for root), and mount -o loop mounts it through a loop device, which makes a file look like a disk (/dev/loop0; the number can differ). The outputs come from a virtual machine with Ubuntu 24.04. Install lsof if it is missing (sudo apt install -y lsof).

1. Save the lab script

Read the script. fault N mounts a fresh volume, plants the fault, and down removes the volume and the user. Planting a fault again resets the volume.

storlab.sh

#!/bin/bash
# storlab.sh - plants storage faults on a small ext4 volume at /mnt/krvol. Run it with sudo.
#   fault N   1 the volume is full, 2 it has no free inodes, 3 a deleted file is still open,
#             4 the application user may not write, 5 the volume is read-only
#   down      remove the volume, its image file and the test user
set -u
[ "$(id -u)" -eq 0 ] || { echo "run it with sudo"; exit 1; }
M=/mnt/krvol
IMG=/srv/krvol.img
PIDFILE=/run/krlab-storlab.pid

reset() {    # a new, empty ext4 file system with at most $1 inodes, in an image file
    [ -f "$PIDFILE" ] && kill "$(cat "$PIDFILE")" 2> /dev/null
    rm -f "$PIDFILE"
    sleep 1
    umount "$M" 2> /dev/null
    rm -f "$IMG"
    truncate -s 8M "$IMG"
    mkfs.ext4 -q -F -m 0 -N "$1" -L krvol "$IMG"
    mkdir -p "$M"
    mount -o loop "$IMG" "$M"
    chmod 1777 "$M"    # every user may create files in the root, as in /tmp
    id kr_app > /dev/null 2>&1 || useradd --system --shell /usr/sbin/nologin kr_app
}

case "${1:-}" in
fault)
    case "${2:-}" in
    1) reset 1024
       mkdir "$M/logs" "$M/data"
       head -c 2097152 /dev/zero > "$M/logs/old-2024.log"
       head -c 614400 /dev/zero > "$M/logs/app.log"
       head -c 100M /dev/zero > "$M/logs/old-2025.log" 2> /dev/null ;;
    2) reset 40
       mkdir "$M/spool"
       for i in $(seq 1 100); do touch "$M/spool/msg-$i" 2> /dev/null; done ;;
    3) reset 1024
       echo "keep this note" > "$M/README.txt"
       head -c 3145728 /dev/zero > "$M/held.dat"
       tail -f "$M/held.dat" > /dev/null 2>&1 &
       echo $! > "$PIDFILE"
       sleep 1
       rm "$M/held.dat" ;;
    4) reset 1024
       mkdir "$M/data"
       chown root:root "$M/data" ;;
    5) reset 1024
       echo "kept" > "$M/status.txt"
       mount -o remount,ro "$M" ;;
    *) echo "fault 1 to 5"; exit 1 ;;
    esac
    echo "fault $2 is planted on $M"
    ;;
down)
    [ -f "$PIDFILE" ] && kill "$(cat "$PIDFILE")" 2> /dev/null
    rm -f "$PIDFILE"
    sleep 1
    umount "$M" 2> /dev/null
    rm -f "$IMG"
    rmdir "$M" 2> /dev/null
    userdel kr_app 2> /dev/null
    echo "volume removed"
    ;;
*)
    echo "usage: sudo bash storlab.sh fault N|down"
    exit 1
    ;;
esac

2. Fault 1: the volume is full

cd ~/krlab/linux-operations
sudo bash storlab.sh fault 1
df -h /mnt/krvol
df -i /mnt/krvol
sudo du -ah /mnt/krvol | sort -rh | head -n 4
fault 1 is planted on /mnt/krvol
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      3.8M  3.6M     0 100% /mnt/krvol
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/loop0       1024    16  1008    2% /mnt/krvol
3.6M	/mnt/krvol/logs
3.6M	/mnt/krvol
2.0M	/mnt/krvol/logs/old-2024.log
980K	/mnt/krvol/logs/old-2025.log

Read the df -h line like this:

The df line of the full volume
/dev/loop0the device of the file system3.8Mits size3.6Mused0available100%share used/mnt/krvolwhere it is mounted/dev/loop0the device of the file system3.8Mits size3.6Mused0available100%share used/mnt/krvolwhere it is mounted

The size is less than 8 MB, because the journal and the inode tables of ext4 take space. df -i is the second look: the inodes are almost all free, so the blocks are the problem and not the inodes. du -ah lists every file and folder with a readable size (-a all, -h human), and sort -rh puts the biggest first. It runs with sudo, because only root may read the folder lost+found, where fsck puts the pieces of damaged files. The folder logs holds it all, and the old log of 2024 is the biggest file. The name says 2024, but a name proves nothing: first ask whether a program holds the file open. lsof FILE lists the processes that have that one file open and prints nothing when there are none. Then free the space by moving the file to another volume, not by deleting it. A safer way to free a log that a program writes is : > FILE (: does nothing, and > FILE empties the file), which keeps the name and the open handle. Here the file is not in use.

sudo lsof /mnt/krvol/logs/old-2024.log
sudo mv /mnt/krvol/logs/old-2024.log /var/tmp/
df -h /mnt/krvol | tail -n 1
/dev/loop0      3.8M  1.6M  2.0M  45% /mnt/krvol

lsof printed nothing, so nobody holds the file. Move first, delete later: when the service has run for a day and nobody misses the file, remove it from /var/tmp. Never rm a file that you cannot get back unless you know what it is.

3. Fault 3: deleted, but the space does not return

A background program reads a big file, and somebody deleted that file. Plant it, and compare df with du.

sudo bash storlab.sh fault 3
df -h /mnt/krvol | tail -n 1
sudo du -sk /mnt/krvol
sudo lsof -nP +aL1 /mnt/krvol 2>/dev/null
fault 3 is planted on /mnt/krvol
/dev/loop0      3.8M  3.1M  564K  85% /mnt/krvol
24	/mnt/krvol
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NLINK NODE NAME
tail    1186 root    3r   REG    7,0  3145728     0   13 /mnt/krvol/held.dat (deleted)

df counts 3.1 MB and du counts 24 KB: the folders and the one small note. lsof names the holder (2>/dev/null hides its warnings about files that it cannot read):

The lsof line of the deleted file
tailthe program that holds it1186its process id3rfile descriptor 3, read0links: no name is left/mnt/krvol/held.dat (deleted)the file that has no nametailthe program that holds it1186its process id3rfile descriptor 3, read0links: no name is left/mnt/krvol/held.dat (deleted)the file that has no name

The program tail, process 1186, holds a file of 3,145,728 bytes that has no name any more. Two ways to free the space: end the holder (kill 1186), or, when it must keep running, empty the file through its handle with : > /proc/1186/fd/3. A service would be restarted with systemctl restart.

sudo kill $(sudo lsof -t +aL1 /mnt/krvol 2>/dev/null)
sleep 1
df -h /mnt/krvol | tail -n 1
/dev/loop0      3.8M   28K  3.6M   1% /mnt/krvol

4. Fault 5: the volume is read-only

sudo bash storlab.sh fault 5
touch /mnt/krvol/new.txt
findmnt -no OPTIONS /mnt/krvol
fault 5 is planted on /mnt/krvol
touch: cannot touch '/mnt/krvol/new.txt': Read-only file system
ro,relatime

The message names the cause, and findmnt shows ro first in the mount options. findmnt -no OPTIONS PATH prints the options of the mount (-n no header, -o the column). Here somebody remounted it by hand, and the repair is a remount. On a real disk the usual cause is different: after an error the kernel remounts the file system read-only to protect it (the option errors=remount-ro). Then you must read the kernel log (sudo dmesg or sudo journalctl -k) for I/O errors, back up what you can, and run fsck on the unmounted file system before you remount.

sudo mount -o remount,rw /mnt/krvol
touch /mnt/krvol/new.txt && echo "writable again"
writable again

5. Check the file system with fsck

fsck (file system check) reads a file system and reports damage. Unmount it first. -f forces a full check, even when the file system was cleanly unmounted, and -n answers "no" to every repair, so the first run changes nothing. Then mount it again.

sudo umount /mnt/krvol
sudo fsck -f -n /srv/krvol.img
echo "exit $?"
sudo mount -o loop /srv/krvol.img /mnt/krvol
fsck from util-linux 2.39.3
e2fsck 1.47.0 (5-Feb-2023)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
krvol: 13/1024 files (7.7% non-contiguous), 1099/2048 blocks
exit 0

The five passes found nothing, and the exit code 0 means no errors. If a pass reports damage, copy the image or the disk first, then run sudo fsck -f -y to repair it (-y answers "yes"). On a real disk the device is a partition such as /dev/sdb1, and the same rule applies: never on a mounted file system.

Your turn

Two faults are left. Plant each with sudo bash storlab.sh fault N, find the cause with the order of the checks, repair it and prove the repair.

  • Fault 2. Creating a file fails although the volume has space. Find the directory that uses the inodes, and free enough of them to create a new file in the root of the volume. sudo du --inodes -d 1 PATH counts inodes by folder.
  • Fault 4. The application user kr_app must write into /mnt/krvol/data, and it cannot. Test with sudo -u kr_app touch /mnt/krvol/data/x, which runs one command as that user. Give the folder to the user and nothing else.

Answer from your own terminal, with the fault in place when you look.

When you have finished, remove the lab with sudo bash storlab.sh down.

Check yourself

df -h shows 30% used, and creating a file still fails with "No space left on device". What do you check?

df -i. The volume may have used all its inodes, with many small files. The blocks are free, but there is no inode for a new file.

You delete a 10 GB log, and df still shows the volume full. What happened, and how do you fix it without a reboot?

A process still holds the deleted file open. lsof +aL1 VOLUME names the process. Restart the service so that it closes the file, or empty the file through /proc/PID/fd/N.

A file system is read-only after a power failure. Why is "mount -o remount,rw" not the first step?

The kernel made it read-only because it found errors. Writing to a damaged file system can make the damage worse. Read the kernel log, back up what you can, run fsck on the unmounted volume, and only then remount.

Why give the folder to the application user with chown and not set mode 777?

Mode 777 lets every user and every program write there, which is a weakness. chown gives the right to one account and nobody else (lesson 10 of Part 1).

Field notes

  • Put logs on their own volume, or limit them with logrotate, so that a runaway log fills only its own volume and not the root file system.
  • Monitor inodes as well as space. A mail spool or a cache directory with millions of small files is a common cause of "disk full" with free blocks.
  • Reserved blocks: ext4 keeps about 5% for root by default, so a volume can be full for ordinary users while root still writes. df shows Avail for ordinary users.
  • Never run fsck on a mounted file system, and never use it as a first guess. Snapshot or copy the disk first when the data matters.
  • When you free space in an emergency, move files to another volume instead of deleting them, and write down what you moved.