本文的參考網站:
在分析linux內核源碼的過程中,要是能夠修改內核源碼並運行修改後的內核,我想肯定是令人高興的事,哪怕第一次修改僅僅是在啓動時打印一行"Hello, Wang Jiankun!",肯定也是令我高興的。爲了能成功編譯修改後的內核,今天先編譯一遍內核。
爲了有一個完整的記錄,今天的起點是一臺裸機。
1、在裸機上安裝一個最小的debian系統
爲了能夠儘可能清晰的顯示編譯一個內核所需要的組件,在安裝系統時,僅僅安裝最小系統,然後需要什麼,就使用apt-get安裝什麼。
使用網絡安裝,在安裝過程中出現Software selection界面時不要選擇任何選項,這樣安裝的系統將是最小的系統。
爲了使用ssh遠程登錄,最小系統安裝完成後,要安裝ssh服務器並且要設置靜態ip地址(debian安裝過程中是通過dhcp獲取的ip地址)。
2、安裝ssh服務器
apt-get install ssh
3、設置靜態ip地址
修改文件/etc/network/interfaces,其中藍色部分是增加的,紅色部分是屏蔽掉的,黑色部分是沒有變化的。
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug eth0
allow-hotplug eth0
# Wang Jiankun commented the following line
#iface eth0 inet dhcp
#iface eth0 inet dhcp
# Wang Jiankun added the the following lines
iface eth0 inet static
address 192.168.1.251
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
iface eth0 inet static
address 192.168.1.251
netmask 255.255.255.0
broadcast 192.168.1.255
network 192.168.1.0
gateway 192.168.1.1
重啓系統後,修改將生效。
4、通過wget下載linux內核源碼
5、解壓文件linux-2.6.19.tar.bz2
administrator@wangjk:~/kernel$ tar jxf linux-2.6.19.tar.bz2
tar: bzip2: Cannot exec: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
administrator@wangjk:~/kernel$
administrator@wangjk:~/kernel$ tar jxf linux-2.6.19.tar.bz2
tar: bzip2: Cannot exec: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
administrator@wangjk:~/kernel$
看來是沒有bzip2這個包,那就安裝一個:
apt-get install bzip2
6、安裝debian的kernel-package軟件包
在安裝kernel-package軟件包時,最好使用命令:apt-get build-dep kernel-package,而不要使用命令:
apt-get install kernel-package,後者安裝的軟件包是前者的子集,使用後者安裝kernel-package軟件包後,執行make menuconfig命令會出現頭文件找不到的錯誤,如下所示:
administrator@wangjk:~/kernel/linux-2.6.19$ make menuconfig
HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c:105:23: error: sys/types.h: No such file or directory
scripts/basic/fixdep.c:106:22: error: sys/stat.h: No such file or directory
scripts/basic/fixdep.c:107:22: error: sys/mman.h: No such file or directory
scripts/basic/fixdep.c:108:20: error: unistd.h: No such file or directory
scripts/basic/fixdep.c:109:19: error: fcntl.h: No such file or directory
scripts/basic/fixdep.c:110:20: error: string.h: No such file or directory
scripts/basic/fixdep.c:111:20: error: stdlib.h: No such file or directory
scripts/basic/fixdep.c:112:19: error: stdio.h: No such file or directory
HOSTCC scripts/basic/fixdep
scripts/basic/fixdep.c:105:23: error: sys/types.h: No such file or directory
scripts/basic/fixdep.c:106:22: error: sys/stat.h: No such file or directory
scripts/basic/fixdep.c:107:22: error: sys/mman.h: No such file or directory
scripts/basic/fixdep.c:108:20: error: unistd.h: No such file or directory
scripts/basic/fixdep.c:109:19: error: fcntl.h: No such file or directory
scripts/basic/fixdep.c:110:20: error: string.h: No such file or directory
scripts/basic/fixdep.c:111:20: error: stdlib.h: No such file or directory
scripts/basic/fixdep.c:112:19: error: stdio.h: No such file or directory
主要是因爲libc6-dev軟件包沒有安裝。所以即使使用了apt-get install kernel-package還得使用apt-get build-dep kernel-package,不如一次使用apt-get build-dep kernel-package完成方便。
7、安裝libncurses5-dev軟件包來支持make menuconfig
通過apt-get build-dep kernel-package安裝完成kernel-package後,執行make menuconfig仍然報錯,如下所示:
administrator@wangjk:~/kernel/linux-2.6.19$ make menuconfig
HOSTCC scripts/kconfig/lxdialog/checklist.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:32:20: error: curses.h: No such file or directory
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:97: error: expected specifier-qualifier-list before 'chtype'
scripts/kconfig/lxdialog/dialog.h:187: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:193: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:195: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:196: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:197: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:198: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:200: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:31: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:59: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:95: error: expected ')' before '*' token
[省略其後部分]
HOSTCC scripts/kconfig/lxdialog/checklist.o
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:32:20: error: curses.h: No such file or directory
In file included from scripts/kconfig/lxdialog/checklist.c:24:
scripts/kconfig/lxdialog/dialog.h:97: error: expected specifier-qualifier-list before 'chtype'
scripts/kconfig/lxdialog/dialog.h:187: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:193: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:195: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:196: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:197: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:198: error: expected ')' before '*' token
scripts/kconfig/lxdialog/dialog.h:200: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:31: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:59: error: expected ')' before '*' token
scripts/kconfig/lxdialog/checklist.c:95: error: expected ')' before '*' token
[省略其後部分]
原來是最小系統不支持圖形的原因,安裝libncurses5-dev後即可。
8、將系統的config文件拷貝到內核目錄下
cp /boot/config-2.6.18-6-686 ./.config
9、執行make menuconfig
雖然是將原來系統的config文件拷貝過來的,但是如果所以的配置都採用默認的配置仍然會有問題,在我的系統上在加載文件系統時會死掉,所以還是要做必要的配置,主要是將scsi和sata部分編譯進內核而不要編譯成模塊,如下所示:
Device Drivers --->
Serial ATA (prod) and Parallel ATA (experimental) drivers --->
Serial ATA (prod) and Parallel ATA (experimental) drivers --->
SCSI device support --->
將藍色兩部分級聯的選項全部編譯進內核(其實沒有必要全部,但爲了簡單起見,暫時這樣做)。
10、安裝fakeroot軟件包
11、編譯內核
fakeroot make-kpkg --initrd --revision=custom.1.0 kernel_image
12、安裝內核
wangjk:/home/administrator/kernel# dpkg -i linux-image-2.6.19_custom.1.0_i386.deb
Selecting previously deselected package linux-image-2.6.19.
(Reading database ... 17679 files and directories currently installed.)
Unpacking linux-image-2.6.19 (from linux-image-2.6.19_custom.1.0_i386.deb) ...
Done.
Setting up linux-image-2.6.19 (custom.1.0) ...
Running depmod.
Finding valid ramdisk creators.
Using mkinitramfs-kpkg to build the ramdisk.
Running postinst hook script /sbin/update-grub.
You shouldn't call /sbin/update-grub. Please call /usr/sbin/update-grub instead!
(Reading database ... 17679 files and directories currently installed.)
Unpacking linux-image-2.6.19 (from linux-image-2.6.19_custom.1.0_i386.deb) ...
Done.
Setting up linux-image-2.6.19 (custom.1.0) ...
Running depmod.
Finding valid ramdisk creators.
Using mkinitramfs-kpkg to build the ramdisk.
Running postinst hook script /sbin/update-grub.
You shouldn't call /sbin/update-grub. Please call /usr/sbin/update-grub instead!
Searching for GRUB installation directory ... found: /boot/grub
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.19
Found kernel: /boot/vmlinuz-2.6.18-6-686
Updating /boot/grub/menu.lst ... done
13、重啓系統引導新內核後查看版本號
Searching for default file ... found: /boot/grub/default
Testing for an existing GRUB menu.lst file ... found: /boot/grub/menu.lst
Searching for splash image ... none found, skipping ...
Found kernel: /boot/vmlinuz-2.6.19
Found kernel: /boot/vmlinuz-2.6.18-6-686
Updating /boot/grub/menu.lst ... done
13、重啓系統引導新內核後查看版本號
administrator@wangjk:~$ cat /proc/version
Linux version 2.6.19 (root@wangjk) (gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) #1 SMP Thu May 7 21:52:10 CST 2009
administrator@wangjk:~$
Linux version 2.6.19 (root@wangjk) (gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) #1 SMP Thu May 7 21:52:10 CST 2009
administrator@wangjk:~$
可以看出已經是我編譯的內核了。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/jiankun_wang/archive/2009/05/04/4147806.aspx
------------------------------------
ubuntu不帶linux內核源碼,需要自己下載安裝。
1,查看自己的內核版本
uname -r
2,查看源內的內核源碼類表
apt-cache search linux-source
3,下載安裝內核源代碼
sudo apt-get install linux-source-2.6.27 //我選的是這一個,自己看着辦吧
4,等待........
下載完成後,在/usr/src下會有一個文件名爲linux-source-2.6.xx.tar.bz2的壓縮包
5,解壓縮包
tar jxvf linux-source-2.6.27.tar.bz2 //看清自己的版本
解壓後會生成一個源代碼目錄/usr/src/linux-source-2.6.27
6,進入源碼目錄後,配置文件
make oldconfig
7,生成內核
make
8,瘋狂等待,大約1個多小時
9,編譯模塊
make modules
10,安裝模塊
make modules_install
大功告成!^_^
下面說一下Makefile文件
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install #PWD當前工作目錄的變量
obj-m := hello.o
obj-m := hello.o
#hello.o是你要生成的驅動,以後可以自己改
KERNELDIR:=/lib/modules/2.6.27-7-generic/build
KERNELDIR:=/lib/modules/2.6.27-7-generic/build
#這裏別抄,寫成你自己的版本,這個目錄執行了內核源碼目錄
PWD:=$(shell pwd) #將當前工作目錄賦值該PWD
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
PWD:=$(shell pwd) #將當前工作目錄賦值該PWD
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
下面是一個經典的Hello,world!例子自己make一下就行了。
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world!\n");
return 0;
}
{
printk(KERN_ALERT "Hello, world!\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world!\n");
}
{
printk(KERN_ALERT"Goodbye, cruel world!\n");
}
module_init(hello_init);
module_exit(hello_exit);
module_exit(hello_exit);
-----------------------------------------------------------------------------
修改的這篇文章,自己加了幾個注意點
http://blog.theosoft.net/article.asp?id=57
第一次的Linux kernel上機內容是編譯一個內核。我用的是Ubuntu,有很多地方和其它 Linux不一樣,所以就來把我的過程寫下來,以後也好有個參照
首先當然是下載內核源代碼,如果你要最新的內核,可以去ftp.kernel.org。當然,國內速度可能會很慢。如果你是教育網用戶,可以去上海交大的鏡像站下載,地址是http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/,缺點就是沒有最新的內核(更新需要一定的時間)。
==================================================備註
如果是就是編譯ubuntu本省自帶的內核,只需要
我下載的是linux-2.6.19.2.tar.gz可以下到的最新版本了。下完之後當然是解壓縮。不過還不能直接編譯,因爲Ubuntu的默認安裝裏缺少必要的組建。打開終端,輸入一下命令:
sudo -i
apt-get install build-essential kernel-package libncurses5-dev
然後到新立得裏把所有以ncurses開頭的軟件包全部裝上,這樣就可以開始編譯內核了。
先運行以下命令,複製當前內核的配置文件。
cp /boot/config-`uname -r` ./.config
然後
make menuconfig
http://blog.theosoft.net/article.asp?id=57
第一次的Linux kernel上機內容是編譯一個內核。我用的是Ubuntu,有很多地方和其它 Linux不一樣,所以就來把我的過程寫下來,以後也好有個參照
首先當然是下載內核源代碼,如果你要最新的內核,可以去ftp.kernel.org。當然,國內速度可能會很慢。如果你是教育網用戶,可以去上海交大的鏡像站下載,地址是http://ftp.sjtu.edu.cn/sites/ftp.kernel.org/,缺點就是沒有最新的內核(更新需要一定的時間)。
==================================================備註
如果是就是編譯ubuntu本省自帶的內核,只需要
新立得搜索linux souce,下載帶ubuntupatch的kernel的source code。
sudo apt-get source linux-source-2.6.27
==================================================備註我下載的是linux-2.6.19.2.tar.gz可以下到的最新版本了。下完之後當然是解壓縮。不過還不能直接編譯,因爲Ubuntu的默認安裝裏缺少必要的組建。打開終端,輸入一下命令:
sudo -i
apt-get install build-essential kernel-package libncurses5-dev
然後到新立得裏把所有以ncurses開頭的軟件包全部裝上,這樣就可以開始編譯內核了。
先運行以下命令,複製當前內核的配置文件。
cp /boot/config-`uname -r` ./.config
然後
make menuconfig
選擇 "Load an Alternate Configuration File",再選擇你剛纔拷貝過來的.config文件作爲配置文件。然後確定。當結束後,你選擇退出時,會提示問你 "Do you wish to save your new kernel configuration?"選擇yes即可。
接下來就要編譯了。輸入命令:
make
你也可以將編譯任務分成2個線程來執行,只要輸入:
make -j2
編譯一般需要1~1.5小時左右,就看cpu的性能如何
make
你也可以將編譯任務分成2個線程來執行,只要輸入:
make -j2
編譯一般需要1~1.5小時左右,就看cpu的性能如何
編譯完成後,開始安裝:
make module_install
make install
然後添加引導信息,不過之前還是要裝一個組件initramfs-tools,裝完以後輸入:
make module_install
make install
然後添加引導信息,不過之前還是要裝一個組件initramfs-tools,裝完以後輸入:
mkinitramfs -o /boot/initrd.img-2.X.XX
/lib/modules/2.X.XX
==================================================備註
後面的參數不要忘記了,否則啓動新內核會出現錯誤:
WARNING: Couldn’t open directory /lib/modules/2.6.15.7-ubuntu1: No such file or directory
FATAL: Could not open /lib/modules/2.6.15.7-ubuntu1/modules.dep.temp for writing: No such file or directory
FATAL: Could not open /lib/modules/2.6.15.7-ubuntu1/modules.dep.temp for writing: No such file or directory
==================================================備註
最後打開 /boot/grub/menu.lst
在 ## ## End Default Options ## 下面添加類似下面的兩段
title Ubuntu, kernel 2.6.19.2
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6
initrd /initrd.img-2.6.19.2
savedefault
boot
title Ubuntu, kernel 2.6.19.2 (recovery mode)
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6 ro single
initrd /initrd.img-2.6.19.2
boot
注意 root和kernel字段要模仿menu.lst下面已有的內容寫。下面是 (hd0,4),那麼你也寫(hd0,4),下面寫root=/dev/hdd6,你也寫root=/dev/hdd6,只是內核的版本號改爲現在編譯的版本號。然後重新啓動計算機,在GRUB中選擇新內核啓動。如果啓動失敗,你可以重啓選擇老內核。
在 ## ## End Default Options ## 下面添加類似下面的兩段
title Ubuntu, kernel 2.6.19.2
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6
initrd /initrd.img-2.6.19.2
savedefault
boot
title Ubuntu, kernel 2.6.19.2 (recovery mode)
root (hd0,4)
kernel /vmlinuz-2.6.19.2 root=/dev/hdd6 ro single
initrd /initrd.img-2.6.19.2
boot
注意 root和kernel字段要模仿menu.lst下面已有的內容寫。下面是 (hd0,4),那麼你也寫(hd0,4),下面寫root=/dev/hdd6,你也寫root=/dev/hdd6,只是內核的版本號改爲現在編譯的版本號。然後重新啓動計算機,在GRUB中選擇新內核啓動。如果啓動失敗,你可以重啓選擇老內核。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/unikingest/archive/2009/03/10/3977747.aspx