Ubuntu 14.04与16.04(Linux Mint或者Debain)内核与模块编译

版权声明:转载请注明出处: http://blog.csdn.net/sy373466062 作者: TonyHo https://blog.csdn.net/sy373466062/article/details/71124567

编译Ubuntu的内核, 如果按照官方的说明, 那么一般是编译出deb包, 然后安装,但是如果我们改了驱动, 或者内核那么我们很可能只想编译bzImage而不是重新生成deb包.


一般的编译步骤

参考Ubuntu 的wiki:

Kernel Compile

BuildYourOwnKernel


基本步骤如下

安装需要的软件包, 或者kernel source:

apt-get source linux-image-$(uname -r)
sudo apt-get build-dep linux-image-$(uname -r)

如果还需要menuconfig之类的话, 安装ncurse即可.

然后需要chmod脚本

chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
fakeroot debian/rules clean

然后就是修改menuconfig了:

fakeroot debian/rules editconfigs

这个过程会提示修改哪个的menuconfig, 第一个弹出的就是适合我们架构(running)的menuconfig, 例如如果是X86, 那么就是AMD64, 修改后会提示是否更改其他arch的menuconfig, 例如arm64, ppc64等等. 

更改完成后, 第一次需要全编译, 直接:

fakeroot debian/rules clean
# quicker build:
fakeroot debian/rules binary-headers binary-generic binary-perarch
# if you need linux-tools or lowlatency kernel, run instead:
fakeroot debian/rules binary

注意后面的binary这个目标将编译所有的arch, 但是其实我们只需要编译适合我们现在在跑的这个机器即可. 因此可以这样子:

fakeroot debian/rules binary-generic

这个generic是flavour, 还可以是其他的架构. 

编译完成后, 安装deb包

重新编译

如果后来又修改了内核中的代码, 这个时候就可以快速编译一个bzImage即可, 步骤如下:

进入到需要source 目录, 即apt-get source获取下来patched后的source, 然后运行类似下面的commands即可. 如果还需要修改menuconfig, 那么就使用前面的editconfig.

make ARCH=x86_64 CROSS_COMPILE= KERNELVERSION=4.4.0-75-generic CONFIG_DEBUG_SECTION_MISMATCH=y KBUILD_BUILD_VERSION="96" LOCALVERSION= localver-extra= CFLAGS_MODULE="-DPKG_ABI=75" O=/home/hexiongjun/LinuxRC/linux-4.4.0/debian/build/build-generic -j4 bzImage modules 

注意里面的O=, 这个是build output目录,这样子可以避免污染source. 编译完成后就可以将需要的kernel替换现有的了.

也可以使用grub来加载指定的kernel.


模块编译

如果是自己编译的内核,那么编译out of tree模块依赖kernel编译的目录和source. 对此, 可以使用下面这个模块:

CONFIG_MODULE_SIG=n

ifneq ($(KERNELRELEASE),)
        obj-m := c.o
        remote-y = a.o b.o
else
        PWD := $(shell pwd)
        KVER := $(shell uname -r)
        #KDIR := /lib/modules/$(KVER)/build
        KDIR := /home/hexiongjun/LinuxRC/linux-4.4.0/debian/build/build-generic/

all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules
        #$(MAKE) -C $(KDIR) M=$(PWD) modules -s

clean:
        rm -rf *.o *.mod.c *.mod.o *.ko *.symvers *.order *.a

endif

编译完成后,生成ko, 然后insmod即可. 


Grub2 bootloader加载说明

对于编译好的内核可以使用grub2在直接指定, 而不替换原有的image, 可以参考/boot/grub2/grub.cfg, 例如对于Linux Mint 18的entry:

menuentry 'Linux Mint 18.1 KDE 64-bit' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-0415fbcf-6c1f-4e68-8921-f415b6f6fd0c' {
	recordfail
	load_video
	gfxmode $linux_gfx_mode
	insmod gzio
	if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
	insmod part_msdos
	insmod ext2
	set root='hd1,msdos5'
	if [ x$feature_platform_search_hint = xy ]; then
	  search --no-floppy --fs-uuid --set=root --hint-bios=hd1,msdos5 --hint-efi=hd1,msdos5 --hint-baremetal=ahci1,msdos5  0415fbcf-6c1f-4e68-8921-f415b6f6fd0c
	else
	  search --no-floppy --fs-uuid --set=root XXXXXXXX-6c1f-4e68-8921-YYYYYYY
	fi
	linux	/boot/vmlinuz-4.4.0-75-generic root=UUID=XXXXX-6c1f-4e68-8921-YYYYYY ro  quiet splash $vt_handoff
	initrd	/boot/initrd.img-4.4.0-75-generic
}
前面的insmod为bootloader/grub2加载驱动, 然后设置root, 接着指定kernel与initrd, 最后自动调用boot完成启动, 因此我们可以在grub2中手动指定bzImage.

关于vmlinux, vmlinuz, zImage等信息如果不是很熟悉可以参考SOF:

https://unix.stackexchange.com/questions/5518/what-is-the-difference-between-the-following-kernel-makefile-terms-vmlinux-vml/5602#5602



猜你喜欢

转载自blog.csdn.net/sy373466062/article/details/71124567
今日推荐