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

#1 问题描述

最近本身编译内核安装内核的时候, 老是遇到 /lib/modules 下空间不够, 致使内核安装有问题. 因此就想裁剪下.
分析的时候发现, 系统原生内核 /lib/modules/uname -r` 目录驱动大小只有 100M 左右, 可是我本身编译的驱动目录 1.4G 左右.linux

#2 问题分析

##2.1 问题缘由

若是咱们内核开启了 CONFIG_DEBUG_INFO 选项, 那么咱们编译的二进制会带上不少调试信息. 内核镜像 Image 都是通过裁剪和优化的. 可是驱动没有. 因此致使单个 KO 的大小就很大.web

所以能够把每一个 KO 都 strip 一会儿.app

make modules_install 安装驱动以后, strip 一会儿.ide

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

##2.2 INSTALL_MOD_STRIP 选项

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

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#L1481ui

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 进行处理this

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

可见spa

  • 若是设置了 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_cmddebug

# 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)