2.6 编译linux

2007-05-30 09:48:00 leopard_ray 阅读数 992

实战内核编译linux 2.4 and linux 2.6


网上看了linux2.4和2.6的内核编译,写的要么很复杂,要么无法编译通过。有的就是没有介绍全部的步骤。自己实际编译了一次2.4和2.6的内核,并且记录下来。不用重复N次Google了:)
Linux-2.4-x内核编译步骤 

#make mrproper
[说明]清除所有的配置信息,如.config等。

#make clean
[说明]清除所有的*.o文件。
[注]如果已经配置并且编译过了内核源码,只是修改了一些内核代码,那么1,2步可以跳过。

#make dep
[说明]读取配置过程生成的配置文件,创建对应于配置的依赖关系树,从而决定哪些需要编译,哪些不需要。

#make bzImage (或#make zImage)
[说明]完全编译内核。二者生成的内核都是使用gzip格式压缩的,区别在于编译后的文件大小。

#make modules
[说明]生成相应的模块。

#make modules_install
[说明]把模块拷贝到需要的目录中。

#make install
[说明]安装内核

#cp /usr/src/kernel-source-2.4.22/arch/i386/boot/bzImage /boot
[说明]复制bzImage文件到/boot目录下,以便生成initrd文件

#mkinitrd -o /boot/initrd.img-2.4.22 2.4.22
[说明]生成2.4.22内核的initrd.img

修改grub

reboot.....


Linux-2.6.x内核编译步骤

#make mrproper
[说明]清除所有中间文件,除了配置文件以外。

#make
[说明]此命令完成了make bzImage和make modules的工作。

#make modules_install
[说明]把内核模块安装到/lib/modules/2.6.x下。

#make install
[说明]完成mkinitrd命令及内核(bzImage)和System.map的复制。如果系统使用的是GRUB,会自动修改引导选项;对于LILO则要手工修改,重写引导记录。

#shutdown -r now
[说明]重启。



  另外,再记一下Debian的Patch使用方法:(比如安装的Patch包是kernel-patch-2.6.8,aptitude中显示的版本是2.6.8-16)

#cd /usr/src/kernel-source-2.6.8
#/usr/src/kernel-patches/all/2.6.8/apply/debian 2.6.8-16 
2011-04-11 19:33:00 myspor 阅读数 8302

HOST:WinXP

VM:6.5

VMOS:Ubuntu 8.04.1TLS

安装VMwareTools后,可以通过共享拷贝文件

拷贝linux-2.6.25.tar.bz2到$HOME

$tar xvf linux-2.6.25.tar.bz2

$cd linux-2.6.25

拷贝2.6.25-at91.patch.gz到$HOME/linux-2.6.25

打补丁:AT91SAM9261EK for linux2.6.25

$zcat 2.6.25-at91.patch.gz | patch -p1

修改Makefile:

ARCH ?=arm

CROSS_COMPILE ?=/usr/local/arm/3.4.1/bin/arm-linux-

清空以前的编译痕迹:

$make distclean

$cp arch/arm/configs/at91sam9261ek_defconfig ./.config

拷贝mkimage、tools文件到$HOME/linux-2.6.25(注:mkimage与tools都可以从原有的ebd9261的linux源码包中取得),修改这两个文件的属性为可执行:

$chmod 777 ./mkimage

$chmod 777 ./tools

刚开始make menuconfig时出错,提示要安装ncurses,安装下列包即可:

$sudo apt-get install libncurses5-dev

以下是成功编译的总结:

$make menuconfig

修改内核配置如下:

去除[Kernel Features]->EABI选项。arm-linux-3.4.1交叉编译工具不支持EABI

修改[Boot Option]中的配置:

旧的:mem=64M console=ttyS0,115200 initrd=0x21100000, 3145728 root=/dev/ram0 rw

新的:mem=64M console=ttyS0,115200 initrd=0x21100000,17000000 root=/dev/ram0 rw

修改所能支持的RAMDISK大小:

[Devices driver]->[Block devices]->Default RAM disk size,修改该值为40960

$./mkimage 注:使用mkimage可以生成uImage映像用于Uboot

将uImage与原EBD9261的ramdisk.gz下载到目标板:

U-Boot> setenv ipaddr 172.16.10.211

U-Boot> setenv serverip 172.16.10.212

U-Boot> saveenv

上述三个步骤在第一次使用时设置下即可,不需每次都设置

U-Boot> tftp 20400000 uImage

U-Boot> tftp 21100000 ramdisk.gz

U-Boot> bootm 0x20400000

Linux系统可跑起来。但显示、声音等驱动都不正常。

以下是失败的过程:

$make menuconfig

去除[Kernel Features]->EABI选项

注:由于我用的是arm-linux-3.4.1交叉编译工具,该工具不支持EABI,因此在配置Kernel时要去除[Kernel Features]中的EABI选项。

$./mkimage最后生成Kernel:uImage9261

将新生成的Kernel映像uImage9261及原板自带的ramdisk.gz下载到目标板EBD9261:

U-Boot> setenv ipaddr 172.16.10.211

U-Boot> setenv serverip 172.16.10.212

U-Boot> saveenv

上述三个步骤在第一次使用时设置下即可,不需每次都设置

U-Boot> tftp 20400000 uImage

U-Boot> tftp 21100000 ramdisk.gz

U-Boot> bootm 0x20400000

这边必须用uImage,不能用zImage、bzImage等格式Kernel镜像

uImage是使用mkImage工具生成的

启动过程如下(2.6.25启动信息1.txt):

## Booting image at 20400000 ...

Image Name: linux-2.6

Image Type: ARM Linux Kernel Image (gzip compressed)

Data Size: 1506280 Bytes = 1.4 MB

Load Address: 20008000

Entry Point: 20008000

Verifying Checksum ... OK

Uncompressing Kernel Image ... OK

Starting kernel ...

Linux version 2.6.25 (cmy@cmy-ubuntu) (gcc version 3.4.1) #1 Tue Dec 16 21:19:49

CST 2008

CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=00053177

Machine: Atmel AT91SAM9261-EK

Ignoring unrecognised tag 0x54410008

Memory policy: ECC disabled, Data cache writeback

Clocks: CPU 198 MHz, master 99 MHz, main 18.432 MHz

CPU0: D VIVT write-back cache

CPU0: I cache: 16384 bytes, associativity 4, 32 byte lines, 128 sets

CPU0: D cache: 16384 bytes, associativity 4, 32 byte lines, 128 sets

Built 1 zonelists in Zone order, mobility grouping on. Total pages: 16256

Kernel command line: mem=64M console=ttyS0,115200 initrd=0x21100000,3145728 root

=/dev/ram0 rw

AT91: 96 gpio irqs in 3 banks

PID hash table entries: 256 (order: 8, 1024 bytes)

Console: colour dummy device 80x30

console [ttyS0] enabled

Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)

Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)

Memory: 64MB = 64MB total

Memory: 58636KB available (2788K code, 236K data, 124K init)

Mount-cache hash table entries: 512

CPU: Testing write buffer coherency: ok

net_namespace: 152 bytes

NET: Registered protocol family 16

AT91: Power Management

AT91: Starting after general reset

SCSI subsystem initialized

usbcore: registered new interface driver usbfs

usbcore: registered new interface driver hub

usbcore: registered new device driver usb

NET: Registered protocol family 2

IP route cache hash table entries: 1024 (order: 0, 4096 bytes)

TCP established hash table entries: 2048 (order: 2, 16384 bytes)

TCP bind hash table entries: 2048 (order: 1, 8192 bytes)

TCP: Hash tables configured (established 2048 bind 2048)

TCP reno registered

checking if image is initramfs...it isn't (no cpio magic); looks like an initrd

Freeing initrd memory: 3072K

NetWinder Floating Point Emulator V0.97 (double precision)

JFFS2 version 2.2. (NAND) (SUMMARY) © 2001-2006 Red Hat, Inc.

io scheduler noop registered

io scheduler anticipatory registered (default)

atmel_lcdfb atmel_lcdfb.0: 160KiB frame buffer at 00300000 (mapped at c4880000)

Console: switching to colour frame buffer device 30x40

atmel_lcdfb atmel_lcdfb.0: fb0: Atmel LCDC at 0x00600000 (mapped at c485c000), i

rq 21

atmel_usart.0: ttyS0 at MMIO 0xfefff200 (irq = 1) is a ATMEL_SERIAL

brd: module loaded

loop: module loaded

ssc ssc.1: Atmel SSC device at 0xc4860000 (irq 15)

dm9000 Ethernet Driver, V1.30

eth0: dm9000 at c485e000,c4866044 IRQ 107 MAC: 22:34:56:78:99:aa (chip)

Driver 'sd' needs updating - please use bus_type methods

NAND device: Manufacturer ID: 0xec, Chip ID: 0x76 (Samsung NAND 64MiB 3,3V 8-bit

)

AT91 NAND: 8-bit, Software ECC

Scanning device for bad blocks

Creating 2 MTD partitions on "at91_nand":

0x00000000-0x00040000 : "Partition 1"

0x00040000-0x04000000 : "Partition 2"

atmel_spi atmel_spi.0: Atmel SPI Controller at 0xfffc8000 (irq 12)

mtd_dataflash spi0.0: AT45DB161x (2112 KBytes)

usbmon: debugfs is not available

at91_ohci at91_ohci: AT91 OHCI

at91_ohci at91_ohci: new USB bus registered, assigned bus number 1

at91_ohci at91_ohci: irq 20, io mem 0x00500000

usb usb1: configuration #1 chosen from 1 choice

hub 1-0:1.0: USB hub found

hub 1-0:1.0: 2 ports detected

Initializing USB Mass Storage driver...

usbcore: registered new interface driver usb-storage

USB Mass Storage support registered.

udc: at91_udc version 3 May 2006

mice: PS/2 mouse device common for all mice

input: gpio-keys as /class/input/input0

ads7846 spi0.2: touchscreen, irq 29

input: ADS784x Touchscreen as /class/input/input1

rtc-at91sam9 at91_rtt.0: rtc core: registered at91_rtt as rtc0

rtc-at91sam9 at91_rtt.0: rtc0: SET TIME!

Registered led device: ds7

Registered led device: ds8

Registered led device: ds1

usbcore: registered new interface driver usbhid

drivers/hid/usbhid/hid-core.c: v2.6:USB HID core driver

Advanced Linux Sound Architecture Driver Version 1.0.16rc2 (Thu Jan 31 16:40:16

2008 UTC).

at73c213 spi0.3: at73c213: supported bitrate is 48500 (64 divider)

ALSA device list:

#0: AT91SAM9261-EK external DAC on irq 15

TCP cubic registered

NET: Registered protocol family 1

NET: Registered protocol family 17

RPC: Registered udp transport module.

RPC: Registered tcp transport module.

rtc-at91sam9 at91_rtt.0: hctosys: unable to read the hardware clock

RAMDISK: Compressed image found at block 0

RAMDISK: incomplete write (-28 != 32768) 8388608

RAMDISK: ran out of compressed data

invalid compressed format (err=1)

EXT2-fs warning: maximal mount count reached, running e2fsck is recommended

VFS: Mounted root (ext2 filesystem).

Freeing init memory: 124K

attempt to access beyond end of device

ram0: rw=0, want=49164, limit=16384

EXT2-fs error (device ram0): ext2_get_inode: unable to read inode block - inode=

6145, block=24581

Warning: unable to open an initial console.

attempt to access beyond end of device

ram0: rw=0, want=49178, limit=16384

EXT2-fs error (device ram0): ext2_get_inode: unable to read inode block - inode=

6205, block=24588

attempt to access beyond end of device

ram0: rw=0, want=65544, limit=16384

EXT2-fs error (device ram0): ext2_get_inode: unable to read inode block - inode=

8193, block=32771

attempt to access beyond end of device

ram0: rw=0, want=16396, limit=16384

EXT2-fs error (device ram0): ext2_get_inode: unable to read inode block - inode=

2049, block=8197

attempt to access beyond end of device

ram0: rw=0, want=16396, limit=16384

EXT2-fs error (device ram0): ext2_get_inode: unable to read inode block - inode=

2049, block=8197

Kernel panic - not syncing: No init found. Try passing init= option to kernel.

从“RAMDISK: incomplete write (-28 != 32768) 8388608”开始就出错了,google了一下,说是分配给RamDisk的空间太小,然后我修改Boot参数为:

mem=64M console=ttyS0,115200 initrd=0x21100000,17000000 root=/dev/ram0 rw

重新编译生成uImage,跟ramdisk.gz一起下载到目标板上,启动信息如下(2.6.25启动信息2.txt):

……

RAMDISK: Compressed image found at block 0

RAMDISK: incomplete write (-28 != 32768) 8388608

EXT2-fs warning: maximal mount count reached, running e2fsck is recommended

……

可以看到有所改善,但系统还是没起来。

会不会是ramdisk.gz的问题,我决定重新做一个:

交叉编译器还是使用arm-linux-3.4.1,先编译busybox工具

拷贝busybox-1.11.1到$HOME后进行编译

$tar xvf busybox-1.11.1.tar.bz2

$cd busybox-1.11.1

修改Makefile文件中的ARCH与CROSS_COMPIL,可参考Kernel编译时的设置

$make

我使用busybox-1.11.1,编译出错:

networking/interface.c:818: error: `ARPHRD_INFINIBAND' undeclared here (not in a function)
networking/interface.c:818: error: initializer element is not constant
networking/interface.c:818: error: (near initialization for `ib_hwtype.type')
networking/interface.c:818: error: `ARPHRD_INFINIBAND' undeclared here (not in a function)
networking/interface.c:818: error: initializer element is not constant
networking/interface.c:818: error: (near initialization for `ib_hwtype.type')

可能busybox版本太新缘故,换了个低版本的(busybox-1.3.2),编译出现错误:

applets/applets.c:22:2: #warning Static linking against glibc produces buggy executables

applets/applets.c:23:2: #warning (glibc does not cope well with ld --gc-sections).

applets/applets.c:24:2: #warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400

applets/applets.c:25:2: #warning Note that glibc is utterly unsuitable for static linking anyway.

make[1]: *** [applets/applets.o] 错误 1

注释掉如下警告信息:

clip_image001

重新编译,出现如下错误

/home/cmy/busybox-1.3.2/scripts/trylink: 5: function: not found

/home/cmy/busybox-1.3.2/scripts/trylink: 11: Syntax error: "}" unexpected

make: *** [busybox_unstripped] 错误 2

由于ubuntu默认的sh是dash,要将它改成bash:

$sudo rm /bin/sh

$sudo ln –s /bin/bash /bin/sh

再次编译,出错:

In file included from e2fsprogs/ext2fs/ismounted.c:22:

/usr/local/arm/3.4.1/lib/gcc/arm-linux/3.4.1/../../../../arm-linux/sys-include/linux/fd.h:342: error: variable or field `__user' declared void

/usr/local/arm/3.4.1/lib/gcc/arm-linux/3.4.1/../../../../arm-linux/sys-include/linux/fd.h:342: error: parse error before '*' token

/usr/local/arm/3.4.1/lib/gcc/arm-linux/3.4.1/../../../../arm-linux/sys-include/linux/fd.h:360: error: parse error before '}' token

make[1]: *** [e2fsprogs/ext2fs/ismounted.o] 错误 1

make: *** [e2fsprogs/ext2fs] 错误 2

gcc 3.4.1中新增类型 __user ,需要引入编译器定义头文件,在引用#include 的前面加上#include 后,继续编译,出现如下错误:

modutils/lib.a(insmod.o)(.text.insmod_main+0x370): In function `insmod_main':
: undefined reference to `query_module'
modutils/lib.a(insmod.o)(.text.insmod_main+0x3a4): In function `insmod_main':
: undefined reference to `query_module'
modutils/lib.a(insmod.o)(.text.insmod_main+0x44c): In function `insmod_main':
: undefined reference to `query_module'

去除Linux Module Utilities->Support version 2.2.x to 2.4.x Linux kernels选项,继续编译,这次编译成功了,接着要进行安装:

$make install

完成后会在当前目录下生成_install文件夹。

紧接着可以开始制作Ramdisk根文件系统了,主要参考该文章:

http://www.cnitblog.com/zouzheng/articles/14423.html

最后生成根文件系统:ramdisk.gz

将uImage、ramdisk.gz下载到目标板上:

U-Boot> tftp 20400000 uImage

U-Boot> tftp 21100000 ramdisk.gz

U-Boot> bootm 0x20400000

启动信息如下:

## Booting image at 20400000 ...

Image Name: linux-2.6

Image Type: ARM Linux Kernel Image (gzip compressed)

Data Size: 1506275 Bytes = 1.4 MB

Load Address: 20008000

Entry Point: 20008000

Verifying Checksum ... OK

Uncompressing Kernel Image ... OK

Starting kernel ...

Linux version 2.6.25 (cmy@cmy-ubuntu) (gcc version 3.4.1) #3 Tue Dec 16 22:10:45

CST 2008

CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=00053177

Machine: Atmel AT91SAM9261-EK

Ignoring unrecognised tag 0x54410008

Memory policy: ECC disabled, Data cache writeback

Clocks: CPU 198 MHz, master 99 MHz, main 18.432 MHz

CPU0: D VIVT write-back cache

CPU0: I cache: 16384 bytes, associativity 4, 32 byte lines, 128 sets

CPU0: D cache: 16384 bytes, associativity 4, 32 byte lines, 128 sets

Built 1 zonelists in Zone order, mobility grouping on. Total pages: 16256

Kernel command line: mem=64M console=ttyS0,115200 initrd=0x21100000,17000000 roo

t=/dev/ram0 rw

AT91: 96 gpio irqs in 3 banks

PID hash table entries: 256 (order: 8, 1024 bytes)

Console: colour dummy device 80x30

console [ttyS0] enabled

Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)

Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)

Memory: 64MB = 64MB total

Memory: 45104KB available (2788K code, 236K data, 124K init)

Mount-cache hash table entries: 512

CPU: Testing write buffer coherency: ok

net_namespace: 152 bytes

NET: Registered protocol family 16

AT91: Power Management

AT91: Starting after user reset

SCSI subsystem initialized

usbcore: registered new interface driver usbfs

usbcore: registered new interface driver hub

usbcore: registered new device driver usb

NET: Registered protocol family 2

IP route cache hash table entries: 1024 (order: 0, 4096 bytes)

TCP established hash table entries: 2048 (order: 2, 16384 bytes)

TCP bind hash table entries: 2048 (order: 1, 8192 bytes)

TCP: Hash tables configured (established 2048 bind 2048)

TCP reno registered

checking if image is initramfs...it isn't (no cpio magic); looks like an initrd

Freeing initrd memory: 16601K

NetWinder Floating Point Emulator V0.97 (double precision)

JFFS2 version 2.2. (NAND) (SUMMARY) © 2001-2006 Red Hat, Inc.

io scheduler noop registered

io scheduler anticipatory registered (default)

atmel_lcdfb atmel_lcdfb.0: 160KiB frame buffer at 00300000 (mapped at c4880000)

Console: switching to colour frame buffer device 30x40

atmel_lcdfb atmel_lcdfb.0: fb0: Atmel LCDC at 0x00600000 (mapped at c485c000), i

rq 21

atmel_usart.0: ttyS0 at MMIO 0xfefff200 (irq = 1) is a ATMEL_SERIAL

brd: module loaded

loop: module loaded

ssc ssc.1: Atmel SSC device at 0xc4860000 (irq 15)

dm9000 Ethernet Driver, V1.30

eth0: dm9000 at c485e000,c4866044 IRQ 107 MAC: 22:34:56:78:99:aa (chip)

Driver 'sd' needs updating - please use bus_type methods

NAND device: Manufacturer ID: 0xec, Chip ID: 0x76 (Samsung NAND 64MiB 3,3V 8-bit

)

AT91 NAND: 8-bit, Software ECC

Scanning device for bad blocks

Creating 2 MTD partitions on "at91_nand":

0x00000000-0x00040000 : "Partition 1"

0x00040000-0x04000000 : "Partition 2"

atmel_spi atmel_spi.0: Atmel SPI Controller at 0xfffc8000 (irq 12)

mtd_dataflash spi0.0: AT45DB161x (2112 KBytes)

usbmon: debugfs is not available

at91_ohci at91_ohci: AT91 OHCI

at91_ohci at91_ohci: new USB bus registered, assigned bus number 1

at91_ohci at91_ohci: irq 20, io mem 0x00500000

usb usb1: configuration #1 chosen from 1 choice

hub 1-0:1.0: USB hub found

hub 1-0:1.0: 2 ports detected

Initializing USB Mass Storage driver...

usbcore: registered new interface driver usb-storage

USB Mass Storage support registered.

udc: at91_udc version 3 May 2006

mice: PS/2 mouse device common for all mice

input: gpio-keys as /class/input/input0

ads7846 spi0.2: touchscreen, irq 29

input: ADS784x Touchscreen as /class/input/input1

rtc-at91sam9 at91_rtt.0: rtc core: registered at91_rtt as rtc0

rtc-at91sam9 at91_rtt.0: rtc0: SET TIME!

Registered led device: ds7

Registered led device: ds8

Registered led device: ds1

usbcore: registered new interface driver usbhid

drivers/hid/usbhid/hid-core.c: v2.6:USB HID core driver

Advanced Linux Sound Architecture Driver Version 1.0.16rc2 (Thu Jan 31 16:40:16

2008 UTC).

at73c213 spi0.3: at73c213: supported bitrate is 48500 (64 divider)

ALSA device list:

#0: AT91SAM9261-EK external DAC on irq 15

TCP cubic registered

NET: Registered protocol family 1

NET: Registered protocol family 17

RPC: Registered udp transport module.

RPC: Registered tcp transport module.

rtc-at91sam9 at91_rtt.0: hctosys: unable to read the hardware clock

RAMDISK: Compressed image found at block 0

EXT2-fs warning: mounting unchecked fs, running e2fsck is recommended

VFS: Mounted root (ext2 filesystem).

Freeing init memory: 124K

mount: can't find / in /etc/mtab

dm9000 dm9000.0: WARNING: no IRQ resource flags set.

eth0: link down

mount: mounting /dev/fd0 on / failed

+++++++++++++ Welcom to Lianic Arm Linux ++++++++++++++++++

+ This is a arm linux system which is running on at91rm9200.

+ It contains more than 100 basic Linux commands and tools.

+ All these great features are powered by BusyBox 1.1.1

+ This is a free system tool developed by Edward Lianic.

+ If you have any problem please mailto : Lianic@163.com

+ Enjoy!!!

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Please press Enter to activate this console.

BusyBox v1.3.2 (2008-12-18 12:18:52 CST) Built-in shell (msh)

Enter 'help' for a list of built-in commands.

#

至此,Linux2.6.25在EBD9261板上跑起来了,接着,还有很多事情要做。

发现当ramdisk超过了8MB,目标板启动出错,出错信息是:

RAMDISK: incomplete write (-28 != 32768) 8388608

修改内核配置中的:

Device Drivers->Block Device->RAM disk size 将该值修改为40960。

重新编译得到uImage,将该uImage与原EBD9261的ramdisk下载到目标板上,文件系统加载正常,但驱动显然不对,LCD屏图像显示异常、播放MP3没有声音输出。

下一步的工作就是移驱动了。

To be continue…

2018-10-06 23:54:54 pleasecallmekaige 阅读数 1706

环境:Ubuntu14.4 以及7G的磁盘空间

因为编译后的内核会很大,所以要预留7G的空间

 

1、前期准备,到如下网址下载需要编译的linux-2.6的内核,我选的是linux-2.6.32.27.tar.bz2

2.6.32.X:其中X选择最大数字的,应为最大数字的就不需要打补丁了。

https://mirrors.edge.kernel.org/pub/linux/kernel/v2.6/

需要的gcc编译器版本,V4.8

 

2、切换到root用户(怎么切换自行百度),将下载的内核解压到/usr/src目录下,使用tar -xvf 压缩包名 -C 解压到目标个目录  

首先进入到内核压缩包所在的目录。 在命令行:tar -xvf linux-2.6.32.27.tar.bz2 -C /usr/src

 

3、进入解压后的内核源码文件夹。

在命令行:cd /usr/src/linux-2.6.32.27/

 

4、安装一些必要的依赖包。

apt-get install build-essential kernel-package libncurses5-dev

 

5、执行配置命令。执行make config 或者make menuconfig,不知道要选什么的话可以默认,可能编译的时间会长一些,如果是menuconfig的话,采用默认配置,直接选择最后的save选项,默认的.config不需要更改,保存后选择exit。清理操作简介:

#make clean: 清理大多数编译生成的文件,但会保留config文件等;

#make mrproper: 清理所有编译生成的文件、config及某些备份文件;

#make distclean: 清理mrproper、patches以及编辑器备份文件。

 

6、执行完配置命令之后,可以执行:make mrproper (清除以前曾经编译过的旧文件,第一次编译,可不执行) 或者make clean,这行这个命令只会删除以前编译过的一些中间文件,不会删掉一些配置文件;

 

7、开始编译。

命令行执行:make

编译过程遇到的问题:

(1)gcc: error: elf_x86_64: 没有那个文件或目录
         gcc: error: unrecognized command line option ‘-m’

解决的办法是在arch/x86/vdso/Makefile中做一些修改:VDSO_LDFLAGS_vdso.lds 开头所在行的 "-m elf_x86_64" 替换为 "-m64"。将以 VDSO_LDFLAGS_vdso32.lds 开头所在行的 "-m elf_386" 替换为 "-m32"。  这么修改之后就没问题了。 

 

(2)drivers/net/igbvf/igbvf.h:129:15: error: duplicate member'page'。按照提示的错误说是drivers/net/igbvf/igbvf.h中的第129行,打开文件看到第129行和第124行重复了struct page*page,因为这两行代码都定义在内部的一个结构体中,这是不允许重复的。解决办法:drivers/net/igbvf/igbvf.h中修改129行的structpage *page为struct page *pagep即可。

 

(3)compiler-gcc5.h:No such file解决办法:将附件compiler-gcc5.h放入/linux-2.6.34/include/linux中。

 

(4)Can't use 'defined(@array)' (Maybe you should just omit thedefined()?) at kernel/timeconst.pl line 373.。解决办法:将/linux-2.6.34/kernel/timeconst.pl中第373行的defined()去掉只留下@val

 

(5)安装好之后重启如果出现无法启动内核等错误则在原来的内核中打开终端依次输入下面指令,虽然有点长,但验证没问题,不然重启停留在initramfs处无法启动,这种情况说是磁盘受损,我上网查了很多资料,参考别人的做法都不行,逐条输入下面指令之后重启完美解决。(linux的版本是自己安装的版本,别复制了版本都忘了改,下图为重启遇到的initramfs的问题。注意输入指令的一些空格问题,最好直接复制在修改版本号即可。)

 

sudo su

cd /boot

cp initrd.img-2.6.34 initrd-2.6.34.old (备份)

depmod -a

update-initramfs -k 2.6.34 -c

cd /tmp

gzip -dc /boot/initrd.img-2.6.34| cpio -id

touch lib/modules/2.6.34/modules.dep

find ./ | cpio -H newc -o > /boot/initrd.img-2.6.34.new 

gzip /boot/initrd.img-2.6.34.new

cd /boot

mv initrd.img-2.6.34.new.gz initrd.img-2.6.34

 

(6)注意事项:由于ubuntu16.04一些依赖项目版本过高,可能导致安装内核成功后,出现缺失模块无法正常启动,建议使用ubuntu14.04编译安装,或者直接使用ubuntu10/ubuntu12编译内核。

 

(7)可能会在make menuconfig之后执行了make clean 或者make  mrproper,然后可能会提示如下错误。解决的办法是再重新执行make mrproper应该就可以了,这个也可以多多参考别人优秀的做法。

8、执行sudo make modules_install install,这部应该是可以直接编译和安装模块,就不需要先make modules(编译内核)再make modules_install(安装内核)了。

 

9、执行make stall安装内核;

 

10、创建 initrd文件:mkinitramfs -o/boot/initrd.img-2.6.34;

 

11、配置grub引导。

执行命令gedit /etc/default/grub,配置/etc/default/grub文件在文件中添加GRUB_TIMEOUT_STYLE=menu,GRUB_HIDDEN_TIMEOUT=0改为大于0的数即可,或者直接就添加GRUB_TIMEOUT_STYLE=menu,保存退出(一般两种方式都可以),执行命令update-grub;执行完这一步基本上就完成了内核的编译和安装,重启ubuntu应该就行了。

 

 

 

 

 

 

 

 

 

 

 

 

2018-06-22 23:48:23 think_ycx 阅读数 2396

0x00前言

很早就想开始学习linux内核漏洞的利用,之前的调试方式是借助kgdb来在两个vmware中调试,实际操作起来是没问题的。然而最近看到很多文章是使用qemu+gdb来调试的,因此决定尝试一下这种调试方式,第一步就是编译linux源码,本文将简述一下编译linux2.6源码和4.10源码时遇到的坑。

0x01 编译linux2.6源码

编译linux2.6的源码的过程参考这里:https://www.anquanke.com/post/id/85837。在按照文章中的步骤走的时候同遇到了很多问题,后来遇到某些坑绕不过去了....遇到的坑点大多数是make时遇到的。

问了一下编译过的同学,说是采用低版本的ubuntu来编译试试。经过了不断的搜索和尝试,发现linux2.6的源码使用gcc version>=4.6会遇到各种错误,有网友说用gcc4.5可以编译成功。下载源码 https://ftp.gnu.org/gnu/gcc/gcc-4.5.4/

编译时发现需要这些:configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.

编译这些依赖的源码时也遇到了问题。最后通过apt-get gcc-4.4解决。用gcc4.4编译时2.6源码时,没有遇到任何报错。

因此,最终解决办法就是更换gcc版本。

下载和切换gcc的版本方法

 首先添加ppa到库:  
sudo add-apt-repository ppa:ubuntu-toolchain-r/test  
sudo apt-get update
sudo apt-cache search gcc-4.*  
# 安装gcc/g++  
sudo apt-get install gcc-4.4  
# 手工更换gcc软连接  
cd /usr/bin  
sudo ln -s gcc-4 gcc  sudo apt-cache search gcc-4.*  
# 安装gcc/g++  
sudo apt-get install gcc-4.4  
# 手工更换gcc软连接  
cd /usr/bin  
sudo ln -s gcc-4 gcc  

linux2.6 source 编译环境

    ubuntu14.04 / (ubuntu16.04测试也编译成功,因此觉得和ubuntu版本、内核版本没多大关系)
    gcc4.4

gcc --version
gcc (Ubuntu/Linaro 4.4.7-8ubuntu1) 4.4.7
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

uname -a
Linux ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

正常的编译步骤如下:

wget https://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.32.1.tar.gz -O linux-2.6.32.1.tar.gz
tar zxvf linux-2.6.32.1.tar.gz
cd linux-2.6.32.1
make i386_defconfig
make -j4

PS: 在ubuntu16.04上编译时遇到了一个小问题,是由于perl版本过高导致的,参考这里的错误二:
https://blog.csdn.net/think_ycx/article/details/80740028

0x02 编译linux4.10源码

linux source 4.10 编译环境
同样,编译linux source 4.10时,使用的gcc版本使用的是gcc 5.5.0,在ubuntu16.04上编译成功。

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.10.6.tar.gz -O linux-4.10.6.tar.gz
tar zxvf linux-4.10.6.tar.gz
cd linux-4.10.6
make defconfig
make -j4

 

参考:
1. https://askubuntu.com/questions/873278/linux-kernel-version-and-gcc-version-match
2. gcc4.5 version成功编译
    http://www.cnblogs.com/QuLory/archive/2012/09/27/2706429.html  
3. 编译gcc 
    http://blog.cuicc.com/blog/2010/08/22/install-gcc-4-5-1-in-debian-lenny/
4. http://staff.ustc.edu.cn/~xlanchen/ULK2011Spring/slides/2_2build+run+gdb%20linux-2.6.26.pdf

 

2011-05-05 14:06:00 ivan240 阅读数 844

http://blogold.chinaunix.net/u3/112205/showart_2191169.html

 

 

Linux内核2.4和2.6编译模块的方法
 
 
2.6下的模块,扩展名为.ko,而不是2.4下的.o。很多初学者写完模块之后,会使用2.4的方法来编译模块。

/*filename: test.c*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int dummy_init(void)
{
    printk("hello,world./n");
    return 0;
}
static void dummy_exit(void)
{
    return;
}

module_init(dummy_init);
module_exit(dummy_exit);

MODULE_LICENSE("GPL")


执行如下命令:

$ gcc -c -O2 -DMODULE -D__KERNEL__ -I/usr/src/linux test.c
$ insmod test.o

No module found in object
insmod: error inserting 'test.o': -1 Invalid module format


正确的做法是写一个Makefile,由内核的Kbuild来帮你编译。
#filename: Makefile
obj-m := test.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

执行如下命令:

$make
make -C /lib/modules/2.6.5-1.358/build SUBDIRS=/test modules
make[1]: Entering directory `/lib/modules/2.6.5-1.358/build'
  CC [M] /test/modinject/test.o
  Building modules, stage 2.
  MODPOST
  CC /test/modinject/test.mod.o
  LD [M] /test/modinject/test.ko
make[1]: Leaving directory `/lib/modules/2.6.5-1.358/build'

$ls -l
-rw-r--r-- 1 root root 268 Jan 7 08:31 test.c
-rw-r--r-- 1 root root 2483 Jan 8 09:19 test.ko
-rw-r--r-- 1 root root 691 Jan 8 09:19 test.mod.c
-rw-r--r-- 1 root root 1964 Jan 8 09:19 test.mod.o
-rw-r--r-- 1 root root 1064 Jan 8 09:19 test.o


其实上边的test.o就是用gcc生成的test.o,而test.ko是使用下列命令来生成的。

$ld -m elf_i386 -r -o test.ko test.o test.mod.o

再来看看test.mod.c,它是由/usr/src/linux/scripts/modpost.c来生成的。

$ cat test.mod.c
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>

MODULE_INFO(vermagic, VERMAGIC_STRING);
#undef unix

struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = __stringify(KBUILD_MODNAME),
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD

.exit = cleanup_module,
#endif

};
static const struct modversion_info ____versions[]
__attribute_used__
__attribute__((section("__versions"))) = {
        { 0, "cleanup_module" },
        { 0, "init_module" },
        { 0, "struct_module" },
        { 0, "printk" },
};
static const char __module_depends[]
__attribute_used__
__attribute__((section(".modinfo"))) =
"depends=";


可见,test.mod.o只是产生了几个ELF的节,分别是modinfo, .gun.linkonce.this_module(用于重定位,引进了rel.gnu.linkonce.this_module), __versions。而test.ko是test.o和test.mod.o合并的结果。

ubuntu 2.6 编译内核

阅读数 941