target是 deb-pkg,在文件Make.package
中
71 PHONY += deb-pkg
72 deb-pkg:
73 $(MAKE) clean
74 $(CONFIG_SHELL) $(srctree)/scripts/package/mkdebian
75 $(call cmd,src_tar,$(KDEB_SOURCENAME))
76 origversion=$$(dpkg-parsechangelog -SVersion |sed 's/-[^-]*$$//');\
77 mv $(KDEB_SOURCENAME).tar.gz ../$(KDEB_SOURCENAME)_$${origversion}.orig.tar.gz
78 +dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) -i.git -us -uc
展开
- make clean
- sh scripts/package/mkdebian 执行这个脚本
- 调用 cmd_src_tar $(KDEB_SOURCENAME)
origversion=$$(dpkg-parsechangelog -SVersion |sed 's/-[^-]*$$//'); mv $(KDEB_SOURCENAME).tar.gz ../$(KDEB_SOURCENAME)_$${origversion}.orig.tar.gz
-
dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) -i.git -us -uc
打包
mkdebian
code太长不贴~
- 定义变量:
- version
- packageversion
- revision
- sourcename
- packagename = linux-image-$version
- kernel_headers_packagename=linux-headers-$version
- dbg_packagename=$packagename-dbg
- debarch
- mkdir -p debian/source/
- echo "1.0" > debian/source/format
- echo $debarch > debian/arch
- cat <<EOF > debian/changelog 填充简单内容
- cat <<EOF > debian/copyright 无所谓了
- cat <<EOF > debian/control
这里包的数量定了。173 Source: $sourcename 174 Section: kernel 175 Priority: optional 176 Maintainer: $maintainer 177 Build-Depends: bc, rsync, kmod, cpio, bison, flex | flex:native $extra_build_depends 178 Homepage: http://www.kernel.org/ 179 180 Package: $packagename 181 Architecture: $debarch 182 Description: Linux kernel, version $version 183 This package contains the Linux kernel, modules and corresponding other 184 files, version: $version. 185 186 Package: $kernel_headers_packagename 187 Architecture: $debarch 188 Description: Linux kernel headers for $version on $debarch 189 This package provides kernel header files for $version on $debarch 190 . 191 This is useful for people who need to build external modules 192 193 Package: linux-libc-dev 194 Section: devel 195 Provides: linux-kernel-headers 196 Architecture: $debarch 197 Description: Linux support headers for userspace development 198 This package provides userspaces headers from the Linux kernel. These headers 199 are used by the installed headers for GNU glibc and other system libraries. 200 Multi-Arch: same 201 202 Package: $dbg_packagename 203 Section: debug 204 Architecture: $debarch 205 Description: Linux kernel debugging symbols for $version 206 This package will come in handy if you need to debug the kernel. It provides 207 all the necessary debug symbols for the kernel and its modules.
- cat <<EOF > debian/rules 创建这个文件并赋予执行权限
有3个target,默认target是编译内核。211 #!$(command -v $MAKE) -f 212 213 srctree ?= . 214 215 build: 216 \$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} \ 217 KBUILD_BUILD_VERSION=${revision} -f \$(srctree)/Makefile 218 219 binary-arch: 220 \$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} \ 221 KBUILD_BUILD_VERSION=${revision} -f \$(srctree)/Makefile intdeb-pkg 222 223 clean: 224 rm -rf debian/*tmp debian/files 225 \$(MAKE) clean 226 227 binary: binary-arch
cmd_src_tar $(KDEB_SOURCENAME)
- KDEB_SOURCENAME ?= linux-$(KERNELRELEASE)
- $(srctree)/scripts/setlocalversion --save-scmversion;
从git等代码管理工具中取版本号(大概这个意思) -
tar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz --transform 's:^:$(2)/:S' (TAR_CONTENT) $(3);
- KGZIP = gzip
- TAR_CONTENT := $(KBUILD_ALLDIRS) .config .scmversion Makefile
- export KBUILD_ALLDIRS :=
$(sort $(filter-out arch/%,$(vmlinux-alldirs)) LICENSES arch include scripts tools)
- export RCS_TAR_IGNORE := --exclude SCCS --exclude BitKeeper --exclude .svn --exclude CVS --exclude .pc --exclude .hg --exclude .git
- 总结:将命令展开,就是将linux源码打成 tar.gz包。
origversion=$$(dpkg-parsechangelog -SVersion |sed 's/-[^-]*$$//'); mv $(KDEB_SOURCENAME).tar.gz ../$(KDEB_SOURCENAME)_$${origversion}.orig.tar.gz
重命名源码包
dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) -i.git -us -uc
- 编译
上面生成的debian/rule文件中默认target就是内核编译 - 打包
dpkg-buildpackage
dpkg-buildpackage 流程参考这里。
主要流程就是 : clean -----> build -----> build_binary ------>签名 ------> 打包等。
上面步骤生成的debian/rules中包含了 build build_binary步骤。
build
215 build:
216 \$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} \
217 KBUILD_BUILD_VERSION=${revision} -f \$(srctree)/Makefile
这个就是直接开始内核编译了。
build-binary
219 binary-arch:
220 \$(MAKE) KERNELRELEASE=${version} ARCH=${ARCH} \
221 KBUILD_BUILD_VERSION=${revision} -f \$(srctree)/Makefile intdeb-pkg
intdeb-pkg这个target在文件scripts/Makefile.package
中:
85 PHONY += intdeb-pkg
86 intdeb-pkg:
87 +$(CONFIG_SHELL) $(srctree)/scripts/package/builddeb
builddeb这个代码较长,关键流程如下:
- 定义变量,包含包名、安装位置等关键信息
49 version=$KERNELRELEASE 50 tmpdir="$objtree/debian/tmp" 51 kernel_headers_dir="$objtree/debian/hdrtmp" 52 libc_headers_dir="$objtree/debian/headertmp" 53 dbg_dir="$objtree/debian/dbgtmp" 54 packagename=linux-image-$version 55 kernel_headers_packagename=linux-headers-$version 56 libc_headers_packagename=linux-libc-dev 57 dbg_packagename=$packagename-dbg
- 安装
System.map
和内核配置文件
93 cp System.map "$tmpdir/boot/System.map-$version" 94 cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
- 安装内核镜像
96 cp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path"
- 安装模块
105 if is_enabled CONFIG_MODULES; then 106 INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_install 107 rm -f "$tmpdir/lib/modules/$version/build" 108 rm -f "$tmpdir/lib/modules/$version/source" 109 if [ "$ARCH" = "um" ] ; then 110 mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/" 111 rmdir "$tmpdir/lib/modules/$version" 112 fi 113 if [ -n "$BUILD_DEBUG" ] ; then 114 for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do 115 module=lib/modules/$module 116 mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module) 117 # only keep debug symbols in the debug file 118 $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module 119 # strip original module from debug symbols 120 $OBJCOPY --strip-debug $tmpdir/$module 121 # then add a link to those 122 $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module 123 done 124 125 # resign stripped modules 126 if is_enabled CONFIG_MODULE_SIG_ALL; then 127 INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_sign 128 fi 129 fi 130 fi
- 安装其他文件
167 # Build kernel header package 168 (cd $srctree; find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl) > "$objtree/debian/hdrsrcfiles" 169 (cd $srctree; find arch/*/include include scripts -type f -o -type l) >> "$objtree/debian/hdrsrcfiles" 170 (cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles" 171 (cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles" 172 if is_enabled CONFIG_STACK_VALIDATION; then 173 (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles" 174 fi 175 (cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles" 176 if is_enabled CONFIG_GCC_PLUGINS; then 177 (cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles" 178 fi 179 destdir=$kernel_headers_dir/usr/src/linux-headers-$version 180 mkdir -p "$destdir" 181 (cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -) 182 (cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -) 183 (cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be 184 ln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build" 185 rm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles"
- 打包
- 头文件
create_package "$kernel_headers_packagename" "$kernel_headers_dir"
- libc头文件
create_package "$libc_headers_packagename" "$libc_headers_dir"
- 内核
create_package "$packagename" "$tmpdir"
- debug包
194 if [ -n "$BUILD_DEBUG" ] ; then 195 # Build debug package 196 # Different tools want the image in different locations 197 # perf 198 mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/ 199 cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/ 200 # systemtap 201 mkdir -p $dbg_dir/usr/lib/debug/boot/ 202 ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version 203 # kdump-tools 204 ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version 205 create_package "$dbg_packagename" "$dbg_dir" 206 fi
- 头文件
总结
完全清楚了。