打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Linux kernel编译生成的版本多一个加号“+”

其实,一直以来,我们编译KVM(Linux kernel)生成的RPM包中的kernel版本总是带有一个“莫名其妙”的加号(+),其实我知道大概是因为我们修改了Linux.git(或kvm.git)中的一些文件。但是我们只是修改了一下Makefile,让我们做RPM包是方便而已,一般我也没有在编译时修改其他的源代码文件,所以我想把这个加号去掉,对其进行了简单的研究,问题已经搞定了,记录如下吧。

kernel版本出现一个加号(plug sign)的原因可能是如下两点,当然前提是使用Linux的GIT repository,且CONFIG_LOCALVERSION_AUTO和LOCALVERSION都没有设置。
(1)如果当前repository的commit ID不是某一个tag,则默认有一个加号。因为在最上层的Makefile中只有该repository中最近一次tag的版本信息,需要用加号(+)来标识它并非一个tag(如:3.5.0)。
(2)如果当前repository的commit ID刚好是一个tag,且其中有GIT管理下的文件被改动,这默认有一个加号(+)。
如果想避免这个烦人的加号,可以将scripts/setlocalversion脚本中带有’ echo “+” ‘和’ res=”$res${scm:++}” ‘的这两行删掉即可。
To avoid the additional plus sign in kernel release version, just remove ‘ echo “+” ‘ line and ‘ res=”$res${scm:++}” ‘ line in scripts/setlocalversion file.

首先,Linux的顶层的Makefile关于版本的信息,以及对”make kernelrelease”的定义如下:

1234567891011121314
VERSION = 3PATCHLEVEL = 5SUBLEVEL = 0EXTRAVERSION =NAME = Saber-toothed Squirrel#..............        @echo  '  kernelrelease   - Output the release version string'        @echo  '  kernelversion   - Output the version stored in Makefile'#..............kernelrelease:        @echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))" kernelversion:        @echo $(KERNELVERSION)

然后,我们执行“make kernelrelease”则会生成kernel发布的版本信息,如下:

12345
[root@jay-linux linux.git]# make kernelrelease3.5.0[root@jay-linux linux.git]# vi mm/oom_kill.c[root@jay-linux linux.git]# make kernelrelease3.5.0+

注意,这个加号其实是作为本地的版本号加进去的,详见scripts/setlocalversion这个脚本,有如下的部分重要内容。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
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 -d .git && head=`git rev-parse --verify --short HEAD 2>/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 "+"                                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                        fi                fi         if git diff-index --name-only HEAD | grep -qv "^scripts/package"; then                        printf '%s' -dirty                fi                 # All done with git                return        fi#................}#................# CONFIG_LOCALVERSION and LOCALVERSION (if set)res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}" # scm version string if not at a tagged commitif 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:++}"        fifiecho "$res"

如下的一些信息可以方便你理解上面的这段脚本。

123456789101112131415
[root@jay-linux linux.git]# git rev-parse --verify --short HEAD28a33cb[root@jay-linux linux.git]# git describe --exact-match 2>/dev/nullv3.5[root@jay-linux linux.git]# git diff-index --name-only HEADmm/oom_kill.c[root@jay-linux linux.git]# man git     #........... git-rev-parse(1)           Pick out and massage parameters. git-describe(1)           Show the most recent tag that is reachable from a commit. git-diff-index(1)           Compares content and mode of blobs between the index and repository.    #............

另外,对于setlocalversion中的一个语法解释一下:
${var:-value1} 在变量var不为空时,保持var原有的值不变;如果var变量未设置或者为空,这表达式结果为value1,但是变量var的值并不改变(未设置或为空)。
${var:+value1} 在变量var不为空时,表达式结果为value1;如果var变量未设置或者为空,这表达式结果为空。${var+value1}的效果一样。
${var:=value1} 在变量var不为空时,保持var原有的值不变;如果var变量未设置或者为空,这表达式结果为value1,变量var也被赋值为value1。
${var:?value1} 在变量var未设置或为空时,脚本会退出并抛出一个错误信息(包含value1)。

另外,如下一篇博客也研究了这个问题,供大家参考。

http://blog.csdn.net/adaptiver/article/details/7225980

标签: , ,
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
向linux内核版本号添加字符/为何有时会自动添加“ ”号
基于jenkins结合git实现web程序的多服务器批量发布
git 自动生成tag版本脚本
基于Linux的环境变量的定制
Linux中的脚本编程
系统管理中 bash shell 脚本常用方法总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服