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. Scripts can be placed into the 9fe233cb6SFrans Pop# preinst, postinst, prerm and postrm directories in /etc/kernel or 10fe233cb6SFrans Pop# /usr/share/kernel. A different list of search directories can be given 11fe233cb6SFrans Pop# via KDEB_HOOKDIR. Scripts in directories earlier in the list will 121da177e4SLinus Torvalds# override scripts of the same name in later directories. The script will 13c0414419SMasahiro Yamada# be called on package installation and removal. 141da177e4SLinus Torvalds 15515f4c63SMasahiro Yamadaset -eu 166fb7ef5aSMasahiro Yamada 17515f4c63SMasahiro Yamadais_enabled() { 18515f4c63SMasahiro Yamada grep -q "^$1=y" include/config/auto.conf 19515f4c63SMasahiro Yamada} 20515f4c63SMasahiro Yamada 21515f4c63SMasahiro Yamadaif_enabled_echo() { 22515f4c63SMasahiro Yamada if is_enabled "$1"; then 23515f4c63SMasahiro Yamada echo -n "$2" 24515f4c63SMasahiro Yamada elif [ $# -ge 3 ]; then 25515f4c63SMasahiro Yamada echo -n "$3" 26515f4c63SMasahiro Yamada fi 27b611daaeSMasahiro Yamada} 286185d321SMasahiro Yamada 296185d321SMasahiro Yamadainstall_linux_image () { 30b611daaeSMasahiro Yamada pname=$1 31b611daaeSMasahiro Yamada pdir=debian/$1 32b611daaeSMasahiro Yamada 33b611daaeSMasahiro Yamada # Only some architectures with OF support have this target 34b611daaeSMasahiro Yamada if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then 35b611daaeSMasahiro Yamada ${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install 3616c36f88SMasahiro Yamada fi 37b611daaeSMasahiro Yamada 38b611daaeSMasahiro Yamada ${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" INSTALL_MOD_STRIP=1 modules_install 39b611daaeSMasahiro Yamada rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build" 40b611daaeSMasahiro Yamada 411240dabeSMasahiro Yamada # Install the kernel 421240dabeSMasahiro Yamada if [ "${ARCH}" = um ] ; then 43b611daaeSMasahiro Yamada mkdir -p "${pdir}/usr/lib/uml/modules" 44b611daaeSMasahiro Yamada mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}" 45b611daaeSMasahiro Yamada mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}" 46b611daaeSMasahiro Yamada cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map" 47b611daaeSMasahiro Yamada cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config" 48b611daaeSMasahiro Yamada gzip "${pdir}/usr/share/doc/${pname}/config" 49b611daaeSMasahiro Yamada else 50b611daaeSMasahiro Yamada mkdir -p "${pdir}/boot" 51b611daaeSMasahiro Yamada cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}" 52b611daaeSMasahiro Yamada cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}" 53b611daaeSMasahiro Yamada fi 54b611daaeSMasahiro Yamada 55b611daaeSMasahiro Yamada # Not all arches have the same installed path in debian 56b611daaeSMasahiro Yamada # XXX: have each arch Makefile export a variable of the canonical image install 57b611daaeSMasahiro Yamada # path instead 58b611daaeSMasahiro Yamada case "${SRCARCH}" in 59b611daaeSMasahiro Yamada um) 60b611daaeSMasahiro Yamada installed_image_path="usr/bin/linux-${KERNELRELEASE}";; 61b611daaeSMasahiro Yamada parisc|mips|powerpc) 62b611daaeSMasahiro Yamada installed_image_path="boot/vmlinux-${KERNELRELEASE}";; 63b611daaeSMasahiro Yamada *) 64b611daaeSMasahiro Yamada installed_image_path="boot/vmlinuz-${KERNELRELEASE}";; 65b611daaeSMasahiro Yamada esac 66*54956567SNicolas Schier cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}" 67*54956567SNicolas Schier 68*54956567SNicolas Schier if [ "${ARCH}" != um ]; then 69*54956567SNicolas Schier install_maint_scripts "${pdir}" 70*54956567SNicolas Schier fi 71*54956567SNicolas Schier} 72b611daaeSMasahiro Yamada 73b611daaeSMasahiro Yamadainstall_maint_scripts () { 74b611daaeSMasahiro Yamada # Install the maintainer scripts 75b611daaeSMasahiro Yamada # Note: hook scripts under /etc/kernel are also executed by official Debian 76b611daaeSMasahiro Yamada # kernel packages, as well as kernel packages built using make-kpkg. 77b611daaeSMasahiro Yamada # make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and 78b611daaeSMasahiro Yamada # so do we; recent versions of dracut and initramfs-tools will obey this. 79b611daaeSMasahiro Yamada debhookdir=${KDEB_HOOKDIR:-/etc/kernel /usr/share/kernel} 80b611daaeSMasahiro Yamada for script in postinst postrm preinst prerm; do 81b611daaeSMasahiro Yamada mkdir -p "${pdir}/DEBIAN" 82b611daaeSMasahiro Yamada cat <<-EOF > "${pdir}/DEBIAN/${script}" 83b611daaeSMasahiro Yamada #!/bin/sh 84b611daaeSMasahiro Yamada 85b611daaeSMasahiro Yamada set -e 86b611daaeSMasahiro Yamada 87b611daaeSMasahiro Yamada # Pass maintainer script parameters to hook scripts 88b611daaeSMasahiro Yamada export DEB_MAINT_PARAMS="\$*" 89b611daaeSMasahiro Yamada 90b611daaeSMasahiro Yamada # Tell initramfs builder whether it's wanted 91b611daaeSMasahiro Yamada export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No) 92b611daaeSMasahiro Yamada 93b611daaeSMasahiro Yamada # run-parts will error out if one of its directory arguments does not 94b611daaeSMasahiro Yamada # exist, so filter the list of hook directories accordingly. 95b611daaeSMasahiro Yamada hookdirs= 96b611daaeSMasahiro Yamada for dir in ${debhookdir}; do 97b611daaeSMasahiro Yamada test -d "\$dir/${script}.d" || continue 98b611daaeSMasahiro Yamada hookdirs="\$hookdirs \$dir/${script}.d" 99b611daaeSMasahiro Yamada done 100b611daaeSMasahiro Yamada hookdirs="\${hookdirs# }" 1016185d321SMasahiro Yamada test -n "\$hookdirs" && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" \$hookdirs 102b611daaeSMasahiro Yamada exit 0 10316c36f88SMasahiro Yamada EOF 10416c36f88SMasahiro Yamada chmod 755 "${pdir}/DEBIAN/${script}" 105bcbbf493SMatt Fleming done 10616c36f88SMasahiro Yamada} 10716c36f88SMasahiro Yamada 10816c36f88SMasahiro Yamadainstall_linux_image_dbg () { 10916c36f88SMasahiro Yamada pdir=debian/$1 11016c36f88SMasahiro Yamada 111b611daaeSMasahiro Yamada # Parse modules.order directly because 'make modules_install' may sign, 11216c36f88SMasahiro Yamada # compress modules, and then run unneeded depmod. 11316c36f88SMasahiro Yamada if is_enabled CONFIG_MODULES; then 11416c36f88SMasahiro Yamada while read -r mod; do 11516c36f88SMasahiro Yamada mod="${mod%.o}.ko" 116bcbbf493SMatt Fleming dbg="${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/kernel/${mod}" 117b611daaeSMasahiro Yamada buildid=$("${READELF}" -n "${mod}" | sed -n 's@^.*Build ID: \(..\)\(.*\)@\1/\2@p') 118b611daaeSMasahiro Yamada link="${pdir}/usr/lib/debug/.build-id/${buildid}.debug" 119b611daaeSMasahiro Yamada 120b611daaeSMasahiro Yamada mkdir -p "${dbg%/*}" "${link%/*}" 121b611daaeSMasahiro Yamada "${OBJCOPY}" --only-keep-debug "${mod}" "${dbg}" 122b611daaeSMasahiro Yamada ln -sf --relative "${dbg}" "${link}" 123b611daaeSMasahiro Yamada done < modules.order 124b611daaeSMasahiro Yamada fi 125b611daaeSMasahiro Yamada 126b611daaeSMasahiro Yamada # Build debug package 127b611daaeSMasahiro Yamada # Different tools want the image in different locations 128b611daaeSMasahiro Yamada # perf 129b611daaeSMasahiro Yamada mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/ 13036862e14SMasahiro Yamada cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/ 1316185d321SMasahiro Yamada # systemtap 1326185d321SMasahiro Yamada mkdir -p ${pdir}/usr/lib/debug/boot/ 1333126c17dSMasahiro Yamada ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE} 134e2c31822SMasahiro Yamada # kdump-tools 1353126c17dSMasahiro Yamada ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE} 1363126c17dSMasahiro Yamada} 1373126c17dSMasahiro Yamada 1383126c17dSMasahiro Yamadainstall_kernel_headers () { 1393126c17dSMasahiro Yamada pdir=debian/$1 14036862e14SMasahiro Yamada version=${1#linux-headers-} 1416185d321SMasahiro Yamada 142451dff37SMasahiro Yamada CC="${DEB_HOST_GNU_TYPE}-gcc" "${srctree}/scripts/package/install-extmod-build" "${pdir}/usr/src/linux-headers-${version}" 143451dff37SMasahiro Yamada 144451dff37SMasahiro Yamada mkdir -p $pdir/lib/modules/$version/ 145451dff37SMasahiro Yamada ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build 146451dff37SMasahiro Yamada} 147159956f3SMasahiro Yamada 148159956f3SMasahiro Yamadainstall_libc_headers () { 149451dff37SMasahiro Yamada pdir=debian/$1 150451dff37SMasahiro Yamada 1511d7bae8fSMasahiro Yamada $MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr 1521da177e4SLinus Torvalds 1531d7bae8fSMasahiro Yamada # move asm headers to /usr/include/<libc-machine>/asm to match the structure 15436862e14SMasahiro Yamada # used by Debian-based distros (to support multi-arch) 1556185d321SMasahiro Yamada mkdir "$pdir/usr/include/${DEB_HOST_MULTIARCH}" 15636862e14SMasahiro Yamada mv "$pdir/usr/include/asm" "$pdir/usr/include/${DEB_HOST_MULTIARCH}" 1576185d321SMasahiro Yamada} 15836862e14SMasahiro Yamada 1596185d321SMasahiro Yamadapackage=$1 16036862e14SMasahiro Yamada 1616185d321SMasahiro Yamadacase "${package}" in 16236862e14SMasahiro Yamada*-dbg) 163 install_linux_image_dbg "${package}";; 164linux-image-*|user-mode-linux-*) 165 install_linux_image "${package}";; 166linux-libc-dev) 167 install_libc_headers "${package}";; 168linux-headers-*) 169 install_kernel_headers "${package}";; 170esac 171