程序包的编译安装:
./configure, make, make install
前提:
开发环境(开发工具,开发库),头文件:/usr/include
- 准备好开发环境;
- 获取目标主机上硬件设备的相关信息;
- 获取到目标主机系统功能的相关信息,例如要启用的文件系统;
- 获取内核源代码包:www.kernel.org
准备开发环境:
CentOS 6.7:
包组:
Development Tools
Server Platform DevelopmentCentOS 7:
包组:
Development Tools
Server Platform Development包:
ncurses-devel
获取目标主机上硬件设备的相关信息:
CPU:
~]# cat /proc/cpuinfo
~]# lscpu
~]# x86info -a
-
PCI设备:
~]# lspci
-v
-vv~]# lsusb
-v
-vv[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT fd0 2:0 1 4K 0 disk sda 8:0 0 120G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 119G 0 part ├─centos-root 253:0 0 50G 0 lvm / ├─centos-swap 253:1 0 2G 0 lvm [SWAP] └─centos-home 253:2 0 67G 0 lvm /home sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 109.8M 0 part ├─sdb2 8:18 0 2G 0 part └─sdb3 8:19 0 5G 0 part sr0 11:0 1 4.3G 0 rom
了解全部硬件设备信息:
~]# hal-device
内核编译过程:
-
步骤:
~]# tar xf linux-3.10.67.tar.xz -C /usr/src
~]# cd /usr/src
~]# ln -s linux-3.10.67 linux
~]# cd linux~]# make menuconfig 配置内核选项
~]# make [-j #] 编译内核,可使用-j指定编译线程数量
~]# make modules_install 安装内核模块
~]# make install 安装内核重启系统,选择使用新内核;
screen命令:
打开screen:~]# screen
拆除screen: Ctrl+a, d
列出screen:~]# screen -ls
连接至screen:~]# screen -r SCREEN_ID
关闭screen:~]# exit
make 过程可以在screen命令中执行,以防止SSH终端异常退出而导致make中断
过程的详细说明:
配置内核选项
支持“更新”模式进行配置:在已有的.config文件的基础之上进行“修改”配置;
(a) make config:基于命令行以遍历的方式去配置内核中可配置的每个选项;
(b) make menuconfig:基于cureses的文本配置窗口;
(c) make gconfig:基于GTK开发环境的窗口界面; 包组“桌面平台开发”
(d) make xonfig:基于QT开发环境的窗口界面;
支持“全新配置”模式进行配置:
(a) make defconfig:基于内核为目标平台提供的“默认”配置为模板进行配置;
(b) make allnoconfig:所有选项均为“no”;-
编译
(a) 多线程编译:
make [-j #]
(b) 编译内核中的一部分代码:
(i) 只编译某子目录中的相关代码:
# cd /usr/src/linux
# make path/to/dir/
(ii)只编译一个特定的模块
# cd /usr/src/linux
# make path/to/dir/file.ko
(c) 如何交叉编译:
目标平台与当前编译操作所在的平台不同;
# make ARCH=arch_name
要获取特定目标平台的使用帮助:
# make ARCH=arch_name help
如何在执行过编译操作的内核源码树上做重新编译:
事先清理操作:
# make clean
:清理编译生成的绝大多数文件,但会保留config,及编译外部模块所需要的文件
# make mrproper
:清理编译生成的所有文件,包括配置生成的config文件及某些备份文件
# make distclean
:相当于mrproper,额外清理各种patches以及编辑器备份文件
练习:在centos7上编译linux_3.18.163
[root@localhost ~]# tar xJf /tmp/linux-3.18.136.tar.xz -C /tmp
[root@localhost ~]# ln -s /tmp/linux-3.18.136 /usr/src/linux
[root@localhost ~]# ll /usr/src
total 0
drwxr-xr-x. 2 root root 6 Apr 11 2018 debug
drwxr-xr-x. 3 root root 45 Mar 15 22:17 kernels
lrwxrwxrwx. 1 root root 19 Mar 16 00:03 linux -> /tmp/linux-3.18.136
[root@localhost linux]# ls
arch COPYING crypto drivers fs init Kbuild kernel MAINTAINERS mm README samples security tools virt
block CREDITS Documentation firmware include ipc Kconfig lib Makefile net REPORTING-BUGS scripts sound usr
[root@localhost linux]# yum install *curses*x86* -y
[root@localhost linux]# make menuconfig
make
[root@localhost linux]# screen
[detached from 43337.pts-1.localhost]
[root@localhost linux]# screen -ls
There is a screen on:
43337.pts-1.localhost (Detached)
1 Socket in /var/run/screen/S-root.
[root@localhost linux]# screen -r 43337
[root@localhost linux]# make -j 4
make modules install安装报错(命令打错了应为make modules_install)
[root@localhost linux]# make modules install
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
make[1]: `arch/x86/purgatory/kexec-purgatory.c' is up to date.
CALL scripts/checksyscalls.sh
CHK include/generated/compile.h
BC kernel/time/timeconst.h
/bin/sh: bc: command not found
make[2]: *** [kernel/time/timeconst.h] Error 127
make[1]: *** [kernel/time] Error 2
make: *** [kernel] Error 2
解决:yum install bc -y
make modules_install
[root@localhost linux]# make modules_install -j 4
...
INSTALL /lib/firmware/whiteheat_loader.fw
INSTALL /lib/firmware/whiteheat.fw
INSTALL /lib/firmware/keyspan_pda/keyspan_pda.fw
INSTALL /lib/firmware/keyspan_pda/xircom_pgs.fw
DEPMOD 3.18.136
安装玩modules后
[root@localhost ~]# ls /lib/modules 3.10.0-957.el7.x86_64 3.18.136
再执行make install 报错(提示之前没有执行make)
make install 要用到make执行生成的bzimage
文件
[root@localhost linux]# make -j 4
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
make[1]: `arch/x86/purgatory/kexec-purgatory.c' is up to date.
CALL scripts/checksyscalls.sh
X.509 certificate list changed
CHK include/generated/compile.h
CERTS kernel/x509_certificate_list
- Including cert signing_key.x509
AS kernel/system_certificates.o
LD kernel/built-in.o
CHK include/generated/uapi/linux/version.h
LINK vmlinux
LD vmlinux.o
MODPOST vmlinux.o
GEN .version
CHK include/generated/compile.h
UPD include/generated/compile.h
CC init/version.o
LD init/built-in.o
KSYM .tmp_kallsyms1.o
KSYM .tmp_kallsyms2.o
LD vmlinux
SORTEX vmlinux
SYSMAP System.map
HOSTCC arch/x86/tools/test_get_len
HOSTCC arch/x86/tools/insn_sanity
Building modules, stage 2.
TEST posttest
Succeed: decoded and checked 1812636 instructions
TEST posttest
arch/x86/tools/insn_sanity: Success: decoded and checked 1000000 random instructions with 0 errors (seed:0x3a92c85b)
CC arch/x86/boot/a20.o
AS arch/x86/boot/bioscall.o
CC arch/x86/boot/cmdline.o
AS arch/x86/boot/copy.o
HOSTCC arch/x86/boot/mkcpustr
CC arch/x86/boot/cpuflags.o
CC arch/x86/boot/cpucheck.o
CC arch/x86/boot/early_serial_console.o
CC arch/x86/boot/edd.o
VOFFSET arch/x86/boot/voffset.h
CC arch/x86/boot/main.o
LDS arch/x86/boot/compressed/vmlinux.lds
CC arch/x86/boot/mca.o
CC arch/x86/boot/memory.o
CC arch/x86/boot/pm.o
AS arch/x86/boot/pmjump.o
AS arch/x86/boot/compressed/head_64.o
CC arch/x86/boot/printf.o
CC arch/x86/boot/regs.o
CC arch/x86/boot/string.o
CC arch/x86/boot/tty.o
CC arch/x86/boot/compressed/misc.o
CC arch/x86/boot/video.o
CC arch/x86/boot/compressed/string.o
CC arch/x86/boot/video-mode.o
CC arch/x86/boot/compressed/cmdline.o
CC arch/x86/boot/version.o
CC arch/x86/boot/video-vga.o
CC arch/x86/boot/video-vesa.o
CC arch/x86/boot/video-bios.o
HOSTCC arch/x86/boot/tools/build
OBJCOPY arch/x86/boot/compressed/vmlinux.bin
RELOCS arch/x86/boot/compressed/vmlinux.relocs
HOSTCC arch/x86/boot/compressed/mkpiggy
CC arch/x86/boot/compressed/cpuflags.o
CC arch/x86/boot/compressed/early_serial_console.o
CC arch/x86/boot/compressed/aslr.o
CC arch/x86/boot/compressed/eboot.o
AS arch/x86/boot/compressed/efi_stub_64.o
AS arch/x86/boot/compressed/efi_thunk_64.o
CPUSTR arch/x86/boot/cpustr.h
GZIP arch/x86/boot/compressed/vmlinux.bin.gz
CC arch/x86/boot/cpu.o
MKPIGGY arch/x86/boot/compressed/piggy.S
AS arch/x86/boot/compressed/piggy.o
LD arch/x86/boot/compressed/vmlinux
ZOFFSET arch/x86/boot/zoffset.h
OBJCOPY arch/x86/boot/vmlinux.bin
AS arch/x86/boot/header.o
LD arch/x86/boot/setup.elf
OBJCOPY arch/x86/boot/setup.bin
BUILD arch/x86/boot/bzImage
Setup is 17068 bytes (padded to 17408 bytes).
System is 5652 kB
CRC cf5a1aa3
Kernel: arch/x86/boot/bzImage is ready (#2)
MODPOST 2276 modules
以防万一重新执行make modules_install
完成后再执行make install
[root@localhost linux]# make install -j 4
sh ./arch/x86/boot/install.sh 3.18.136 arch/x86/boot/bzImage \
System.map "/boot"
重启 init 6
进入
[root@localhost ~]# uname -r
3.18.136
编译内核之前的linux目录
[root@localhost ~]# ls /linux-3.18.136/arch/
alpha arm avr32 c6x frv ia64 m32r metag mips openrisc powerpc score sparc um x86
arc arm64 blackfin cris hexagon Kconfig m68k microblaze mn10300 parisc s390 sh tile unicore32 xtensa
[root@localhost ~]# ls /linux-3.18.136/arch/x86/
boot ia32 Kconfig kernel lib Makefile.um net platform realmode um xen
configs include Kconfig.cpu kvm Makefile math-emu oprofile power syscalls vdso
crypto Kbuild Kconfig.debug lguest Makefile_32.cpu mm pci purgatory tools video
[root@localhost ~]# ls /linux-3.18.136/arch/x86/boot/
a20.c cmdline.c cpucheck.c edd.c mca.c pmjump.S string.h video-bios.c video-vga.c
apm.c code16gcc.h cpuflags.c header.S memory.c printf.c tools video.c
bioscall.S compressed cpuflags.h install.sh mkcpustr.c regs.c tty.c video.h
bitops.h copy.S ctype.h main.c mtools.conf.in setup.ld version.c video-mode.c
boot.h cpu.c early_serial_console.c Makefile pm.c string.c vesa.h video-vesa.c
编译完成后的linux目录
[root@localhost linux]# ls ./arch
alpha arm avr32 c6x frv ia64 m32r metag mips openrisc powerpc score sparc um x86 xtensa
arc arm64 blackfin cris hexagon Kconfig m68k microblaze mn10300 parisc s390 sh tile unicore32 x86_64
[root@localhost linux]# ls ./arch/x86
boot crypto Kconfig kvm Makefile_32.cpu modules.builtin pci realmode vdso
built-in.mod.c ia32 Kconfig.cpu lguest Makefile.um modules.order platform syscalls video
built-in.o include Kconfig.debug lib math-emu net power tools xen
configs Kbuild kernel Makefile mm oprofile purgatory um
[root@localhost linux]# ls ./arch/x86/boot/
a20.c cmdline.o cpuflags.h header.o memory.o printf.o tools video.h voffset.h
a20.o code16gcc.h cpuflags.o header.S mkcpustr regs.c tty.c video-mode.c zoffset.h
apm.c compressed cpu.o install.sh mkcpustr.c regs.o tty.o video-mode.o
bioscall.o copy.o cpustr.h main.c mtools.conf.in setup.bin version.c video.o
bioscall.S copy.S ctype.h main.o pm.c setup.elf version.o video-vesa.c
bitops.h cpu.c early_serial_console.c Makefile pmjump.o setup.ld vesa.h video-vesa.o
boot.h cpucheck.c early_serial_console.o mca.c pmjump.S string.c video-bios.c video-vga.c
bzImage cpucheck.o edd.c mca.o pm.o string.h video-bios.o video-vga.o
cmdline.c cpuflags.c edd.o memory.c printf.c string.o video.c vmlinux.bin
编译内核之前的/boot目录
[root@localhost ~]# ls /boot
config-3.10.0-957.el7.x86_64 initramfs-0-rescue-5f47125a1faf4b438a9b7aec3758819e.img vmlinuz-0-rescue-5f47125a1faf4b438a9b7aec3758819e
efi initramfs-3.10.0-957.el7.x86_64.img vmlinuz-3.10.0-957.el7.x86_64
grub symvers-3.10.0-957.el7.x86_64.gz
grub2 System.map-3.10.0-957.el7.x86_64
编译完成后的linux目录
[root@localhost linux]# ls /boot
config-3.10.0-957.el7.x86_64 System.map
efi System.map-3.10.0-957.el7.x86_64
grub System.map-3.18.136
grub2 vmlinuz
initramfs-0-rescue-5f47125a1faf4b438a9b7aec3758819e.img vmlinuz-0-rescue-5f47125a1faf4b438a9b7aec3758819e
initramfs-3.10.0-957.el7.x86_64.img vmlinuz-3.10.0-957.el7.x86_64
initramfs-3.18.136.img vmlinuz-3.18.136
symvers-3.10.0-957.el7.x86_64.gz