xref: /linux/scripts/package/builddeb (revision bcbbf493f2fa6fa1f0832f6b5b4c80a65de242d6)
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
13c0414419SMasahiro Yamadaset -eu
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
27b611daaeSMasahiro Yamadainstall_linux_image () {
286185d321SMasahiro Yamada	pname=$1
296185d321SMasahiro Yamada	pdir=debian/$1
30b611daaeSMasahiro Yamada
31b611daaeSMasahiro Yamada	# Only some architectures with OF support have this target
32b611daaeSMasahiro Yamada	if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then
33b611daaeSMasahiro Yamada		${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install
34b611daaeSMasahiro Yamada	fi
35b611daaeSMasahiro Yamada
3616c36f88SMasahiro Yamada	${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" INSTALL_MOD_STRIP=1 modules_install
37b611daaeSMasahiro Yamada	rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build"
38b611daaeSMasahiro Yamada
39b611daaeSMasahiro Yamada	# Install the kernel
40b611daaeSMasahiro Yamada	if [ "${ARCH}" = um ] ; then
411240dabeSMasahiro Yamada		mkdir -p "${pdir}/usr/lib/uml/modules"
421240dabeSMasahiro Yamada		mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}"
43b611daaeSMasahiro Yamada		mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}"
44b611daaeSMasahiro Yamada		cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map"
45b611daaeSMasahiro Yamada		cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config"
46b611daaeSMasahiro Yamada		gzip "${pdir}/usr/share/doc/${pname}/config"
47b611daaeSMasahiro Yamada	else
48b611daaeSMasahiro Yamada		mkdir -p "${pdir}/boot"
49b611daaeSMasahiro Yamada		cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}"
50b611daaeSMasahiro Yamada		cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}"
51b611daaeSMasahiro Yamada	fi
52b611daaeSMasahiro Yamada
53b611daaeSMasahiro Yamada	# Not all arches have the same installed path in debian
54b611daaeSMasahiro Yamada	# XXX: have each arch Makefile export a variable of the canonical image install
55b611daaeSMasahiro Yamada	# path instead
56b611daaeSMasahiro Yamada	case "${SRCARCH}" in
57b611daaeSMasahiro Yamada	um)
58b611daaeSMasahiro Yamada		installed_image_path="usr/bin/linux-${KERNELRELEASE}";;
59b611daaeSMasahiro Yamada	parisc|mips|powerpc)
60b611daaeSMasahiro Yamada		installed_image_path="boot/vmlinux-${KERNELRELEASE}";;
61b611daaeSMasahiro Yamada	*)
62b611daaeSMasahiro Yamada		installed_image_path="boot/vmlinuz-${KERNELRELEASE}";;
63b611daaeSMasahiro Yamada	esac
64b611daaeSMasahiro Yamada	cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}"
65b611daaeSMasahiro Yamada
66b611daaeSMasahiro Yamada	# Install the maintainer scripts
67b611daaeSMasahiro Yamada	# Note: hook scripts under /etc/kernel are also executed by official Debian
68b611daaeSMasahiro Yamada	# kernel packages, as well as kernel packages built using make-kpkg.
69b611daaeSMasahiro Yamada	# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
70b611daaeSMasahiro Yamada	# so do we; recent versions of dracut and initramfs-tools will obey this.
71b611daaeSMasahiro Yamada	debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
72b611daaeSMasahiro Yamada	for script in postinst postrm preinst prerm; do
73b611daaeSMasahiro Yamada		mkdir -p "${pdir}${debhookdir}/${script}.d"
74b611daaeSMasahiro Yamada
75b611daaeSMasahiro Yamada		mkdir -p "${pdir}/DEBIAN"
76b611daaeSMasahiro Yamada		cat <<-EOF > "${pdir}/DEBIAN/${script}"
77b611daaeSMasahiro Yamada		#!/bin/sh
78b611daaeSMasahiro Yamada
79b611daaeSMasahiro Yamada		set -e
80b611daaeSMasahiro Yamada
81b611daaeSMasahiro Yamada		# Pass maintainer script parameters to hook scripts
82b611daaeSMasahiro Yamada		export DEB_MAINT_PARAMS="\$*"
83b611daaeSMasahiro Yamada
84b611daaeSMasahiro Yamada		# Tell initramfs builder whether it's wanted
85b611daaeSMasahiro Yamada		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
86b611daaeSMasahiro Yamada
87b611daaeSMasahiro Yamada		test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
88b611daaeSMasahiro Yamada		exit 0
89b611daaeSMasahiro Yamada		EOF
90b611daaeSMasahiro Yamada		chmod 755 "${pdir}/DEBIAN/${script}"
91b611daaeSMasahiro Yamada	done
92b611daaeSMasahiro Yamada}
93b611daaeSMasahiro Yamada
94b611daaeSMasahiro Yamadainstall_linux_image_dbg () {
956185d321SMasahiro Yamada	pdir=debian/$1
96b611daaeSMasahiro Yamada
9716c36f88SMasahiro Yamada	# Parse modules.order directly because 'make modules_install' may sign,
9816c36f88SMasahiro Yamada	# compress modules, and then run unneeded depmod.
99*bcbbf493SMatt Fleming	if is_enabled CONFIG_MODULES; then
10016c36f88SMasahiro Yamada		while read -r mod; do
10116c36f88SMasahiro Yamada			mod="${mod%.o}.ko"
10216c36f88SMasahiro Yamada			dbg="${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/kernel/${mod}"
10316c36f88SMasahiro Yamada			buildid=$("${READELF}" -n "${mod}" | sed -n 's@^.*Build ID: \(..\)\(.*\)@\1/\2@p')
10416c36f88SMasahiro Yamada			link="${pdir}/usr/lib/debug/.build-id/${buildid}.debug"
105b611daaeSMasahiro Yamada
10616c36f88SMasahiro Yamada			mkdir -p "${dbg%/*}" "${link%/*}"
10716c36f88SMasahiro Yamada			"${OBJCOPY}" --only-keep-debug "${mod}" "${dbg}"
10816c36f88SMasahiro Yamada			ln -sf --relative "${dbg}" "${link}"
10916c36f88SMasahiro Yamada		done < modules.order
110*bcbbf493SMatt Fleming	fi
111b611daaeSMasahiro Yamada
112b611daaeSMasahiro Yamada	# Build debug package
113b611daaeSMasahiro Yamada	# Different tools want the image in different locations
114b611daaeSMasahiro Yamada	# perf
115b611daaeSMasahiro Yamada	mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
116b611daaeSMasahiro Yamada	cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
117b611daaeSMasahiro Yamada	# systemtap
118b611daaeSMasahiro Yamada	mkdir -p ${pdir}/usr/lib/debug/boot/
119b611daaeSMasahiro Yamada	ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE}
120b611daaeSMasahiro Yamada	# kdump-tools
121b611daaeSMasahiro Yamada	ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE}
122b611daaeSMasahiro Yamada}
123b611daaeSMasahiro Yamada
12436862e14SMasahiro Yamadainstall_kernel_headers () {
1256185d321SMasahiro Yamada	pdir=debian/$1
1266185d321SMasahiro Yamada	version=${1#linux-headers-}
1273126c17dSMasahiro Yamada
128e2c31822SMasahiro Yamada	CC="${DEB_HOST_GNU_TYPE}-gcc" "${srctree}/scripts/package/install-extmod-build" "${pdir}/usr/src/linux-headers-${version}"
1293126c17dSMasahiro Yamada
1303126c17dSMasahiro Yamada	mkdir -p $pdir/lib/modules/$version/
1313126c17dSMasahiro Yamada	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
1323126c17dSMasahiro Yamada}
1333126c17dSMasahiro Yamada
13436862e14SMasahiro Yamadainstall_libc_headers () {
1356185d321SMasahiro Yamada	pdir=debian/$1
136451dff37SMasahiro Yamada
137451dff37SMasahiro Yamada	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr
138451dff37SMasahiro Yamada
139451dff37SMasahiro Yamada	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
140451dff37SMasahiro Yamada	# used by Debian-based distros (to support multi-arch)
141159956f3SMasahiro Yamada	mkdir "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
142159956f3SMasahiro Yamada	mv "$pdir/usr/include/asm" "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
143451dff37SMasahiro Yamada}
144451dff37SMasahiro Yamada
1451d7bae8fSMasahiro Yamadapackage=$1
1461da177e4SLinus Torvalds
1471d7bae8fSMasahiro Yamadacase "${package}" in
14836862e14SMasahiro Yamada*-dbg)
1496185d321SMasahiro Yamada	install_linux_image_dbg "${package}";;
15036862e14SMasahiro Yamadalinux-image-*|user-mode-linux-*)
1516185d321SMasahiro Yamada	install_linux_image "${package}";;
15236862e14SMasahiro Yamadalinux-libc-dev)
1536185d321SMasahiro Yamada	install_libc_headers "${package}";;
15436862e14SMasahiro Yamadalinux-headers-*)
1556185d321SMasahiro Yamada	install_kernel_headers "${package}";;
15636862e14SMasahiro Yamadaesac
157