SYNOPSIS
I use a Raspberry Pi 5 as a media player.
My music and movies are stored on an external USB drive, which I sync via Unison.
I want MPD user service to automatically start when this USB drive is plugged in, and gracefully stop when it is unplugged.
On Raspberry Pi OS (Bookworm), udisks2 manages dynamic storage. It watches udev events and exposes disk state changes via D-Bus.
gvfs-udisks2-volume-monitor listens to these D-Bus signals and requests udisks2 to mount the drive under /media/<USER>/<UUID>.
DOCUMENTATION
PROCEDURE
1. Enable MPD Boot Check
To allow MPD to start on boot only if the USB drive is already attached, create a systemd user override.
This prevents MPD from failing or throwing missing directory errors when booting without the drive.
|
|
2. Configure Hotplug Watcher (Systemd Path Unit)
When a watched directory is removed, the kernel destroys the inotify watch handle associated with it and sends an IN_IGNORED event to systemd.
So to support hotplugging, you must monitor the parent directory /media/jeyzu/ instead of the drive subdirectory.
|
|
3. Create the Service Toggle
Create a helper user service that checks for the existence of the music folder whenever the parent mount path changes, starting or stopping mpd.service accordingly.
|
|
4. Create the associated script
Notice that -M jeyzu@ is not needed because the script executes as USER and inherits DBUS_SESSION_BUS_ADDRESS & XDG_RUNTIME_DIR.
|
|
5. Enable Linger and Activate
Ensure user systemd services continue to run in the background even when no active interactive login session is open.
|
|