404. Disk health
This page is for anyone who wants to know whether a disk is healthy, be warned before one fails, or rescue data from one that is failing.
A KDOS machine watches its disks with two background programs:
- smartd polls every disk that answers SMART (the self-monitoring a drive does of its own health) and writes what it finds to the system log;
xfs_healerwatches each XFS filesystem mounted at boot and, where allowed, has the kernel repair damaged metadata while the filesystem stays mounted.
The page shows which ports provide the tools and how the two watchers are started and configured, then walks through the tasks: reading a disk’s health, running a self-test, routing smartd’s warnings, checking an NVMe drive, setting a drive’s power behaviour and copying data off a failing disk. Why a watcher skips itself, or stops for good on some exits, is explained in the KDOS book’s Services that skip themselves.
Installation
| Port | Provides |
|---|---|
| smartmontools | smartctl, which reads a disk’s SMART data and starts its self-tests, and the smartd daemon |
| nvme-cli | nvme, for an NVMe drive’s own logs and settings |
| hdparm | hdparm, which sets an ATA drive’s power management and spindown |
| xfsprogs | xfs_healer, at /usr/libexec/xfsprogs/xfs_healer and started only by its boot script; xfs_scrub and xfs_property |
| ddrescue | ddrescue, which copies a failing disk with retries and a resumable map file |
| partclone | Images a healthy filesystem by copying only its used blocks |
| f3 | f3probe, f3write and f3read, which find a stick that reports a false size |
| libatasmart | The SMART library udisks2 reads for the native disk applications |
All are on the live medium and on an installed system except ddrescue, partclone and f3, which are not on the installation image; sudo kdos update install <port> adds each (see Installing software). Two boot scripts, part of the base filesystem, start the watchers, and both are enabled on the image (/etc/service.disabled/ holds no marker for either):
| Script | Service name | Starts | Skips or stops |
|---|---|---|---|
52_smartd |
smartd |
smartd -n under the supervisor |
Stops for good, without a restart, when no disk answers SMART (exit 17), as in any virtual machine, or when /etc/smartd.conf cannot be parsed (2) or read (6) |
53_xfs_healer |
xfs_healer |
One xfs_healer per XFS filesystem mounted when it runs, each named xfs_healer-N |
Skips a filesystem whose kernel has no XFS health monitor or whose autofsck property is none, and prints no XFS filesystem to watch when none is left. A healer stops for good, without a restart, when its filesystem goes away (exit 0) or on an error (1) |
plasma-disks reads each disk’s SMART health for the desktop through smartctl behind a KAuth helper, because no session may open a raw block device.
Configuration
/etc/smartd.conf is smartmontools’ own file, installed unchanged. Its one active line is DEVICESCAN: watch every disk smartd can find, with the default checks, every thirty minutes. The port’s configure line, from ports/core/disk/smartmontools/build.sh:
./configure \
--prefix=/usr \
--sysconfdir=/etc \
--without-update-smart-drivedb \
--without-systemdsystemunitdir \
--with-systemdenvfile=no \
--with-libsystemd=no \
--with-libcap-ng=yes \
--with-initscriptdir=no \
--with-nvme-devicescan
make
make DESTDIR=$PKG install
ports/core/disk/smartmontools/build.sh, lines 12–23
What the flags mean for you:
--with-nvme-devicescanmakesDEVICESCANinclude NVMe drives.--without-update-smart-drivedbinstalls noupdate-smart-drivedb, so the drive database is the one built into this release of smartmontools.--with-initscriptdir=noand thesystemdswitches install none of upstream’s service files; KDOS’s own boot script starts the daemon.
/etc/init.d/52_smartd.sh, from fs/etc/init.d/52_smartd.sh:
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
echo "[KDOS] Starting $NAME..."
supervise --final-exit 2 --final-exit 6 --final-exit 17 "$NAME" \
"$DAEMON" -n
;;
fs/etc/init.d/52_smartd.sh, lines 21–30
-n keeps smartd in the foreground, where the supervisor needs it. The three --final-exit codes are the exits a restart cannot fix, so the supervisor does not retry them.
/etc/init.d/53_xfs_healer.sh, from fs/etc/init.d/53_xfs_healer.sh, walks the mount table once at boot:
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
_n=0
_started=0
# /proc/self/mounts escapes a space, tab, newline or backslash in a
# path as octal; printf %b turns it back into the path.
while read -r _dev _mnt _type _rest; do
_n=$((_n + 1))
[ "$_type" = xfs ] || continue
_mnt=$(printf '%b' "$_mnt")
if ! "$DAEMON" --supported "$_mnt" >/dev/null 2>&1; then
echo "[SKIP] $NAME: $_mnt: no health monitor, or autofsck=none"
continue
fi
echo "[KDOS] Starting $NAME for $_mnt..."
supervise --final-exit 0 --final-exit 1 "$NAME-$_n" "$DAEMON" "$_mnt"
_started=$((_started + 1))
done < /proc/self/mounts
[ "$_started" -gt 0 ] || echo "[SKIP] $NAME: no XFS filesystem to watch"
;;
fs/etc/init.d/53_xfs_healer.sh, lines 34–57
The autofsck property of each XFS filesystem decides what its healer does, and is read and set with xfs_property (xfs_healer(8)):
autofsck |
xfs_healer |
|---|---|
repair |
Has the kernel repair damaged metadata online |
check, optimize |
Logs the damage |
none |
Exits; the boot script skips that filesystem |
| not set | Logs the damage when the filesystem has back-reference metadata, and exits when it has none |
Usage
Every command that reads a disk needs root, because SMART is read from the raw block device. The desktop’s tools are the exception: udisks2 reads the disk on their behalf.
In a virtual machine the disks answer no SMART, and every command below reports that it cannot: smartctl --scan lists nothing, a virtio disk answers Unable to detect device type, and an emulated USB stick answers Unknown USB bridge.
Reading a disk’s health
List the disks smartctl can reach:
sudo smartctl --scanAsk one for its verdict, and for everything it reports:
sudo smartctl -H /dev/sda sudo smartctl -x /dev/sda-HprintsPASSEDorFAILED!for an ATA or NVMe drive, andSMART Health Status: OKfor a SCSI or SAS one. In the-xreport, the reallocated, pending and uncorrectable sector counts, and the error log, show a drive in decline before the verdict changes.
From the desktop, the disk-health applet in the tray, from plasma-disks, warns when a drive’s SMART reports a failure, and System Settings → Storage Device Health lists every drive with its verdict. KDE Partition Manager’s properties and GNOME Disks show the whole report.
Running a self-test
Start a short test; the drive runs it in the background:
sudo smartctl -t short /dev/sdasmartctlprints how long it will take.-t longreads the whole surface and takes hours.When it is done, read the self-test log:
sudo smartctl -l selftest /dev/sda
Checking an NVMe drive
sudo nvme list
sudo nvme smart-log /dev/nvme0
sudo nvme error-log /dev/nvme0
smart-log reports the drive’s spare capacity, the percentage of its rated endurance used, its temperature and its media errors. sudo smartctl -x /dev/nvme0 reports the same through smartmontools.
Reading what smartd found
smartd logs through syslog under its own name, and its supervisor keeps its output in /run:
sudo grep smartd /var/log/messages
sudo cat /run/kdos-svc.smartd.log
A failing health check, a growing count of pending or uncorrectable sectors, a new entry in the drive’s error log and a failed self-test are each logged. The supervisor’s own lines go to the system log as well, tagged smartd:, so the first grep also shows when the daemon started and how it exited.
Sending smartd’s warnings somewhere else
The system log is the only route on the image. To add one, give the DEVICESCAN line in /etc/smartd.conf a -m address and an -M exec program, which smartd runs with the warning in its environment (smartd.conf(5)):
Edit the line with
sudoedit /etc/smartd.conf, for example toDEVICESCAN -m root -M exec /usr/local/sbin/disk-warning, and write that program.Check the file and the disks once, without leaving a daemon running:
sudo smartd -q onecheckIt prints how it read each line (
Drive: DEVICESCAN, implied '-a' Directive on line 18 of file /etc/smartd.conffor the shipped one) and what it found, and exits with the code the daemon would: 17 when no disk answers SMART.Restart the daemon:
sudo service restart smartd
smartd’s own warning script, /etc/smartd_warning.sh, sends -m mail with the mail command, which the image does not carry, so -m alone delivers nothing. -M exec with a program of your own is the route that works.
Repairing XFS online
A filesystem made by this mkfs.xfs with its default reverse-mapping and parent-pointer metadata already carries autofsck=repair; sudo xfs_property /srv/data get autofsck says so. For one made elsewhere, or set otherwise:
Set the property on the mounted filesystem:
sudo xfs_property /srv/data set autofsck=repairRestart the healers so the boot script reads it again:
sudo service restart xfs_healer service status xfs_healerThe restart prints
[KDOS] Starting xfs_healer for /srv/data..., and the status lists one healer per filesystem,[ OK ] xfs_healer-17 (pid …); the number is the filesystem’s line in/proc/self/mounts. A restart prints its output to the terminal and leaves/run/kdos-init.xfs_healer.logholding what the boot printed. Each healer’s own output is in/run/kdos-svc.xfs_healer-N.log, root only, and in the system log taggedxfs_healer-N.
sudo xfs_scrub /srv/data checks the same filesystem on demand, and ends with what it found and what it changed (/srv/data: optimizations made: 3.). An XFS filesystem mounted after boot, by hand or by udisks2, has no healer until the service is restarted.
Setting a drive’s power behaviour
For an ATA drive, as root:
sudo hdparm -I /dev/sda # what the drive supports
sudo hdparm -B 254 /dev/sda # advanced power management: 128 to 254 never spin down, 1 to 127 may
sudo hdparm -S 120 /dev/sda # spin down after 120 × 5 seconds idle
No boot script applies either setting; hdparm(8) describes each value.
Copying data off a failing disk
Every read can make a failing disk worse, so read it as little as possible, and only once. Copy it whole to an image on another disk with ddrescue, keeping the map file, which lets the copy stop and resume (ddrescue’s manual is info ddrescue):
Copy everything that reads easily, skipping the hard areas:
sudo ddrescue -n /dev/sdb /srv/rescue/disk.img /srv/rescue/disk.mapGo back over the bad areas, with direct access and three retries:
sudo ddrescue -d -r3 /dev/sdb /srv/rescue/disk.img /srv/rescue/disk.mapEach run ends
Finished; the second reads the map first (Initial status (read from mapfile)) and goes over only what the first could not read.Work on the image, never the disk: check and mount the image file as a loop device, for example
sudo mount -o loop,ro /srv/rescue/disk.img /mntfor a filesystem without a partition table.
For a healthy disk, partclone images an unmounted filesystem by its used blocks only; see Imaging a whole filesystem.
Tips
- A
PASSEDverdict says only that no attribute has crossed the maker’s own threshold. A non-zero count of pending or reallocated sectors insmartctl -xis the earlier sign, and the one smartd logs. - A disk behind a USB bridge may answer SMART only when told the bridge’s type, for example
sudo smartctl -d sat -H /dev/sdb. A bridge that passes no ATA commands answers even that withRead Device Identity failed: scsi error unsupported scsi opcode, and has no SMART to read. smartdin a virtual machine logsIn the system's table of devices NO devices found to scan, thenUnable to monitor any SMART enabled devices. Exiting., and stops; that is expected, andservice status smartdreports it as[DOWN] smartd.- Test a new stick or card with
f3before trusting data to it; see Storage and removable media.
Troubleshooting
service status smartd says it is not running. Read the boot’s output and the supervisor’s log:
cat /run/kdos-init.smartd.log
sudo cat /run/kdos-svc.smartd.log
[KDOS] (smartd) Exited with code 17, a final status: not restarting means no disk answered SMART, which is the case in a virtual machine and for some USB bridges. Code 2 is a line in /etc/smartd.conf it cannot parse; sudo smartd -q onecheck names it. Code 6 is a file it cannot read.
smartctl reports Unable to detect device type or Unknown USB bridge. Give the type with -d, as in Tips; smartctl --scan lists what it detected.
service status xfs_healer says not watching any filesystem. No XFS filesystem was mounted at boot, or each was skipped. /run/kdos-init.xfs_healer.log, and the output of a sudo service restart xfs_healer, give the reason per mount point: [SKIP] xfs_healer: /srv/data: no health monitor, or autofsck=none is a kernel without the XFS health monitor or a property set to none, and [SKIP] xfs_healer: no XFS filesystem to watch follows when nothing was left.
smartd logs errors on a disk that seems fine. Run a long self-test as in Running a self-test, and back up the disk first; see Backups.
Known gaps
kdos doctorcannot check hardware a machine does not have: neither can smartd, which in a virtual machine stops at boot- The native applications have been opened, not used, among them GNOME Disks, the one that draws udisks2’s SMART data
See also
- Services that skip themselves: final exits and the supervisor
- Services:
52_smartdand53_xfs_healeramong the boot scripts - Supervision and logging: the
/run/kdos-svc.*.logfiles - Logs: where the system log is kept
- Filesystems and fstab, LVM and RAID, Storage and removable media, Backups, Services