builddeb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #!/bin/sh
  2. #
  3. # builddeb 1.3
  4. # Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
  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. create_package() {
  14. local pname="$1" pdir="$2"
  15. mkdir -m 755 -p "$pdir/DEBIAN"
  16. mkdir -p "$pdir/usr/share/doc/$pname"
  17. cp debian/copyright "$pdir/usr/share/doc/$pname/"
  18. cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
  19. gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
  20. sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
  21. | xargs -r0 md5sum > DEBIAN/md5sums"
  22. # Fix ownership and permissions
  23. chown -R root:root "$pdir"
  24. chmod -R go-w "$pdir"
  25. # in case we are in a restrictive umask environment like 0077
  26. chmod -R a+rX "$pdir"
  27. # Create the package
  28. dpkg-gencontrol $forcearch -Vkernel:debarch="${debarch}" -p$pname -P"$pdir"
  29. dpkg --build "$pdir" ..
  30. }
  31. set_debarch() {
  32. # Attempt to find the correct Debian architecture
  33. case "$UTS_MACHINE" in
  34. i386|ia64|alpha)
  35. debarch="$UTS_MACHINE" ;;
  36. x86_64)
  37. debarch=amd64 ;;
  38. sparc*)
  39. debarch=sparc ;;
  40. s390*)
  41. debarch=s390$(grep -q CONFIG_64BIT=y $KCONFIG_CONFIG && echo x || true) ;;
  42. ppc*)
  43. debarch=$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo ppc64el || echo powerpc) ;;
  44. parisc*)
  45. debarch=hppa ;;
  46. mips*)
  47. debarch=mips$(grep -q CPU_LITTLE_ENDIAN=y $KCONFIG_CONFIG && echo el || true) ;;
  48. aarch64|arm64)
  49. debarch=arm64 ;;
  50. arm*)
  51. if grep -q CONFIG_AEABI=y $KCONFIG_CONFIG; then
  52. if grep -q CONFIG_VFP=y $KCONFIG_CONFIG; then
  53. debarch=armhf
  54. else
  55. debarch=armel
  56. fi
  57. else
  58. debarch=arm
  59. fi
  60. ;;
  61. *)
  62. debarch=$(dpkg --print-architecture)
  63. echo "" >&2
  64. echo "** ** ** WARNING ** ** **" >&2
  65. echo "" >&2
  66. echo "Your architecture doesn't have it's equivalent" >&2
  67. echo "Debian userspace architecture defined!" >&2
  68. echo "Falling back to using your current userspace instead!" >&2
  69. echo "Please add support for $UTS_MACHINE to ${0} ..." >&2
  70. echo "" >&2
  71. esac
  72. if [ -n "$KBUILD_DEBARCH" ] ; then
  73. debarch="$KBUILD_DEBARCH"
  74. fi
  75. forcearch="-DArchitecture=$debarch"
  76. }
  77. # Some variables and settings used throughout the script
  78. version=$KERNELRELEASE
  79. revision=$(cat .version)
  80. if [ -n "$KDEB_PKGVERSION" ]; then
  81. packageversion=$KDEB_PKGVERSION
  82. else
  83. packageversion=$version-$revision
  84. fi
  85. sourcename=$KDEB_SOURCENAME
  86. tmpdir="$objtree/debian/tmp"
  87. fwdir="$objtree/debian/fwtmp"
  88. kernel_headers_dir="$objtree/debian/hdrtmp"
  89. libc_headers_dir="$objtree/debian/headertmp"
  90. dbg_dir="$objtree/debian/dbgtmp"
  91. packagename=linux-image-$version
  92. fwpackagename=linux-firmware-image-$version
  93. kernel_headers_packagename=linux-headers-$version
  94. libc_headers_packagename=linux-libc-dev
  95. dbg_packagename=$packagename-dbg
  96. debarch=
  97. forcearch=
  98. set_debarch
  99. if [ "$ARCH" = "um" ] ; then
  100. packagename=user-mode-linux-$version
  101. fi
  102. # Not all arches have the same installed path in debian
  103. # XXX: have each arch Makefile export a variable of the canonical image install
  104. # path instead
  105. case $ARCH in
  106. um)
  107. installed_image_path="usr/bin/linux-$version"
  108. ;;
  109. parisc|mips|powerpc)
  110. installed_image_path="boot/vmlinux-$version"
  111. ;;
  112. *)
  113. installed_image_path="boot/vmlinuz-$version"
  114. esac
  115. BUILD_DEBUG="$(grep -s '^CONFIG_DEBUG_INFO=y' $KCONFIG_CONFIG || true)"
  116. # Setup the directory structure
  117. rm -rf "$tmpdir" "$fwdir" "$kernel_headers_dir" "$libc_headers_dir" "$dbg_dir" $objtree/debian/files
  118. mkdir -m 755 -p "$tmpdir/DEBIAN"
  119. mkdir -p "$tmpdir/lib" "$tmpdir/boot"
  120. mkdir -p "$fwdir/lib/firmware/$version/"
  121. mkdir -p "$kernel_headers_dir/lib/modules/$version/"
  122. # Build and install the kernel
  123. if [ "$ARCH" = "um" ] ; then
  124. mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename"
  125. $MAKE linux
  126. cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map"
  127. cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config"
  128. gzip "$tmpdir/usr/share/doc/$packagename/config"
  129. else
  130. cp System.map "$tmpdir/boot/System.map-$version"
  131. cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
  132. fi
  133. # Not all arches include the boot path in KBUILD_IMAGE
  134. if [ -e $KBUILD_IMAGE ]; then
  135. cp $KBUILD_IMAGE "$tmpdir/$installed_image_path"
  136. else
  137. cp arch/$ARCH/boot/$KBUILD_IMAGE "$tmpdir/$installed_image_path"
  138. fi
  139. if grep -q "^CONFIG_OF=y" $KCONFIG_CONFIG ; then
  140. # Only some architectures with OF support have this target
  141. if grep -q dtbs_install "${srctree}/arch/$SRCARCH/Makefile"; then
  142. $MAKE KBUILD_SRC= INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install
  143. fi
  144. fi
  145. if grep -q '^CONFIG_MODULES=y' $KCONFIG_CONFIG ; then
  146. INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_install
  147. rm -f "$tmpdir/lib/modules/$version/build"
  148. rm -f "$tmpdir/lib/modules/$version/source"
  149. if [ "$ARCH" = "um" ] ; then
  150. mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
  151. rmdir "$tmpdir/lib/modules/$version"
  152. fi
  153. if [ -n "$BUILD_DEBUG" ] ; then
  154. for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
  155. module=lib/modules/$module
  156. mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
  157. # only keep debug symbols in the debug file
  158. $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
  159. # strip original module from debug symbols
  160. $OBJCOPY --strip-debug $tmpdir/$module
  161. # then add a link to those
  162. $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
  163. done
  164. # resign stripped modules
  165. MODULE_SIG_ALL="$(grep -s '^CONFIG_MODULE_SIG_ALL=y' $KCONFIG_CONFIG || true)"
  166. if [ -n "$MODULE_SIG_ALL" ]; then
  167. INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_sign
  168. fi
  169. fi
  170. fi
  171. if [ "$ARCH" != "um" ]; then
  172. $MAKE headers_check KBUILD_SRC=
  173. $MAKE headers_install KBUILD_SRC= INSTALL_HDR_PATH="$libc_headers_dir/usr"
  174. fi
  175. # Install the maintainer scripts
  176. # Note: hook scripts under /etc/kernel are also executed by official Debian
  177. # kernel packages, as well as kernel packages built using make-kpkg.
  178. # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
  179. # so do we; recent versions of dracut and initramfs-tools will obey this.
  180. debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
  181. if grep -q '^CONFIG_BLK_DEV_INITRD=y' $KCONFIG_CONFIG; then
  182. want_initrd=Yes
  183. else
  184. want_initrd=No
  185. fi
  186. for script in postinst postrm preinst prerm ; do
  187. mkdir -p "$tmpdir$debhookdir/$script.d"
  188. cat <<EOF > "$tmpdir/DEBIAN/$script"
  189. #!/bin/sh
  190. set -e
  191. # Pass maintainer script parameters to hook scripts
  192. export DEB_MAINT_PARAMS="\$*"
  193. # Tell initramfs builder whether it's wanted
  194. export INITRD=$want_initrd
  195. test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d
  196. exit 0
  197. EOF
  198. chmod 755 "$tmpdir/DEBIAN/$script"
  199. done
  200. # Try to determine maintainer and email values
  201. if [ -n "$DEBEMAIL" ]; then
  202. email=$DEBEMAIL
  203. elif [ -n "$EMAIL" ]; then
  204. email=$EMAIL
  205. else
  206. email=$(id -nu)@$(hostname -f 2>/dev/null || hostname)
  207. fi
  208. if [ -n "$DEBFULLNAME" ]; then
  209. name=$DEBFULLNAME
  210. elif [ -n "$NAME" ]; then
  211. name=$NAME
  212. else
  213. name="Anonymous"
  214. fi
  215. maintainer="$name <$email>"
  216. # Try to determine distribution
  217. if [ -n "$KDEB_CHANGELOG_DIST" ]; then
  218. distribution=$KDEB_CHANGELOG_DIST
  219. # In some cases lsb_release returns the codename as n/a, which breaks dpkg-parsechangelog
  220. elif distribution=$(lsb_release -cs 2>/dev/null) && [ -n "$distribution" ] && [ "$distribution" != "n/a" ]; then
  221. : # nothing to do in this case
  222. else
  223. distribution="unstable"
  224. echo >&2 "Using default distribution of 'unstable' in the changelog"
  225. echo >&2 "Install lsb-release or set \$KDEB_CHANGELOG_DIST explicitly"
  226. fi
  227. # Generate a simple changelog template
  228. cat <<EOF > debian/changelog
  229. $sourcename ($packageversion) $distribution; urgency=low
  230. * Custom built Linux kernel.
  231. -- $maintainer $(date -R)
  232. EOF
  233. # Generate copyright file
  234. cat <<EOF > debian/copyright
  235. This is a packacked upstream version of the Linux kernel.
  236. The sources may be found at most Linux ftp sites, including:
  237. ftp://ftp.kernel.org/pub/linux/kernel
  238. Copyright: 1991 - 2015 Linus Torvalds and others.
  239. The git repository for mainline kernel development is at:
  240. git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
  241. This program is free software; you can redistribute it and/or modify
  242. it under the terms of the GNU General Public License as published by
  243. the Free Software Foundation; version 2 dated June, 1991.
  244. On Debian GNU/Linux systems, the complete text of the GNU General Public
  245. License version 2 can be found in \`/usr/share/common-licenses/GPL-2'.
  246. EOF
  247. build_depends="bc, kmod, cpio "
  248. # Generate a control file
  249. cat <<EOF > debian/control
  250. Source: $sourcename
  251. Section: kernel
  252. Priority: optional
  253. Maintainer: $maintainer
  254. Build-Depends: $build_depends
  255. Standards-Version: 3.8.4
  256. Homepage: http://www.kernel.org/
  257. EOF
  258. if [ "$ARCH" = "um" ]; then
  259. cat <<EOF >> debian/control
  260. Package: $packagename
  261. Provides: linux-image, linux-image-2.6, linux-modules-$version
  262. Architecture: any
  263. Description: User Mode Linux kernel, version $version
  264. User-mode Linux is a port of the Linux kernel to its own system call
  265. interface. It provides a kind of virtual machine, which runs Linux
  266. as a user process under another Linux kernel. This is useful for
  267. kernel development, sandboxes, jails, experimentation, and
  268. many other things.
  269. .
  270. This package contains the Linux kernel, modules and corresponding other
  271. files, version: $version.
  272. EOF
  273. else
  274. cat <<EOF >> debian/control
  275. Package: $packagename
  276. Provides: linux-image, linux-image-2.6, linux-modules-$version
  277. Suggests: $fwpackagename
  278. Architecture: any
  279. Description: Linux kernel, version $version
  280. This package contains the Linux kernel, modules and corresponding other
  281. files, version: $version.
  282. EOF
  283. fi
  284. # Build kernel header package
  285. (cd $srctree; find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl) > "$objtree/debian/hdrsrcfiles"
  286. (cd $srctree; find arch/*/include include scripts -type f -o -type l) >> "$objtree/debian/hdrsrcfiles"
  287. (cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles"
  288. (cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles"
  289. if grep -q '^CONFIG_STACK_VALIDATION=y' $KCONFIG_CONFIG ; then
  290. (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles"
  291. fi
  292. (cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles"
  293. if grep -q '^CONFIG_GCC_PLUGINS=y' $KCONFIG_CONFIG ; then
  294. (cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles"
  295. fi
  296. destdir=$kernel_headers_dir/usr/src/linux-headers-$version
  297. mkdir -p "$destdir"
  298. (cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -)
  299. (cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -)
  300. (cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be
  301. ln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build"
  302. rm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles"
  303. cat <<EOF >> debian/control
  304. Package: $kernel_headers_packagename
  305. Provides: linux-headers, linux-headers-2.6
  306. Architecture: any
  307. Description: Linux kernel headers for $KERNELRELEASE on \${kernel:debarch}
  308. This package provides kernel header files for $KERNELRELEASE on \${kernel:debarch}
  309. .
  310. This is useful for people who need to build external modules
  311. EOF
  312. # Do we have firmware? Move it out of the way and build it into a package.
  313. if [ -e "$tmpdir/lib/firmware" ]; then
  314. mv "$tmpdir/lib/firmware"/* "$fwdir/lib/firmware/$version/"
  315. rmdir "$tmpdir/lib/firmware"
  316. cat <<EOF >> debian/control
  317. Package: $fwpackagename
  318. Architecture: all
  319. Description: Linux kernel firmware, version $version
  320. This package contains firmware from the Linux kernel, version $version.
  321. EOF
  322. create_package "$fwpackagename" "$fwdir"
  323. fi
  324. cat <<EOF >> debian/control
  325. Package: $libc_headers_packagename
  326. Section: devel
  327. Provides: linux-kernel-headers
  328. Architecture: any
  329. Description: Linux support headers for userspace development
  330. This package provides userspaces headers from the Linux kernel. These headers
  331. are used by the installed headers for GNU glibc and other system libraries.
  332. EOF
  333. if [ "$ARCH" != "um" ]; then
  334. create_package "$kernel_headers_packagename" "$kernel_headers_dir"
  335. create_package "$libc_headers_packagename" "$libc_headers_dir"
  336. fi
  337. create_package "$packagename" "$tmpdir"
  338. if [ -n "$BUILD_DEBUG" ] ; then
  339. # Build debug package
  340. # Different tools want the image in different locations
  341. # perf
  342. mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
  343. cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
  344. # systemtap
  345. mkdir -p $dbg_dir/usr/lib/debug/boot/
  346. ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
  347. # kdump-tools
  348. ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version
  349. cat <<EOF >> debian/control
  350. Package: $dbg_packagename
  351. Section: debug
  352. Provides: linux-debug, linux-debug-$version
  353. Architecture: any
  354. Description: Linux kernel debugging symbols for $version
  355. This package will come in handy if you need to debug the kernel. It provides
  356. all the necessary debug symbols for the kernel and its modules.
  357. EOF
  358. create_package "$dbg_packagename" "$dbg_dir"
  359. fi
  360. if [ "x$1" = "xdeb-pkg" ]
  361. then
  362. cat <<EOF > debian/rules
  363. #!/usr/bin/make -f
  364. build:
  365. \$(MAKE)
  366. binary-arch:
  367. \$(MAKE) KDEB_SOURCENAME=${sourcename} KDEB_PKGVERSION=${packageversion} bindeb-pkg
  368. clean:
  369. rm -rf debian/*tmp debian/files
  370. mv debian/ debian.backup # debian/ might be cleaned away
  371. \$(MAKE) clean
  372. mv debian.backup debian
  373. binary: binary-arch
  374. EOF
  375. mv ${sourcename}.tar.gz ../${sourcename}_${version}.orig.tar.gz
  376. tar caf ../${sourcename}_${packageversion}.debian.tar.gz debian/{copyright,rules,changelog,control}
  377. dpkg-source -cdebian/control -ldebian/changelog --format="3.0 (custom)" --target-format="3.0 (quilt)" \
  378. -b / ../${sourcename}_${version}.orig.tar.gz ../${sourcename}_${packageversion}.debian.tar.gz
  379. mv ${sourcename}_${packageversion}*dsc ..
  380. dpkg-genchanges > ../${sourcename}_${packageversion}_${debarch}.changes
  381. else
  382. dpkg-genchanges -b > ../${sourcename}_${packageversion}_${debarch}.changes
  383. fi
  384. exit 0