# kernel_cfg - My Kernel configuration script

## bell

I want to show up my kernel configuration script I started some months ago and use them on all my Gentoo installations.

Before the script I did always manual configuration and updated by "oldconfig". My issue was I did not know in retrospect why I set any specific setting? Did I set any specific setting? or maybe it's was default in previous kernel?

My solution now: kernel_cfg 

https://github.com/bell07/bashscripts-kernel_cfg (GPL-3)

Basically they are config file snippets with additional comments in "cfg" folder, which are processed to the target "/usr/src/linux/.config" file. The base is the kernel provided "defconfig", then the config snippets are applied.

Own adjustments could be done by own snippet files (cfg/99_*.config)

It is possible to select which snippets should be used in CFG_MODULES parameter in "settings.txt" file.

 *Quote:*   

> 00* 	Default configuration adjustments that should be in any kernel
> 
> 10* 	Disable stuff to get the kernel smaller by default. Useful for non-universal kernels bound to a hardware. Some parameters are re-activated in other config files
> 
> 20* 	Try to support all hardware. Useful for live-media kernels that should work with any hardware
> ...

 

1. clone the repo

2. copy the settings.txt.example and adjust to settings.txt

3. backup your "/usr/src/linux/.config"

4. run the "kernel_cfg.sh" 

5. compare new "/usr/src/linux/.config" with backup

6. Enable additional settings related to your hardware or needs in cfg/99_my.config

7. repeat steps 4-7 if necessary

8. provide feedback

All additional config snippets and feedback to existing snippets welcome!

----------

## Hu

kernel_cfg.sh should be run under set -e.

```
    10   source "$APPL_DIR"/settings.txt
```

If you source the file, it probably should not have a .txt extension.  People tend to expect that .txt files can have relatively freeform text in it.

```
    20   ALL_MODULES=$(eval ls -1 $CFG_MODULES | grep '.config$')
```

Use of eval looks unnecessary here.  Use of ls is wrong.  Never use ls to produce text for program consumption.  In most cases, find is a better choice.  If you need the results to be sorted, include sort in the pipeline.  I suggest ALL_MODULES=$(find . -maxdepth 1 -name '*.config' -a -name $CFG_MODULES | LC_ALL=C sort).  This is still imperfect if you want to handle filenames with unusual characters.  For that, use mapfile to load the values into an array:

```
find . -maxdepth 1 -name '*.config' -a -name $CFG_MODULES -print0 | LC_ALL=C sort -z | { mapfile -d '' ALL_MODULES;

# Use ${ALL_MODULES[@]}

# ...

; }
```

```
    24   cat "$KERNEL_SOURCE_PATH"/arch/x86/configs/x86_64_defconfig $ALL_MODULES > "$KERNEL_SOURCE_PATH"/.config
```

This will misbehave if there are spaces in the names of any files listed in $ALL_MODULES.

```
    26   cd "$KERNEL_SOURCE_PATH"

    27   make olddefconfig 2> /dev/null
```

This could be simplified by using make's option -C.

----------

## Ant P.

Probably a good idea to `emerge shellcheck-bin` and run it on your code.

----------

## Muso

 *Ant P. wrote:*   

> Probably a good idea to `emerge shellcheck-bin` and run it on your code.

 

Currently emerging shellcheck, any reason to pick bin over source?

----------

## bell

Thank you for the feedback!.

@Hu, what do you mean with "run unter set -e"? I am not a noob but not a bash expert  :Sad: .

Renamed settings.txt to settings.env

"eval" is necessary to solve settings like 

```
CFG_MODULES='01* 02*'
```

```
find . -maxdepth 1 -name '*.config' -a -name $CFG_MODULES
```

does not work with setting above.

What is wrong with "ls -1" to get files per line? 

```
-1     list one file per line.  Avoid '\n' with -q or -b
```

I do not plan to add config snippets with spaces or special charecters in filenames to the cfg/ folder. Plain Ascii only as usual. Therefore prefer readeble instead of strictly code.

emerge -vaj dev-util/shellcheck needs time to compile ghc at the first :-/

----------

## NeddySeagoon

bell,

Users migrating away from windows will have windows habits.

The hard bit about interacting with users is all the input checking you need to do to validate user input.

If you do get unexpected symbols in your input stream you need to deal with them or fail gracefully with a useful error message.

Its a lot of work. When you think its complete and correct, the first new user you put in front of it will break it.

----------

## pjp

When configuring the kernel using 'make menuconfig' aren't options available or unavailable based on other configuration settings? Doesn't selecting and deselecting options automatically select or deselect other options? I'm asking because I'm wondering how the configuration files are generated to handle those indirect configuration changes.

 *Muso wrote:*   

>  *Ant P. wrote:*   Probably a good idea to `emerge shellcheck-bin` and run it on your code. 
> 
> Currently emerging shellcheck, any reason to pick bin over source?

  My guess is at least in part due to the ghc dependency.

----------

## bell

 *pjp wrote:*   

> When configuring the kernel using 'make menuconfig' aren't options available or unavailable based on other configuration settings? Doesn't selecting and deselecting options automatically select or deselect other options? I'm asking because I'm wondering how the configuration files are generated to handle those indirect configuration changes. 

 

The script calls "make olddefconfig" and resolve the dependencies this way, like "make oldconfig" does. Eather an option is skipped because the dependencies aren't fullfilled or other depending options are selected automatically

----------

## bell

redone the files processing, now it is just simple but more robust 

```
for file in $CFG_MODULES; do

   if [ -z "${file##*.config}" ]; then

      echo "apply $file"

      cat "$file" >> "$KERNEL_SOURCE_PATH"/.config

   else

      echo "skip $file"

   fi

done
```

This consturct support spaces and special charecters in filenames, if anyone need it.

Use "make -C" for now.

Shellcheck says only 

```
$ shellcheck kernel_cfg.sh 

In kernel_cfg.sh line 10:

source "$APPL_DIR"/settings.env

^-- SC1090: Can't follow non-constant source. Use a directive to specify location.

^-- SC2039: In POSIX sh, 'source' in place of '.' is undefined.

```

I do not understand  :Sad: 

Any other optimization hints? Any comments to provided configuration sets?

----------

## pjp

Interesting, thanks. I may eventually try that approach. For now I'm just managing minor upgrades (primarily compilation) with olddefconfig.

----------

## Hu

 *bell wrote:*   

> @Hu, what do you mean with "run unter set -e"? I am not a noob but not a bash expert .

 Add set -e as the first step of any Bash script.  For new scripts, consider using also set -u.  These make the shell less tolerant of errors.  The default behavior is to blunder onward, potentially making a bigger mess than if the script aborted on the first hint of a bug. *bell wrote:*   

> "eval" is necessary to solve settings like 
> 
> ```
> CFG_MODULES='01* 02*'
> ```
> ...

 That was not my experience in testing.

```
$ touch ab ac

$ z='a*'

$ echo "$z"

a*

$ echo $z

ab ac
```

 *bell wrote:*   

> 
> 
> ```
> find . -maxdepth 1 -name '*.config' -a -name $CFG_MODULES
> ```
> ...

 You need a separate -name before each pattern. *bell wrote:*   

> What is wrong with "ls -1" to get files per line? 
> 
> ```
> -1     list one file per line.  Avoid '\n' with -q or -b
> ```
> ...

 It involves using ls.  Using ls to get input for a script is always wrong. *bell wrote:*   

> I do not plan to add config snippets with spaces or special charecters in filenames to the cfg/ folder. Plain Ascii only as usual. Therefore prefer readeble instead of strictly code.

 The first user who gets burned by this lack of error checking will feel differently.  Always plan for users to expect the program to protect them from themselves.

----------

## bell

The "set -e" does no changes in execution in current version.

 *Hu wrote:*   

> That was not my experience in testing.

 

```
$ export CFG_MODULES='10*'

$ ls "$CFG_MODULES"

ls: cannot access '10*': No such file or directory

$ eval ls "$CFG_MODULES"

10_disable_all_net_vendors.config
```

But anyway it is solved in other way in the meantime.

 *Hu wrote:*   

> The first user who gets burned by this lack of error checking will feel differently. 

 Usually I write things for me only, not for any users. If I see it is something useful for other people I show my work. Of course I am interested to optimize and to learn new ways.

----------

## Ant P.

 *Muso wrote:*   

>  *Ant P. wrote:*   Probably a good idea to `emerge shellcheck-bin` and run it on your code. 
> 
> Currently emerging shellcheck, any reason to pick bin over source?

 

It's a lot to install for, for most people, the only haskell program on the system.

----------

## Hu

 *bell wrote:*   

>  *Hu wrote:*   That was not my experience in testing. 
> 
> ```
> $ export CFG_MODULES='10*'
> 
> ...

 Yes, as I showed, you must leave the expression unquoted if you want glob expansion to occur. *bell wrote:*   

>  *Hu wrote:*   The first user who gets burned by this lack of error checking will feel differently.  Usually I write things for me only, not for any users. If I see it is something useful for other people I show my work. Of course I am interested to optimize and to learn new ways.

 If you post something to draw attention to it, you should expect some people will use it.  Some of those people may not know about and avoid the quirks in the code.  As a courtesy to them, if you don't want to make the code robust, you should document that it is fragile.

----------

## bell

We talk about issues in code that was writen 2 months ago and changed/fixed a day and 2 posts ago. Any issues with the current implementation?

Unquoted expression had issue it works only if you are already in cfg folder during settings load if I remember correctly. But the cfg-folder setting is in settings file too...

find -name a -name b is "a and b" or "a or b"?  In case of "and" the expression '10* 20*' does not work, in case of "or" the -name '*.config' hint always. To add "-name" to each entry in CFG_MODULES in settings.env does affect the useability for me.

I take any reported issue seriously and fix it, if (1) I understand the issue, (2) understand the proposed solution and (3) the solution does not affect useability. "ls is bad" is an explanation I do not understand, I miss the "because".

The script is relatively simple and nothing special, the value of kernel_cfg are the config snippets (i intend to talk about) and the idea to concatenate them, and get clean config file using "make olddefconfig"

----------

## bell

Since I switched to sys-kernel/gentoo-kernel, I do not use the scripts byself anymore.

But the snippets are still useable with the sys-kernel/gentoo-kernel. The package supports snippets, stored in /etc/kernel/config.d/ folder

 *Quote:*   

> # ls -l /etc/kernel/config.d/
> 
> insgesamt 4
> 
> lrwxrwxrwx 1 root root  42  4. Jan 08:36 00_defaults.config -> /scripts/kernel_cfg/cfg/00_defaults.config
> ...

 

Since the gentoo-kernel default config is more blown-up (more features enabled by default), the most "_enable_" snippets are redundant.

But I still interested for new "_disable_" snippets, to disable features usually not needed on modern desktop computers.

----------

