builddeb 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 its 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. cp "$($MAKE -s image_name)" "$tmpdir/$installed_image_path"
  134. if grep -q "^CONFIG_OF=y" $KCONFIG_CONFIG ; then
  135. # Only some architectures with OF support have this target
  136. if grep -q dtbs_install "${srctree}/arch/$SRCARCH/Makefile"; then
  137. $MAKE KBUILD_SRC= INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install
  138. fi
  139. fi
  140. if grep -q '^CONFIG_MODULES=y' $KCONFIG_CONFIG ; then
  141. INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_install
  142. rm -f "$tmpdir/lib/modules/$version/build"
  143. rm -f "$tmpdir/lib/modules/$version/source"
  144. if [ "$ARCH" = "um" ] ; then
  145. mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
  146. rmdir "$tmpdir/lib/modules/$version"
  147. fi
  148. if [ -n "$BUILD_DEBUG" ] ; then
  149. for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
  150. module=lib/modules/$module
  151. mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
  152. # only keep debug symbols in the debug file
  153. $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
  154. # strip original module from debug symbols
  155. $OBJCOPY --strip-debug $tmpdir/$module
  156. # then add a link to those
  157. $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
  158. done
  159. # resign stripped modules
  160. MODULE_SIG_ALL="$(grep -s '^CONFIG_MODULE_SIG_ALL=y' $KCONFIG_CONFIG || true)"
  161. if [ -n "$MODULE_SIG_ALL" ]; then
  162. INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_sign
  163. fi
  164. fi
  165. fi
  166. if [ "$ARCH" != "um" ]; then
  167. $MAKE headers_check KBUILD_SRC=
  168. $MAKE headers_install KBUILD_SRC= INSTALL_HDR_PATH="$libc_headers_dir/usr"
  169. fi
  170. # Install the maintainer scripts
  171. # Note: hook scripts under /etc/kernel are also executed by official Debian
  172. # kernel packages, as well as kernel packages built using make-kpkg.
  173. # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
  174. # so do we; recent versions of dracut and initramfs-tools will obey this.
  175. debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
  176. if grep -q '^CONFIG_BLK_DEV_INITRD=y' $KCONFIG_CONFIG; then
  177. want_initrd=Yes
  178. else
  179. want_initrd=No
  180. fi
  181. for script in postinst postrm preinst prerm ; do
  182. mkdir -p "$tmpdir$debhookdir/$script.d"
  183. cat <<EOF > "$tmpdir/DEBIAN/$script"
  184. #!/bin/sh
  185. set -e
  186. # Pass maintainer script parameters to hook scripts
  187. export DEB_MAINT_PARAMS="\$*"
  188. # Tell initramfs builder whether it's wanted
  189. export INITRD=$want_initrd
  190. test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d
  191. exit 0
  192. EOF
  193. chmod 755 "$tmpdir/DEBIAN/$script"
  194. done
  195. # Try to determine maintainer and email values
  196. if [ -n "$DEBEMAIL" ]; then
  197. email=$DEBEMAIL
  198. elif [ -n "$EMAIL" ]; then
  199. email=$EMAIL
  200. else
  201. email=$(id -nu)@$(hostname -f 2>/dev/null || hostname)
  202. fi
  203. if [ -n "$DEBFULLNAME" ]; then
  204. name=$DEBFULLNAME
  205. elif [ -n "$NAME" ]; then
  206. name=$NAME
  207. else
  208. name="Anonymous"
  209. fi
  210. maintainer="$name <$email>"
  211. # Try to determine distribution
  212. if [ -n "$KDEB_CHANGELOG_DIST" ]; then
  213. distribution=$KDEB_CHANGELOG_DIST
  214. # In some cases lsb_release returns the codename as n/a, which breaks dpkg-parsechangelog
  215. elif distribution=$(lsb_release -cs 2>/dev/null) && [ -n "$distribution" ] && [ "$distribution" != "n/a" ]; then
  216. : # nothing to do in this case
  217. else
  218. distribution="unstable"
  219. echo >&2 "Using default distribution of 'unstable' in the changelog"
  220. echo >&2 "Install lsb-release or set \$KDEB_CHANGELOG_DIST explicitly"
  221. fi
  222. # Generate a simple changelog template
  223. cat <<EOF > debian/changelog
  224. $sourcename ($packageversion) $distribution; urgency=low
  225. * Custom built Linux kernel.
  226. -- $maintainer $(date -R)
  227. EOF
  228. # Generate copyright file
  229. cat <<EOF > debian/copyright
  230. This is a packacked upstream version of the Linux kernel.
  231. The sources may be found at most Linux archive sites, including:
  232. https://www.kernel.org/pub/linux/kernel
  233. Copyright: 1991 - 2017 Linus Torvalds and others.
  234. The git repository for mainline kernel development is at:
  235. git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
  236. This program is free software; you can redistribute it and/or modify
  237. it under the terms of the GNU General Public License as published by
  238. the Free Software Foundation; version 2 dated June, 1991.
  239. On Debian GNU/Linux systems, the complete text of the GNU General Public
  240. License version 2 can be found in \`/usr/share/common-licenses/GPL-2'.
  241. EOF
  242. build_depends="bc, kmod, cpio "
  243. # Generate a control file
  244. cat <<EOF > debian/control
  245. Source: $sourcename
  246. Section: kernel
  247. Priority: optional
  248. Maintainer: $maintainer
  249. Build-Depends: $build_depends
  250. Homepage: http://www.kernel.org/
  251. EOF
  252. if [ "$ARCH" = "um" ]; then
  253. cat <<EOF >> debian/control
  254. Package: $packagename
  255. Architecture: any
  256. Description: User Mode Linux kernel, version $version
  257. User-mode Linux is a port of the Linux kernel to its own system call
  258. interface. It provides a kind of virtual machine, which runs Linux
  259. as a user process under another Linux kernel. This is useful for
  260. kernel development, sandboxes, jails, experimentation, and
  261. many other things.
  262. .
  263. This package contains the Linux kernel, modules and corresponding other
  264. files, version: $version.
  265. EOF
  266. else
  267. cat <<EOF >> debian/control
  268. Package: $packagename
  269. Suggests: $fwpackagename
  270. Architecture: any
  271. Description: Linux kernel, version $version
  272. This package contains the Linux kernel, modules and corresponding other
  273. files, version: $version.
  274. EOF
  275. fi
  276. # Build kernel header package
  277. (cd $srctree; find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl) > "$objtree/debian/hdrsrcfiles"
  278. (cd $srctree; find arch/*/include include scripts -type f) >> "$objtree/debian/hdrsrcfiles"
  279. (cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles"
  280. (cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles"
  281. if grep -q '^CONFIG_STACK_VALIDATION=y' $KCONFIG_CONFIG ; then
  282. (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles"
  283. fi
  284. (cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles"
  285. if grep -q '^CONFIG_GCC_PLUGINS=y' $KCONFIG_CONFIG ; then
  286. (cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles"
  287. fi
  288. destdir=$kernel_headers_dir/usr/src/linux-headers-$version
  289. mkdir -p "$destdir"
  290. (cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -)
  291. (cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -)
  292. (cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be
  293. ln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build"
  294. rm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles"
  295. cat <<EOF >> debian/control
  296. Package: $kernel_headers_packagename
  297. Architecture: any
  298. Description: Linux kernel headers for $KERNELRELEASE on \${kernel:debarch}
  299. This package provides kernel header files for $KERNELRELEASE on \${kernel:debarch}
  300. .
  301. This is useful for people who need to build external modules
  302. EOF
  303. # Do we have firmware? Move it out of the way and build it into a package.
  304. if [ -e "$tmpdir/lib/firmware" ]; then
  305. mv "$tmpdir/lib/firmware"/* "$fwdir/lib/firmware/$version/"
  306. rmdir "$tmpdir/lib/firmware"
  307. cat <<EOF >> debian/control
  308. Package: $fwpackagename
  309. Architecture: all
  310. Description: Linux kernel firmware, version $version
  311. This package contains firmware from the Linux kernel, version $version.
  312. EOF
  313. create_package "$fwpackagename" "$fwdir"
  314. fi
  315. cat <<EOF >> debian/control
  316. Package: $libc_headers_packagename
  317. Section: devel
  318. Provides: linux-kernel-headers
  319. Architecture: any
  320. Description: Linux support headers for userspace development
  321. This package provides userspaces headers from the Linux kernel. These headers
  322. are used by the installed headers for GNU glibc and other system libraries.
  323. EOF
  324. if [ "$ARCH" != "um" ]; then
  325. create_package "$kernel_headers_packagename" "$kernel_headers_dir"
  326. create_package "$libc_headers_packagename" "$libc_headers_dir"
  327. fi
  328. create_package "$packagename" "$tmpdir"
  329. if [ -n "$BUILD_DEBUG" ] ; then
  330. # Build debug package
  331. # Different tools want the image in different locations
  332. # perf
  333. mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
  334. cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
  335. # systemtap
  336. mkdir -p $dbg_dir/usr/lib/debug/boot/
  337. ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
  338. # kdump-tools
  339. ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version
  340. cat <<EOF >> debian/control
  341. Package: $dbg_packagename
  342. Section: debug
  343. Architecture: any
  344. Description: Linux kernel debugging symbols for $version
  345. This package will come in handy if you need to debug the kernel. It provides
  346. all the necessary debug symbols for the kernel and its modules.
  347. EOF
  348. create_package "$dbg_packagename" "$dbg_dir"
  349. fi
  350. if [ "x$1" = "xdeb-pkg" ]
  351. then
  352. cat <<EOF > debian/rules
  353. #!/usr/bin/make -f
  354. build:
  355. \$(MAKE)
  356. binary-arch:
  357. \$(MAKE) KDEB_SOURCENAME=${sourcename} KDEB_PKGVERSION=${packageversion} bindeb-pkg
  358. clean:
  359. rm -rf debian/*tmp debian/files
  360. mv debian/ debian.backup # debian/ might be cleaned away
  361. \$(MAKE) clean
  362. mv debian.backup debian
  363. binary: binary-arch
  364. EOF
  365. mv ${sourcename}.tar.gz ../${sourcename}_${version}.orig.tar.gz
  366. tar caf ../${sourcename}_${packageversion}.debian.tar.gz debian/{copyright,rules,changelog,control}
  367. dpkg-source -cdebian/control -ldebian/changelog --format="3.0 (custom)" --target-format="3.0 (quilt)" \
  368. -b / ../${sourcename}_${version}.orig.tar.gz ../${sourcename}_${packageversion}.debian.tar.gz
  369. mv ${sourcename}_${packageversion}*dsc ..
  370. dpkg-genchanges > ../${sourcename}_${packageversion}_${debarch}.changes
  371. else
  372. dpkg-genchanges -b > ../${sourcename}_${packageversion}_${debarch}.changes
  373. fi
  374. exit 0