builddeb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/sh
  2. #
  3. # builddeb 1.3
  4. # Copyright 2003 Wichert Akkerman <[email protected]>
  5. #
  6. # Simple script to generate a deb package for a Linux kernel. All the
  7. # complexity of what to do with a kernel after it is installed or removed
  8. # is left to other scripts and packages: they can install scripts in the
  9. # /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
  10. # specified in KDEB_HOOKDIR) that will be called on package install and
  11. # removal.
  12. set -e
  13. is_enabled() {
  14. grep -q "^$1=y" include/config/auto.conf
  15. }
  16. if_enabled_echo() {
  17. if is_enabled "$1"; then
  18. echo -n "$2"
  19. elif [ $# -ge 3 ]; then
  20. echo -n "$3"
  21. fi
  22. }
  23. create_package() {
  24. local pname="$1" pdir="$2"
  25. local dpkg_deb_opts
  26. mkdir -m 755 -p "$pdir/DEBIAN"
  27. mkdir -p "$pdir/usr/share/doc/$pname"
  28. cp debian/copyright "$pdir/usr/share/doc/$pname/"
  29. cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
  30. gzip -n -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
  31. sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
  32. | xargs -r0 md5sum > DEBIAN/md5sums"
  33. # Fix ownership and permissions
  34. if [ "$DEB_RULES_REQUIRES_ROOT" = "no" ]; then
  35. dpkg_deb_opts="--root-owner-group"
  36. else
  37. chown -R root:root "$pdir"
  38. fi
  39. chmod -R go-w "$pdir"
  40. # in case we are in a restrictive umask environment like 0077
  41. chmod -R a+rX "$pdir"
  42. # in case we build in a setuid/setgid directory
  43. chmod -R ug-s "$pdir"
  44. # Create the package
  45. dpkg-gencontrol -p$pname -P"$pdir"
  46. dpkg-deb $dpkg_deb_opts ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
  47. }
  48. deploy_kernel_headers () {
  49. pdir=$1
  50. rm -rf $pdir
  51. (
  52. cd $srctree
  53. find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
  54. find include scripts -type f -o -type l
  55. find arch/$SRCARCH -name Kbuild.platforms -o -name Platform
  56. find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
  57. ) > debian/hdrsrcfiles
  58. {
  59. if is_enabled CONFIG_OBJTOOL; then
  60. echo tools/objtool/objtool
  61. fi
  62. find arch/$SRCARCH/include Module.symvers include scripts -type f
  63. if is_enabled CONFIG_GCC_PLUGINS; then
  64. find scripts/gcc-plugins -name \*.so
  65. fi
  66. } > debian/hdrobjfiles
  67. destdir=$pdir/usr/src/linux-headers-$version
  68. mkdir -p $destdir
  69. tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir
  70. tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir
  71. rm -f debian/hdrsrcfiles debian/hdrobjfiles
  72. # copy .config manually to be where it's expected to be
  73. cp $KCONFIG_CONFIG $destdir/.config
  74. mkdir -p $pdir/lib/modules/$version/
  75. ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
  76. }
  77. deploy_libc_headers () {
  78. pdir=$1
  79. rm -rf $pdir
  80. $MAKE -f $srctree/Makefile headers
  81. $MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr
  82. # move asm headers to /usr/include/<libc-machine>/asm to match the structure
  83. # used by Debian-based distros (to support multi-arch)
  84. host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH)
  85. mkdir $pdir/usr/include/$host_arch
  86. mv $pdir/usr/include/asm $pdir/usr/include/$host_arch/
  87. }
  88. version=$KERNELRELEASE
  89. tmpdir=debian/linux-image
  90. dbg_dir=debian/linux-image-dbg
  91. packagename=linux-image-$version
  92. dbg_packagename=$packagename-dbg
  93. if [ "$ARCH" = "um" ] ; then
  94. packagename=user-mode-linux-$version
  95. fi
  96. # Not all arches have the same installed path in debian
  97. # XXX: have each arch Makefile export a variable of the canonical image install
  98. # path instead
  99. case $ARCH in
  100. um)
  101. installed_image_path="usr/bin/linux-$version"
  102. ;;
  103. parisc|mips|powerpc)
  104. installed_image_path="boot/vmlinux-$version"
  105. ;;
  106. *)
  107. installed_image_path="boot/vmlinuz-$version"
  108. esac
  109. BUILD_DEBUG=$(if_enabled_echo CONFIG_DEBUG_INFO Yes)
  110. # Setup the directory structure
  111. rm -rf "$tmpdir" "$dbg_dir" debian/files
  112. mkdir -m 755 -p "$tmpdir/DEBIAN"
  113. mkdir -p "$tmpdir/lib" "$tmpdir/boot"
  114. # Install the kernel
  115. if [ "$ARCH" = "um" ] ; then
  116. mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename"
  117. cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map"
  118. cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config"
  119. gzip "$tmpdir/usr/share/doc/$packagename/config"
  120. else
  121. cp System.map "$tmpdir/boot/System.map-$version"
  122. cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
  123. fi
  124. cp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path"
  125. if is_enabled CONFIG_OF_EARLY_FLATTREE; then
  126. # Only some architectures with OF support have this target
  127. if [ -d "${srctree}/arch/$SRCARCH/boot/dts" ]; then
  128. $MAKE -f $srctree/Makefile INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install
  129. fi
  130. fi
  131. if is_enabled CONFIG_MODULES; then
  132. INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_install
  133. rm -f "$tmpdir/lib/modules/$version/build"
  134. rm -f "$tmpdir/lib/modules/$version/source"
  135. if [ "$ARCH" = "um" ] ; then
  136. mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
  137. rmdir "$tmpdir/lib/modules/$version"
  138. fi
  139. if [ -n "$BUILD_DEBUG" ] ; then
  140. for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
  141. module=lib/modules/$module
  142. mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
  143. # only keep debug symbols in the debug file
  144. $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
  145. # strip original module from debug symbols
  146. $OBJCOPY --strip-debug $tmpdir/$module
  147. # then add a link to those
  148. $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
  149. done
  150. # resign stripped modules
  151. if is_enabled CONFIG_MODULE_SIG_ALL; then
  152. INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_sign
  153. fi
  154. fi
  155. fi
  156. # Install the maintainer scripts
  157. # Note: hook scripts under /etc/kernel are also executed by official Debian
  158. # kernel packages, as well as kernel packages built using make-kpkg.
  159. # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
  160. # so do we; recent versions of dracut and initramfs-tools will obey this.
  161. debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
  162. for script in postinst postrm preinst prerm ; do
  163. mkdir -p "$tmpdir$debhookdir/$script.d"
  164. cat <<EOF > "$tmpdir/DEBIAN/$script"
  165. #!/bin/sh
  166. set -e
  167. # Pass maintainer script parameters to hook scripts
  168. export DEB_MAINT_PARAMS="\$*"
  169. # Tell initramfs builder whether it's wanted
  170. export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
  171. test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d
  172. exit 0
  173. EOF
  174. chmod 755 "$tmpdir/DEBIAN/$script"
  175. done
  176. if [ "$ARCH" != "um" ]; then
  177. if is_enabled CONFIG_MODULES; then
  178. deploy_kernel_headers debian/linux-headers
  179. create_package linux-headers-$version debian/linux-headers
  180. fi
  181. deploy_libc_headers debian/linux-libc-dev
  182. create_package linux-libc-dev debian/linux-libc-dev
  183. fi
  184. create_package "$packagename" "$tmpdir"
  185. if [ -n "$BUILD_DEBUG" ] ; then
  186. # Build debug package
  187. # Different tools want the image in different locations
  188. # perf
  189. mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
  190. cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
  191. # systemtap
  192. mkdir -p $dbg_dir/usr/lib/debug/boot/
  193. ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
  194. # kdump-tools
  195. ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version
  196. create_package "$dbg_packagename" "$dbg_dir"
  197. fi
  198. exit 0