builddeb 12 KB

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