如何以非交互方式打开 Linux 内核 .config 文件中的功能?

发布于 2024-12-05 23:31:52 字数 424 浏览 0 评论 0原文

我遇到过这样的情况:我们的软件需要与几个不同的 Linux 内核发行版/内核树一起工作。 (包括 Android 分支)

在尝试自动化我们的构建过程时,我发现我们需要支持的特定构建的一些 defconfig 文件不包含我们依赖的内核模块。

例如,假设我的 .config 中需要一个名为 XXX 的选项。对于某些依赖项,我可以这样做:

sed -i 's/# CONFIG_XXX is not set/CONFIG_XXX=m/' .config

对于其他依赖项,这并不容易,因为依赖项可能跨越多行 .config 语句。

是否有更受支持的方式以非交互方式执行此操作,或者我是否一直在编写更复杂的搜索和替换脚本?

I have a situation where our software needs to work with several different Linux kernel distributions / kernel trees. (including Android forks)

In trying to automate our build process, I am finding that some of the defconfig files for particular builds we need to support do not include the kernel modules we depend on.

For example, let's imagine I need an option called XXX in my .config. For some dependencies, I can do something like this:

sed -i 's/# CONFIG_XXX is not set/CONFIG_XXX=m/' .config

For others, it's not so easy since the dependency may span multiple lines of .config statements.

Is there a more supported way to do this non-interactively, or am I stuck writing a more complex search-and-replace script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽袖吟 2024-12-12 23:31:52

内核有一个工具(./scripts/config)来更改.config 上的特定选项。这是一个示例:

./scripts/config --set-val CONFIG_OPTION y

尽管如此,它不会检查 .config 文件的有效性。

The kernel has a tool (./scripts/config) to change specific options on .config. Here is an example:

./scripts/config --set-val CONFIG_OPTION y

Although, it doesn't check the validity of the .config file.

习ぎ惯性依靠 2024-12-12 23:31:52

merge_config.sh 配置片段

$ cd linux
$ git checkout v4.9
$ make x86_64_defconfig
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
# CONFIG_DEBUG_INFO is not set
$ # GDB_SCRIPTS depends on CONFIG_DEBUG_INFO in lib/Kconfig.debug.
$ cat <<EOF >.config-fragment
> CONFIG_DEBUG_INFO=y
> CONFIG_GDB_SCRIPTS=y
> EOF
$ # Order is important here. Must be first base config, then fragment.
$ ./scripts/kconfig/merge_config.sh .config .config-fragment
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y

不幸的是,进程替换无法工作:

./scripts/kconfig/merge_config.sh arch/x86/configs/x86_64_defconfig \
    <( printf 'CONFIG_DEBUG_INFO=y\nCONFIG_GDB_SCRIPTS=y\n' ) 

因为:https://unix.stackexchange.com/a/164109/32558

merge_config.shmake alldefconfig 目标的简单前端。

交叉编译时,运行merge_config.sh时必须导出ARCH,eg:

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make defconfig
./scripts/kconfig/merge_config.sh .config .config-fragment

合并后的输出文件可以通过KCONFIG_CONFIG环境显式指定多变的;否则它只会覆盖 .config

KCONFIG_CONFIG=some/path/.config ./scripts/kconfig/merge_config.sh .config .config-fragment

Buildroot 使用 BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES 实现自动化:如何在 Buildroot 中配置 Linux 内核?

相关:https://unix.stackexchange.com/questions/19905/ how-to-non-interactively-configure-the-linux-kernel-build 提前 20 天迁移:-)

merge_config.sh config fragments

$ cd linux
$ git checkout v4.9
$ make x86_64_defconfig
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
# CONFIG_DEBUG_INFO is not set
$ # GDB_SCRIPTS depends on CONFIG_DEBUG_INFO in lib/Kconfig.debug.
$ cat <<EOF >.config-fragment
> CONFIG_DEBUG_INFO=y
> CONFIG_GDB_SCRIPTS=y
> EOF
$ # Order is important here. Must be first base config, then fragment.
$ ./scripts/kconfig/merge_config.sh .config .config-fragment
$ grep -E 'CONFIG_(DEBUG_INFO|GDB_SCRIPTS)[= ]' .config
CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y

Process substitution does not work unfortunately:

./scripts/kconfig/merge_config.sh arch/x86/configs/x86_64_defconfig \
    <( printf 'CONFIG_DEBUG_INFO=y\nCONFIG_GDB_SCRIPTS=y\n' ) 

because of: https://unix.stackexchange.com/a/164109/32558

merge_config.sh is a simple front-end for the make alldefconfig target.

When cross compiling, ARCH must be exported when you run merge_config.sh, e.g.:

export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
make defconfig
./scripts/kconfig/merge_config.sh .config .config-fragment

The merged output file can be specified explicitly with the KCONFIG_CONFIG environment variable; otherwise it just overwrites .config:

KCONFIG_CONFIG=some/path/.config ./scripts/kconfig/merge_config.sh .config .config-fragment

Buildroot automates it with BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES: How do I configure the Linux kernel within Buildroot?

Related: https://unix.stackexchange.com/questions/19905/how-to-non-interactively-configure-the-linux-kernel-build Migrated 20 days earlier :-)

夜深人未静 2024-12-12 23:31:52

是的,某些配置选项在版本之间会更改名称,有时作为微妙语义变化的指示。

我编写了 python 类来将一组配置片段合并到基本内核配置文件中。不过,这对你来说有点过分了;你可以做与 sed 脚本相同的事情;你并不局限于一句台词。

sed -ir 's/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
         s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
         s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/' .config

或者甚至创建一个单独的脚本。假设 config.sed 包含以下行:

s/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/;

然后您可以运行

sed -ire config.sed .config

希望有帮助!

Yes, some config options change names between releases, sometimes as an indication of subtle semantic changes.

I have written python classes to merge together a set of configuration fragments into base kernel configuration files. That's overkill for you, though; you can do the same thing as a sed script; you aren't limited to one-liners.

sed -ir 's/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
         s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
         s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/' .config

Or even create a separate script. Say, config.sed that contains the lines:

s/^(CONFIG_XXX=.*|# CONFIG_XXX is not set)/CONFIG_XXX=m/;
s/^(CONFIG_FOO=.*|# CONFIG_FOO is not set)/CONFIG_FOO=m/;
s/^(CONFIG_BAR=.*|# CONFIG_BAR is not set)/CONFIG_BAR=m/;

Then you can run

sed -ire config.sed .config

Hope that helps!

明月松间行 2024-12-12 23:31:52

要在更新任何和所有配置依赖项时对单个配置进行一系列简单的翻转:

单选项方法

./scripts/config --set-val CONFIG_OPTION y
./scripts/config --enable CONFIG_BRIDGE
./scripts/config --enable CONFIG_MODULES
./scripts/config --disable CONFIG_X25
./scripts/config --module CONFIG_NFT
make oldconfig
(updates dependencies; may prompt with new dependencies, but old deps silently goes away)

多文件合并方法

如果您有几个想要的 .config-* 文件小片段有选择地合并到主.config文件中,执行:

# Merge IP fragment CONFIG_ settings into the main .config file
./scripts/kconfig/merge_config.sh .config .config-fragment
# Merge  Notebook HW-specific CONFIG_ settings into main .config file
./scripts/kconfig/merge_config.sh .config .config-notebook-toshiba

# Auto-add/auto-remove CONFIG_ dependencies
make oldconfig

References

To do a series of simple flipping of a single config while updating any and all config dependencies:

Single Option approach

./scripts/config --set-val CONFIG_OPTION y
./scripts/config --enable CONFIG_BRIDGE
./scripts/config --enable CONFIG_MODULES
./scripts/config --disable CONFIG_X25
./scripts/config --module CONFIG_NFT
make oldconfig
(updates dependencies; may prompt with new dependencies, but old deps silently goes away)

Multiple-File Merge approach

If you have several small snippets of .config-* files that you want to selectively merge into the main .config file, execute:

# Merge IP fragment CONFIG_ settings into the main .config file
./scripts/kconfig/merge_config.sh .config .config-fragment
# Merge  Notebook HW-specific CONFIG_ settings into main .config file
./scripts/kconfig/merge_config.sh .config .config-notebook-toshiba

# Auto-add/auto-remove CONFIG_ dependencies
make oldconfig

References

泛泛之交 2024-12-12 23:31:52
cat .config-fragment >> .config && make oldconfig

与 aarch64 跨链:

cat .config-fragment >> .config && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make oldconfig
cat .config-fragment >> .config && make oldconfig

cross-chain with aarch64:

cat .config-fragment >> .config && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make oldconfig
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文