linux内核默认会包含git的commit ID。

  而linux的内核在insmod模块时,会对模块和内核本身的版本做严格的校验。在开发产品时,改动内核后,由于commit ID变更,会导致linux内核变更,之前已编译发布的模块与升级后的linux版本不一致,必须重新编译,非常麻烦。

为了解决这个问题,很多开发者通过配置make menuconfig,去掉CONFIG_LOCALVERSION_AUTO选项,从而不再包含git commit ID。

  关于linux包含git commit的实现原理,可以参考博文:

  转: linux内核版本本地版本号的检查——setlocalversion

  http://www.cnblogs.com/liuwanpeng/p/6148572.html

  但是git的commit ID对于版本追溯又很有帮助,能够帮助我们追溯到特定的内核版本,本文通过以下方法,把git

 commit ID包含到内核中,但是不属于内核版本的一部分,从而解决上述问题。

  1.make menuconfig,去掉CONFIG_LOCALVERSION_AUTO选项,不自动包含git commit ID

  2. 修改 /scripts/setlocalverison脚本,把git commit ID导出到一个单独的文件,命名为git_ver.h

#!/bin/sh
#
# This scripts adds local version information from the version
# control systems git, mercurial (hg) and subversion (svn).
#
# If something goes wrong, send a mail the kernel build mailinglist
# (see MAINTAINERS) and CC Nico Schottelius
# <nico-linuxsetlocalversion -at- schottelius.org>.
#
# usage() {
echo "Usage: $0 [--save-scmversion] [srctree]" >&
exit
} scm_only=false
srctree=.
if test "$1" = "--save-scmversion"; then
scm_only=true
shift
fi
if test $# -gt ; then
srctree=$
shift
fi
if test $# -gt -o ! -d "$srctree"; then
usage
fi scm_version()
{
local short
short=false cd "$srctree"
if test -e .scmversion; then
cat .scmversion
return
fi
if test "$1" = "--short"; then
short=true
fi # Check for git and a git repo.
if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
head=`git rev-parse --verify --short HEAD >/dev/null`; then # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
# it, because this version is defined in the top level Makefile.
if [ -z "`git describe --exact-match 2>/dev/null`" ]; then # If only the short version is requested, don't bother
# running further git commands
if $short; then
#echo "+" // modify by *,不要+号了,没啥意义
return
fi
# If we are past a tagged commit (like
# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
if atag="`git describe 2>/dev/null`"; then
echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),$(NF))}' # If we don't have a tag at all we print -g{commitish}.
else
#printf '%s%s' -g $head // modify by *
printf '%s%s' $head
fi
fi # Is this git on svn?
if git config --get svn-remote.svn.url >/dev/null; then
printf -- '-svn%s' "`git svn find-rev $head`"
fi # Check for uncommitted changes
if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then
printf '%s' -dirty
fi # All done with git
return
fi # Check for mercurial and a mercurial repo.
if test -d .hg && hgid=`hg id >/dev/null`; then
# Do we have an tagged version? If so, latesttagdistance ==
if [ "`hg log -r . --template '{latesttagdistance}'`" == "" ]; then
id=`hg log -r . --template '{latesttag}'`
printf '%s%s' -hg "$id"
else
tag=`printf '%s' "$hgid" | cut -d' ' -f2`
if [ -z "$tag" -o "$tag" = tip ]; then
id=`printf '%s' "$hgid" | sed 's/[+ ].*//'`
printf '%s%s' -hg "$id"
fi
fi # Are there uncommitted changes?
# These are represented by + after the changeset id.
case "$hgid" in
*+|*+\ *) printf '%s' -dirty ;;
esac # All done with mercurial
return
fi # Check for svn and a svn repo.
if rev=`LANG= LC_ALL= LC_MESSAGES=C svn info >/dev/null | grep '^Last Changed Rev'`; then
rev=`echo $rev | awk '{print $NF}'`
printf -- '-svn%s' "$rev" # All done with svn
return
fi
} collect_files()
{
local file res for file; do
case "$file" in
*\~*)
continue
;;
esac
if test -e "$file"; then
res="$res$(cat "$file")"
fi
done
echo "$res"
} if $scm_only; then
if test ! -e .scmversion; then
res=$(scm_version)
echo "$res" >.scmversion
fi
exit
fi if test -e include/config/auto.conf; then
. include/config/auto.conf
else
echo "Error: kernelrelease not valid - run 'make prepare' to update it"
exit
fi # localversion* files in the build and source directory
res="$(collect_files localversion*)"
if test ! "$srctree" -ef .; then
res="$res$(collect_files "$srctree"/localversion*)"
fi # 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 # add by *,save git commit id to ./include/generated/git_ver.h
rm -rf "$srctree"/include/generated/git_ver.h
GIT_VER_INC_PATH="$srctree/include/generated/git_ver.h"  // 将git commit ID导出到git_ver.h中
echo "/*" >> $GIT_VER_INC_PATH
echo "* auto generate. Don't edit!" >> $GIT_VER_INC_PATH
echo "*/" >> $GIT_VER_INC_PATH
echo "#ifndef __GIT_VERSION_H__" >> $GIT_VER_INC_PATH
echo >> $GIT_VER_INC_PATH
echo "#define __GIT_VERSION_H__" >> $GIT_VER_INC_PATH
echo >> $GIT_VER_INC_PATH
echo "#define GIT_COMMIT_ID_STR \"$(scm_version)\" ">> $GIT_VER_INC_PATH
echo >> $GIT_VER_INC_PATH
echo "#endif" >> $GIT_VER_INC_PATH echo "$res"

上述代码红色部分是修改的地方,编译linux内核以后,自动生成的git_ver.h如下:

/*
* auto generate. Don't edit!
*/
#ifndef __GIT_VERSION_H__ #define __GIT_VERSION_H__ #define GIT_COMMIT_ID_STR "afb275c"    // 可以通过git show afb275c,方便的查询这个版本的log #endif

  

3. 从git_ver.h中获取git commit ID,并打印,且存放到到 /proc/version文件系统中

linux启动时,start_kernel()函数会打印版本号

init/main.c:

asmlinkage void __init start_kernel(void)
{
......
boot_cpu_init();
page_address_init();
pr_notice("%s", linux_banner);
......
}
linux_banner的定义:
init/version.c:

/* FIXED STRINGS! Don't touch! */
const char linux_banner[] =
"Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\nGit commit id: " GIT_COMMIT_ID_STR "\n";  // 再次全局变量中增加git commit ID用于显示 const char linux_proc_banner[] = 
"%s version %s"
" (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
" (" LINUX_COMPILER ") %s\n""Git commit id: " GIT_COMMIT_ID_STR"\n";

除了初始化打印,/proc/version文件系统中也提供版本相关的信息,正好用到linux_proc_banner变量,也添加上git commit ID

fs/proc/version.c:

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/utsname.h> static int version_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, linux_proc_banner,
utsname()->sysname,
utsname()->release,
utsname()->version);
return ;
} static int version_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, version_proc_show, NULL);
} static const struct file_operations version_proc_fops = {
.open = version_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
}; static int __init proc_version_init(void)
{
proc_create("version", , NULL, &version_proc_fops);
return ;
}
fs_initcall(proc_version_init);

4.修改后的结果

(1)串口打印:

Starting kernel ...

machid 0, r2(bi_boot_params): 16ffa000
gd 1fb8df38, bd 1fb8dfe0 bi_arch_number: 0
kernel entry 00008000
Booting Linux on physical CPU 0x0
Linux version 3.14.0-xilinx (****@ubuntu) (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-23) ) #8 SMP PREEMPT Mon Dec 12 11:28:17 CST 2016
Git commit id: afb275c

(2)/proc/version

zynq> cat /proc/version
Linux version 3.14.0-xilinx (liuwanpeng@ubuntu) (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-23) ) #8 SMP PREEMPT Mon Dec 12 11:28:17 CST 2016
Git commit id: afb275c

OK,job‘s done !

linux显示git commit id,同时解决insmod模块时版本不一致导致无法加载问题的更多相关文章

  1. django 解决css,js文件304导致无法加载显示问题

    这种情况一般会在windows系统下出现 1.前台.后台如果无法加载css等样式.(建议通过此办法来解决) 这是因为你安装的某些IDE 或者其他更改了注册表导致的系统的注册表\HKEY_CLASSES ...

  2. 解决Github使用Fastly CDN而导致不能加载网页的方法 转自 沙丘:http://www.enkoo.net/fastly-cdn-in-gifhub.html

    Github现在基本属于“安全”网站,但Github使用fastly.net的CDN服务后,其网站在国内经常不能正常加载网页.github.global.ssl.fastly.net的亚洲IP一般为1 ...

  3. 解决Github使用Fastly CDN而导致不能加载网页的方法

    Github现在基本属于“安全”网站,但 Github使用fastly.net的CDN服务后,其网站在国内经常不能正常加载网页.github.global.ssl.fastly.net的亚洲IP一般为 ...

  4. 解决redis集群版本不一致导致RDB同步失败的问题

    某天,运维反馈某两个机房的出口流量和入口流量过大,并且持续了好一段时间. 再仔细排查后发现是 redis 集群的几台服流量问题,于是开始查日志. 在日志中发现出现大量的 Can't handle RD ...

  5. Linux显示用户的ID

    Linux显示用户的ID youhaidong@youhaidong-ThinkPad-Edge-E545:~$ id uid=1000(youhaidong) gid=1000(youhaidong ...

  6. Adobe Edge Animate –解决图形边缘精确检测问题-通过jquery加载svg图片

    Adobe Edge Animate –解决图形边缘精确检测问题-通过jquery加载svg图片 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在edge ...

  7. git commit报错解决,绕过代码检查

    上一个项目用的svn,新项目用了git,很开心,终于学习了git了,本以为把git都学会了,但是还是遇到了一个不在自己学习的知识点范围内的问题,最后是同事帮忙解决的. 问题:第一次代码commit的时 ...

  8. 事件ID:7026(“下列引导或系统启动驱动程序无法加载: cdrom”)的解决方法

     电脑没有安装光驱,而是使用USB光驱/虚拟光驱软件,每次开机后"事件查看器"都显示错误:"下列引导或系统启动驱动程序无法加载: cdrom"(事件ID:7 ...

  9. 解决Javascript大数据列表引起的网页加载慢/卡死问题。

    在一些网页应用中,有时会碰到一个超级巨大的列表,成千上万行,这时大部份浏览器解析起来就非常痛苦了(有可能直接卡死). 也许你们会说可以分页或动态加载啊?但是有可能需求不允许分页,动态加载?网络的延迟也 ...

随机推荐

  1. js获取网页高度

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  2. jquery数组删除指定元素的方法:grep()

    jquery数组删除指定元素的方法:grep() 金刚 数组 jquery javascript 元素 遇到的问题 今天遇到一个问题,删除数组中的一个指定元素,并返回新的数组. 我定义的js数组是这样 ...

  3. linux 学习随笔-shell简单编写

    脚本最好都放在/usr/local/sbin中 脚本的执行 sh -x 脚本.sh -x可以查看执行过程 1在脚本中使用变量 使用变量的时候,需要使用$符号:  #!/bin/bash  ##把命令赋 ...

  4. #研发解决方案介绍#基于ES的搜索+筛选+排序解决方案

    郑昀 基于胡耀华和王超的设计文档 最后更新于2014/12/3 关键词:ElasticSearch.Lucene.solr.搜索.facet.高可用.可伸缩.mongodb.SearchHub.商品中 ...

  5. iPhone被盗后怎么?这篇文章只办针对iOS7后的系统

    中午准备去吃饭的时候,今天看到Tungbaby的手机被盗后怎么做?http://www.jianshu.com/p/f13f49cd9b90 碰巧我的手机也被盗了.就来分享下我的经验吧.由于我当时是在 ...

  6. shell实现SSH自动登陆

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  7. 为什么我还不推荐内存中OLTP给用户

    嗯,有些人在看玩这篇文章后会恨我,但我还是要说.1个月来我在内存中OLTP这个里领域里做了大量的工作,很多用户都请求使用这个惊艳的新技术.遗憾的是,关于内存中OLTP没有一个是真的令人激动的——看完你 ...

  8. 【转】深入理解 Java 垃圾回收机制

    深入理解 Java 垃圾回收机制   一.垃圾回收机制的意义 Java语言中一个显著的特点就是引入了垃圾回收机制,使c++程序员最头疼的内存管理的问题迎刃而解,它使得Java程序员在编写程序的时候不再 ...

  9. java int与integer的区别

    int与integer的区别从大的方面来说就是基本数据类型与其包装类的区别: int 是基本类型,直接存数值,而integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型 ...

  10. 【java开发】数据类型

    ok,为期两天的ubuntu常用命令学习结束,现在开始java语言的学习. 上篇结尾说了ubuntu下的jdk文件安装,现在顺便说一下win下的jdk环境变量配置 在官网下载符合系统的jdk文件,可以 ...