Using kconfig for own projects


    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.


    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
    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  KConfig

    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 
        prompt "Select data destination"
        default DESTINATION_TTY
        bool "show data in console"
        bool "save data to file"
        string "destination file"
        depends on SELECT_DESTINATION_FILE
        default "last.log"
          Write destination file with relative or full path
        bool "show data only for current user"
        default y
          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

    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$ ./ 
    boozlach pts/0      Sun Jul 19 06:00   still logged in
    boozlach pts/0      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
    boozlachu@debian:~/demo-kbuild$ ./ 
    boozlachu@debian:~/demo-kbuild$ cat last.log 
    boozlach pts/0      Sun Jul 19 06:00   still logged in
    boozlach pts/0      Sun Jul 19 05:58 - 05:58  (00:00)


    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.

    AdBlock has stolen the banner, but banners are not teeth — they will be back


    Comments 1

      for example, kconfig-frontends project

      Какие есть альтернативы?

      Ситуация с фрагментацией KConfig какая-то нездоровая: buildroot тащит свой чуть патченный, busybox — свой, отпочковавшийся ещё до появления make nconfig. И, думаю, так с каждым проектом.

      Only users with full accounts can post comments. Log in, please.