builddeb 13 KB

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