builddeb 14 KB

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