builddeb 14 KB

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