人人一试就会,Linux核心的编译一点都不神秘

博客介绍了Linux版本,即开发Linux操作系统的公司发布的安装光盘。市场占有率较高的套件有RedHat、Mandrake、OpenLinux等,这些公司用相同Linux核心开发,本质一样,差异在于套件或新增工具,掌握一个版本,其他大多相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容


1、重新编译核心的原因


Linux的核心有几个主要的特色,除了可以随时、随个人喜好而更改之外,其版本更动次数太频繁也是一个特点,所以,除非你有特殊要求,否则一次编译成功就好了,不要随时保持最新的核心版本,也没有必要(因为核心编译需要很长的时间)。此外需要重新编译核心的情况主要有以下:


1)、新功能的需求:需要新的功能,而这个功能只有在新的核心里才有,为了获得这个功能,只好重新编译核心(例如:iptables防火墙机制只有在2.4.xx版本中才有,而新出产的AGP显卡,很多也需要推出新的核心之后才能正常有效地工作)。


2)、原来的核心太臃肿:如果你的系统稳定性要求很高,则需要重新编译核心,把不需要的许多莫名其妙的功能去掉。


3)、与硬件搭配的稳定性:原本Linux的核心主要是针对Intel的CPU做开发工作的,如果你的CPU是AMD等其他系统时,有可能让系统运行不太稳定,特别是在K6-2,K6-3 以上的Linux,通常需要重新编译核心才会解决问题。


4)、其他:在你有特殊的环境需求是,就得自行设计你的系统核心了。如一些商业的软件包系统,它们可能需要较为干净的操作系统,它们的核心需要更简洁有力。


但是需要注意的是,重新编译核心虽然可以针对你的硬件作最优化的处理,不过其对整体效能的影响可能还不到5%。因此如果为了增加效能而编译核心,基本上收益不大,但如果是针对系统的


稳定性,则是绝对必要的。


因此,可以这样说:重新编译核心的目的是为了系统的稳定性。




2、核心的编译


由于编译核心的行为只有root才可以进行,所以要求以root身份操作。


2.1设置模块的必要性。


每次新的硬件或软件的推出,就需要编译一次核心,这样做非常不方便,因为编译一次核心至少要30分钟左右(这与PC的配置有关)。目前的核心已经具有外挂模块的功能,因此,我们可以把可能用到的功能都编译成模块,那么要用的时候只需把它加载到核心就可以了。通过模块的设置,核心不会主动加载没有用到的功能,避免过多的系统资源消耗。比如在旧的计算机一般没有AGP插槽,也就不需要在核心里编译进AGP的支持了。

2.2安装原则


在Linux中安装任何软件的铁则是:注意看套件解压缩之后的INSTALL或README文件。

所以在解压了核心文件之后,先进入/usr/src/linux中先瞧瞧README,接着,执行make mrproper指令,避免前面已经有人编译过该核心。


2.3操作指令:


a、下载核心文件


# wget http://www.kernel.org > kernel/v2.4/linux-2.4.28.tar.gz

(注意:http://www.kernel.org是核心的下载主站)


b、解压缩,并放置核心到/usr/src中!


# cd /usr/src (kernel的默认source文件放置目录)

# mv linux linux.old (将旧的核心编译的目录)

# tar –zxvf /root/linux-2.4.28.tar.gz (将核心文件解压缩)

# ln –s linux-2.4.28 linux (建立链接)

# cd linux (进入工作目录)

2.4设定编译模式


目前的Kernel允许多种编译模式,包括:

1).make config:这种模式需要逐行输入需要设定的项,无法往前修改设定,所以很少使用这种方式来设定Kernel的选项。

2)make xconfig:这种模式使用 X-Window为主要环境的设定模式,除非您在执行X-Window,否则没有办法使用这种模式。

3)make menuconfig:这种模式有点象文字界面的菜单模式,较为简单,而且可以走纯文字界面下执行编辑动作,是目前最常用的模式。

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype><stroke joinstyle="miter"></stroke><formulas></formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_i1025" style="WIDTH: 342pt; HEIGHT: 215.25pt" type="#_x0000_t75"></shape><imagedata src="file:///C:DOCUME~1HOUWE~1LOCALS~1Tempmsohtml13clip_image001.jpg" o:title="1"></imagedata> <shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype><stroke joinstyle="miter"></stroke><formulas></formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f><lock v:ext="edit" aspectratio="t"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"></shapetype><stroke joinstyle="miter"></stroke><formulas></formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f><lock v:ext="edit" aspectratio="t"></lock><shape id="_x0000_i1025" style="WIDTH: 342pt; HEIGHT: 215.25pt" type="#_x0000_t75"></shape><imagedata src="file:///C:DOCUME~1HOUWE~1LOCALS~1Tempmsohtml14clip_image001.jpg" o:title="1"></imagedata>

输入指令#make menuconfig,会出现的界面如上图所示。

2.4.1开始设定

开始设定项回非常多,如果不确定选项的内容是什么,可以进入Help查看该项的设定值,如果还是不清楚就直接保留Kernel的默认值即可。我在下面的设定列出了比较重要的项。如果理解了下面的现,核心编译基本上没有很严重的困难。

# make mrproper 去除.o的已编译文件

# make menuconfig 以文字菜单模式进行编译



Code maturity level options ---> 关于核心测试功能的code开启项

[ ]Prompt for development and /or imcomplete code/drivers

因为我们不需要研发核心,所以不选



Loadable module support---> 这个就是我们所说的模块

[*]Enable loadable module support

让核心可以加载模块项(必选)

[ ]Set version information on all module symbols

除非你要使用旧版本的数据,这个通常不选

[*]Kernel module loader 也是必选

Processor type and features---> 选择CPU的型号项

(Pentium-MMX)Processor family

请依照你的实际情况选择你的CPU型号

<>Toshiba Laptop support 底下这个用到(笔记本电脑)时才选择

<>Dell laptop support

<m></m>/dev/cpu/microcode-Intel IA32 CPU microcode support

如果使用的是Intel,可以编译成模块

<m></m>/dev/cpu/*/msr-Model-specific register support

如果使用的是Intel,可以编译成模块

<m></m>/dev/cpu/*/cupid-CPU information support

如果使用的是Intel,可以编译成模块

(off)High Memory Support 这个东西跟你的RAM有关,RAM小于1GB则使用off

[ ]Math emulation 是否需要数值模拟?当然不需要,我们的CPU够强了

[ ]MTRR(Memory Type Range Register)support

P6(PII等级以上)并且要运行X-Window时才加上,与AGP有关

[ ]Symmetric multi-processing support

如果是双CPU的主机板才选择,否则不选

[*]Local APIC support on uniprocessors(NEW)

如果你的CPU支持APIC则选择,通常可以选择

[*]IO-APIC support on uniprocessors

同上

General setup---> 一般设定部分

[*]Networking support 底下4个一定要选,跟基本的硬件设备有关

[*]PCI support

(Any)PCI access mode

[*]PCI device name database

[ ]EISA support 底下这两个则与主机板上的插槽有关,除非笔记本电脑,否则不选

[ ]MCA support

[*]Support for hot-pluggable devices

有点类似即插即用,可以选择



PCI Hotplug Support--->

[*]System V IPC 下面这几个东西可以编译成模块,但是我就直接编译到核心

<*>Kernel support for a.out binaries

<*>Kernel support for ELF binaries

<*>Kernel support for MISC binary

[*]Power Management support

< >Advanced Power Managerment BIOS support

如果你想让你的BIOS接管电源管理,可以选择,我没有选

Memory Technology Devices (MTD)-à

< >Parallel port support 由于我的主机并没有接打印机,所以没选如果你有打印机,一定要选

Plug and Play configuration---> 底下两个即插即用,马上勾选

< >ISA Plug and Play support



Block devices---> 一些比较冷门的存储机器,如果不确定,可以直接编译成模块

<*>Normal PC floppy disk support

< >XT hard disk support

< >Compaq SMART2 support

< >Compaq Smart Array 5xxx support

< >Mylex DAC960/DAC1100 PCI RAID Controller support

< >Loopback device support

<m></m>Network block device support 底下这两样可以选择编译成为模块,因为有可能会用到

<m></m>RAM disk support

(4096)Default RAM disk size(NEW)



Multi-device support (RAID and LVM)-->

[ ]Multiple devices drives support(RAID and LVM)

除非要玩RAID(磁盘阵列)与LVM,否则不用选
Networking options-à 这一部分是最重要的,除非特别强调,否则尽量编成模块

<*>Packet socket

[*]Packet socket:mmapped IO

<m></m>Netlink device emulation(NEW)

[*]Network packet filtering(replaces ipchains)

[ ]Newwork packet filtering debugging(NEW)

[*]Socket Filtering

<*>Unix domain sockets

[*]TCP/IP networking

[*]IP:multicasting

[*]IP:advanced router

[*]IP:policy routing(NEW)

[*]IP:use netfilter MARK value as routing key(NEW)

[*]IP:fast network address translation(NEW)

[*]IP:equal cost multipath(NEW)

[*]IP:use TOS value as routing key(NEW)

[*]IP:verbose route monitoring(NEW)

[ ]IP:large routing tables(NEW)

[*]IP:kernel level autoconfiguration

[*]IP:DHCP support(NEW)


[*]IP:BOOTP support(NEW)

[*]IP:RARP support(NEW)

<m></m>IP:tunneling

<m></m>IP:GRE tunnels over IP

[*]IP:broadcast GRE over IP(NEW)

[*]IP:multicast routing

[*]IP:PIM-SM version 1 support(NEW)

[*]IP:PIM-SM version 2 support(NEW)

[*]IP:TCP Explicit Congestion Notification support

[*]IP:TCP syncookie support(disabled per default)


IP:Netfilter Configuration---> 这是防火墙机制中的iptables,都编成模块

<m></m>Connection tracking(required for masq/NAT)(NEW)

<m></m>FTP protocol support(NEW)

<m></m>IRC protocol support(NEW)

<m></m>IP tables support(required for filtering/masq/NAT)(NEW)

<m></m>limit match support(NEW)

<m></m>MAC address match support(NEW)

<m></m>netfilter MARK match support(NEW)

<m></m>Multiple port match support(NEW)

<m></m>TOS match support(NEW)

<m></m>AH/esp MATCH SUPPORT(NEW)

<m></m>LENGTH match support(NEW)

<m></m>TTL match support(NEW)

<m></m>tcpmass match support(NEW)

<m></m>Connection state match support(NEW)

<m></m>Packet filtering(NEW)

<m></m>REJECT target support(NEW)

<m></m>Full NAT(NEW)

<m></m>MASQUERADE target support(NEW)

<m></m>REDIRECT target support(NEW)

<m></m>Packet mangling(NEW)

<m></m>TOS target support(NEW)

<m></m>MARK target support(NEW)

<m></m>LOG target support(NEW)

<m></m>TCPMSS target support(NEW)

<m></m>ipchains(2.2-style)support(NEW)

<m></m>IPFWADM(2.0-style)support(NEW)

<m></m>The IPX protocol

[ ]IPX:Full internal IPX network(NEW)

< >Appletalk Protocol support

< >DECnet Support

< >802.1d Ethernet Bridging



Qos and/or fair queueing--->

[ ]Qos and/or fair queueing

Telephony Support---> 这东西我也不知道它的实际用途

< >Linux telephony support

ATA/IDE/MFM/RLL support---> 存储界面的选择,当然一定要选IDE。里面如果有属于你的芯片组,请记得选择它,例如如果使用VIA的芯片组,那么最后一项VIA82CXXX chipset support就一定要选择。如果你使用较老的主机板,那么就用系统默认值。

<*>ATA/IDE/MFM/RLL support

IDE,ATA and ATAPI Block devices--->

<*>Enhanced IDE/MFM/RLL disk/cdrom/tape/floppy support

<*>Include IDE/ATA-2 DISK support

<*>Use multi-mode by default

<*>Include IDE/ATAPI CDROM support

----IDE chipset support/bugfixes

[*]CMD640 chipset bugfix/support

[*]RZ1000 chipset bugfix/support

[*]Generic PCI IDE chipset support

[*]Sharing PCI IDE interrupts support

[*]Generic PCI bus-master DMA support

[*]Use PCI DMA by default when available

[*]Intel PIIXn chipsets support

[*]PIIXn Tuning support

SUSI support---> 我的旧机器并没有安装SCSI卡,我确定我不会用到SCSI,但是如果你可能会安装,请编译它

< >SCSI support

Fusion MPT device support--->

I20 device support---> 如果你的机器较新,已经支持I20的硬件检测,可以测量温度,那么吧它编进核心

< >I20 support

Network device support---> 仔细将你的网络设备编译进来,这个也很重要,如果有可能,把一些比较好的网卡芯片组编译成模块,因为,以后你可以在升级时使用

[*]Network device support

<*>Dummy net driver support这个一定要装,但是请注意,如果你之前已经使用了MRTC,这个装完之后MTRG要重运行依次

<m></m>Bonding driver support

<m></m>EQL (serial line load balancing)support

<m></m>Universal TUN/TAP device driver support

<m></m>General Instruments Surfboard 1000

Ethernet(10 or 100Mbit)----> 这里面有很多卡,我只列出我要的卡,请选择你自己的网卡

[*]Ethernet(10 or 100Mbit)

[*]EISA,VLB,PCI and on board controllers

<*>EtherExpressPro/100 support

<m></m>RealTek RTL-8139 PCI Fast Ehternet Adapter support

[*]Use PIO instead of MMIO

[*]Support for older RTL-8129/8130 boards

<m></m>VIA Rhine support 这就是传说中的D-Link 530卡的驱动程序

<*>PPP(point-to-point protocol)support

这个一定要编进核心,不然无法使用ADSL的 rp-pppoe拨号上网

[*]PPP filtering(NEW)

<m></m>PPP support for async serial orts(NEW)

<m></m>PPP support for sync tty ports(NEW)

<m></m>PPP Deflate compression(NEW)

<m></m>PPP BSD-Compress compression(NEW)

<m></m>PPP over ATM(EXPERIMENTAL)(NEW)

Amateru Radio support--->

IrDA(infrared)support--->

ISDN subsystem--->

Old CD-ROM drivers(not SCSI,not IDE)--->



Input core support--->

<m></m>Input core support

<m></m>Keyboard support

<m></m>Mouse support

(1024)Horizontal screen resolution(NEW)

(768)Vertical screen resolution(NEW)

<m></m>Joystick support

<m></m>Event interface support



Character devices--->

[*]Virtual terminal

[*]Support for console on virtual terminal

[*]Standard/generic(8250/16550 and compatible UARTs)serial support

[*]Unix98 PTY support

<m></m>Mouse Support(not serial and bus mice)

[*]PS/2 mouse(aka”auxiliary device”)support

< >/dev/agpgart(AGP Support) 这个相当重要,就是AGP显卡的支持核心,请选择(若你有AGP的话)

[ ]Intel 440LX/BX/GX and I815/I830M/I840/I850 support

只有选中上面的选项后才会出现下面的选项,请注意选择适合你主机的芯片组

[ ]Intel I810/I815/I830M(on-board)support

[ ]VIA chipset support

[ ]AMD Irongate,761,and 762 support

[ ]Generic Sis support

[ ]ALI chipset support

[ ]Serverworks LE/HE support

[ ]Direct Rendering Manager(Xfree86 DRI support)

[ ]Buld drivers for old(Xfree 4.0)DRM(NEW)

---DRM4.1 drivers

[ ]3dfx Banshee/Voodoo3+

[ ]ATI Rage 128

[ ]Intel i810

[ ]Matrox g200/g400

[ ]Sis(NEW)

[ ]Direct Rendering Manager(Xfrdd86 DRI support)

Multimedia devices--->

File systems---> 下面的选项主要提供核心支持的文件格式,相当重要。如果忘记编入,系统会读不到东西

[*]Quota support 没有他,Quota就无法启动,请选择

<m></m>Kernel automounter support

<*>Kernel automounter version 4 support (also supports v3)

<m></m>Ext3 jouranlling file system support(EXPERIMENTAL)

<m></m>DOS FAT fs support

<m></m>MSDOS fs support

<m></m>UMSDOS:Unix-like file system on top of standard MSDOS fss

<m></m>VFAT (Windows-95) fs support

<m></m>Compressed ROM file system support

[*]Virtual memory file system support(former shm fs)

<*>ISO 9660 CDROM file system support

在Red Hat7.2中,这里被编成模块,每次使用CDROM是都需要加载

[*]Microsoft Joliet CDROM extensions

<m></m>NTFS file system support(read only)

如果你想装载windows 2000或WindowsXP的NTFS文件格式的磁盘,那么这里一定要挂成模块

[*]/proc file system support

[*]/dev/pts file system for Unix98 PTYS

<*>Second extended ds support

<m></m>System V/Xenix/V7/Coherent file system support

Network File Systems---> 请注意,这里面的设定相当重要,尤其是NFS与SMB等网络文件格式

<m></m>Coda file system support(advanced network fs)

<*>NFS file system supportNFS也是需要的

<m></m>SMB file system support (to <place></place><placetype></placetype> mount <placename></placename> Windows shares etc.)

网络总是需要的

[*]Use a default NLS(NEW)

Default Remote NLS Option:”cp950”(NEW)

这个项可以改成cp950,这样可以支持中文语言

Partition Types--->

Native Language Support---> 除了下面说的几个*之外,其他的全部都编译成模块(以备不时之需)

Default NLS Option:”iso8859-1”(NEW)

<*>Traditional Chinese charset (Big5)(NEW)

<*>NLS ISO 8859-1(Latin 1;Western European Languages)(NES)

<m></m>for others

Console drivers--->

[*]VAG text console

Sound--->

< >Sound card support 你有声卡吗?如果有,务必编译它

USB support--->

<m></m>Suport for USB

Kernel hacking--->

[ ]Kernel debugging

好了,设定完之后,选择Save Configuration to an Alternate File,然后为其命名一个你喜欢的文件名,这个文件内容就是你刚刚编译完成的数据。然后选择<exit></exit>,再保存一次到系统中,接下来准备编译。

上面的资料主要使用于我自己的机器,因为我主要放在网络服务器上,因此缺少许多个人桌面型的硬件编译。无论如何,请随时以Help选项查看内容,多花一点时间重新编译一次,然后将该编译完成的参数文件保存下来,以后就可以直接将该文件调出来读入了。

2.4.2开始编译

编译成模块的数据会被系统安装在/lib/modules/`uname –r`下,另外,编译出来的Kenrnel的文件名称为vmlinuz,这个东西就是我们需要的核心文件,他的正确位置是在/boot中。此外,系统地图数据System.map也很重要,同样在/boot中。

下面是编译的步骤:

a、#make dep 建立相依属性关系。这个动作用于为你在上面所选中属于模块的部分建立相依属性,以方便未来执行软件或程序是调用模块。

b、make clean 去除旧的数据。这个动作会删除你前一次编译的目标文件(*.o),所以编译的内容会是新的核心。

c、make bzImage 开始编译核心,这个核心是经过压缩的,此外,这个步骤也很长。注意,其中的I是i的大写字母。这个步骤主要用于产生可引导的核心文件,也就是我们在启动程序中提到的vmlinuz文件。但是由于目前的核心文件较大,因此需要经过压缩,所以,编译出来的核心文件放置在如下目录中:/usr/src/linux/arch/i386/boot/bzImage。此外,还有个未经压缩的核心放在/usr/src/linux下,即/usr/src/linux/vmlinux,不过我们不使用这个文件,因此你只要记得上面提到的那个压缩过的核心文件即可。

d、make modules 开始编译模块。

e、make modules_install这个步骤用于安装模块。模块安装在我们之前提国的/lib/modules/’uname –r’目录中,因为我们的核心是2.4.28,所以安装在/lib/modules/2.4.28中。

如果在编译过程中发生问题,通常的可能是挑选的现有问题,这时请重新挑选设定内容make menuconfig,之后编译就可以成功了。接下来,请将核心放置在启动核心是的默认目录下:

cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-2.4.28

核心搬移完毕之后人,接下来做好做开机的设定,这样一个新的Linux的Kernel就完全编译安装成功了。




结束


Linux版本(distribution),就是各个开发Linux操作系统的公司发布的安装光盘。目前市场占有率较高的几个套件包括RedHat,Mandrake,OpenLinux等,这些都是开发Linux操作系统的公司。这么多公司使用的是相同的Linux核心进行套件的开发,也就是说,他们本质上都是一样的,不同的地方可能是他们使用的套间不同或者新加入的某些工具不同。基本上,他们都是Linux,所以,掌握一个版本的使用,其他版本绝大部分都是相同的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值