PDA

View Full Version : Blacklist drivers using systemctl command , a systemd mechanism



Acropolis
26th February 2016, 06:06 PM
The problem is that the loaded drivers are controlled by conf files and are rewritten every time from kernel updates and in fedora we have a lot of updates :) . old fedora had the blacklist.conf file as most linux distros , the new fedora has the dist-blacklist.conf file or can blacklisted by grub or by sysconf file or manual with rmmod command but every time the system updated or upgraded these files are re-written by the system and you loose your preferences .



My solution in this problem , is to use the new systemd service system ( systemctl ) to create a simple service from a script so i can have full control what drivers i don't want loaded again during startup after updates or after every operating system startup .

How to create a service from a script ?

1 . create ( as root or with sudo ) your script ( sh extension ) in a a folder that is dedicated for users and not controlled by linux distro like /usr/local/bin or by user ( to not deleted by mistake )


touch /usr/local/bin/your_script_name.sh

this is my simple script to block fedora load my laptop camera driver ( uvcvideo ) and to test the intel driver by disabling the radeon driver ( i have a hybrid laptop hp tm 1010ea tm2 with both intel and amd gpus ) . i used an array to add any driver i want inside "" separated with spaces .

i changed the name of radeon driver ( you can find the driver names with lsmod command and the description of the driver with modinfo command , but this is an other guide :) ) to radeons to not used by mistake from new linux users . if you want remove radeon or nouveau or glx or whatever add it inside ""



#!/bin/bash

sleep 5;

modules=("uvcvideo" "radeons")

for i in "${modules[@]}"
do
if lsmod | grep "$i" &> /dev/null ; then
echo "$i driver is loaded and i will remove it !"
rmmod -v "$i"
if [ $? -ne 0 ]; then
echo -e "\e[1;31m WARNING \e[0m : Failed to remove $i driver !"
else
echo -e "\e[1;32m SUCCESS \e[0m : The $i driver succefully removed!"
fi
else
echo -e "\e[1;36m NICE \e[0m : The $i driver is not loaded!"

fi
done



and check the script if it is working .


sh /usr/local/bin/your_script_name.sh

if the results are ok , you can continue to create your service , but first you should give the executable rights to the systemctl with this command :


chmod +x /usr/local/bin/your_script_name.sh


otherwise the system will fail to run your script.


if some drivers are failing to removed maybe you just need to change the order . an example of what i mean is this:


i have an older netbook ( the first acer aspire one :p ) with broken screen and damaged ethernet card that i am using it as NAS with 2 external usb disks to have my own home lab with NAS support :). i bought an usb2ethernet adapter and i want to remove the ethernet card driver ( r819 ) and the wireless driver ( ath ) since it has not a mechanic button to do it .

if i add the modules to remove in this order ("r8169" "ath" "ath5k" "mac80211" "cfg80211") it fails to remove the atheros driver and i have this output from the script


[root@irida ~]# sh /usr/local/bin/disabled_drivers.sh
r8169 driver is loaded and i will remove it !
SUCCESS : The r8169 driver succefully removed!
ath driver is loaded and i will remove it !
rmmod: ERROR: Module ath is in use by: ath5k
WARNING : Failed to remove ath driver !
ath5k driver is loaded and i will remove it !
SUCCESS : The ath5k driver succefully removed!
mac80211 driver is loaded and i will remove it !
SUCCESS : The mac80211 driver succefully removed!
[root@irida ~]#



if i check the remained drivers i am seeing thet the ath driver remains


[root@irida ~]# lsmod | grep "ath"
ath 24576 0
cfg80211 475136 1 ath
[root@irida ~]#


if i add the modules to remove in this order ("r8169" "ath5k" "ath" "mac80211" "cfg80211") the result of the script is fine and i can continue to create the service to execute my script every time the OS is starting



[root@irida ~]# sh /usr/local/bin/disabled_drivers.sh
r8169 driver is loaded and i will remove it !
SUCCESS : The r8169 driver succefully removed!
ath5k driver is loaded and i will remove it !
SUCCESS : The ath5k driver succefully removed!
ath driver is loaded and i will remove it !
SUCCESS : The ath driver succefully removed!
mac80211 driver is loaded and i will remove it !
SUCCESS : The mac80211 driver succefully removed!






2. create ( as root or with sudo ) a service file for your service in the folder


touch /etc/systemd/system/your_service_name.service

use any name you want for your service but be careful to not conflict with any existent service you want install in the future .

and in this /etc/systemd/system/your_service_name.service file add these



[Unit]
Description=a description whatever you want with spaces
ConditionPathExists=/usr/local/bin/your_script_name.sh

[Service]
Type=oneshot
ExecStart=/usr/local/bin/your_script_name.sh

[Install]
WantedBy=multi-user.target


now you have only to start , enable and check the service status of your service with these commands



systemctl enable your_service_name.service
systemctl start your_service_name.service
systemctl status your_service_name.service



Tip :

with the last command (systemctl status your_service_name.service ) you can check anytime as user ( no need root rights ) the status and the errors of your own service and ofc the main goal if your service successful unload the drivers by reading the results of the echo commands . Also you can not disable all the drivers . some drivers modules can not removed because they are builtin on kernel like ipv6 and you can see what are they with this command


cat /lib/modules/$(uname -r)/modules.builtin

for these drivers please do not use this script and this method.

p.s. Sorry for my English and any suggestion for the script appreciated :)

Sources for systemd :

Full documentation (https://freedesktop.org/wiki/Software/systemd/)
Wikipedia (https://en.wikipedia.org/wiki/Systemd)
The history of systemd (http://0pointer.de/blog/projects/systemd.html)
Main developers : Kay Sievers (https://en.wikipedia.org/wiki/Kay_Sievers) & Lennart Poettering (https://en.wikipedia.org/wiki/Lennart_Poettering)

Acropolis
5th November 2016, 10:18 PM
i see someone hide my post ., nvm i switched to other distro where there is still using blacklist.conf

marko
6th November 2016, 01:24 AM
The problem is that the loaded drivers are controlled by conf files and are rewritten every time from kernel updates and in fedora we have a lot of updates :) . old fedora had the blacklist.conf file as most linux distros , the new fedora has the dist-blacklist.conf file or can blacklisted by grub or by sysconf file or manual with rmmod command but every time the system updated or upgraded these files are re-written by the system and you loose your preferences .


(I see this is old: Feb 2016 but it's probably of interest):
An update of 'hwdata' package will update your /usr/lib/modprobe.d/dist-blacklist.conf that's why you should put your blacklists into /etc/modprobe.d/dist-blacklist.conf which would NOT be updated when hwdata updates. In general files in any <whatever> directory ending with a '.d' under /etc will append or override changes to their permanent matching system down under /usr/lib/<whatever>.d/.....

antikythera
6th November 2016, 11:55 AM
also, the thread was not hidden. it was not visible because nobody posted to it since you did in March, views do not count towards activity and it would have been found by a simple search if anyone needed the information you posted. The forum only displays active threads from the last month by default. you can change this yourself by using the display options at the bottom of any list of threads rather than in a thread itself.

http://forums.fedoraforum.org/picture.php?albumid=391&pictureid=2357