DulyNotes

How to keep a missing mergerfs drive from filling your Linux root filesystem

When a USB drive goes missing, its mount directory can still accept files. A transfer might look successful while it fills the Linux system disk. This guide shows how to block those writes and use systemd to stop apps that depend on the mergerfs pool when a required disk fails a check.

Why we kept mergerfs and our old USB drives

We put a similar setup in place on Ubuntu. Several systems use our mergerfs pool as temporary working space, and our external USB bays hold older drives of different sizes. We can live with losing temporary files on a failed drive. A transfer that silently fills the disk Ubuntu runs on can take the whole system down.

mergerfs uses FUSE (Filesystem in Userspace) to present several directories, called branches, through one shared path. Each file lives on an individual disk. That lets us use the capacity we already own and keep the path our other systems expect. If you need every file to survive a disk failure, redundancy and backups belong in your design. For this pool, we want to know which bay failed and have new writes stop.

What a directory can hide

Linux mounts a filesystem over a directory. When the mount disappears, the same name exposes whatever lies underneath. This can happen at a USB branch or at the public pool after FUSE goes away. Both /media and /mnt can contain ordinary directories backed by the root filesystem. Remounting the disk hides any files written there in the meantime, while those files keep using space on the OS disk.

/mnt/archive
  Active: mergerfs pool
  Underneath: read-only tmpfs

/mnt/archive-branches/disk1 and disk2
  Active: one ext4 USB disk at each path
  Underneath: directories on a second read-only tmpfs

The example uses two small filesystems in memory (tmpfs). One sits beneath /mnt/archive. The other holds the directories where the individual disks mount. Both are read-only once setup finishes. If a mount disappears, an app that tries to save a file there gets a “Read-only file system” error. Run apps without permission to change mounts, and leave the read-only filesystems mounted until shutdown.

mergerfs documentation on missing branch mounts

1. Map the bays in a disposable Linux VM

Start with two disposable, already formatted ext4 data disks separate from the OS disk. Install mergerfs, systemd, Python 3, Bash, util-linux, GNU coreutils and unzip using your distribution’s packages. Record your kernel and mergerfs versions. Match each bay to its filesystem UUID.

uname -r
mergerfs --version
lsblk -o NAME,SIZE,MODEL,SERIAL,FSTYPE,UUID,MOUNTPOINTS
findmnt -T / -o TARGET,SOURCE,FSTYPE

Point apps that read or save files in the pool at /mnt/archive. Reserve /mnt/archive-branches for the individual disks. Use fresh paths. If either already exists, check for active mounts and hidden files before changing permissions. Preserve anything you find. Make sure no fstab entry or automounter manages these devices or paths. Each data disk needs its own UUID.

The download contains the Python guard, three shell scripts, the disk list, four systemd unit files and a sample service settings file. The sample is named archive-writer.service.d/storage.conf. Extract the ZIP as a regular user and review the files before installing them.

Download the complete storage guard examples (.zip)

unzip storage-guard.zip -d storage-guard-example
cd storage-guard-example

Edit etc/storage-guard/disks.conf in that directory. Each row names a required member, its filesystem UUID and its type. Replace both placeholders with the actual ext4 UUIDs from lsblk.

disk1 11111111-1111-4111-8111-111111111111 ext4
disk2 22222222-2222-4222-8222-222222222222 ext4

2. Install the guard before starting apps that use the pool

Stop the apps and background jobs that read or write files in the mergerfs pool. Confirm the example directories are empty and have nothing mounted on them, then install the files from the extracted directory. The mode-000 permissions keep regular apps out of the bare directories if a disk is missing at boot and the guard cannot start.

sudo install -d -o root -g root -m 000 /mnt/archive /mnt/archive-branches
sudo install -d -o root -g root -m 0755 /etc/storage-guard /usr/local/libexec/storage-guard
sudo install -o root -g root -m 0644 etc/storage-guard/disks.conf /etc/storage-guard/
sudo install -o root -g root -m 0755 usr/local/libexec/storage-guard/* /usr/local/libexec/storage-guard/
sudo install -o root -g root -m 0644 etc/systemd/system/storage-guard* /etc/systemd/system/
sudo systemd-analyze verify /etc/systemd/system/storage-guard*.service /etc/systemd/system/storage-guard-probe.timer

If systemd starts your app, add the sample storage.conf to the settings directory for that app’s service. The command below uses archive-writer.service as the example name. If your service has a different name, change the destination directory to match it. These settings make the app wait for the disk checks before it starts and stop when the guard stops. Allow enough startup time to check every disk. Give the app’s regular account the access it needs on the data directories. Each disk needs enough free space to satisfy the mergerfs settings for creating files.

mergerfs uses the app’s account permissions when it accesses files on each disk. The parent directory uses mode 0711 so it can reach a known path. The sample service settings use InaccessiblePaths to keep the app from opening /mnt/archive-branches directly. Point network shares at /mnt/archive, and use equivalent restrictions for containers or other local accounts that should access only the pool.

# Example only if your unit is named archive-writer.service
sudo install -D -o root -g root -m 0644 etc/systemd/system/archive-writer.service.d/storage.conf /etc/systemd/system/archive-writer.service.d/storage.conf

If systemd does not manage an app or job that uses the pool, run the guard check before starting it. Add the commands to stop it to stop-dependents.sh. The sample script is ready for you to fill in. Include network shares, scheduled transfers and containers that use the pool.

sudo systemctl daemon-reload
sudo systemctl enable --now storage-guard.service
sudo systemctl start storage-guard-probe.service
findmnt -R /mnt/archive-branches
findmnt --mountpoint /mnt/archive -o TARGET,SOURCE,FSTYPE,OPTIONS
systemctl list-timers storage-guard-probe.timer

Look for the two distinct writable ext4 branches, a writable fuse.mergerfs mount named guarded-archive, and a scheduled probe. Through the same account and path your application uses, create a small, uniquely named file in its data directory. Locate that file on one of the actual disks.

systemd documentation for BindsTo and After

mergerfs documentation on caller permissions

What happens if a disk is mounted but stops answering?

A USB fault can leave a disk listed as mounted even when it no longer responds. The guard checks that the expected disk is mounted at the right path and accepts writes. It writes a few bytes to an unnamed temporary file on each disk and asks Linux to flush the write with fsync. It also lists the top of the mergerfs pool and checks whether any mount changed while it was testing.

If a check fails, the guard creates /var/lib/storage-guard/locked on the OS disk, then requests that the pool and timer stop. Apps configured with the sample service settings stop with the guard. When the pool unmounts, its path exposes the read-only filesystem beneath it. The lock file keeps the guard from starting again until you clear it during recovery.

Wire failed-unit or journal monitoring to a channel someone watches. The example logs the failure locally. Email or text delivery requires your own monitoring integration. Keep space on / for the incident flag and logs. If the flag cannot be written, the pool is still stopped, but a reboot may allow it to start again. Use SMART diagnostics where the USB bridge exposes them.

Adjust how often the guard checks the pool

By default, the next check starts about 30 seconds after the previous one finishes. The guard gives each disk or pool I/O test up to eight seconds to respond. You can adjust both timings.

In /etc/systemd/system/storage-guard-probe.timer, OnActiveSec sets how long to wait after the timer starts, and OnUnitInactiveSec sets how long to wait after a check finishes. After editing the timer file, run sudo systemctl daemon-reload and restart storage-guard-probe.timer while the pool is healthy.

In /usr/local/libexec/storage-guard/guard.py, change IO_TIMEOUT to give each disk or pool I/O test more or less time. The value is in seconds. A longer timeout gives slow disks more time to answer and can delay detection of a stalled disk.

systemd documentation for timer settings

3. Rehearse a missing pool, a missing disk and a reboot

Run these tests only in the disposable VM with idle disks and fresh test filenames. Begin each scenario with a known healthy baseline.

The public pool disappears

Stop the apps that use the pool, then stop the guard. Even with sudo, trying to create a file at the pool path should fail with “Read-only file system.” Unmount one idle data disk and try the same test there. If either command creates a file, keep the apps stopped and run findmnt --target on that path to see which filesystem received it.

sudo systemctl stop storage-guard.service
sudo touch /mnt/archive/public-fallback-test
sudo umount /mnt/archive-branches/disk2
sudo touch /mnt/archive-branches/disk2/branch-fallback-test

A required disk is absent at boot

Shut down the VM, detach one disposable data disk and boot. The guard should fail to start, the lock file should appear and any app that depends on the guard should stay stopped. Try to create a file at /mnt/archive using the app’s regular account. Before the read-only filesystem is mounted, the bare directory should return a permission error. Restore the disk and follow the recovery steps before the next test.

A member disappears while the pool is online

With the pool healthy again, unmount one idle test disk. Start the check or wait for the timer. Confirm the journal names the failure, the lock file exists and apps that depend on the guard have stopped. Check that /mnt/archive now points to the read-only filesystem. Reboot with the lock file in place and confirm those apps stay stopped.

sudo systemctl start storage-guard-probe.service
journalctl -b -u storage-guard-probe.service -u storage-guard-lockdown.service
sudo cat /var/lib/storage-guard/locked
findmnt --target /mnt/archive -o TARGET,SOURCE,FSTYPE,OPTIONS

4. Recover deliberately

Keep the apps and jobs that use the pool stopped. Read the journal to find the missing bay, then check its disk, cable and power supply. If you replace a disk, verify its contents, UUID and directory permissions. Update disks.conf as needed. Before mounting over an exposed directory, check it for files that may have landed there.

Reboot with the lock file still in place to clear old mounts. Once every required disk is accounted for, remove the lock file, start the guard and run a fresh check. Resume apps, jobs and network shares after you confirm a test file saved through the pool reaches one of the data disks.

lsblk -f
sudo rm -- /var/lib/storage-guard/locked
sudo systemctl start storage-guard.service
sudo systemctl start storage-guard-probe.service
systemctl status storage-guard.service storage-guard-probe.timer

What we verified in a disposable VM

We ran the downloadable example on Debian 12 with Linux 6.1.0-56-amd64, mergerfs 2.33.5 and two virtual ext4 disks. A test app running as a regular user saved a file through the pool. Its service settings blocked direct access to the individual disks. After clean unmounts, both the pool path and an exposed disk directory rejected file creation even as root.

Unmounting a required disk and running the check stopped the test app and pool. The lock file survived reboot and kept both the guard and timer inactive. After we restored the expected disks and cleared the lock, the full check passed and the app started again.

We also booted with a required UUID deliberately absent. Startup failed and the journal identified the missing UUID. The test app’s account could not create a file on the bare pool path. Restoring the correct UUID and clearing the lock let the check pass. A physical USB power fault or failing disk can behave differently from these virtual disks.

Will this work on your system?

It may or may not. This example expects ext4 disks with free space and support for unnamed temporary files. A small test write tells you the disk answered that request. A stalled operation can continue inside Linux after the check times out, and an app can still hold a file open after its mount is detached. Test with your own Linux and mergerfs versions, USB hardware and the apps that use the pool. Keep separate backups for files you need to recover.

For our workload, taking the whole pool offline over one missing disk is a reasonable cost. The bays and drives can stay, and several systems can keep using their familiar shared path. That is what getting an existing system dulyfixed looks like here: decide which loss you can live with, protect against the one you cannot, and rehearse it while the data is disposable.

More articles from DulyNotes