TExcelTAD
Home › Linux Server Administration › When the Disk Is Full

When the Disk Is Full

"No space left on device" is one of the most common server incidents, and one of the most damaging: databases stop accepting writes, logs stop (so you lose the evidence), package upgrades break halfway, and some services crash on start because they can't write a PID file. df, du & free introduced the tools; this lesson is the incident routine, including the two cases where df and du seem to disagree.

Step 1: which filesystem, and is it space or inodes?

bash
$ df -h -x tmpfs -x devtmpfs -x squashfs
Filesystem                 Size  Used Avail Use% Mounted on
/dev/sda3                   38G   36G     0 100% /
/dev/sda2                  2.0G  180M  1.7G  10% /boot
/dev/mapper/vg_data-lv_pg   25G   14G   10G  58% /var/lib/postgresql
$ df -i /
Filesystem      Inodes   IUsed IFree IUse% Mounted on
/dev/sda3      2490368  612405 1877963   25% /

Avail 0 with Used below Size is normal: ext4 reserves 5% of blocks for root, so root-owned services can keep working a little longer and the filesystem doesn't fragment badly. If df -i shows 100% instead, you have plenty of bytes but no inodes (file slots) left; the cause is millions of tiny files (session files, a mail queue, a cache that never expires), and du won't point at it, but find / -xdev -type f | cut -d/ -f2-3 | sort | uniq -c | sort -rn | head will.

Step 2: where did the space go?

du walks the tree. Stay on one filesystem (-x), go a couple of levels deep, sort by size, and repeat inside the biggest directory:

bash · try it yourself
mkdir -p var/log/app var/cache/pkgs var/lib/db
head -c 30M /dev/zero > var/log/app/app.log
head -c 5M  /dev/zero > var/log/app/app.log.1
head -c 12M /dev/zero > var/cache/pkgs/old.deb
head -c 2M  /dev/zero > var/lib/db/data
du -xh --max-depth=2 var | sort -rh | head -5
echo "--- files over 10 MB:"
find var -xdev -type f -size +10M -printf '%s\t%p\n' | sort -rn | awk -F'\t' '{printf "%6.1f MB  %s\n", $1/1048576, $2}'
50M	var
36M	var/log/app
36M	var/log
13M	var/cache/pkgs
13M	var/cache
--- files over 10 MB:
  30.0 MB  var/log/app/app.log
  12.0 MB  var/cache/pkgs/old.deb

The usual suspects on a server, and the safe fix for each:

Where Typical cause Safe fix
/var/log a log without rotation, debug logging left on fix rotation (logrotate); truncate (don't delete) the live file
journal no size cap journalctl --vacuum-size=500M, set SystemMaxUse=
/var/lib/docker old images, stopped containers, build cache, container logs docker system df, docker image prune -a, docker builder prune, log-size limits
/var/cache/apt, old kernels upgrades apt clean, apt autoremove --purge
/tmp, app caches, core dumps crashes, forgotten exports delete what you understand; coredumpctl lists dumps
a database volume real growth, or WAL/binlogs piling up because replication or archiving is broken fix the cause; never delete database files by hand

Step 3: df says full, du says not (deleted but open)

You deleted the 40 GB log, yet df still shows the disk full. A file's space is only freed when its last name is removed and the last process closes it. A daemon that still has the log open keeps all of it allocated, invisibly: the name is gone, so du can't count it. The kernel still shows it under /proc/<pid>/fd/:

bash · try it yourself
head -c 40M /dev/zero > app.log
sleep 300 3>>app.log &            # a "daemon" that keeps app.log open as fd 3
pid=$!
rm app.log                         # the admin's first instinct
ls app.log 2>&1
echo "--- open files whose name was deleted:"
find /proc/[0-9]*/fd -lname '*(deleted)' 2>/dev/null -printf '%p -> %l\n' | sed "s#/proc/$pid/#/proc/<pid>/#"
stat -L -c 'still allocated: %s bytes' /proc/$pid/fd/3
: > /proc/$pid/fd/3                # truncate through the open descriptor
stat -L -c 'after truncating: %s bytes' /proc/$pid/fd/3
kill $pid
ls: cannot access 'app.log': No such file or directory
--- open files whose name was deleted:
/proc/<pid>/fd/3 -> /home/learner/app.log (deleted)
still allocated: 41943040 bytes
after truncating: 0 bytes

On a server, lsof +L1 (open files with zero links) or the find /proc line above finds them, as root. The fixes, best first: restart or reload the process so it closes the file (systemctl restart, or the app's reopen-logs signal), or truncate it through /proc/<pid>/fd/<n> as shown when a restart isn't possible right now. And for next time: truncate live logs with : > file (or truncate -s 0 file) instead of rm, so the space is freed immediately.

Step 4: buy time, then fix the cause

  1. Free a little space fast and safely: vacuum the journal, apt clean, compress old rotated logs, remove known caches.
  2. Get the service writing again, and check it recovered (databases may need attention after a failed write).
  3. Find the cause (Step 2), fix rotation, retention or the runaway writer.
  4. Add the alert that would have warned you: disk usage and the rate of change ("full in 4 hours" is more useful than "85%"). The Observability course does this with predict_linear().

Two preventive habits: put /var/log (or /var) and data directories on their own filesystems or LVs, so a log flood can't fill /; and keep a few hundred MB of "ballast" (a file you can delete in an emergency, e.g. fallocate -l 500M /var/ballast) on servers where you can't easily grow the disk.

Test yourself

df shows / at 100%, but du -xsh / adds up to much less. What is the most likely cause?

  1. du is broken
  2. A deleted file is still held open by a process, so its space isn't freed
  3. The disk is failing
  4. Compression

df -h shows plenty of free space, yet creating files fails with 'No space left on device'. What should you check?

  1. df -i, for inode exhaustion
  2. free -m
  3. The network
  4. The CPU load

What is the safest way to empty a live log file that a daemon is writing to?

  1. rm the file
  2. Truncate it with : > file (or truncate -s 0)
  3. mv it to /tmp
  4. chmod 000 it