使用 INSTALL_MOD_STRIP 在 modules_install 的时候 strip 驱动, 减少磁盘占用

GitHubCSDNKernelShow知乎掘金
NACHENG JianNA3584592006977729781592915999

1 问题描述


最近自己编译内核安装内核的时候, 总是遇到 /lib/modules 下空间不够, 导致内核安装有问题. 所以就想裁剪下.
分析的时候发现, 系统原生内核 /lib/modules/uname -r` 目录驱动大小只有 100M 左右, 但是我自己编译的驱动目录 1.4G 左右.

2 问题分析


2.1 问题原因


如果我们内核开启了 CONFIG_DEBUG_INFO 选项, 那么我们编译的二进制会带上很多调试信息. 内核镜像 Image 都是经过裁剪和优化的. 但是驱动没有. 所以导致单个 KO 的大小就很大.

因此可以把每个 KO 都 strip 一下子.

make modules_install 安装驱动之后, strip 一下子.

find /lib/modules/XXX -name *.ko | xargs strip -g

2.2 INSTALL_MOD_STRIP 选项


内核难道没有提供现成的选项么:

https://elixir.bootlin.com/linux/v5.3.6/source/Documentation/kbuild/kbuild.rst#182

https://elixir.bootlin.com/linux/v5.3.6/source/Documentation/kbuild/makefiles.rst#L1481

The default kernel configuration is configured to support as many hardware as possible. A non-stripped kernel with default configuration resulted in a size of 1897996 kB (including kernel + modules). When stripping many unnecessary drivers and options, it resulted in a size of 892892 kB which is a size reduction of 53% compared to the stock kernel.

    When installing the kernel modules, append the INSTALL_MOD_STRIP=1 option. This will strip all debugging symbols and reduced the size by 92% for me (from 892892 kB to 69356 kB). Note this will only affects modules to be installed and not the kernel (vmlinuz) itself.


Use the INSTALL_MOD_STRIP option for removing debugging symbols:

# make INSTALL_MOD_STRIP=1 modules_install
Similarly, for building the deb packages:
# make INSTALL_MOD_STRIP=1 deb-pkg

2.3 INSTALL_MOD_STRIP 的实现


如果在 build and install ko 的时候(make modules_install) 的时候加上INSTALL_MOD_STRIP =1的话
则build ko的时候 会加上–strip-debug 这样会让build出的ko size大幅缩小.
具体是在 kernel 根目录下面 Makefile 中有对INSTALL_MOD_STRIP=1 进行处理

#
# INSTALL_MOD_STRIP, if defined, will cause modules to be
# stripped after they are installed. If INSTALL_MOD_STRIP is '1', then
# the default option --strip-debug will be used. Otherwise,
# INSTALL_MOD_STRIP value will be used as the options to the strip command.

ifdef INSTALL_MOD_STRIP
ifeq ($(INSTALL_MOD_STRIP),1)
mod_strip_cmd = $(STRIP) --strip-debug
else
mod_strip_cmd = $(STRIP) $(INSTALL_MOD_STRIP)
endif # INSTALL_MOD_STRIP=1
else
mod_strip_cmd = true
endif # INSTALL_MOD_STRIP
export mod_strip_cmd

可见

  • 如果设置了 INSTALL_MOD_STRIP 为 1, 那么 mod_strip_cmd = strip --strip-debug
  • 如果设置了 INSTALL_MOD_STRIP 为其他参数, 那么 mod_strip_cmd = strip $(INSTALL_MOD_STRIP)
  • 如果没设置 INSTALL_MOD_STRIP, mod_strip_cmd 只会设置为 true

最终会在 scripts/Makefile.modinst中用到mod_strip_cmd

# Don't stop modules_install if we can't sign external modules.
quiet_cmd_modules_install = INSTALL $@
      cmd_modules_install = \
    mkdir -p $(2) ; \
    cp $@ $(2) ; \
    $(mod_strip_cmd) $(2)/$(notdir $@) ; \
    $(mod_sign_cmd) $(2)/$(notdir $@) $(patsubst %,|| true,$(KBUILD_EXTMOD)) ; \
    $(mod_compress_cmd) $(2)/$(notdir $@)
# Modules built outside the kernel source tree go into extra by default
INSTALL_MOD_DIR ?= extra
ext-mod-dir = $(INSTALL_MOD_DIR)$(subst $(patsubst %/,%,$(KBUILD_EXTMOD)),,$(@D))

modinst_dir = $(if $(KBUILD_EXTMOD),$(ext-mod-dir),kernel/$(@D))

$(modules):
 $(call cmd,modules_install,$(MODLIB)/$(modinst_dir))

modules_install 中会用驱动拷贝到安装目录, 然后用 mod_strip_cmd 处理驱动.
如果设置了 INSTALL_MOD_STRIP, 就会对驱动做 strip, 否则就什么也不做.

3 参考资料


why-is-install-mod-strip-not-on-by-default

如何編譯(Compile) Linux的核心(Kernel)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值