builddeb 13 KB

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