11.5. Maintaining Storage Devices
A filesystem in service needs three kinds of attention: keeping the device informed about freed space (discard), watching capacity in both of its dimensions (blocks and inodes), and recognizing the failure signals - read-only remounts and I/O errors - before they become mysteries. This section exercises all three on the practice disks.
11.5.1. Discard, Periodic TRIM, and SSD Considerations
Flash storage cannot overwrite in place: writing a block requires erasing the larger erase block that contains it first, and the drive's firmware relocates data internally to hide that. The consequence is that deleting a file does not tell the drive anything - from the device's view the blocks simply stop being updated. Over time the drive accumulates stale-but-unerased pages that must be shuffled before every rewrite, which is why an SSD that has never been told about deletions slows down as it fills.
TRIM is the notification. The kernel knows which blocks a filesystem has freed; TRIM commands pass that knowledge down so the drive can erase in the background. There are two ways to deliver it:
- Online discard - the
discardmount option. Every deletion sends TRIMs immediately. The example system's root entry carries it - theFS options: discard,errors=remount-roline for/dev/vda1in Section 11.3.3's verifier output - the same pair of options Chapter 2 recorded on the freshly installed system's root in Section 2.4.1, where the installer addeddiscardon a device that reports support. - Periodic TRIM -
fstrimrun on a schedule. One command walks each mounted filesystem's free space and sends it as fewer, larger batches, which the drive can process more efficiently than a stream of tiny per-deletion TRIMs.
Debian enables the periodic form by default: the fstrim.timer unit
triggers fstrim.service weekly. Only one of the two is needed; running
both is harmless but redundant. Which to keep is a documented tradeoff
(continuous freshness versus batch efficiency), and Debian's default
answer is the weekly batch.
Before trimming anything, check what the device actually supports - the
discard columns of lsblk -D:
# lsblk -D -o NAME,DISC-ALN,DISC-GRAN,DISC-MAX,DISC-ZERO /dev/vdb
NAME DISC-ALN DISC-GRAN DISC-MAX DISC-ZERO
vdb 0 512B 2G 0
├─vdb1 0 512B 2G 0
├─vdb2 0 512B 2G 0
└─vdb3 0 512B 2G 0
DISC-GRAN and DISC-MAX are the granularity and maximum size of a
discard the device accepts - here the hypervisor advertises 512-byte
granularity and a whole-disk maximum for its virtual disks; on physical
hardware these values come from the drive itself and are typically
larger. A zero DISC-MAX would mean the device wants no TRIMs at all.
Run the trim by hand once, verbosely:
# fstrim -av
/srv/handbook/data: 487 MiB (510705664 bytes) trimmed on /dev/vdb1
/: 96.8 GiB (103916584960 bytes) trimmed on /dev/vda1
fstrim option |
Description |
|---|---|
-a |
Trim all mounted filesystems that support it. |
-v |
Print one line per filesystem: how much was trimmed, on which device. |
Both mounted filesystems trimmed: the root disk (96.8 GiB of free space
on the 100 GiB root) and the data partition. The data partition reports
487 MiB rather than its full 512 MiB because the ext4 journal and
metadata are not free - only free space is trimmable. The figure is
close to df's Size minus Used (488 MiB − 164 KiB), and larger than
df's Avail (452 MiB): the 5% reserved blocks of Section 11.5.2 are
held back for root, not allocated, so they count as free and are trimmed
with the rest.
The timer that automates this is enabled, and one query shows both the state and the schedule:
# systemctl is-enabled fstrim.timer
enabled
# systemctl list-timers fstrim.timer --no-pager
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2026-11-23 00:37:38 UTC 2 days Fri 2026-11-20 01:31:50 UTC - fstrim.timer fstrim.service
1 timers listed.
Pass --all to see loaded but inactive timers, too.
The next run is Monday at 00:37 UTC - the weekly schedule, with the
timer's randomized minute - and the timer's unit activates
fstrim.service, which runs the same trim across all supported mounted
filesystems. Nothing else needs configuring for a default SSD
installation; the timer is on unless it has been disabled.
Two boundary notes. First, layered stacks must pass discards through
every layer - an LVM or LUKS layer in the middle either supports discard
or silently drops it, and Chapter 13 covers the
per-layer options; plain partitions, this chapter's case, always pass.
Second, do not confuse "supports TRIM" with "is an SSD": the example
virtio disk advertises discard support because the hypervisor does.
Rotational-disk detection through the kernel is unreliable on virtual
hardware anyway - Chapter 9 demonstrated
lsblk's ROTA column mislabeling virtio disks in Section 9.4.3 - which
is the general rule for all device-property questions on virtualized
storage: read what the device reports, and remember who reported it.
11.5.2. Monitoring Capacity and Inode Consumption
A filesystem has two exhaustible resources. Blocks hold file content;
inodes hold file identity - the metadata record every file, directory,
and symlink needs. df reports both, and a full filesystem can be full
in either dimension while looking healthy in the other.
The data filesystem from the earlier sections, which now holds three small files:
# df -h /srv/handbook/data
Filesystem Size Used Avail Use% Mounted on
/dev/vdb1 488M 164K 452M 1% /srv/handbook/data
# df -i /srv/handbook/data
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vdb1 32768 15 32753 1% /srv/handbook/data
df option |
Description |
|---|---|
| (none) | Blocks in 1 KiB units - the least readable form. |
-h |
Blocks in human units (MiB, GiB). |
-i |
Inodes instead of blocks. |
PATH |
Report only the filesystem containing PATH. |
Both dimensions are near-empty. Two numbers in the block view deserve an
explanation, because they confuse everyone once: the partition is
512 MiB but Size reads 488 MiB - ext4's journal and metadata consume
the difference - and Avail (452 MiB) falls short of
Size − Used by roughly 36 MiB. The largest part is ext4's reserved
blocks: by default 5% of the filesystem (25.6 MiB here) is held back
for root, so a filesystem filled to 100% by ordinary users still lets
root work (tune2fs -m adjusts the percentage; Chapter 12
covers it); the remaining ~10 MiB is a further safety margin the
free-space accounting keeps beyond that reserve. The
inode view shows 32,768 inodes - ext4 sizes its inode table at creation
from the volume size, here one inode per 16 KiB of data blocks - with 15
already used: the filesystem's own bookkeeping (root directory,
lost+found, journal files), plus the three small files the earlier
sections wrote.
When capacity does run short, the next question is where, and du
answers it directory by directory - du -sh /srv/handbook/* would show
which of the mounted trees holds the bytes; Chapter 8
used the same command to attribute collected log growth in Section 8.6.5.
Inode exhaustion is the counterintuitive failure, so it is worth seeing once under controlled conditions. The exercise builds a throwaway filesystem with an absurdly small inode count - reusing the loop-image technique of Section 11.2.1 - and then fills it. As john, in the workspace, create a 64 MiB image:
$ dd if=/dev/zero of=inode-lab.img bs=1M count=64 status=none
Silent. In the root shell, attach it to a loop device, create an ext4 filesystem with only 64 inodes on it, mount it, and hand it to john:
# loop=$(losetup --find --show /home/john/debian-handbook/storage/inode-lab.img); mkfs.ext4 -q -F -N 64 -L inode-lab "$loop"; mount -o noatime "$loop" /srv/handbook/inodes; chown john:john /srv/handbook/inodes
The whole sequence is silent: the command substitution captures what
losetup --find --show prints, and the three commands after it produce
no output. That captured name is what the cleanup later needs - the
device it chose on the example system was /dev/loop0, so
echo "$loop" after the sequence would confirm yours.
mkfs.ext4 -N 64 sets the inode count explicitly (Section
11.3.1's table); -F is the same force flag that table describes, kept
here so a scripted creation answers no confirmation prompt. Everything
after that is the familiar mount-and-chown
sequence from Section 11.3.2. Check both dimensions before filling:
# df -h /srv/handbook/inodes; df -i /srv/handbook/inodes
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 59M 46K 55M 1% /srv/handbook/inodes
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/loop0 64 12 52 19% /srv/handbook/inodes
55 MiB of free block space, 52 free inodes. Now consume the inodes
without consuming the blocks. This script creates empty files one at a
time until creation fails, then reports the failure - save it as
~/debian-handbook/storage/exhaust-inodes.sh:
#!/bin/sh
# Fill an ext4 filesystem that has very few inodes, then report the failed
# name. Run from inside the mounted practice filesystem.
set -u
cd /srv/handbook/inodes || exit 1
name=1
while touch "file$name"; do
name=$((name + 1))
done
echo "stopped creating files at file$name"
exit 1
Run it as john, from the workspace where the script lives:
$ sh /home/john/debian-handbook/storage/exhaust-inodes.sh
touch: cannot touch 'file53': No space left on device
stopped creating files at file53
The loop created file1 through file52 - 52 empty files, no blocks to
speak of - and the 53rd touch failed; touch reports the failure on
standard error the moment it happens, and the script's echo line
follows on standard output. Look at both dimensions now:
# df -h /srv/handbook/inodes; df -i /srv/handbook/inodes
Filesystem Size Used Avail Use% Mounted on
/dev/loop0 59M 46K 55M 1% /srv/handbook/inodes
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/loop0 64 64 0 100% /srv/handbook/inodes
The block view is unchanged: 55 MiB "available", 1% used. The inode view is full. One more creation attempt as john makes the diagnosis unmistakable:
$ touch /srv/handbook/inodes/one-more
touch: cannot touch '/srv/handbook/inodes/one-more': No space left on device
"No space left on device" with df -h reporting ample space means the
space that ran out was inode space - and that is the whole diagnostic:
when a filesystem refuses writes but df -h disagrees, run df -i
before deleting anything. (The same error with df -h also full means
the reserved blocks ran out for a non-root writer, per the reserved-space
discussion above - the two cases are separated by the two df views.)
Production filesystems hit this for a reason: workloads that create vast
numbers of small files - maildirs, cache directories, version-control
object stores. The uncomfortable fact about ext4 is that the inode count
is fixed at creation; a filesystem whose workload outgrows its inodes
cannot be given more in place. The remedy is planning: size the ratio at
mkfs time (-N, or -i bytes-per-inode), or choose a filesystem whose
metadata allocation is dynamic - XFS and Btrfs both allocate inodes as
files are created - criteria that
Chapter 12's selection section treats in full.
Clean up the exercise, in the root shell, unmounting and detaching the
loop device the setup attached (/dev/loop0 here):
# umount /srv/handbook/inodes; losetup -d /dev/loop0
Silent. The image file stays in john's workspace until the chapter's closing cleanup.
11.5.3. Diagnosing Read-Only Remounts and I/O Failures
Two symptoms end this chapter because they end a lot of storage troubleshooting sessions: a filesystem that quietly became read-only, and I/O errors from a device. Both have boring causes (an administrator remounted something; a tool failed) and serious ones (the drive is dying), and the diagnostic order in Figure 11.3 separates them.
Figure 11.3. Conceptual diagnostic order for the two failure symptoms of this section: check the mount's option set first, then the kernel log, then the device. The sections referenced in the figure are this chapter's; Chapters 12, 15, and 16 cover repair, backup, and recovery.
The read-only state first. The data filesystem is currently mounted read-write from its fstab entry. Remount it read-only, deliberately, in the root shell:
# mount -o remount,ro /srv/handbook/data
Silent. Verify from the option set - this is step one of the figure, and it is a read-only lookup:
# findmnt -no OPTIONS /srv/handbook/data
ro,noatime
ro is right there in the options. As john, attempt a write:
$ printf "%s\n" "should fail" >> /srv/handbook/data/note.txt
bash: line 2: /srv/handbook/data/note.txt: Read-only file system
The error names the condition exactly: the filesystem is read-only, not the file, not the permissions - a different error text than Permission denied, and the distinction tells the administrator where to look. The kernel log records the transition, which is what makes deliberate and accidental remounts distinguishable after the fact:
# dmesg | tail -20
[ 5.678485] [drm] Initialized cirrus 2.0.0 for 0000:00:02.0 on minor 0
[ 5.683077] fbcon: cirrusdrmfb (fb0) is primary device
[ 5.691438] Console: switching to colour frame buffer device 128x48
[ 5.701860] cirrus 0000:00:02.0: [drm] fb0: cirrusdrmfb frame buffer device
[ 5.737601] RAPL PMU: API unit is 2^-32 Joules, 0 fixed counters, 10737418240 ms ovfl timer
[ 5.745557] cryptd: max_cpu_qlen set to 1000
[ 5.769802] AES CTR mode by8 optimization enabled
[ 29.714587] systemd-ssh-generator[873]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[ 36.494715] EXT4-fs (vdb2): mounted filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194 r/w with ordered data mode. Quota mode: none.
[ 38.676430] EXT4-fs (vdb2): unmounting filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194.
[ 41.432165] systemd-ssh-generator[1006]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[ 48.561740] EXT4-fs (vdb2): mounted filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194 r/w with ordered data mode. Quota mode: none.
[ 53.493521] EXT4-fs (vdb2): unmounting filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194.
[ 55.809822] EXT4-fs (vdb3): mounted filesystem 94db6eea-71bb-4d0c-a3a9-994f0defe923 r/w with ordered data mode. Quota mode: none.
[ 73.159949] EXT4-fs (vdb3): unmounting filesystem 94db6eea-71bb-4d0c-a3a9-994f0defe923.
[ 93.905324] loop: module loaded
[ 93.905844] loop0: detected capacity change from 0 to 131072
[ 95.414049] EXT4-fs (loop0): mounted filesystem 8e7f7bec-9389-4002-9f14-933b3de6a674 r/w with ordered data mode. Quota mode: none.
[ 106.367915] EXT4-fs (loop0): unmounting filesystem 8e7f7bec-9389-4002-9f14-933b3de6a674.
[ 109.080810] EXT4-fs (vdb1): re-mounted 67848521-8140-424f-a998-97fbaf0dcbf6 ro.
The whole session is readable in the log: the udisks mount and unmount of
vdb2, the automount exercise (its mount and unmount of the same
device), the scratch mount of vdb3 from Section 11.4.3, the inode
exercise's loop mount, and the last line - the read-only remount of
vdb1, with ext4 naming the device and the filesystem's UUID. Every
mount and remount this chapter performed left one of these lines, which
is the property that makes the log the arbiter when nobody remembers what
happened: searching it for EXT4-fs lines - or the journal's persisted
per-boot copy with journalctl -k - answers "who set this read-only,
and when".
The serious variant arrives without an administrator's help. The root
filesystem's fstab entry carries errors=remount-ro (Section 11.3.3
showed it in the verifier output): that option tells ext4 that when it
detects an internal error or a device that fails writes, it must force
itself read-only rather than keep writing onto a medium it can no longer
trust. A read-only root with such an entry, no deliberate remount in the
log, and I/O errors near it in the same log is therefore a protective
response, not a malfunction of the mount system. The demonstration above
produced the state by hand; a genuine errors=remount-ro remount needs a
device that actually fails, and that is what the second half of this
section simulates from the device side.
Restore the data filesystem:
# mount -o remount,rw /srv/handbook/data
Silent.
I/O errors are the second symptom, and they can be observed without failing hardware, because device-mapper can manufacture a device that always fails. In the root shell, create a 100 MiB error device - the table means "from sector 0 for 204800 sectors, fail every request":
# dmsetup create handbook-error --table "0 204800 error"
Silent. The device exists as /dev/mapper/handbook-error. Read one
sector from it:
# dd if=/dev/mapper/handbook-error of=/dev/null bs=512 count=1
dd: error reading '/dev/mapper/handbook-error': Input/output error
0+0 records in
0+0 records out
0 bytes copied, 0.00117957 s, 0.0 kB/s
Input/output error - EIO, the errno a process receives when the
kernel cannot complete the request on the device. 0+0 records in: not
one complete record was read. This is a synthetic device, but the error
path is the real one - a process reading a failing disk sees exactly this
error, and the kernel logs the device side of it:
# dmesg | tail -20
[ 5.769802] AES CTR mode by8 optimization enabled
[ 29.714587] systemd-ssh-generator[873]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[ 36.494715] EXT4-fs (vdb2): mounted filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194 r/w with ordered data mode. Quota mode: none.
[ 38.676430] EXT4-fs (vdb2): unmounting filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194.
[ 41.432165] systemd-ssh-generator[1006]: Failed to query local AF_VSOCK CID: Cannot assign requested address
[ 48.561740] EXT4-fs (vdb2): mounted filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194 r/w with ordered data mode. Quota mode: none.
[ 53.493521] EXT4-fs (vdb2): unmounting filesystem bcd65ade-67cd-4438-9b2c-3f4319d69194.
[ 55.809822] EXT4-fs (vdb3): mounted filesystem 94db6eea-71bb-4d0c-a3a9-994f0defe923 r/w with ordered data mode. Quota mode: none.
[ 73.159949] EXT4-fs (vdb3): unmounting filesystem 94db6eea-71bb-4d0c-a3a9-994f0defe923.
[ 93.905324] loop: module loaded
[ 93.905844] loop0: detected capacity change from 0 to 131072
[ 95.414049] EXT4-fs (loop0): mounted filesystem 8e7f7bec-9389-4002-9f14-933b3de6a674 r/w with ordered data mode. Quota mode: none.
[ 106.367915] EXT4-fs (loop0): unmounting filesystem 8e7f7bec-9389-4002-9f14-933b3de6a674.
[ 109.080810] EXT4-fs (vdb1): re-mounted 67848521-8140-424f-a998-97fbaf0dcbf6 ro.
[ 117.284833] EXT4-fs (vdb1): re-mounted 67848521-8140-424f-a998-97fbaf0dcbf6 r/w.
[ 119.294136] device-mapper: core: CONFIG_IMA_DISABLE_HTABLE is disabled. Duplicate IMA measurements will not be recorded in the IMA log.
[ 119.294714] device-mapper: uevent: version 1.0.3
[ 119.296832] device-mapper: ioctl: 4.48.0-ioctl (2023-03-01) initialised: dm-devel@lists.linux.dev
[ 119.306138] Buffer I/O error on dev dm-0, logical block 0, async page read
[ 121.540925] Buffer I/O error on dev dm-0, logical block 0, async page read
The last two lines are the device-side record of the failed read: the
kernel's buffer layer reporting Buffer I/O error on dev dm-0, logical
block 0 - the device-mapper device, by its kernel name, with the block
that failed to read. On failing real hardware the log looks
the same in structure - a device name, a block number, and the failing
operation - plus the driver's own diagnostics naming the command that
failed and, for SATA/SAS drives, medium error entries naming the
sectors. The diagnostic use is the same either way: the log tells you
which device is failing, which is more than the reading process's
Input/output error ever will.
Remove the synthetic device:
# dmsetup remove handbook-error
Silent.
The response playbook for a genuinely failing device follows the
figure's lower branch, and most of it belongs to other chapters because
it is their subject: copy the data out while it can still be read -
Chapter 15's file-level copies with metadata
preservation, before anything else touches the device; check SMART
(Section 11.1.4) for the drive's own account; unmount and repair or
replace, with Chapter 12 covering e2fsck and
Chapter 16 covering the repair from recovery media;
and restore from backup onto the replacement. What this section
contributes to that sequence is the first five minutes: findmnt says
whether the filesystem is read-only, dmesg/journalctl -k say whether
the device is failing, and everything after that is data recovery rather
than diagnosis.
The chapter's exercises leave four things on the example system: the two
fstab entries, the /srv/handbook tree, the loop-image files in john's
workspace, and the fstab backup. Removal is the reverse of creation, and
every command is a pattern the chapter already demonstrated. In the root
shell, unmount the data filesystem (the automount and scratch filesystems
are already unmounted from Sections 11.4.2 and 11.4.3):
# umount /srv/handbook/data
Silent. Delete both fstab lines and regenerate the units - the same
sed idiom Section 11.3.4 used for the missing-device entry:
# sed -i "/\/srv\/handbook/d" /etc/fstab; systemctl daemon-reload
Silent. Remove the backup, the mount points, and john's workspace files:
# rm -f /etc/fstab.ch11-test-backup; rm -rf /srv/handbook
$ rm -rf ~/debian-handbook/storage
Both silent. (The last one as john, from whose shell the workspace was
created.) If
the practice disk itself should return to blank, wipefs -a /dev/vdb
erases the signatures listed by the wipefs -n of Section 11.3.1 -
partition table and filesystems alike.
Four lookups verify the end state, and all four should fail:
# grep -n /srv/handbook /etc/fstab
# findmnt /srv/handbook/data
Both print nothing and exit 1 - no fstab entries, no mounted filesystem. The automount unit is gone as well:
# systemctl is-active srv-handbook-optional.automount
inactive
inactive, with exit status 4 - systemctl's answer for a unit that is
not even loaded, which is what removing the fstab line and reloading
leaves behind. And the error device:
# dmsetup info handbook-error
Device does not exist.
Command failed.
The failure is the success: the device is gone. The system is back where the chapter found it, minus one blank disk that now knows how to be partitioned, formatted, mounted, and understood.
What comes next builds directly on this chapter's flat layout. Chapter 12 administers the filesystems themselves - tuning ext4, maintaining XFS, and Btrfs's subvolumes and scrubbing. Chapter 13 inserts the layers Figure 11.1 sketched in outline - LVM's volume groups, md RAID, LUKS encryption - and Chapter 14 grows and shrinks the whole stack. All of them assume the habits this chapter built: identify the device before writing to it, prefer stable names, put persistence where it can be audited, and read the kernel log before theorizing.