0

On Ubuntu 18.04, I would like to run a shell-script before systemd-udevd starts.

The reason for that is this bug: Ubuntu 18.04 systemd-udevd uses high CPU, conflict with nvidia graphics

The solution, just moving rule 71-nvidia.rules so it will not be found, will not work for me: I have a Razer Blade Stealth, which I operate with and without a Razer Core (with Nvidia card). When the laptop is connected to the Razer Core (via Thunderbolt-3), everything is fine. But when the laptop is not connected, I have the same problem as described in the other question.

As a workaround, I wanted to move /lib/udev/rules.d/71-nvidia.rules to /lib/udev/rules.available/71-nvidia.rules. The shell script should then use lspci to determine whether an nvidia card is found. If an Nvidia card is found, it would create a symbolic link to /lib/udev/rules.available/71-nvidia.rules at /lib/udev/rules.d/71-nvidia.rules. If no Nvidia card is found, it would remove that symbolic link.

#!/bin/bash
set -e ; set -o pipefail

mkdir -p /lib/udev/rules.available/
if [ ! -e /lib/udev/rules.available/71-nvidia.rules ] ; then
    mv /lib/udev/rules.{d,available}/71-nvidia.rules
fi

rm -f /lib/udev/rules.d/71-nvidia.rules
if lspci | grep nvidia ; then
    ln -s /lib/udev/rules.{available,d}/71-nvidia.rules
fi

So, how can I get a shell script like the one above to run before systemd-udevd is started?

P.S.: If anyone knows a better, smarter workaround, maybe with the built-in capabilities of systemd-udevd, I'd be curious to know as well.

1 Answers1

0

To answer the immediate question, you may simply write a .service unit that has a Before= ordering dependency on systemd-udevd.service:

[Unit]
Before=systemd-udevd.service

Be aware that systemd-udevd.service starts during early boot, so you will need to use DefaultDependencies=no as well and list any actually required dependencies by hand. Otherwise, you will get an ordering loop.

Throwing it all together, the unit can look something like this:

# /etc/systemd/system/udev-nvidia-hack.service
[Unit]
DefaultDependencies=no
Before=systemd-udevd.service
RequiresMountsFor=/lib/udev /etc/udev /path/to/script

[Service]
Type=oneshot
ExecStart=/path/to/script.sh

[Install]
WantedBy=sysinit.target

However, you will have a much better solution in examining the 71-nvidia.rules file as to why it ever affects the system when there are no matching devices to be found.

intelfx
  • 1,178