編譯和安裝linux核心2.6.24,並且寫一個簡單的核心驅動測試之

來源:互聯網
上載者:User

好久沒有編譯過核心了,最近要弄驅動,查看了一下在/usr/src下面有兩個檔案夾,裡面都是空的,沒有任何核心原始碼。
我用的是CentOS 5.1,其實紅帽系列在redora出現之後就一直沒有附帶linux核心原始碼了,所以這些個標頭檔什麼的在你安裝好系統那刻起是都不會有的了。
當然了,不管是什麼發行版本的linux系統,下面方法都應該適用。
自己從新安裝一個就是,現下載:
www.kernel.org下載,

下載後解壓到/usr/src下:
bzip2 -dc linux-2.6.24.4.tar.bz2 | tar xvf -
把檔案夾linux-2.6.24.4改名字為:linux-kernel

隨便在哪個檔案夾下面,編寫一個shell檔案名稱叫a.sh,內容如下:
   #! /bin/bash
   mkdir  -p /home/name/build/kernel
   cd /usr/src/linux-kernel
   make mrproper
   make O=/home/name/build/kernel menuconfig
   make O=/home/name/build/kernel
   sudo make O=/home/name/build/kernel modules_install install

然後開啟一個終端執行
bash  a.sh
shell檔案裡面O=/home/name/build/kernel表示編譯配置在=號後面的檔案夾裡面進行,中間可以自己查看一下。
後面的過程除了在配置核心的時候,都可以不管了。
這個過程會自動產生新的的核心啟動映象,並且自動複製到/boot目錄下面去,不用手動複製了。

之後修改grub.conf檔案,讓以後的系統用新的的核心啟動:
vim /etc/grub.conf
內容如下:
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You do not have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /, eg.
#          root (hd0,6)
#          kernel /boot/vmlinuz-version ro root=/dev/sda7
#          initrd /boot/initrd-version.img
#boot=/dev/sda
default=2
timeout=5
splashimage=(hd0,6)/boot/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.24.4)
    root (hd0,6)
    kernel /boot/vmlinuz-2.6.24.4 ro root=LABEL=/ rhgb quiet
    initrd /boot/initrd-2.6.24.4.img
title CentOS (2.6.18-53.el5)
    root (hd0,6)
    kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
    initrd /boot/initrd-2.6.18-53.el5.img
title Windows XP
    rootnoverify (hd0,0)
    chainloader +1

把這一部分注釋掉:
title CentOS (2.6.18-53.el5)
    root (hd0,6)
    kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
    initrd /boot/initrd-2.6.18-53.el5.img
修改為
#title CentOS (2.6.18-53.el5)
#    root (hd0,6)
#    kernel /boot/vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
#   initrd /boot/initrd-2.6.18-53.el5.img

再修改default = 0,這裡0對應第一個title,下面一次類推
重啟就可以了。

編譯核心的輸出檔案太大了,就是開始的/home/name/build/kernel,把這個檔案夾可以刪除了最後。要寫驅動的話,不要刪除。

寫一個最簡單的驅動程式:hello.c

/*======================================================================

    A simple kernel module: "hello world"     


======================================================================*/

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)

{

  printk(KERN_ALERT " Hello World enter ");

  return 0;

}



static void hello_exit(void)

{

  printk(KERN_ALERT " Hello World exit  ");

}



module_init(hello_init);

module_exit(hello_exit);



MODULE_AUTHOR("ztz0223");

MODULE_DESCRIPTION("A simple Hello World Module");

MODULE_ALIAS("a simplest module");

然後寫一個Makefile

如下:

PWD = $(shell pwd)
KERNEL_SRC = /usr/src/linux-2.6.24.4/

obj-m := hello.o
module-objs := hello.o

all:
    $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules
   
clean:
    rm *.ko
    rm *.o

開啟終端進入到hello.c路徑下make,2.6的核心好像不支援用gcc直接編譯了,要用make,如下:

[root@BTazuo azuo]# cd hello  //進入驅動程式的路徑,此部分是注釋
[root@BTazuo hello]# dir
hello.c  Makefile
[root@BTazuo hello]# make    //編譯
make -C /lib/modules/2.6.24.4/build M=/azuo/hello  modules
make[1]: Entering directory `/usr/src/linux-2.6.24.4'
  CC [M]  /azuo/hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /azuo/hello/hello.mod.o
  LD [M]  /azuo/hello/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.24.4'
[root@BTazuo hello]# dir //編譯成功
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  Module.symvers
[root@BTazuo hello]#


載入和卸載驅動:

[root@BTazuo hello]# insmod ./hello.ko
[root@BTazuo hello]# rmmod ./hello.ko

開啟/var/log/messages檔案可以看到,最後有核心載入和卸載的資訊:

 Hello World enter
 Hello World exit
表示核心載入和卸載成功!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.