Jonathan Earp Projects / 04
● Project 04 / 2026 Swift daemon, Python CLI Nothing installed on the NAS

YYank-safe AAuto-unmount, NNAS KKept local

My dock has an SSD built in, and my video lives on a NAS. This unmounts the SSD the moment the lid closes, so I can unplug and leave, and copies NAS folders onto it so editing runs at local speed.

0.2 s
Lid close to
phone alert
12 s
What QuickTime cost
the old version
8.4 MB/s
Best NAS pull
over Tailscale
38 MB/s
Dock SSD,
stuck at USB 2.0
01

What it does

Two tools for one dock. A daemon that gets the SSD unmounted before the Mac sleeps, and a cache that copies folders from the NAS onto that SSD and back.

The dock is a UGreen one with an NVMe SSD inside. Unplug it with the drive still mounted and you get macOS's "Disk Not Ejected Properly" warning, and anything that was mid-write can come out corrupted. The NAS is a Synology DS220j that I reach over Tailscale, which is fine for browsing and slow for scrubbing through footage.

shuttle-ejectd is about 800 lines of Swift that talks to IOKit directly and runs as a LaunchAgent. shuttle is about 1,000 lines of Python wrapped around rsync over SSH. The NAS side needs nothing installed, just SSH and DSM's rsync service. On the command line it's still called shuttle, which is what it was called before it got a worse name.

01
Lid closes

Clamshell state flips, sleep is requested.

02
Hold sleep

The daemon delays the power change.

03
Check

Ask lsof whether anything has a file open.

04
Warn

If so, name the process on my phone.

05
Unmount

diskutil flushes and unmounts, never forced.

06
Release

Let the Mac sleep. Remount when the lid opens.

02

One lid, two signals

Closing the lid doesn't always mean the Mac is about to sleep, and neither IOKit signal covers both cases on its own.

AppleClamshellState on IOPMrootDomain changes every time the lid closes, including in clamshell mode, where an external display and AC power keep the Mac awake and no sleep notification ever arrives. What it can't do is hold off sleep.

The sleep notifications can. Between receiving kIOMessageCanSystemSleep or kIOMessageSystemWillSleep and calling IOAllowPowerChange, the system waits for you. That window is the only time I get to finish the unmount and send a phone notification before Wi-Fi goes away. So the daemon registers for both.

It runs diskutil unmountDisk instead of eject. Both flush pending writes and both make the drive safe to pull. Unmount leaves the device node attached, so the volume can come back in software when the lid opens again. An eject would need a physical replug.

The alert that came twice

A single lid close fires both signals. The first version de-duplicated them with a time window, and an unmount can take longer than any window that makes sense, so every alert went out twice. The guard is an in-progress flag now, plus a short debounce that starts when the unmount finishes instead of when it starts.

03

When a file is open

If something on the SSD is open, the unmount fails. The daemon never forces it, since forcing is the data loss this whole thing is meant to prevent.

It leaves the volume mounted and says what's holding it, by name: Shuttle in use. Python has files open. Don't unplug. That goes to my phone as well as the Mac, because a notification behind a closed lid isn't much use. It's sent from inside the sleep-delay window, before the radio goes. Phone alerts go through ntfy by default (free, no account), or Pushover, or any webhook.

The first version waited for diskutil to say what was blocking it. diskutil is the authority on that, but it waits out each process that refuses. A well-behaved one refuses in about 2 seconds. QuickTime Player ignores the request and took 12. All told, the alert landed 15 seconds after the lid closed.

Now it asks lsof first. lsof answers in about 0.2 s, so the warning goes out straight away and diskutil confirms afterwards. The alert takes about another second to leave the machine.

lsof's answer is fast but it isn't final. If the volume ends up releasing cleanly after all, a short "safe to unplug" follow-up corrects the warning.

04

Clamshell mode

With an external display plugged in, closing the lid is when I start working. Unmounting then would pull the drive out from under me.

The daemon tells the two apart with AppleClamshellCausesSleep, also on IOPMrootDomain. macOS sets it to No when an external display is attached on AC power, because the Mac will stay awake with the lid shut. It comes from the display and power state, so it's already right at the instant the lid closes.

What the daemon reads, and what it does
AppleClamshellCausesSleepMeaningAction
YesLid close will sleep the Mac, so I'm leavingUnmount
NoClamshell mode, the Mac stays awakeStay mounted, run sync()
Try it Close the lid Set the scene, then read the log
Power
Display
On the SSD
Causes sleep Yes Volume Unmounted Phone Nothing sent

The timings in there are the measured ones. The one I can't put a number on is how long a clean unmount takes, since that depends on how much is waiting to be written.

Leaving from clamshell mode

Staying mounted leaves one gap: when I'm done, I can unplug the dock without the Mac ever going to sleep. Sleeping it first from the Apple menu covers that, and so does running shuttle eject. There's also an idle watchdog that unmounts after a set number of minutes with no keyboard or mouse input and remounts when input comes back. It's off by default, because watching a long video counts as idle too.

05

Pinning NAS folders

The cache is manual. A folder only gets copied onto the SSD when I pin it.

A transparent, on-demand cache on macOS needs either macFUSE, a kernel extension that means lowering the Mac's security settings, or a File Provider extension, which is a whole app with special entitlements. For video work, pinning folders by hand is enough.

shuttle pinCopy a NAS folder onto the SSD. It shows up at ~/Shuttle/<name>, a symlink, so editing projects keep resolving.
shuttle pushSend changes back to the NAS and keep the local copy.
shuttle pullRefresh the local copy from the NAS.
shuttle unpinPush, verify, then free the space. Section 07 is about the verify.
shuttle doctorChecks the drive, the USB link speed, and whether the NAS and its rsync service answer.

It's rsync over SSH, because SMB is chatty and the NAS is usually on the far end of Tailscale. rsync resumes interrupted transfers with --partial and only sends what changed. The manifest of what's pinned lives on the drive itself and is mirrored to the Mac, so shuttle status still works with the dock unplugged.

The error that looked like a password problem

Every transfer failed at first with a bare Permission denied, please try again. from the NAS, even though SSH had already logged in.

Synology ships its own patched, setuid-root /usr/bin/rsync, and it refuses server mode unless the rsync service is switched on in Control Panel. The real error underneath is rsync service is no running (code 43), typo included. Once it's on, paths have to start at a shared folder, and the bare /volume1/ answers module is write only, which is also normal. shuttle doctor checks for the service specifically now, and every transfer command fails fast with the fix.

06

Picking a cipher

I expected AES to win, since the DS220j's ARM cores have crypto extensions. The AES-GCM modes came last.

Every byte of a pull goes through SSH encryption on a small ARM box, so I timed a 150 MB pull from the NAS with each cipher.

chacha20-poly1305 is the default. It ties aes128-ctr and beats whatever SSH picks on its own by about 15%.

The two GCM modes are 18% slower than chacha20, which is the opposite of what the hardware suggests. There's a note in the README telling whoever touches it next (me) not to switch it back to AES without measuring again.

07

The one-second gap

unpin deletes the local copy, so it has to be sure the NAS has everything first.

The check is a dry-run rsync from the SSD to the NAS. Any file rsync still wants to send isn't safely on the NAS yet, and if there's even one, unpin refuses and leaves everything where it is.

The catch is rsync's quick check. It compares size and modification time, and its protocol carries mtime in whole seconds. A file rewritten to the same size within the same second looks unchanged.

Checksumming everything closes that, but it reads every byte on both ends, which at NAS speeds is hours for a big pin. So there's a second stage: a checksum pass over only the files modified since the push started. That's exactly the window where the quick check can be fooled, and it's usually a handful of files. unpin --checksum still forces the full pass.

The safety-gate tests, run against real rsync on real files
CaseWhat has to happenResult
Nothing copied yetEverything reported as pendingPass
After a real copyVerifies clean, no false alarmsPass
File changed, different sizeQuick check catches itPass
New local fileCaughtPass
Same size, same secondQuick check misses it, stage two catches itPass
Synology clutter (@eaDir, .DS_Store)Ignored, doesn't block the verifyPass

They run real rsync instead of fixtures, because the whole risk is whether my reading of --itemize-changes matches what rsync actually prints. The same-second test first checks that the quick check really does miss the change, then checks that stage two catches it.

08

Stuck at USB 2.0

The SSD writes at 38 MB/s. That's about where USB 2.0 tops out, on a Thunderbolt 5 laptop.

Measured on this machine
PathSpeed
Dock SSD, sequential write38 MB/s
USB 2.0, theoretical ceiling~40 MB/s
NAS to Mac over Tailscale (SFTP, same LAN)7.6 MB/s

Every USB device in the dock, both internal hubs, the Realtek network chip and the storage bridge, reports Device Speed = 2, which is USB 2.0 High Speed. There were zero SuperSpeed devices on the machine, and nothing connected on any of the three Thunderbolt buses.

A USB 3 hub always shows up as two device trees, a USB 2.0 one and a separate SuperSpeed one. Only the 2.0 tree exists here, so the fast link isn't coming up at all. Reformatting the SSD to APFS changed nothing (38.1 to 38.2 MB/s), which rules out the filesystem and the driver.

The likeliest cause is the cable. USB-C charging cables only carry the USB 2.0 pairs and look identical to full ones. A cable rated for 10 Gb/s should take the SSD to somewhere around 800 to 1,000 MB/s. I got that number from the spec and haven't measured it. shuttle doctor checks the link speed every time it runs.

09

Still broken

  • The dock is still on USB 2.0. Until the cable is swapped, the SSD is only about 5× faster than pulling from the NAS, and the cache is a nice-to-have.
  • Unplugging from clamshell mode isn't covered by default. If I never sleep the Mac and never run shuttle eject, the drive is still mounted when I pull it. The idle watchdog fixes that, and it's off.
  • The cache is manual. There's no fetching on demand. If a project references a file I didn't pin, it reads from the NAS at NAS speed.
  • The strongest check is slow. unpin --checksum reads every byte on both ends, so on a large pin it takes hours.
  • It's only been run on my setup. I haven't tried it with any other dock, NAS or Mac.