天天看點

Linux驅動程式學習(一)

從驅動程式編譯開始,驅動程式編譯需要核心源碼支援,是以第一個問題就是下載下傳源碼

下載下傳源碼需要先知道自己的核心版本,

[[email protected] /]# uname -r

3.10.0-862.el7.x86_64

首先找到www.kernel.org發現并沒有所需要的核心版本

官方解釋說看到在-後面有任何東西,表示你現在正在運作一個發行版本(distribute kernel),要核心請找發行這個版本的人。

我的系統是Centos,查了下是http://vault.centos.org/

檢視自己系統版本

[[email protected] /]# cat /proc/version 

Linux version 3.10.0-862.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018

然後下載下傳并相應版本下的rpm包

[[email protected] ~]# rpm -ivh kernel-3.10.0-123.el7.src.rpm

這時會産生目錄/root/rpmbuild/SPECS和/root/rpmbuild/SOURCES。

利用rpmbuild指令安裝,發現系統并沒有帶rpmbuild,是以安裝rpmbuild。

以為是yum install rpmbuild, 沒找到。。。是以知道挂載安裝盤找到相應的rpm包安裝。結果在挂載/dev/cdrom時卡死。。。無法umount

umount: /var/www/html/centos: device is busy.

        (In some cases useful info about processes that use

         the device is found by lsof(8) or fuser(1))

結果又是查查查。。。

fuser /mnt

傳回相應的程序号,

kill -9 程序号

成功挂載後安裝,相應的依賴包用yum安裝,最後發現其實不叫rpmbuild。。叫rpm-build。。。。是以yum裝就可以了。。。

進入SPECS目錄

[[email protected] SPECS]# rpmbuild -bp --target=$(uname -m) kernel.spec 

Building target platforms: x86_64

Building for target x86_64

error: Failed build dependencies:

        pesign >= 0.109-4 is needed by kernel-3.10.0-123.el7.x86_64

        elfutils-devel is needed by kernel-3.10.0-123.el7.x86_64

        zlib-devel is needed by kernel-3.10.0-123.el7.x86_64

        binutils-devel is needed by kernel-3.10.0-123.el7.x86_64

        newt-devel is needed by kernel-3.10.0-123.el7.x86_64

        python-devel is needed by kernel-3.10.0-123.el7.x86_64

        perl(ExtUtils::Embed) is needed by kernel-3.10.0-123.el7.x86_64

        bison is needed by kernel-3.10.0-123.el7.x86_64

        audit-libs-devel is needed by kernel-3.10.0-123.el7.x86_64

        numactl-devel is needed by kernel-3.10.0-123.el7.x86_64

        pciutils-devel is needed by kernel-3.10.0-123.el7.x86_64

缺啥yum啥

成功解析出的核心源碼在/root/rpmbuild/BUILD中,把它放到/usr/src/kernel/裡

新解析出的核心源碼有一個configs檔案,其實還有一個.config檔案,因為是隐藏的是以之前沒有發現,還以為make oldconfig是使用了configs裡的檔案,其實用的是.cofnig。。。

接着開始編譯

make(經曆一段漫長的等待。。。)

編譯核心鏡像

make bzImage

Kernel: arch/x86/boot/bzImage is ready  (#2)

安裝核心子產品

make modules_install

執行結束之後,會在/lib/modules下生成新的目錄/lib/modules/3.13.0-48-generic/, 下面的build檔案就是編譯子產品的要用到的檔案。至此核心編譯完成。 

繼續閱讀