Systemd device units
These are the notes of a training course on systemd I gave as part of my work with Truelite.
Several devices are automatically represented inside systemd by
units, which can be used to activate services when a given device exists in the
systemctl --all --full -t device to see a list of all decives for which
systemd has a unit in your system.
For example, this
.service unit plays a sound as long as a specific USB key
is plugged in my system:
[Unit] Description=Beeps while a USB key is plugged DefaultDependencies=false StopWhenUnneeded=true [Install] WantedBy=dev-disk-by\x2dlabel-ERLUG.device [Service] Type=simple ExecStart=/bin/sh -ec 'while true; do /usr/bin/aplay -q /tmp/beep.wav; sleep 2; done'
If you need to work with a device not seen by default by systemd, you can add a
udev rule that makes it available,
by adding the
systemd tag to the device with
It is also possible to give the device an extra alias using
To figure out all you can use for matching a device:
udevadm monitor --environmentand plug the device
- Look at the
DEVNAME=values and pick one that addresses your device the way you prefer
udevadm info --attribute-walk --name=*the value of devname*will give you all you can use for matching in the udev rule.