`No space left on device` but `df -h` shows free space: inodes and deleted files
This one is a classic and I still fell for it. Our upload endpoint started throwing errors on a Debian VM. The disk looked half empty, so I assumed it was a permissions problem and wasted a good half hour there before I ran the right command.
## The error
From the app log:
OSError: [Errno 28] No space left on device
File "/srv/app/upload.py", line 62, in save
with open(dest, 'wb') as f:
And from the shell:
$ df -h /srv
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 28G 20G 58% /srv
Twenty gigabytes free. Clearly not full. Except df -h only tells you about blocks, and there's a second thing that can run out.
## Check inodes first
Every file needs an inode. If you have millions of tiny files, you can exhaust inodes while blocks are still free:
$ df -i /srv
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3276800 3276798 2 100% /srv
That was it. Two inodes left. The app writes one small session file per request and nothing was cleaning them up. Count them with:
find /srv/app/sessions -type f | wc -l
It was over three million. Deleting the old ones freed the inodes immediately:
find /srv/app/sessions -type f -mtime +7 -delete
## The other cause: deleted but open files
If inodes are fine, check for files that were deleted while a process still holds them open. The space isn't reclaimed until that process closes the handle:
$ sudo lsof +L1 | head
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
nginx 1042 root 12w REG 8,1 2147483648 0 99812 /var/log/nginx/access.log (deleted)
A 2 GB deleted log file held open by nginx. We'd rotated logs with a script that unlinked instead of copying and truncating, and nginx never reopened. Restarting the process released the space:
sudo systemctl restart nginx
Then df -h showed the space come back.
One more case I've hit on a different box: a tmpfs mount with a fixed size can be full while the underlying disk is fine. df -h will show that mount at 100% and df -i will look normal. Check the Mounted on column rather than just the percentages. On this particular VM it was a real disk, but I've been burned by a 64 MB /tmp before and now I look at the filesystem column first. It's a small habit that has saved me more than once.
## Why this was my fault
Our monitoring alerted on block usage at 85%, which never fired because blocks were at 58%. It said nothing about inode usage. I've since added an inode check to the same alert, and changed the log rotation to use copytruncate so nginx doesn't need a restart.
## The short version of the debugging order
When you see Errno 28, run df -h, then df -i, then lsof +L1. Those three commands separate full disk, no inodes, and open deleted files, which between them are almost always the answer. I keep them in a note on my desktop now because I'd rather not relearn this at 2 a.m. again.