linux顯示git commit id,同時解決insmod模塊時版本不一致致使沒法加載問題

  linux內核默認會包含git的commit ID。html

  而linux的內核在insmod模塊時,會對模塊和內核自己的版本作嚴格的校驗。在開發產品時,改動內核後,因爲commit ID變動,會致使linux內核變動,以前已編譯發佈的模塊與升級後的linux版本不一致,必須從新編譯,很是麻煩。node

爲了解決這個問題,不少開發者經過配置make menuconfig,去掉CONFIG_LOCALVERSION_AUTO選項,從而再也不包含git commit ID。linux

  關於linux包含git commit的實現原理,能夠參考博文:git

  轉: linux內核版本本地版本號的檢查——setlocalversionubuntu

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

 

  可是git的commit ID對於版本追溯又頗有幫助,可以幫助咱們追溯到特定的內核版本,本文經過如下方法,把gitsvn

 commit ID包含到內核中,可是不屬於內核版本的一部分,從而解決上述問題。函數

  1.make menuconfig,去掉CONFIG_LOCALVERSION_AUTO選項,不自動包含git commit IDpost

  2. 修改 /scripts/setlocalverison腳本,把git commit ID導出到一個單獨的文件,命名爲git_ver.hui

#!/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]" >&2
    exit 1
}

scm_only=false
srctree=.
if test "$1" = "--save-scmversion"; then
    scm_only=true
    shift
fi
if test $# -gt 0; then
    srctree=$1
    shift
fi
if test $# -gt 0 -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 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 "+"    // 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 2>/dev/null`; then
        # Do we have an tagged version?  If so, latesttagdistance == 1
        if [ "`hg log -r . --template '{latesttagdistance}'`" == "1" ]; 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 2>/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 1
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 0; } 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", 0, NULL, &version_proc_fops); return 0; } 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 !

相關文章
相關標籤/搜索