[Arm]version magic ‘5.6.0-rc5+ SMP mod_unload ‘ should be...问题研究

本文介绍了解决Linux内核模块加载时出现VerMagic不匹配的问题。文章详细解释了VerMagic的生成方式及其与内核版本的关系,并提供了两种有效的解决办法。

前几天编译ko模块驱动的时候,发现会出现Magic num不匹配从而导致无法insmod到系统中的问题,当时听老司机说关了一个什么kernel验证就行了,但是具体怎么关也没和我说,当时也没有深究了,回去找到当时的kernel重新编了一下就行了,今天又想起这个问题来,研究了一下,做个记录。

1, 首先,为什么会出现修改了kernel重新编译后magic num改变

内核版本是如何生成的:

Linux 内核在进行模块装载时先完成模块的 CRC 值校验,再核对 vermagic 中的字符信息,linux版本在 include/generated/utsrelease.h中定义,文件中的内容如下:

#define UTS_RELEASE "4.9.123"

utsrelease.h是kernel编译后自动生成的,用户更改里面的内容不会有效果。
这个值可以通过修改最顶层的Makefile文件来修改。

VERSION = 4
PATCHLEVEL = 9                
SUBLEVEL = 123                
EXTRAVERSION =
...

init/version.c中,定义了kernel启动时的第一条打印信息:

/* FIXED STRINGS! Don't touch! */
const char linux_banner[] =
  "Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
  LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n";

const char linux_proc_banner[] =
  "%s version %s"
  " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
  " (" LINUX_COMPILER ") %s\n";

这里UTS_RELEASE在kernel编译时自动生成

init/main.casmlinkage __visible void __init start_kernel(void)函数中,有kernel启动的第一条打印信息,这条信息是dmesg命令打印出来:

pr_notice("%s", linux_banner);

驱动模块的version magic信息是怎么生成的:

4.x 内核下,在include/linux/vermagic.h中定义有VERMAGIC_STRING,如下:

#include <generated/utsrelease.h>
              
/* Simply sanity version stamp for modules. */
#ifdef CONFIG_SMP
#define MODULE_VERMAGIC_SMP "SMP "
#else
#define MODULE_VERMAGIC_SMP ""
#endif
#ifdef CONFIG_PREEMPT
#define MODULE_VERMAGIC_PREEMPT "preempt "
#else
#define MODULE_VERMAGIC_PREEMPT ""
#endif
#ifdef CONFIG_MODULE_UNLOAD
#define MODULE_VERMAGIC_MODULE_UNLOAD "mod_unload "
#else
#define MODULE_VERMAGIC_MODULE_UNLOAD ""
#endif 
#ifdef CONFIG_MODVERSIONS
#define MODULE_VERMAGIC_MODVERSIONS "modversions "
#else
#define MODULE_VERMAGIC_MODVERSIONS ""
#endif
#ifndef MODULE_ARCH_VERMAGIC
#define MODULE_ARCH_VERMAGIC ""
#endif  

#define VERMAGIC_STRING             \
  UTS_RELEASE " "             \
  MODULE_VERMAGIC_SMP MODULE_VERMAGIC_PREEMPT       \
  MODULE_VERMAGIC_MODULE_UNLOAD MODULE_VERMAGIC_MODVERSIONS \
  MODULE_ARCH_VERMAGIC

VERMAGIC_STRING不仅包含内核版本号,还包含有内核使用的SMP与preempt, MODULE_UNLOAD, 架构等配置信息。模块在编译时,我们可以看到屏幕上会显示"MODPOST"。

  <~/Documents/Demo/driver/debugfs> make
make -C ~/kernel/ SUBDIRS=~/Documents/Demo/driver/debugfs modules
make[1]: Entering directory '~/kernel'
  CC [M]  ~/Documents/Demo/driver/debugfs/my_debugfs.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      ~/Documents/Demo/driver/debugfs/my_debugfs.mod.o
  LD [M]  ~/Documents/Demo/driver/debugfs/my_debugfs.ko
make[1]: Leaving directory '~/kernel'

在此阶段, VERMAGIC_STRING会添加到模块的modinfo段。在内核源码目录下scripts\mod\modpost.c文件中可以看到模块后续处理部分的代码。模块编译生成后,通过modinfo my_debugfs.ko命令可以查看此模块的vermagic等信息。

  <~/Documents/Demo/driver/debugfs> modinfo my_debugfs.ko 
filename:      ~/Documents/Demo/driver/debugfs/my_debugfs.ko
license:        GPL
depends:        
vermagic:       4.9.123 SMP preempt mod_unload aarch64

4.x 内核下的模块装载器里保存有内核的版本信息,在装载模块时,装载器会比较所保存的内核vermagic与此模块的modinfo段里保存的vermagic信息是否一致,两者一致时,模块才能被装载。

为了使两个版本一致:可以把 依赖源码中的include/linux/vermagic.h中的UTS_RELEASE修改成与目标机器的版本一致,这样,再次编译模块就可以了。

内核模块版本和内核版本不一致的处理方法

2, 发现版本号会追加git的版本号,还有个“+”

向linux内核版本号添加字符/为何有时会自动添加“+”号,根据这篇文章,一步一步试一下

第一步:去掉git的附加信息

先在menuconfig中找到

  │ Symbol: LOCALVERSION [=]                                                                                                                                                                                   │  
  │ Type  : string                                                                                                                                                                                             │  
  │ Prompt: Local version - append to kernel release                                                                                                                                                           │  
  │   Location:                                                                                                                                                                                                │  
  │ (1) -> General setup                                                                                                                                                                                       │  
  │   Defined at init/Kconfig:81                                                                                                                                                                               │  
  │                                                                                                                                                                                                            │  
  │                                                                                                                                                                                                            │  
  │ Symbol: LOCALVERSION_AUTO [=n]                                                                                                                                                                             │  
  │ Type  : boolean                                                                                                                                                                                            │  
  │ Prompt: Automatically append version information to the version string                                                                                                                                     │  
  │   Location:                                                                                                                                                                                                │  
  │ (2) -> General setup                                                                                                                                                                                       │  
  │   Defined at init/Kconfig:91                                                                                                                                                                               │  
  │   Depends on: !COMPILE_TEST [=n]                                                                                                                                                                           │  
  │                                                           

关掉LOCALVERSION_AUTO并留空LOCALVERSION,发现git版本确实是不见了,但是‘+’还是存在。

第二步:去掉“+”号

scripts/setlocalversion文件中,找到以下语句:

# CONFIG_LOCALVERSION and LOCALVERSION (if set)
res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"

# scm version string if not at a tagged commit
if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
  # full scm version string
  res="$res$(scm_version)"
else
  # append a plus sign if the repository is not in a clean
  # annotated or signed tagged state (as git describe only
  # looks at signed or annotated tags - git tag -a/-s) and
  # LOCALVERSION= is not specified
  if test "${LOCALVERSION+set}" != "set"; then
    scm=$(scm_version --short)
   # res="$res${scm:++}"	//××××××××××××××注释掉这句话××××××××××××
  fi
fi

下面是注释前后的对比
注释前
在这里插入图片描述
问题解决。


Note:

在网上还看到两种方法,写在下面:

第一种:menuconfig 的时候不要勾选,直接搜MODVERSIONS

在这里插入图片描述
注意不要选图中那行

第二种:modprobe --force-vermagic helloworld.ko

这个大家可以自行尝试一下

conda update -n base -c conda-forge conda --yes Solving environment: failed CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/free/linux-64/repodata.json.bz2> Elapsed: - An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your way. If your current network has https://www.anaconda.com blocked, please file a support request with your network engineering team. ConnectionError(MaxRetryError("HTTPSConnectionPool(host='repo.anaconda.com', port=443): Max retries exceeded with url: /pkgs/free/linux-64/repodata.json.bz2 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x2b165a4e3128>: Failed to establish a new connection: [Errno -2] Name or service not known'))")) [scb3201@ln137%bscc-a6 ~]$ # 测试安装常用包 [scb3201@ln137%bscc-a6 ~]$ conda install -c conda-forge numpy pandas Solving environment: failed CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://repo.anaconda.com/pkgs/r/noarch/repodata.json.bz2> Elapsed: - An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your way. If your current network has https://www.anaconda.com blocked, please file a support request with your network engineering team. ConnectionError(MaxRetryError("HTTPSConnectionPool(host='repo.anaconda.com', port=443): Max retries exceeded with url: /pkgs/r/noarch/repodata.json.bz2 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x2ba3d9347be0>: Failed to establish a new connection: [Errno -2] Name or service not known'))")) [scb3201@ln137%bscc-a6 ~]$ [scb3201@ln137%bscc-a6 ~]$ # 检查环境状态 [scb3201@ln137%bscc-a6 ~]$ conda list --revisions 2018-11-08 03:08:33 (rev 0) +_ipyw_jlab_nb_ext_conf-0.1.0 +alabaster-0.7.11 +anaconda-5.3.1 +anaconda-client-1.7.2 +anaconda-navigator-1.9.2 +anaconda-project-0.8.2 +appdirs-1.4.3 +asn1crypto-0.24.0 +astroid-2.0.4 +astropy-3.0.4 +atomicwrites-1.2.1 +attrs-18.2.0 +automat-0.7.0 +babel-2.6.0 +backcall-0.1.0 +backports-1.0 +backports.shutil_get_terminal_size-1.0.0 +beautifulsoup4-4.6.3 +bitarray-0.8.3 +bkcharts-0.2 +blas-1.0 +blaze-0.11.3 +bleach-2.1.4 +blosc-1.14.4 +bokeh-0.13.0 +boto-2.49.0 +bottleneck-1.2.1 +bzip2-1.0.6 +ca-certificates-2018.03.07 +cairo-1.14.12 +certifi-2018.8.24 +cffi-1.11.5 +chardet-3.0.4 +click-6.7 +cloudpickle-0.5.5 +clyent-1.2.2 +colorama-0.3.9 +conda-4.5.11 +conda-build-3.15.1 +conda-env-2.6.0 +constantly-15.1.0 +contextlib2-0.5.5 +cryptography-2.3.1 +curl-7.61.0 +cycler-0.10.0 +cython-0.28.5 +cytoolz-0.9.0.1 +dask-0.19.1 +dask-core-0.19.1 +datashape-0.5.4 +dbus-1.13.2 +decorator-4.3.0 +defusedxml-0.5.0 +distributed-1.23.1 +docutils-0.14 +entrypoints-0.2.3 +et_xmlfile-1.0.1 +expat-2.2.6 +fastcache-1.0.2 +filelock-3.0.8 +flask-1.0.2 +flask-cors-3.0.6 +fontconfig-2.13.0 +freetype-2.9.1 +fribidi-1.0.5 +get_terminal_size-1.0.0 +gevent-1.3.6 +glib-2.56.2 +glob2-0.6 +gmp-6.1.2 +gmpy2-2.0.8 +graphite2-1.3.12 +greenlet-0.4.15 +gst-plugins-base-1.14.0 +gstreamer-1.14.0 +h5py-2.8.0 +harfbuzz-1.8.8 +hdf5-1.10.2 +heapdict-1.0.0 +html5lib-1.0.1 +hyperlink-18.0.0 +icu-58.2 +idna-2.7 +imageio-2.4.1 +imagesize-1.1.0 +incremental-17.5.0 +intel-openmp-2019.0 +ipykernel-4.9.0 +ipython-6.5.0 +ipython_genutils-0.2.0 +ipywidgets-7.4.1 +isort-4.3.4 +itsdangerous-0.24 +jbig-2.1 +jdcal-1.4 +jedi-0.12.1 +jeepney-0.3.1 +jinja2-2.10 +jpeg-9b +jsonschema-2.6.0 +jupyter-1.0.0 +jupyter_client-5.2.3 +jupyter_console-5.2.0 +jupyter_core-4.4.0 +jupyterlab-0.34.9 +jupyterlab_launcher-0.13.1 +keyring-13.2.1 +kiwisolver-1.0.1 +lazy-object-proxy-1.3.1 +libcurl-7.61.0 +libedit-3.1.20170329 +libffi-3.2.1 +libgcc-ng-8.2.0 +libgfortran-ng-7.3.0 +libpng-1.6.34 +libsodium-1.0.16 +libssh2-1.8.0 +libstdcxx-ng-8.2.0 +libtiff-4.0.9 +libtool-2.4.6 +libuuid-1.0.3 +libxcb-1.13 +libxml2-2.9.8 +libxslt-1.1.32 +llvmlite-0.24.0 +locket-0.2.0 +lxml-4.2.5 +lzo-2.10 +markupsafe-1.0 +matplotlib-2.2.3 +mccabe-0.6.1 +mistune-0.8.3 +mkl-2019.0 +mkl-service-1.1.2 +mkl_fft-1.0.4 +mkl_random-1.0.1 +more-itertools-4.3.0 +mpc-1.1.0 +mpfr-4.0.1 +mpmath-1.0.0 +msgpack-python-0.5.6 +multipledispatch-0.6.0 +navigator-updater-0.2.1 +nbconvert-5.4.0 +nbformat-4.4.0 +ncurses-6.1 +networkx-2.1 +nltk-3.3.0 +nose-1.3.7 +notebook-5.6.0 +numba-0.39.0 +numexpr-2.6.8 +numpy-1.15.1 +numpy-base-1.15.1 +numpydoc-0.8.0 +odo-0.5.1 +olefile-0.46 +openpyxl-2.5.6 +openssl-1.0.2p +packaging-17.1 +pandas-0.23.4 +pandoc-1.19.2.1 +pandocfilters-1.4.2 +pango-1.42.4 +parso-0.3.1 +partd-0.3.8 +patchelf-0.9 +path.py-11.1.0 +pathlib2-2.3.2 +patsy-0.5.0 +pcre-8.42 +pep8-1.7.1 +pexpect-4.6.0 +pickleshare-0.7.4 +pillow-5.2.0 +pip-10.0.1 +pixman-0.34.0 +pkginfo-1.4.2 +pluggy-0.7.1 +ply-3.11 +prometheus_client-0.3.1 +prompt_toolkit-1.0.15 +psutil-5.4.7 +ptyprocess-0.6.0 +py-1.6.0 +pyasn1-0.4.4 +pyasn1-modules-0.2.2 +pycodestyle-2.4.0 +pycosat-0.6.3 +pycparser-2.18 +pycrypto-2.6.1 +pycurl-7.43.0.2 +pyflakes-2.0.0 +pygments-2.2.0 +pylint-2.1.1 +pyodbc-4.0.24 +pyopenssl-18.0.0 +pyparsing-2.2.0 +pyqt-5.9.2 +pysocks-1.6.8 +pytables-3.4.4 +pytest-3.8.0 +pytest-arraydiff-0.2 +pytest-astropy-0.4.0 +pytest-doctestplus-0.1.3 +pytest-openfiles-0.3.0 +pytest-remotedata-0.3.0 +python-3.7.0 +python-dateutil-2.7.3 +pytz-2018.5 +pywavelets-1.0.0 +pyyaml-3.13 +pyzmq-17.1.2 +qt-5.9.6 +qtawesome-0.4.4 +qtconsole-4.4.1 +qtpy-1.5.0 +readline-7.0 +requests-2.19.1 +rope-0.11.0 +ruamel_yaml-0.15.46 +scikit-image-0.14.0 +scikit-learn-0.19.2 +scipy-1.1.0 +seaborn-0.9.0 +secretstorage-3.1.0 +send2trash-1.5.0 +service_identity-17.0.0 +setuptools-40.2.0 +simplegeneric-0.8.1 +singledispatch-3.4.0.3 +sip-4.19.8 +six-1.11.0 +snappy-1.1.7 +snowballstemmer-1.2.1 +sortedcollections-1.0.1 +sortedcontainers-2.0.5 +sphinx-1.7.9 +sphinxcontrib-1.0 +sphinxcontrib-websupport-1.1.0 +spyder-3.3.1 +spyder-kernels-0.2.6 +sqlalchemy-1.2.11 +sqlite-3.24.0 +statsmodels-0.9.0 +sympy-1.2 +tblib-1.3.2 +terminado-0.8.1 +testpath-0.3.1 +tk-8.6.8 +toolz-0.9.0 +tornado-5.1 +tqdm-4.26.0 +traitlets-4.3.2 +twisted-18.7.0 +unicodecsv-0.14.1 +unixodbc-2.3.7 +urllib3-1.23 +wcwidth-0.1.7 +webencodings-0.5.1 +werkzeug-0.14.1 +wheel-0.31.1 +widgetsnbextension-3.4.1 +wrapt-1.10.11 +xlrd-1.1.0 +xlsxwriter-1.1.0 +xlwt-1.3.0 +xz-5.2.4 +yaml-0.1.7 +zeromq-4.2.5 +zict-0.1.3 +zlib-1.2.11 +zope-1.0 +zope.interface-4.5.0 [scb3201@ln137%bscc-a6 ~]$ conda info active environment : base active env location : /public1/home/scb3201/anaconda3 shell level : 1 user config file : /public1/home/scb3201/.condarc populated config files : /public1/home/scb3201/.condarc conda version : 4.5.11 conda-build version : 3.15.1 python version : 3.7.0.final.0 base environment : /public1/home/scb3201/anaconda3 (writable) channel URLs : https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/linux-64 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/noarch https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/linux-64 https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/noarch https://repo.anaconda.com/pkgs/main/linux-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/free/linux-64 https://repo.anaconda.com/pkgs/free/noarch https://repo.anaconda.com/pkgs/r/linux-64 https://repo.anaconda.com/pkgs/r/noarch https://repo.anaconda.com/pkgs/pro/linux-64 https://repo.anaconda.com/pkgs/pro/noarch package cache : /public1/home/scb3201/anaconda3/pkgs /public1/home/scb3201/.conda/pkgs envs directories : /public1/home/scb3201/anaconda3/envs /public1/home/scb3201/.conda/envs platform : linux-64 user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/3.10.0-1160.118.1.el7.x86_64 centos/7 glibc/2.17 UID:GID : 4238:4238 netrc file : None offline mode : False [scb3201@ln137%bscc-a6 ~]$ curl -v https://repo.anaconda.com/pkgs/r/linux-64/repodata.json.bz2 * Could not resolve host: repo.anaconda.com * Closing connection 0 curl: (6) Could not resolve host: repo.anaconda.com [scb3201@ln137%bscc-a6 ~]$ curl -I https://repo.anaconda.com/pkgs/r/linux-64/repodata.json.bz2 curl: (6) Could not resolve host: repo.anaconda.com [scb3201@ln137%bscc-a6 ~]$ echo $http_proxy # 检查代理变量 [scb3201@ln137%bscc-a6 ~]$ ping 8.8.8.8 # 测试基础网络连通性 PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
最新发布
11-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山猫Show

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值