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" 293e854180SGuillem Jover local dpkg_deb_opts 303e2ab256SFrans Pop 31bf7b0055SRiku Voipio mkdir -m 755 -p "$pdir/DEBIAN" 32bf7b0055SRiku Voipio mkdir -p "$pdir/usr/share/doc/$pname" 339461f666SFrans Pop cp debian/copyright "$pdir/usr/share/doc/$pname/" 341ab18486Smaximilian attems cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian" 3551ccdbfbSGuillem Jover gzip -n -9 "$pdir/usr/share/doc/$pname/changelog.Debian" 36b59a1225SFEJES Jozsef sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \ 37b59a1225SFEJES Jozsef | xargs -r0 md5sum > DEBIAN/md5sums" 389461f666SFrans Pop 393e2ab256SFrans Pop # Fix ownership and permissions 403e854180SGuillem Jover if [ "$DEB_RULES_REQUIRES_ROOT" = "no" ]; then 413e854180SGuillem Jover dpkg_deb_opts="--root-owner-group" 423e854180SGuillem Jover else 433e2ab256SFrans Pop chown -R root:root "$pdir" 443e854180SGuillem Jover fi 45a5e40d86SSven Joachim # a+rX in case we are in a restrictive umask environment like 0077 46a5e40d86SSven Joachim # ug-s in case we build in a setuid/setgid directory 47a5e40d86SSven Joachim chmod -R go-w,a+rX,ug-s "$pdir" 483e2ab256SFrans Pop 49dca0c024SRiku Voipio # Create the package 50b41d920aSRiku Voipio dpkg-gencontrol -p$pname -P"$pdir" 513e854180SGuillem Jover dpkg-deb $dpkg_deb_opts ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" .. 52dca0c024SRiku Voipio} 53dca0c024SRiku Voipio 54b611daaeSMasahiro Yamadainstall_linux_image () { 55b611daaeSMasahiro Yamada pdir=$1 56b611daaeSMasahiro Yamada pname=$2 57b611daaeSMasahiro Yamada 58b611daaeSMasahiro Yamada rm -rf ${pdir} 59b611daaeSMasahiro Yamada 60b611daaeSMasahiro Yamada # Only some architectures with OF support have this target 61b611daaeSMasahiro Yamada if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then 62b611daaeSMasahiro Yamada ${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install 63b611daaeSMasahiro Yamada fi 64b611daaeSMasahiro Yamada 65b611daaeSMasahiro Yamada if is_enabled CONFIG_MODULES; then 66b611daaeSMasahiro Yamada ${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" modules_install 67b611daaeSMasahiro Yamada rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build" 68b611daaeSMasahiro Yamada rm -f "${pdir}/lib/modules/${KERNELRELEASE}/source" 69b611daaeSMasahiro Yamada if [ "${SRCARCH}" = um ] ; then 70b611daaeSMasahiro Yamada mkdir -p "${pdir}/usr/lib/uml/modules" 71b611daaeSMasahiro Yamada mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}" 72b611daaeSMasahiro Yamada fi 73b611daaeSMasahiro Yamada fi 74b611daaeSMasahiro Yamada 75b611daaeSMasahiro Yamada # Install the kernel 76b611daaeSMasahiro Yamada if [ "${ARCH}" = um ] ; then 77b611daaeSMasahiro Yamada mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}" 78b611daaeSMasahiro Yamada cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map" 79b611daaeSMasahiro Yamada cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config" 80b611daaeSMasahiro Yamada gzip "${pdir}/usr/share/doc/${pname}/config" 81b611daaeSMasahiro Yamada else 82b611daaeSMasahiro Yamada mkdir -p "${pdir}/boot" 83b611daaeSMasahiro Yamada cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}" 84b611daaeSMasahiro Yamada cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}" 85b611daaeSMasahiro Yamada fi 86b611daaeSMasahiro Yamada 87b611daaeSMasahiro Yamada # Not all arches have the same installed path in debian 88b611daaeSMasahiro Yamada # XXX: have each arch Makefile export a variable of the canonical image install 89b611daaeSMasahiro Yamada # path instead 90b611daaeSMasahiro Yamada case "${SRCARCH}" in 91b611daaeSMasahiro Yamada um) 92b611daaeSMasahiro Yamada installed_image_path="usr/bin/linux-${KERNELRELEASE}";; 93b611daaeSMasahiro Yamada parisc|mips|powerpc) 94b611daaeSMasahiro Yamada installed_image_path="boot/vmlinux-${KERNELRELEASE}";; 95b611daaeSMasahiro Yamada *) 96b611daaeSMasahiro Yamada installed_image_path="boot/vmlinuz-${KERNELRELEASE}";; 97b611daaeSMasahiro Yamada esac 98b611daaeSMasahiro Yamada cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}" 99b611daaeSMasahiro Yamada 100b611daaeSMasahiro Yamada # Install the maintainer scripts 101b611daaeSMasahiro Yamada # Note: hook scripts under /etc/kernel are also executed by official Debian 102b611daaeSMasahiro Yamada # kernel packages, as well as kernel packages built using make-kpkg. 103b611daaeSMasahiro Yamada # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and 104b611daaeSMasahiro Yamada # so do we; recent versions of dracut and initramfs-tools will obey this. 105b611daaeSMasahiro Yamada debhookdir=${KDEB_HOOKDIR:-/etc/kernel} 106b611daaeSMasahiro Yamada for script in postinst postrm preinst prerm; do 107b611daaeSMasahiro Yamada mkdir -p "${pdir}${debhookdir}/${script}.d" 108b611daaeSMasahiro Yamada 109b611daaeSMasahiro Yamada mkdir -p "${pdir}/DEBIAN" 110b611daaeSMasahiro Yamada cat <<-EOF > "${pdir}/DEBIAN/${script}" 111b611daaeSMasahiro Yamada 112b611daaeSMasahiro Yamada #!/bin/sh 113b611daaeSMasahiro Yamada 114b611daaeSMasahiro Yamada set -e 115b611daaeSMasahiro Yamada 116b611daaeSMasahiro Yamada # Pass maintainer script parameters to hook scripts 117b611daaeSMasahiro Yamada export DEB_MAINT_PARAMS="\$*" 118b611daaeSMasahiro Yamada 119b611daaeSMasahiro Yamada # Tell initramfs builder whether it's wanted 120b611daaeSMasahiro Yamada export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No) 121b611daaeSMasahiro Yamada 122b611daaeSMasahiro Yamada test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d 123b611daaeSMasahiro Yamada exit 0 124b611daaeSMasahiro Yamada EOF 125b611daaeSMasahiro Yamada chmod 755 "${pdir}/DEBIAN/${script}" 126b611daaeSMasahiro Yamada done 127b611daaeSMasahiro Yamada} 128b611daaeSMasahiro Yamada 129b611daaeSMasahiro Yamadainstall_linux_image_dbg () { 130b611daaeSMasahiro Yamada pdir=$1 131b611daaeSMasahiro Yamada image_pdir=$2 132b611daaeSMasahiro Yamada 133b611daaeSMasahiro Yamada rm -rf ${pdir} 134b611daaeSMasahiro Yamada 135b611daaeSMasahiro Yamada for module in $(find ${image_pdir}/lib/modules/ -name *.ko -printf '%P\n'); do 136b611daaeSMasahiro Yamada module=lib/modules/${module} 137b611daaeSMasahiro Yamada mkdir -p $(dirname ${pdir}/usr/lib/debug/${module}) 138b611daaeSMasahiro Yamada # only keep debug symbols in the debug file 139b611daaeSMasahiro Yamada ${OBJCOPY} --only-keep-debug ${image_pdir}/${module} ${pdir}/usr/lib/debug/${module} 140b611daaeSMasahiro Yamada # strip original module from debug symbols 141b611daaeSMasahiro Yamada ${OBJCOPY} --strip-debug ${image_pdir}/${module} 142b611daaeSMasahiro Yamada # then add a link to those 143b611daaeSMasahiro Yamada ${OBJCOPY} --add-gnu-debuglink=${pdir}/usr/lib/debug/${module} ${image_pdir}/${module} 144b611daaeSMasahiro Yamada done 145b611daaeSMasahiro Yamada 146b611daaeSMasahiro Yamada # re-sign stripped modules 147b611daaeSMasahiro Yamada if is_enabled CONFIG_MODULE_SIG_ALL; then 148b611daaeSMasahiro Yamada ${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${image_pdir}" modules_sign 149b611daaeSMasahiro Yamada fi 150b611daaeSMasahiro Yamada 151b611daaeSMasahiro Yamada # Build debug package 152b611daaeSMasahiro Yamada # Different tools want the image in different locations 153b611daaeSMasahiro Yamada # perf 154b611daaeSMasahiro Yamada mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/ 155b611daaeSMasahiro Yamada cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/ 156b611daaeSMasahiro Yamada # systemtap 157b611daaeSMasahiro Yamada mkdir -p ${pdir}/usr/lib/debug/boot/ 158b611daaeSMasahiro Yamada ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE} 159b611daaeSMasahiro Yamada # kdump-tools 160b611daaeSMasahiro Yamada ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE} 161b611daaeSMasahiro Yamada} 162b611daaeSMasahiro Yamada 163*36862e14SMasahiro Yamadainstall_kernel_headers () { 1643126c17dSMasahiro Yamada pdir=$1 1653126c17dSMasahiro Yamada 1663126c17dSMasahiro Yamada rm -rf $pdir 1673126c17dSMasahiro Yamada 1683126c17dSMasahiro Yamada ( 1693126c17dSMasahiro Yamada cd $srctree 1703126c17dSMasahiro Yamada find . arch/$SRCARCH -maxdepth 1 -name Makefile\* 1713126c17dSMasahiro Yamada find include scripts -type f -o -type l 172596b0474SMasahiro Yamada find arch/$SRCARCH -name Kbuild.platforms -o -name Platform 1733126c17dSMasahiro Yamada find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f 1743126c17dSMasahiro Yamada ) > debian/hdrsrcfiles 1753126c17dSMasahiro Yamada 1763126c17dSMasahiro Yamada { 17703f16cd0SJosh Poimboeuf if is_enabled CONFIG_OBJTOOL; then 1783126c17dSMasahiro Yamada echo tools/objtool/objtool 1793126c17dSMasahiro Yamada fi 1803126c17dSMasahiro Yamada 1813126c17dSMasahiro Yamada find arch/$SRCARCH/include Module.symvers include scripts -type f 1823126c17dSMasahiro Yamada 1833126c17dSMasahiro Yamada if is_enabled CONFIG_GCC_PLUGINS; then 1843126c17dSMasahiro Yamada find scripts/gcc-plugins -name \*.so 1853126c17dSMasahiro Yamada fi 1863126c17dSMasahiro Yamada } > debian/hdrobjfiles 1873126c17dSMasahiro Yamada 1883126c17dSMasahiro Yamada destdir=$pdir/usr/src/linux-headers-$version 1893126c17dSMasahiro Yamada mkdir -p $destdir 1903126c17dSMasahiro Yamada tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir 1913126c17dSMasahiro Yamada tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir 1923126c17dSMasahiro Yamada rm -f debian/hdrsrcfiles debian/hdrobjfiles 1933126c17dSMasahiro Yamada 1943126c17dSMasahiro Yamada # copy .config manually to be where it's expected to be 1953126c17dSMasahiro Yamada cp $KCONFIG_CONFIG $destdir/.config 1963126c17dSMasahiro Yamada 1973126c17dSMasahiro Yamada mkdir -p $pdir/lib/modules/$version/ 1983126c17dSMasahiro Yamada ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build 1993126c17dSMasahiro Yamada} 2003126c17dSMasahiro Yamada 201*36862e14SMasahiro Yamadainstall_libc_headers () { 202451dff37SMasahiro Yamada pdir=$1 203451dff37SMasahiro Yamada 204451dff37SMasahiro Yamada rm -rf $pdir 205451dff37SMasahiro Yamada 206451dff37SMasahiro Yamada $MAKE -f $srctree/Makefile headers 207451dff37SMasahiro Yamada $MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr 208451dff37SMasahiro Yamada 209451dff37SMasahiro Yamada # move asm headers to /usr/include/<libc-machine>/asm to match the structure 210451dff37SMasahiro Yamada # used by Debian-based distros (to support multi-arch) 211451dff37SMasahiro Yamada host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH) 212451dff37SMasahiro Yamada mkdir $pdir/usr/include/$host_arch 213451dff37SMasahiro Yamada mv $pdir/usr/include/asm $pdir/usr/include/$host_arch/ 214451dff37SMasahiro Yamada} 215451dff37SMasahiro Yamada 216b611daaeSMasahiro Yamadarm -f debian/files 2171da177e4SLinus Torvalds 218*36862e14SMasahiro Yamadapackages_enabled=$(dh_listpackages) 2193126c17dSMasahiro Yamada 220*36862e14SMasahiro Yamadafor package in ${packages_enabled} 221*36862e14SMasahiro Yamadado 222*36862e14SMasahiro Yamada case ${package} in 223*36862e14SMasahiro Yamada *-dbg) 224*36862e14SMasahiro Yamada # This must be done after linux-image, that is, we expect the 225*36862e14SMasahiro Yamada # debug package appears after linux-image in debian/control. 226*36862e14SMasahiro Yamada install_linux_image_dbg debian/linux-image-dbg debian/linux-image;; 227*36862e14SMasahiro Yamada linux-image-*|user-mode-linux-*) 228*36862e14SMasahiro Yamada install_linux_image debian/linux-image ${package};; 229*36862e14SMasahiro Yamada linux-libc-dev) 230*36862e14SMasahiro Yamada install_libc_headers debian/linux-libc-dev;; 231*36862e14SMasahiro Yamada linux-headers-*) 232*36862e14SMasahiro Yamada install_kernel_headers debian/linux-headers;; 233*36862e14SMasahiro Yamada esac 234*36862e14SMasahiro Yamadadone 235d7d357bcSJoerg Roedel 236*36862e14SMasahiro Yamadafor package in ${packages_enabled} 237*36862e14SMasahiro Yamadado 238*36862e14SMasahiro Yamada case ${package} in 239*36862e14SMasahiro Yamada *-dbg) 240*36862e14SMasahiro Yamada create_package ${package} debian/linux-image-dbg;; 241*36862e14SMasahiro Yamada linux-image-*|user-mode-linux-*) 242*36862e14SMasahiro Yamada create_package ${package} debian/linux-image;; 243*36862e14SMasahiro Yamada linux-libc-dev) 244*36862e14SMasahiro Yamada create_package ${package} debian/linux-libc-dev;; 245*36862e14SMasahiro Yamada linux-headers-*) 246*36862e14SMasahiro Yamada create_package ${package} debian/linux-headers;; 247*36862e14SMasahiro Yamada esac 248*36862e14SMasahiro Yamadadone 249b611daaeSMasahiro Yamada 2501da177e4SLinus Torvaldsexit 0 251