You are not logged in.

#1 2012-11-18 10:30:57

Cronanius
Member
From: Alberta Canada
Registered: 2006-07-31
Posts: 15

Making modprobe boot config slightly less annoying using a bash script

I'm posting this here because I feel it's a newbie topic, but I'm not entirely sure if it's appropriate for this forum because [this forum] seems to be focused more on getting help with stuff that isn't working, and I've solved it on my own; I'm putting it here so that it can be found/searched by people looking for it.

Maybe I'm the only person who feels this way, but I hate trying to remember how/where to add new modules for modprobe to run at boot, because I typically do it only about twice/year. A mild irritation, to be sure, but it's like a thin crack in one's curtains early on a summer morning, when all one wants to do is sleep in. So I have concocted a short, simple bash script to alleviate it: modprober.sh.

#!/bin/bash

if [ -f /etc/modules-load.d/$1* ];
        then
                echo "$1 already exists in /etc/modules-load.d!"
        else
                echo "adding $1 to /etc/modules-load.d/ ..."
                echo -e "$1" >> "/etc/modules-load.d/"$1".conf"
fi

[EDIT]: fixed to take arguments directly from the command line (e.g. sudo bash modprober.sh vboxdrv).

You will need to run it as the superuser for file permissions purposes. Also, one only needs to type the name of the module, such as "vboxdrv" (without quotes) for the vitualbox module.

Please note that what little programming skill I have is extremely rusty, and I have not attempted any type of input sanitation except to avoid overwriting files that already exist, as I feel this could be unwise. Improvements, good ideas, and constructive criticism are always welcome. I would particularly like to be able to put the module on the same line - like an argument (eg. bash modprober.sh vboxdrv), but I haven't managed to figure that out - not even sure if it's possible with simple bash scripting.

Hope this helps the odd lazy person like myself smile.
~Cronanius

Last edited by Cronanius (2012-11-18 22:45:50)


Perpetually an Arch Newbie. I'm here for the operating system, not the philosophy.

Offline

#2 2012-11-18 13:00:12

Lord Bo
Member
Registered: 2012-11-11
Posts: 168

Re: Making modprobe boot config slightly less annoying using a bash script

Hi,

OffTopic:
I normally do only german poetry, so please excuse that the following is rather clumsy:

Fading, staying in the mist,
looking all around,
shadows only on my path,
swallow all that sound.

Bearing, tearing thoughts and tears,
staying in the mist,
only shadows on my path,
shadows in the mist.

I also have problems with the pronounciation... Well, you have to excuse that, too. By the way: What is the origin of the "Cronanius" pseudonym?

Benjamin

Offline

#3 2012-11-18 14:12:49

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Making modprobe boot config slightly less annoying using a bash script

LordBo: that's very nice, the men in white coats will be here for you soon.

Cronanius, no doubt useful to you, but can I assume you're aware that each module does not require a separate *.conf file, and that *.conf files do not have to be named after the module(s) listed in them?

You can use $1 ($2 $3 ...) and/or $@ in your script to process command line arguments. Bash documentation is not eactly hard to find, here's one of many good resources.

Offline

#4 2012-11-18 14:16:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Making modprobe boot config slightly less annoying using a bash script

Remembering the name of a script you'd only use a couple times a year is easier than remembering the name of /etc/modprobe.conf and opening it in a text editor?

*shrug*, if it works for you, enjoy - but I can't help but wonder if you are doing something odd in the first place to make this seem like a simpler method.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2012-11-18 14:18:40

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Making modprobe boot config slightly less annoying using a bash script

Trilby, see, even you forgot - it's /etc/modules-load.d/ silly. tongue

Offline

#6 2012-11-18 14:21:15

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,452
Website

Re: Making modprobe boot config slightly less annoying using a bash script

Ah, yeah, forgot a directory - tab completion got that pretty quickly though.

And I guess I never used the modules-load.d directory.

Sorry, perhaps I don't know what I'm talking about .... never had any need of this (empty) directory.

I was thinking of mkinitcpio.conf - sorry again for the noise.

Last edited by Trilby (2012-11-18 14:32:18)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2012-11-18 14:29:20

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Making modprobe boot config slightly less annoying using a bash script

Yeah, most users don't need anything in there, autodetection takes care of it. It's useful for stuff like virtualbox modules etc.

Offline

#8 2012-11-18 15:11:16

Lord Bo
Member
Registered: 2012-11-11
Posts: 168

Re: Making modprobe boot config slightly less annoying using a bash script

tomk wrote:

LordBo: that's very nice, the men in white coats will be here for you soon.

I can't remember the last time someone was confined for writing a poem.

tomk wrote:

Cronanius, no doubt useful to you, but can I assume you're aware that each module does not require a separate *.conf file, and that *.conf files do not have to be named after the module(s) listed in them?

Ok, he can use

for i in /etc/modules-load.d/*; do 
  if grep $module $i; then
    ...
  fi
done

which may still lead to wrong results... . Then one could use sed to define a better fitting condition, and so on. Well, as long as it works: Do not complicate it unnecessarily. I think he knows, how his files in /etc/modules-load.d are structured.

Offline

#9 2012-11-18 15:32:16

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Making modprobe boot config slightly less annoying using a bash script

grep -qFx "$1" /etc/modules-load.d/*.conf || echo "$1" >>"/etc/modules-load.d/$1.conf"

Still don't see the point of it though. It has the word "modules" and "load" in the directory name and it's in /etc. Shell completion for 'man modules<tab>' brings up the manpage. How hard can it really be to remember?

Offline

#10 2012-11-18 15:35:53

Lord Bo
Member
Registered: 2012-11-11
Posts: 168

Re: Making modprobe boot config slightly less annoying using a bash script

It can be hard, I know that wink .

Offline

#11 2012-11-18 22:41:31

Cronanius
Member
From: Alberta Canada
Registered: 2006-07-31
Posts: 15

Re: Making modprobe boot config slightly less annoying using a bash script

tomk wrote:

LordBo: that's very nice, the men in white coats will be here for you soon.

Cronanius, no doubt useful to you, but can I assume you're aware that each module does not require a separate *.conf file, and that *.conf files do not have to be named after the module(s) listed in them?

I actually wasn't aware of this; all of the instructions I've found on setting up new modules have said exactly that I need to have a separate *.conf file for each. Personally, I think that type of setup is completely idiotic, but I'm just a user; who am I to question the powers that be? wink. I'd rather not even have a folder called modules-load.d, and just type the names of the modules I want loaded at boot into a line in modprobe.conf and forget about it - that would be my preferred solution.

tomk wrote:

You can use $1 ($2 $3 ...) and/or $@ in your script to process command line arguments. Bash documentation is not exactly hard to find, here's one of many good resources.

I originally overlooked the use of $1, $2, etc, and have subsequently utilized those to reduce clunkiness. Thanks for your help! smile

Also, thanks for the poetry, Lord Bo.


Perpetually an Arch Newbie. I'm here for the operating system, not the philosophy.

Offline

#12 2012-11-19 00:42:42

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Making modprobe boot config slightly less annoying using a bash script

Cronanius wrote:

I actually wasn't aware of this; all of the instructions I've found on setting up new modules have said exactly that I need to have a separate *.conf file for each.

Read the only authoritative instructions then - the man page.

Cronanius wrote:

I'd rather not even have a folder called modules-load.d, and just type the names of the modules I want loaded at boot into a line in modprobe.conf

modules-load.d.. modprobe.d.. hmm, I wonder which one should I use for loading modules? smile

One minor point - calling it modprober could be misleading, as it's not actually modprobing anything. I'd suggest bootmod.

Offline

Board footer

Powered by FluxBB