udev is a background service that takes care of hardware changes. It takes care of all entries below /dev/, i.e. hard disks, external media, USB devices and also graphics cards. When a cable is plugged in or unplugged, udev triggers an event to which you can respond with the help of an udev rule. But before we create such a rule, let’s take a look at what happens when a cable is plugged in or unplugged. We can use the udev management tool (udevadm) to do this.
udevadm monitor --environment --udev
If we now plug in or unplug the HDMI cable, we get the following output:
UDEV [2247.166677] change /devices/pci0000:00/0000:00:02.0/drm/card0 (drm) ACTION=change DEVNAME=/dev/dri/card0 DEVPATH=/devices/pci0000:00/0000:00:02.0/drm/card0 DEVTYPE=drm_minor HOTPLUG=1 ID_FOR_SEAT=drm-pci-0000_00_02_0 ID_PATH=pci-0000:00:02.0 ID_PATH_TAG=pci-0000_00_02_0 MAJOR=226 MINOR=0 SEQNUM=2120 SUBSYSTEM=drm TAGS=:seat:master-of-seat:uaccess: USEC_INITIALIZED=15405142
udev tells us here which device it is (“card0”), which action was triggered (“CHANGE”) and which subsystem is affected (“DRM”). We need this information immediately for our udev rule. We create these under /etc/udev/rules.d/95-hotplug-hdmi.rules:
There we insert the following line:
ACTION=="change", SUBSYSTEM=="drm", RUN+="/bin/bash /usr/sbin/setHDMIStatus".
So we react to a “change” event in the subsystem “drm” and want to execute the script “/usr/sbin/setHDMIStatus” if this event occurs. The script writes the current state to a file. Actually you can write anything to the file, as long as the file changes when the status changes. Why? We will see in a moment. The script setHDMIStatus has the following content (change username, in case of linuxmuster.net you can use the account of the template user or local admin):
#!/bin/bash STATUS="$(/bin/cat /sys/class/drm/card0-DVI-D-1/status)" /bin/echo $STATUS > /home/user/.hdmi_status