The volume that comes and goes
An always-on local device at my house keeps its application data on an external solid-state volume. The data moved there at the start of August, when the boot disk filled up. A week later the volume unmounted itself. No reboot, no cable touched. It was gone at 12:37 and back by 14:00. It has done it since. I have not found the cause and I expect it to keep happening, so the scripts have to survive it.
Why an unmounted volume is dangerous, not just unavailable
A mount point is an ordinary directory when nothing is mounted on
it. /Volumes/data is a directory whether the volume is
there or not. A script that runs mkdir -p
/Volumes/data/app/incoming and writes files, as root,
succeeds either way. While the volume is away, the files land on the
boot disk under a path that looks exactly right. No error. When the
volume returns, the mount sits on top of that directory and hides
it. The data is invisible, the boot disk is a little fuller, and the
next run cannot see what the last one wrote. That is the same boot
disk whose filling forced the data onto the external volume in the
first place.
That is what happened. A sync job wrote into the bare mount point
while the volume was away. Then came the second-order effect, which
was worse. The boot-time job that mounts the volume found a
directory already on the mount point and not empty. macOS will not
mount over an occupied path; it appends a suffix and mounts at
/Volumes/data 1. The scheduler retried 538 times. Every
retry ran the mount command and every mount succeeded, at the wrong
path. Every configured path pointed at the decoy directory, the
service's own readiness guard failed forever, and the application
was down for ten hours after a reboot with its volume mounted next
door.
The guard: compare device numbers, not existence
[ -d /Volumes/data ] cannot tell the two states apart.
What can is the device number. Every file lives on exactly one
device, and stat will tell you which. If the mount
point reports the same device as /, nothing is mounted
there and every write is going to the boot disk. The check I added
to the sync job:
check_data_volume() {
# A mount point is an ordinary directory when nothing is mounted on
# it, so [ -d ] cannot detect a dropped volume. Compare device numbers.
local dev_root dev_data
dev_root="$(stat -f '%d' / 2>/dev/null)"
dev_data="$(stat -f '%d' "${DATA_MOUNT}" 2>/dev/null)"
if [[ -n "${dev_data}" && "${dev_data}" != "${dev_root}" ]]; then
return 0
fi
# One attempt to bring it back, then refuse.
diskutil mount "${VOLUME_UUID}" >/dev/null 2>&1
dev_data="$(stat -f '%d' "${DATA_MOUNT}" 2>/dev/null)"
if [[ -n "${dev_data}" && "${dev_data}" != "${dev_root}" ]]; then
return 0
fi
log_error "${DATA_MOUNT} is a bare directory on the boot disk; refusing to run"
return 1
}
stat -f '%d' is the BSD form, which is what macOS
ships. On Linux the GNU form is stat -c '%d', and
util-linux also gives you mountpoint -q /path, which
does the same comparison for you. The guard gates every path through
the sync job, including the import-only mode, because importing from
a phantom empty directory is worse than not running at all.
Finding two: the disk identifier is not an address
Two weeks later the volume dropped out again and came back as
disk5. It had been disk9. The mount script
was pinned to disk9s1, with [[ -e /dev/disk9s1
]] as its attached check. That check was now false with the
volume attached and healthy, and the retry loop around it could
never succeed. At the next reboot it would have refused to start both
services forever. The tell, when I went looking:
diskutil info disk9s1 returned empty output. Not an
error. Nothing.
macOS assigns diskN in enumeration order and may
reassign it at any detach. The volume's UUID does not change across
re-enumeration or a rename, so that is the address now. A UUID has
no /dev node, which means the attached check cannot be
a file test. It is a diskutil query:
# True when the volume is attached, mounted or not.
volume_present() {
diskutil info "${VOLUME_UUID}" >/dev/null 2>&1
}
# Current identifier, for log lines only. Never used to address the volume.
current_device() {
diskutil info "${VOLUME_UUID}" 2>/dev/null \
| awk -F': *' '/Device Identifier/ {print $2}'
}
diskutil mount "${VOLUME_UUID}"
One script now owns mounting. The boot-time job calls it, and so do
both service wrappers, so a reboot needs no hands and there is one
place to get this right. A second wrapper used to carry its own copy
of the retry loop; I deleted it. The stale-directory case is handled
with rmdir, never rm -rf: it removes an
empty decoy and refuses a non-empty one, and when it refuses it says
so and tells me to move the directory aside by hand. If something
wrote real data to the boot disk, I want to look at it, not destroy
it.
Proof it was broken and is now fixed: the boot-time mount job's last exit status was 1 before the change and 0 after. I tested the two failure branches against a sandbox mount point, attached but unmounted and volume absent, and both report what they should.
A corollary about counting
Never compare a file count or a du total taken while
the volume was unmounted against one taken while it was mounted. The
first counts the decoy, the second counts the data, and the
difference looks like a loss or a gain that never happened. Confirm
the mount state in the same command that counts, or the number means
nothing.
What to do on your own device
- Guard every write to a removable path with a device-number comparison against
/.[ -d ]is not a guard. - Try one mount, then refuse. Do not run with the data directory missing.
- Address removable volumes by UUID. Test presence with
diskutil info <uuid>, mount by UUID, and printdiskNonly in log lines. - One script owns mounting. Everything else calls it.
- Clear a stale mount point with
rmdir. If that fails, stop and look. - Count and check the mount state in one command.