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 15515f4c63SMasahiro Yamadais_enabled() { 166fb7ef5aSMasahiro Yamada grep -q "^$1=y" include/config/auto.conf 17515f4c63SMasahiro Yamada} 18515f4c63SMasahiro Yamada 19515f4c63SMasahiro Yamadaif_enabled_echo() { 20515f4c63SMasahiro Yamada if is_enabled "$1"; then 21515f4c63SMasahiro Yamada echo -n "$2" 22515f4c63SMasahiro Yamada elif [ $# -ge 3 ]; then 23515f4c63SMasahiro Yamada echo -n "$3" 24515f4c63SMasahiro Yamada fi 25515f4c63SMasahiro Yamada} 26515f4c63SMasahiro Yamada 273e2ab256SFrans Popcreate_package() { 283e2ab256SFrans Pop local pname="$1" pdir="$2" 293e2ab256SFrans Pop 30bf7b0055SRiku Voipio mkdir -m 755 -p "$pdir/DEBIAN" 31bf7b0055SRiku Voipio mkdir -p "$pdir/usr/share/doc/$pname" 329461f666SFrans Pop cp debian/copyright "$pdir/usr/share/doc/$pname/" 331ab18486Smaximilian attems cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian" 341ab18486Smaximilian attems gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian" 35b59a1225SFEJES Jozsef sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \ 36b59a1225SFEJES Jozsef | xargs -r0 md5sum > DEBIAN/md5sums" 379461f666SFrans Pop 383e2ab256SFrans Pop # Fix ownership and permissions 393e2ab256SFrans Pop chown -R root:root "$pdir" 403e2ab256SFrans Pop chmod -R go-w "$pdir" 41ca617dc6SHenning Schild # in case we are in a restrictive umask environment like 0077 42ca617dc6SHenning Schild chmod -R a+rX "$pdir" 433e2ab256SFrans Pop 44dca0c024SRiku Voipio # Create the package 45b41d920aSRiku Voipio dpkg-gencontrol -p$pname -P"$pdir" 46dca0c024SRiku Voipio dpkg --build "$pdir" .. 47dca0c024SRiku Voipio} 48dca0c024SRiku Voipio 491da177e4SLinus Torvaldsversion=$KERNELRELEASE 501da177e4SLinus Torvaldstmpdir="$objtree/debian/tmp" 51ee81b786Smaximilian attemskernel_headers_dir="$objtree/debian/hdrtmp" 52ee81b786Smaximilian attemslibc_headers_dir="$objtree/debian/headertmp" 53810e8437SAnisse Astierdbg_dir="$objtree/debian/dbgtmp" 54f7a2c31fSmaximilian attemspackagename=linux-image-$version 55ee81b786Smaximilian attemskernel_headers_packagename=linux-headers-$version 56ee81b786Smaximilian attemslibc_headers_packagename=linux-libc-dev 57810e8437SAnisse Astierdbg_packagename=$packagename-dbg 58687c3dacSSam Ravnborg 594f66199bSFrans Popif [ "$ARCH" = "um" ] ; then 60687c3dacSSam Ravnborg packagename=user-mode-linux-$version 61687c3dacSSam Ravnborgfi 621da177e4SLinus Torvalds 639de70176SAnisse Astier# Not all arches have the same installed path in debian 649de70176SAnisse Astier# XXX: have each arch Makefile export a variable of the canonical image install 659de70176SAnisse Astier# path instead 669de70176SAnisse Astiercase $ARCH in 679de70176SAnisse Astierum) 689de70176SAnisse Astier installed_image_path="usr/bin/linux-$version" 699de70176SAnisse Astier ;; 709de70176SAnisse Astierparisc|mips|powerpc) 719de70176SAnisse Astier installed_image_path="boot/vmlinux-$version" 729de70176SAnisse Astier ;; 739de70176SAnisse Astier*) 749de70176SAnisse Astier installed_image_path="boot/vmlinuz-$version" 759de70176SAnisse Astieresac 769de70176SAnisse Astier 77515f4c63SMasahiro YamadaBUILD_DEBUG=$(if_enabled_echo CONFIG_DEBUG_INFO Yes) 78810e8437SAnisse Astier 791da177e4SLinus Torvalds# Setup the directory structure 80cc18abbeSMasahiro Yamadarm -rf "$tmpdir" "$kernel_headers_dir" "$libc_headers_dir" "$dbg_dir" $objtree/debian/files 81e86c2412Smaximilian attemsmkdir -m 755 -p "$tmpdir/DEBIAN" 82bf7b0055SRiku Voipiomkdir -p "$tmpdir/lib" "$tmpdir/boot" 83a47b6c61SJoerg Roedelmkdir -p "$kernel_headers_dir/lib/modules/$version/" 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds# Build and install the kernel 864f66199bSFrans Popif [ "$ARCH" = "um" ] ; then 87bf7b0055SRiku Voipio mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename" 88687c3dacSSam Ravnborg $MAKE linux 89687c3dacSSam Ravnborg cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map" 90d2091767SAnisse Astier cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config" 91687c3dacSSam Ravnborg gzip "$tmpdir/usr/share/doc/$packagename/config" 92687c3dacSSam Ravnborgelse 931da177e4SLinus Torvalds cp System.map "$tmpdir/boot/System.map-$version" 94d2091767SAnisse Astier cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version" 959de70176SAnisse Astierfi 9602826a6bSMasahiro Yamadacp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path" 971da177e4SLinus Torvalds 98515f4c63SMasahiro Yamadaif is_enabled CONFIG_OF_EARLY_FLATTREE; then 99ca2a9d2cSArnaud Patard (Rtp) # Only some architectures with OF support have this target 100d5615e47SRob Herring if [ -d "${srctree}/arch/$SRCARCH/boot/dts" ]; then 101175209ccSMasahiro Yamada $MAKE -f $srctree/Makefile INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install 102ca2a9d2cSArnaud Patard (Rtp) fi 103ca2a9d2cSArnaud Patard (Rtp)fi 104ca2a9d2cSArnaud Patard (Rtp) 105515f4c63SMasahiro Yamadaif is_enabled CONFIG_MODULES; then 106175209ccSMasahiro Yamada INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_install 107a47b6c61SJoerg Roedel rm -f "$tmpdir/lib/modules/$version/build" 108a47b6c61SJoerg Roedel rm -f "$tmpdir/lib/modules/$version/source" 1094f66199bSFrans Pop if [ "$ARCH" = "um" ] ; then 110687c3dacSSam Ravnborg mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/" 111687c3dacSSam Ravnborg rmdir "$tmpdir/lib/modules/$version" 112687c3dacSSam Ravnborg fi 113810e8437SAnisse Astier if [ -n "$BUILD_DEBUG" ] ; then 1142d087139SMichal Marek for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do 1152d087139SMichal Marek module=lib/modules/$module 116810e8437SAnisse Astier mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module) 117810e8437SAnisse Astier # only keep debug symbols in the debug file 1182d087139SMichal Marek $OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module 119810e8437SAnisse Astier # strip original module from debug symbols 1202d087139SMichal Marek $OBJCOPY --strip-debug $tmpdir/$module 121810e8437SAnisse Astier # then add a link to those 1222d087139SMichal Marek $OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module 123810e8437SAnisse Astier done 12464178cb6SAndrey Skvortsov 12564178cb6SAndrey Skvortsov # resign stripped modules 126515f4c63SMasahiro Yamada if is_enabled CONFIG_MODULE_SIG_ALL; then 127175209ccSMasahiro Yamada INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_sign 12864178cb6SAndrey Skvortsov fi 129810e8437SAnisse Astier fi 1301da177e4SLinus Torvaldsfi 1311da177e4SLinus Torvalds 132d7d357bcSJoerg Roedelif [ "$ARCH" != "um" ]; then 1330315bb7aSMasahiro Yamada $MAKE -f $srctree/Makefile headers 134175209ccSMasahiro Yamada $MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH="$libc_headers_dir/usr" 1355a464217SCedric Hombourger # move asm headers to /usr/include/<libc-machine>/asm to match the structure 1365a464217SCedric Hombourger # used by Debian-based distros (to support multi-arch) 1375a464217SCedric Hombourger host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH) 1385a464217SCedric Hombourger mkdir $libc_headers_dir/usr/include/$host_arch 1395a464217SCedric Hombourger mv $libc_headers_dir/usr/include/asm $libc_headers_dir/usr/include/$host_arch/ 140d7d357bcSJoerg Roedelfi 141e6807785Smaximilian attems 1421da177e4SLinus Torvalds# Install the maintainer scripts 143fe233cb6SFrans Pop# Note: hook scripts under /etc/kernel are also executed by official Debian 1441c8ddae0SBen Hutchings# kernel packages, as well as kernel packages built using make-kpkg. 1451c8ddae0SBen Hutchings# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and 1461c8ddae0SBen Hutchings# so do we; recent versions of dracut and initramfs-tools will obey this. 147fe233cb6SFrans Popdebhookdir=${KDEB_HOOKDIR:-/etc/kernel} 1481da177e4SLinus Torvaldsfor script in postinst postrm preinst prerm ; do 149fe233cb6SFrans Pop mkdir -p "$tmpdir$debhookdir/$script.d" 1501da177e4SLinus Torvalds cat <<EOF > "$tmpdir/DEBIAN/$script" 1511da177e4SLinus Torvalds#!/bin/sh 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvaldsset -e 1541da177e4SLinus Torvalds 1554964451aSFrans Pop# Pass maintainer script parameters to hook scripts 156241ad11fSmaximilian attemsexport DEB_MAINT_PARAMS="\$*" 1574964451aSFrans Pop 1581c8ddae0SBen Hutchings# Tell initramfs builder whether it's wanted 159515f4c63SMasahiro Yamadaexport INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No) 1601c8ddae0SBen Hutchings 161c95182bfSAnisse Astiertest -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d 1621da177e4SLinus Torvaldsexit 0 1631da177e4SLinus TorvaldsEOF 1641da177e4SLinus Torvalds chmod 755 "$tmpdir/DEBIAN/$script" 1651da177e4SLinus Torvaldsdone 1661da177e4SLinus Torvalds 1678bf13561SFathi Boudra# Build kernel header package 1689945722aSMichał Mirosław(cd $srctree; find . arch/$SRCARCH -maxdepth 1 -name Makefile\*) > "$objtree/debian/hdrsrcfiles" 1699945722aSMichał Mirosław(cd $srctree; find include scripts -type f -o -type l) >> "$objtree/debian/hdrsrcfiles" 1708bf13561SFathi Boudra(cd $srctree; find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform) >> "$objtree/debian/hdrsrcfiles" 1718bf13561SFathi Boudra(cd $srctree; find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f) >> "$objtree/debian/hdrsrcfiles" 172515f4c63SMasahiro Yamadaif is_enabled CONFIG_STACK_VALIDATION; then 17315f6d337SWilfried Klaebe (cd $objtree; find tools/objtool -type f -executable) >> "$objtree/debian/hdrobjfiles" 17415f6d337SWilfried Klaebefi 175a765a7ceSMichal Marek(cd $objtree; find arch/$SRCARCH/include Module.symvers include scripts -type f) >> "$objtree/debian/hdrobjfiles" 176515f4c63SMasahiro Yamadaif is_enabled CONFIG_GCC_PLUGINS; then 1776b90bd4bSEmese Revfy (cd $objtree; find scripts/gcc-plugins -name \*.so -o -name gcc-common.h) >> "$objtree/debian/hdrobjfiles" 178d3e2773cSKees Cookfi 179ee81b786Smaximilian attemsdestdir=$kernel_headers_dir/usr/src/linux-headers-$version 180ee81b786Smaximilian attemsmkdir -p "$destdir" 181a765a7ceSMichal Marek(cd $srctree; tar -c -f - -T -) < "$objtree/debian/hdrsrcfiles" | (cd $destdir; tar -xf -) 182a765a7ceSMichal Marek(cd $objtree; tar -c -f - -T -) < "$objtree/debian/hdrobjfiles" | (cd $destdir; tar -xf -) 183d2091767SAnisse Astier(cd $objtree; cp $KCONFIG_CONFIG $destdir/.config) # copy .config manually to be where it's expected to be 184a47b6c61SJoerg Roedelln -sf "/usr/src/linux-headers-$version" "$kernel_headers_dir/lib/modules/$version/build" 1856c635224SBen Hutchingsrm -f "$objtree/debian/hdrsrcfiles" "$objtree/debian/hdrobjfiles" 186cd8d60a2STheodore Ts'o 187d7d357bcSJoerg Roedelif [ "$ARCH" != "um" ]; then 188d7d357bcSJoerg Roedel create_package "$kernel_headers_packagename" "$kernel_headers_dir" 189ee81b786Smaximilian attems create_package "$libc_headers_packagename" "$libc_headers_dir" 190d7d357bcSJoerg Roedelfi 191d7d357bcSJoerg Roedel 1923e2ab256SFrans Popcreate_package "$packagename" "$tmpdir" 1931da177e4SLinus Torvalds 194810e8437SAnisse Astierif [ -n "$BUILD_DEBUG" ] ; then 195810e8437SAnisse Astier # Build debug package 196810e8437SAnisse Astier # Different tools want the image in different locations 197810e8437SAnisse Astier # perf 198810e8437SAnisse Astier mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/ 199810e8437SAnisse Astier cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/ 200810e8437SAnisse Astier # systemtap 201810e8437SAnisse Astier mkdir -p $dbg_dir/usr/lib/debug/boot/ 202810e8437SAnisse Astier ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version 203810e8437SAnisse Astier # kdump-tools 204810e8437SAnisse Astier ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version 205810e8437SAnisse Astier create_package "$dbg_packagename" "$dbg_dir" 206810e8437SAnisse Astierfi 207810e8437SAnisse Astier 2081da177e4SLinus Torvaldsexit 0 209