Using kconfig for own projects

2023-06-06    https://www.cnblogs.com/NJ-Leon/

 

Intro

Every Linux professional write scripts. Someеimes light, linear. Sometimes complex script with functions and libs(yes, you can write your bash-library for use in other scripts).

But some of the scripts need a configuration file to work. For instance, I wrote a script that builds the ubuntu image for pxe, and I need to change the build process without build-script changes. The best way to resolve this task is to add configuration files.

Writing this config by hands is not a great idea:

  • every user should know all available options
  • hight risk of incorrect data entry
  • uncomfortable
  • maybe something else

Linux kernel gave us a great solution — KConfig. This system resolves all these troubles:

  • New config generated automatically
  • You can set the default value
  • The previous version of config saved automatically
  • text/graphical interface available

These are not all the benefits. You can see more here.

How it works

Simplified schema:

  • All configuration parameters described in KConfig-format file (including available ones)
  • Interpreter (mconf, gconf, qconf, others) reads this file, current config (if it exists), and shows the menu
  • The user selects needed options and selects the SAVE option
  • KConfig moves old config to .config.old and creates new config (default name .config)

After that, you get a ready-made config that can be used in scripts(or for project build).

Example project

Now, I'll show you the demo-project. I placed it on github.

overview

It's a simple script that shows the information about the last logins. Available options:

  • Select destination (file or console)
  • If the selected destination is a file, the user can set the filename
  • Shows information about both all and only current users

Just clone this repo and go to its directory:

boozlachu@debian:~$ git clone https://github.com/skif-web/demo-kbuild.git
Cloning into 'demo-kbuild'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 8 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
boozlachu@debian:~$ cd demo-kbuild/
boozlachu@debian:~/demo-kbuild$ ls
getLast.sh  KConfig  README.md

KConfig interpreter notes

KConfig is just a language. You have to use the interpreter to show the menu. In the Linux kernel, the interpreter is built when you run the "make menuconfig" command. But I'm a lazy potato and will use the kconfig-frontends package. It is available in Debian/Ubuntu, and you can also build it from the source. Or add an interpreter build from source to your project.

KConfig file

The main file here is KConfig. KConfig supports a lot of operators, but I'll use only some of them.

Various types of variables are available there. The most useful are:

  • string to save string data
  • bool to save the bool value (y or commented string in config)
  • tristate (*/m/commented)
boozlachu@debian:~/demo-kbuild$ cat KConfig 
choice SELECT_DESTINATION
    prompt "Select data destination"
    default DESTINATION_TTY

config SELECT_DESTINATION_TTY
    bool "show data in console"
config SELECT_DESTINATION_FILE
    bool "save data to file"
endchoice

config SELECT_DESTINATION_FILE_FILENAME
    string "destination file"
    depends on SELECT_DESTINATION_FILE
    default "last.log"
    help
      Write destination file with relative or full path

config SHOW_ONLY_CURRENT_USER
    bool "show data only for current user"
    default y
    help
      script will get data only for current user

Let's see it. Choice-endchoice pair allows creating SELECT of variants. Inside these keywords, I set definition (prompt), default value (default), and added available variants (SELECT_DESTINATION_TTY, SELECT_DESTINATION_FILE).

The next option (SELECT_DESTINATION_FILE_FILENAME) will only be shown if the destination is the file. The keyword "depends on" allows us to do this. Also, the available keyword "selects" that works in the opposite direction.

Option SHOW_ONLY_CURRENT_USER will always be shown.

Run kconfig in text mode:

boozlachu@debian:~/demo-kbuild$ kconfig-mconf KConfig

 And with qt:

boozlachu@debian:~/demo-kbuild$ kconfig-qconf KConfig

Test run

After configuring it, we get .config file with options:

boozlachu@debian:~/demo-kbuild$ cat .config 
#
# Automatically generated file; DO NOT EDIT.
# Configuration
#
CONFIG_SELECT_DESTINATION_TTY=y
# CONFIG_SELECT_DESTINATION_FILE is not set
CONFIG_SHOW_ONLY_CURRENT_USER=y

As you can see, non-selected options are commented on. It's useful for my script.

I run it with this config:

boozlachu@debian:~/demo-kbuild$ ./getLast.sh 
boozlach pts/0        192.168.2.4      Sun Jul 19 06:00   still logged in
boozlach pts/0        192.168.2.4      Sun Jul 19 05:58 - 05:58  (00:00)

wtmp begins Thu Apr 16 05:48:31 2020

I changed the parameters via KConfig and got another result. Now the date was saved to a file, instead of being shown in the console:

boozlachu@debian:~/demo-kbuild$ cat .config
#
# Automatically generated file; DO NOT EDIT.
# Configuration
#
# CONFIG_SELECT_DESTINATION_TTY is not set
CONFIG_SELECT_DESTINATION_FILE=y
CONFIG_SELECT_DESTINATION_FILE_FILENAME="last.log"
CONFIG_SHOW_ONLY_CURRENT_USER=y
boozlachu@debian:~/demo-kbuild$ ./getLast.sh 
boozlachu@debian:~/demo-kbuild$ cat last.log 
boozlach pts/0        192.168.2.4      Sun Jul 19 06:00   still logged in
boozlach pts/0        192.168.2.4      Sun Jul 19 05:58 - 05:58  (00:00)

Conclusion

As you can see, KConfig is very powerful and friendly util for configuring. It can be used for scripts, package building, and other variants.

If you want to see an improved variant of KConfig using a makefile in the real project — write in comments.

Originally published at https://habr.com.
 
 

posted on 2023-06-06 19:51  NJ_Leon  阅读(8)  评论(0编辑  收藏  举报

导航