React on docking-undocking on Lenovo

We want to run a script whenever the laptop is docked or undocked. The idea is to add a udev role to run a given script. The main nuisance is that each dock reports a different USB ID, so we need to add one for each of the models. On gentoo, udev rules need to be added to /etc/udev/rules.d/

We create a file named /etc/udev/rules.d/81-thinkpad-dock.rules and permissions 644 with the following content:

# Save this file (after modifying ID_VENDOR and ID_MODEL if necessary) as /etc/udev/rules.d/81-thinkpad-dock.rules

# Ultra dock 1
SUBSYSTEM=="usb", ACTION=="add|remove", ENV{PRODUCT}=="17ef/1010/5041", RUN+="/sbin/thinkpad-dock.sh"
# Ultra dock 2
SUBSYSTEM=="usb", ACTION=="add|remove", ENV{PRODUCT}=="17ef/1010/5031", RUN+="/sbin/thinkpad-dock.sh"
# Basic dock
SUBSYSTEM=="usb", ACTION=="add|remove", ENV{PRODUCT}=="17ef/1013/5031", RUN+="/sbin/thinkpad-dock.sh"
# Pro dock
SUBSYSTEM=="usb", ACTION=="add|remove", ENV{PRODUCT}=="17ef/1012/5040", RUN+="/sbin/thinkpad-dock.sh"

The correct PRODUCT string can be found by running

udevadm monitor --subsystem-match=usb --property --udev

Once the file is in place, udev rules can be reloaded with:

udevadm control --reload-rules