歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> x86平台下Linux配置編譯與啟動

x86平台下Linux配置編譯與啟動

日期:2017/3/1 10:44:27   编辑:Linux編程

重新在x86平台上面配置編譯了一下linux內核,並且設置啟動。所有的工作是在虛擬機下面進行的。

下面首先談談理論上編譯配置內核需要用哪些步驟:

1、首先肯定是在www.linux.org上面下載自己需要的linux內核版本啊。建議到官方下載,不要下載經過別人裁剪過的內核。

2、解壓縮。下載的時候可以下載經過gzip壓縮的,也可以選擇經過bzip2壓縮的。經過gzip壓縮的用:tar -xvz -f 來解壓縮;經過bzip壓縮的用:tar -xvj -f來解壓縮。

3、清理。下載後,最好清理下 。這裡介紹三個清理命令:

make clean :remove all generate files but config files;

make mrproper:remove all generate files include config files;

make distclean:mrproper and remove editor backup and patch files

4、配置。配置,有四個配置命令,下面分別說明:

make config:交互式文本配置。

make menuconfig:菜單式配置。一般選取這個配置。

make xconfig:只能在有xwindow的情況下配置,在純粹命令行界面的時候不能夠使用,而上面兩個配置能夠使用。

一般配置選項有很多,使用make menuconfig後,會在內核的根目錄下面生成.config的配置文件。我們可以選取一個已經配置好的.config文件,修改這個文件即可。

5、編譯內核。編譯內核的命令有:

make zImage :在x86平台下,只能夠編譯生成小於512K的內核;

make bzImage:沒有大小的限制。

6、編譯內核模塊。編譯內核模塊的命令:

make modules

之所以要編譯內核模塊,是因為在配置選項裡面的那些選擇“M”的,只編譯,沒有連接;而“*”既要編譯也要連接,最後生成zImage。都是以內核模塊的形式加載到內核的。

7、安裝內核模塊。安裝內核模塊命令:

make modules_install

8、制作init ramdisk。制作ramdisk的命令:

mkinitrd initrd-$Version $Version 其中第二個$Version為內核的實際版本。

initramdisk作用:提供一種讓內核可以簡單實用ramdisk的能力。這些能力包括:格式化一個ramdisk、加載文件系統到ramdisk、將ramdisk作為根文件系統

9、安裝內核。

由於Linux系統啟動時候,會從/boot目錄下來尋找文件與init ramdisk,所以需要將編譯好的內核和init ramdisk復制到/boot目錄下;

為了讓grub在啟動時能提供一項我們自己制作的linux內核的選擇項,需要修改grub配置文件。

下面列出自己編譯和配置內核的步驟:
1、創建目錄,存放內核源碼:

  1. [root@localhost /]# mkdir linux_kernel
2、進入該目錄,解壓下載的linux內核源碼:
  1. [root@localhost linux_kernel]# ls
  2. linux-2.6.29.tar.gz
  3. [root@localhost linux_kernel]# tar -xvz -f linux-2.6.29.tar.gz
3、進入內核源碼根目錄,清理痕跡:
  1. [root@localhost linux_kernel]# cd linux-2.6.29
  2. [root@localhost linux-2.6.29]# make distclean
4、配置內核。這裡使用正在運行的Fedora9的配置文件作為此內核的配置文件:
  1. [root@localhost linux-2.6.29]# cp /boot/config-2.6.25-14.fc9.i686 .config
  2. [root@localhost linux-2.6.29]# make menuconfig

5、按照配置文件的說明手冊對文件進行配置。

6、編譯內核。編譯完成後,生成的內核位於:/arch/x86/boot目錄下。

  1. [root@localhost linux-2.6.29]# make bzImage
7、編譯好內核後,就可以編譯模塊了。
  1. [root@localhost linux-2.6.29]# make modules
8、編譯好模塊後,就可以安裝模塊了。
  1. [root@localhost linux-2.6.29]# make modules_install
完成安裝後,編譯好的內核模塊會從內核源代碼目錄拷貝至/lib/modules下面。

9、制作 init ramdisk。

  1. [root@localhost linux_kernel]# mkinitrd initrd-2.6.29 2.6.29
10、拷貝內核和制作好的init ramdisk到/boot目錄下面。
  1. [root@localhost linux_kernel]# cp /linux_kernel/linux-2.6.29/arch/x86/boot/bzImage /boot/vmlinuz-2.6.29
  2. [root@localhost linux_kernel]# cp /linux_kernel/initrd-2.6.29 /boot/
注意:vmlinuz-2.6.29可以任意取名,只是下面修改grub的時候,注意名字要一致就行。

11、修改grub 啟動項。

  1. [root@localhost linux_kernel]# vi /etc/grub.conf
在裡面添加:
  1. title xgg's Linux(2.6.29)
  2. root (hd0,0)
  3. kernel /vmlinuz-2.6.29 ro root=UUID=3f0b3cdc-c7e6-4649-a214-124f469262f4 rhgb quiet
  4. initrd /initrd-2.6.29
到此為止,就已經完成了linux內核的編譯、配置了,很簡單的。下次啟動的時候就會出現雙啟動項。
Copyright © Linux教程網 All Rights Reserved