← Whiteboard questions

A server runs out of disk space

df says full, du adds up to 11 GB

Troubleshooting, Medium

An alert says the root volume of a Linux server is full, and the application cannot write. What you see: - df -h / shows 50 GB size, 50 GB used, 0 available (100%). - df -i / shows 8% of inodes used. - du -xsh /* adds up to only 11 GB. Say which commands you run, in order, what explains the missing 39 GB, how you free space safely without losing live data, and how you stop it happening again.

Examples

Input df -h / and df -i /
Output Blocks: 50 GB size, 50 GB used, 0 available (100%). Inodes: 8% used.
Why The numbers from the statement. Read both before you choose the next command.
Input sudo du -xh --max-depth=1 / | sort -h | tail -4
Output 6.1G /var, 3.2G /usr, 0.9G /home, 11G /
Why Shows where the visible files are and how much they add up to. Compare the total with what df reports.
Input Decoy: du -sh /var/cache/apt /var/lib/apt/lists
Output About 1.2 GB.
Why Real waste you could clean, but small next to the amount that is missing.

Constraints

Your answer must contain

A strong answer covers A strong answer compares df and du, finds a deleted file that a process still holds open, frees it safely, and adds alerts and log rotation.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Inodes are fine, so the problem is blocks. Why can df and du disagree by 39 GB? Think about what du can and cannot see.

Hint 2 the approach

A file deleted while a process still has it open keeps its blocks, but no name shows it. Find a command that lists open files that were deleted.

Hint 3 the key step

Free the space by restarting the service, or by truncating the file through the process file descriptor. Then fix log rotation so the program reopens its log.

Walkthrough

Full walkthrough brute force to best

First minutes, in order

The commands, in order. Run df -h and df -i to see blocks and inodes. Inodes are at 8 percent, so the problem is blocks, not many small files. Run sudo du -xh --max-depth=1 / | sort -h | tail to find big directories. The -x option stays on one filesystem. The visible files add up to 11 GB, so 39 GB is allocated but not visible.

Narrow down the cause

The usual reasons for such a gap: a file deleted while a process holds it open, data written under a directory that was later used as a mount point, space used by container layers or snapshots, and reserved blocks, which are small. Run sudo lsof +L1 to list open files with no name left, and findmnt to check for a mount that hides data. To look under a mount point, bind-mount the root filesystem somewhere else and measure it: mount --bind / /mnt && du -sh /mnt/*.

Cause and fix

Cause: an application log file was removed by hand, or by log rotation, while the application kept writing to it. The file has no name, so du cannot see it, but its 39 GB stays allocated until the process closes it. The lsof output shows the process id and the size. Safe fix: do not delete live data blindly. Restart the service, which closes the file and frees the space. If a restart is not allowed, truncate the file through the process file descriptor, for example : > /proc/<pid>/fd/<fd>. Then run df -h to confirm that the space is back. Free the small waste afterwards, such as old rotated logs, the package cache and old container images, after you look at what each one is.

Read the evidence

Read the evidence. df shows 50 GB used and du shows 11 GB, so 39 GB is missing. df -i shows 8 percent of inodes, so it is not small files. lsof +L1 shows the application log with a link count of 0 and a size of 39 GB. That one file explains the whole gap. The 1.2 GB in the apt cache is real waste, but it is too small to explain 39 GB.

Commands

df -h /
df -i /
sudo du -xh --max-depth=1 / | sort -h | tail -4
sudo lsof +L1
findmnt
: > /proc/<pid>/fd/<fd>
sudo mount --bind / /mnt && sudo du -sh /mnt/*

Confirm and prevent

Prevention: alert at 80 and 90 percent on blocks and on inodes. Use logrotate with size and age limits and a postrotate signal or copytruncate, so the program reopens its log. Ship logs off the box. Keep retention rules for artifacts and images. Put application data on its own volume, so the root volume cannot fill.

Common mistakes

  • Deleting the biggest file blindly.
  • Running rm -rf on log directories while the application runs.
  • Checking only df -h and never asking why du disagrees.
  • Fixing the space but not the log rotation, so it happens again.

Related problems

A server runs out of disk space: walkthrough and solution