builddeb 12 KB

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