11da177e4SLinus Torvalds#!/bin/sh 21da177e4SLinus Torvalds# 34964451aSFrans Pop# builddeb 1.3 41da177e4SLinus Torvalds# Copyright 2003 Wichert Akkerman <wichert@wiggy.net> 51da177e4SLinus Torvalds# 61da177e4SLinus Torvalds# Simple script to generate a deb package for a Linux kernel. All the 74f66199bSFrans Pop# complexity of what to do with a kernel after it is installed or removed 81da177e4SLinus Torvalds# is left to other scripts and packages: they can install scripts in the 9fe233cb6SFrans Pop# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location 10fe233cb6SFrans Pop# specified in KDEB_HOOKDIR) that will be called on package install and 11fe233cb6SFrans Pop# removal. 121da177e4SLinus Torvalds 131da177e4SLinus Torvaldsset -e 141da177e4SLinus Torvalds 153e2ab256SFrans Popcreate_package() { 163e2ab256SFrans Pop local pname="$1" pdir="$2" 173e2ab256SFrans Pop 18bf7b0055SRiku Voipio mkdir -m 755 -p "$pdir/DEBIAN" 19bf7b0055SRiku Voipio mkdir -p "$pdir/usr/share/doc/$pname" 209461f666SFrans Pop cp debian/copyright "$pdir/usr/share/doc/$pname/" 211ab18486Smaximilian attems cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian" 221ab18486Smaximilian attems gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian" 23b59a1225SFEJES Jozsef sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \ 24b59a1225SFEJES Jozsef | xargs -r0 md5sum > DEBIAN/md5sums" 259461f666SFrans Pop 263e2ab256SFrans Pop # Fix ownership and permissions 273e2ab256SFrans Pop chown -R root:root "$pdir" 283e2ab256SFrans Pop chmod -R go-w "$pdir" 29ca617dc6SHenning Schild # in case we are in a restrictive umask environment like 0077 30ca617dc6SHenning Schild chmod -R a+rX "$pdir" 313e2ab256SFrans Pop 32dca0c024SRiku Voipio # Create the package 33b41d920aSRiku Voipio dpkg-gencontrol -p$pname -P"$pdir" 34dca0c024SRiku Voipio dpkg --build "$pdir" .. 35dca0c024SRiku Voipio} 36dca0c024SRiku Voipio 371da177e4SLinus Torvaldsversion=$KERNELRELEASE 381da177e4SLinus Torvaldstmpdir="$objtree/debian/tmp" 39ee81b786Smaximilian attemskernel_headers_dir="$objtree/debian/hdrtmp" 40ee81b786Smaximilian attemslibc_headers_dir="$objtree/debian/headertmp" 41810e8437SAnisse Astierdbg_dir="$objtree/debian/dbgtmp" 42f7a2c31fSmaximilian attemspackagename=linux-image-$version 43ee81b786Smaximilian attemskernel_headers_packagename=linux-headers-$version 44ee81b786Smaximilian attemslibc_headers_packagename=linux-libc-dev 45810e8437SAnisse Astierdbg_packagename=$packagename-dbg 46687c3dacSSam Ravnborg 474f66199bSFrans Popif [ "$ARCH" = "um" ] ; then 48687c3dacSSam Ravnborg packagename=user-mode-linux-$version 49687c3dacSSam Ravnborgfi 501da177e4SLinus Torvalds 519de70176SAnisse Astier# Not all arches have the same installed path in debian 529de70176SAnisse Astier# XXX: have each arch Makefile export a variable of the canonical image install 539de70176SAnisse Astier# path instead 549de70176SAnisse Astiercase $ARCH in 559de70176SAnisse Astierum) 569de70176SAnisse Astier installed_image_path="usr/bin/linux-$version" 579de70176SAnisse Astier ;; 589de70176SAnisse Astierparisc|mips|powerpc) 599de70176SAnisse Astier installed_image_path="boot/vmlinux-$version" 609de70176SAnisse Astier ;; 619de70176SAnisse Astier*) 629de70176SAnisse Astier installed_image_path="boot/vmlinuz-$version" 639de70176SAnisse Astieresac 649de70176SAnisse Astier 65810e8437SAnisse AstierBUILD_DEBUG="$(grep -s '^CONFIG_DEBUG_INFO=y' $KCONFIG_CONFIG || true)" 66810e8437SAnisse Astier 671da177e4SLinus Torvalds# Setup the directory structure 68cc18abbeSMasahiro Yamadarm -rf "$tmpdir" "$kernel_headers_dir" "$libc_headers_dir" "$dbg_dir" $objtree/debian/files 69e86c2412Smaximilian attemsmkdir -m 755 -p "$tmpdir/DEBIAN" 70bf7b0055SRiku Voipiomkdir -p "$tmpdir/lib" "$tmpdir/boot" 71a47b6c61SJoerg Roedelmkdir -p "$kernel_headers_dir/lib/modules/$version/" 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds# Build and install the kernel 744f66199bSFrans Popif [ "$ARCH" = "um" ] ; then 75bf7b0055SRiku Voipio mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename" 76687c3dacSSam Ravnborg $MAKE linux 77687c3dacSSam Ravnborg cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map" 78d2091767SAnisse Astier cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config" 79687c3dacSSam Ravnborg gzip "$tmpdir/usr/share/doc/$packagename/config" 80687c3dacSSam Ravnborgelse 811da177e4SLinus Torvalds cp System.map "$tmpdir/boot/System.map-$version" 82d2091767SAnisse Astier cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version" 839de70176SAnisse Astierfi 8402826a6bSMasahiro Yamadacp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path" 851da177e4SLinus Torvalds 86d5615e47SRob Herringif grep -q "^CONFIG_OF_EARLY_FLATTREE=y" $KCONFIG_CONFIG ; then 87ca2a9d2cSArnaud Patard (Rtp) # Only some architectures with OF support have this target 88d5615e47SRob Herring if [ -d "${srctree}/arch/$SRCARCH/boot/dts" ]; then 89ca2a9d2cSArnaud Patard (Rtp) $MAKE KBUILD_SRC= INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install 90ca2a9d2cSArnaud Patard (Rtp) fi 91ca2a9d2cSArnaud Patard (Rtp)fi 92ca2a9d2cSArnaud Patard (Rtp) 93d2091767SAnisse Astierif grep -q '^CONFIG_MODULES=y' $KCONFIG_CONFIG ; then 94dddcbb7cSJoerg Roedel INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_install 95a47b6c61SJoerg Roedel rm -f "$tmpdir/lib/modules/$version/build" 96a47b6c61SJoerg Roedel rm -f "$tmpdir/lib/modules/$version/source" 974f66199bSFrans Pop if [ "$ARCH" = "um" ] ; then 98687c3dacSSam Ravnborg mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/" 99687c3dacSSam Ravnborg rmdir "$tmpdir/lib/modules/$version" 100687c3dacSSam Ravnborg fi 101810e8437SAnisse Astier if [ -n "$BUILD_DEBUG" ] ; then 1022d087139SMichal Marek for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do 1032d087139SMichal Marek module=lib/modules/$module 104810e8437SAnisse Astier mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module) 105810e8437SAnisse Astier # only keep debug symbols in the debug file 1062d087139SMichal Marek $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module 107810e8437SAnisse Astier # strip original module from debug symbols 1082d087139SMichal Marek $OBJCOPY --strip-debug $tmpdir/$module 109810e8437SAnisse Astier # then add a link to those 1102d087139SMichal Marek $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module 111810e8437SAnisse Astier done 11264178cb6SAndrey Skvortsov 11364178cb6SAndrey Skvortsov # resign stripped modules 11464178cb6SAndrey Skvortsov MODULE_SIG_ALL="$(grep -s '^CONFIG_MODULE_SIG_ALL=y' $KCONFIG_CONFIG || true)" 11564178cb6SAndrey Skvortsov if [ -n "$MODULE_SIG_ALL" ]; then 11664178cb6SAndrey Skvortsov INSTALL_MOD_PATH="$tmpdir" $MAKE KBUILD_SRC= modules_sign 11764178cb6SAndrey Skvortsov fi 118810e8437SAnisse Astier fi 1191da177e4SLinus Torvaldsfi 1201da177e4SLinus Torvalds 121d7d357bcSJoerg Roedelif [ "$ARCH" != "um" ]; then 122dddcbb7cSJoerg Roedel $MAKE headers_check KBUILD_SRC= 123dddcbb7cSJoerg Roedel $MAKE headers_install KBUILD_SRC= INSTALL_HDR_PATH="$libc_headers_dir/usr" 124d7d357bcSJoerg Roedelfi 125e6807785Smaximilian attems 1261da177e4SLinus Torvalds# Install the maintainer scripts 127fe233cb6SFrans Pop# Note: hook scripts under /etc/kernel are also executed by official Debian 1281c8ddae0SBen Hutchings# kernel packages, as well as kernel packages built using make-kpkg. 1291c8ddae0SBen Hutchings# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and 1301c8ddae0SBen Hutchings# so do we; recent versions of dracut and initramfs-tools will obey this. 131fe233cb6SFrans Popdebhookdir=${KDEB_HOOKDIR:-/etc/kernel} 1321c8ddae0SBen Hutchingsif grep -q '^CONFIG_BLK_DEV_INITRD=y' $KCONFIG_CONFIG; then 1331c8ddae0SBen Hutchings want_initrd=Yes 1341c8ddae0SBen Hutchingselse 1351c8ddae0SBen Hutchings want_initrd=No 1361c8ddae0SBen Hutchingsfi 1371da177e4SLinus Torvaldsfor script in postinst postrm preinst prerm ; do 138fe233cb6SFrans Pop mkdir -p "$tmpdir$debhookdir/$script.d" 1391da177e4SLinus Torvalds cat <<EOF > "$tmpdir/DEBIAN/$script" 1401da177e4SLinus Torvalds#!/bin/sh 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvaldsset -e 1431da177e4SLinus Torvalds 1444964451aSFrans Pop# Pass maintainer script parameters to hook scripts 145241ad11fSmaximilian attemsexport DEB_MAINT_PARAMS="\$*" 1464964451aSFrans Pop 1471c8ddae0SBen Hutchings# Tell initramfs builder whether it's wanted 1481c8ddae0SBen Hutchingsexport INITRD=$want_initrd 1491c8ddae0SBen Hutchings 150c95182bfSAnisse Astiertest -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d 1511da177e4SLinus Torvaldsexit 0 1521da177e4SLinus TorvaldsEOF 1531da177e4SLinus Torvalds chmod 755 "$tmpdir/DEBIAN/$script" 1541da177e4SLinus Torvaldsdone 1551da177e4SLinus Torvalds 1568bf13561SFathi Boudra# Build kernel header package 157a765a7ceSMichal Marek(cd $srctree; find . -name Makefile\* -o -name Kconfig\* -o -name \*.pl) > "$objtree/debian/hdrsrcfiles" 158f8437520SJan Kiszka(cd $srctree; find arch/*/include include scripts -type f -o -type l) >> "$objtree/debian/hdrsrcfiles" 1598bf13561SFathi Boudra(cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles" 1608bf13561SFathi Boudra(cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles" 16115f6d337SWilfried Klaebeif grep -q '^CONFIG_STACK_VALIDATION=y' $KCONFIG_CONFIG ; then 16215f6d337SWilfried Klaebe (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles" 16315f6d337SWilfried Klaebefi 164a765a7ceSMichal Marek(cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles" 165d3e2773cSKees Cookif grep -q '^CONFIG_GCC_PLUGINS=y' $KCONFIG_CONFIG ; then 1666b90bd4bSEmese Revfy (cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles" 167d3e2773cSKees Cookfi 168ee81b786Smaximilian attemsdestdir=$kernel_headers_dir/usr/src/linux-headers-$version 169ee81b786Smaximilian attemsmkdir -p "$destdir" 170a765a7ceSMichal Marek(cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -) 171a765a7ceSMichal Marek(cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -) 172d2091767SAnisse Astier(cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be 173a47b6c61SJoerg Roedelln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build" 1746c635224SBen Hutchingsrm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles" 175cd8d60a2STheodore Ts'o 176d7d357bcSJoerg Roedelif [ "$ARCH" != "um" ]; then 177d7d357bcSJoerg Roedel create_package "$kernel_headers_packagename" "$kernel_headers_dir" 178ee81b786Smaximilian attems create_package "$libc_headers_packagename" "$libc_headers_dir" 179d7d357bcSJoerg Roedelfi 180d7d357bcSJoerg Roedel 1813e2ab256SFrans Popcreate_package "$packagename" "$tmpdir" 1821da177e4SLinus Torvalds 183810e8437SAnisse Astierif [ -n "$BUILD_DEBUG" ] ; then 184810e8437SAnisse Astier # Build debug package 185810e8437SAnisse Astier # Different tools want the image in different locations 186810e8437SAnisse Astier # perf 187810e8437SAnisse Astier mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/ 188810e8437SAnisse Astier cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/ 189810e8437SAnisse Astier # systemtap 190810e8437SAnisse Astier mkdir -p $dbg_dir/usr/lib/debug/boot/ 191810e8437SAnisse Astier ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version 192810e8437SAnisse Astier # kdump-tools 193810e8437SAnisse Astier ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version 194810e8437SAnisse Astier create_package "$dbg_packagename" "$dbg_dir" 195810e8437SAnisse Astierfi 196810e8437SAnisse Astier 1971da177e4SLinus Torvaldsexit 0 198