xref: /linux/scripts/package/builddeb (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
1#!/bin/sh
2#
3# builddeb 1.3
4# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
5#
6# Simple script to generate a deb package for a Linux kernel. All the
7# complexity of what to do with a kernel after it is installed or removed
8# is left to other scripts and packages. Scripts can be placed into the
9# preinst, postinst, prerm and postrm directories in /etc/kernel or
10# /usr/share/kernel. A different list of search directories can be given
11# via KDEB_HOOKDIR. Scripts in directories earlier in the list will
12# override scripts of the same name in later directories.  The script will
13# be called on package installation and removal.
14
15set -eu
16
17is_enabled() {
18	grep -q "^$1=y" include/config/auto.conf
19}
20
21if_enabled_echo() {
22	if is_enabled "$1"; then
23		echo -n "$2"
24	elif [ $# -ge 3 ]; then
25		echo -n "$3"
26	fi
27}
28
29install_linux_image () {
30	pname=$1
31	pdir=debian/$1
32
33	# Only some architectures with OF support have this target
34	if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then
35		${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install
36	fi
37
38	${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" INSTALL_MOD_STRIP=1 modules_install
39	rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build"
40
41	# Install the kernel
42	if [ "${ARCH}" = um ] ; then
43		mkdir -p "${pdir}/usr/lib/uml/modules"
44		mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}"
45		mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}"
46		cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map"
47		cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config"
48		gzip "${pdir}/usr/share/doc/${pname}/config"
49	else
50		mkdir -p "${pdir}/boot"
51		cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}"
52		cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}"
53	fi
54
55	# Not all arches have the same installed path in debian
56	# XXX: have each arch Makefile export a variable of the canonical image install
57	# path instead
58	case "${SRCARCH}" in
59	um)
60		installed_image_path="usr/bin/linux-${KERNELRELEASE}";;
61	parisc|mips|powerpc)
62		installed_image_path="boot/vmlinux-${KERNELRELEASE}";;
63	*)
64		installed_image_path="boot/vmlinuz-${KERNELRELEASE}";;
65	esac
66	cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}"
67
68	if [ "${ARCH}" != um ]; then
69		install_maint_scripts "${pdir}"
70	fi
71}
72
73install_maint_scripts () {
74	# Install the maintainer scripts
75	# Note: hook scripts under /etc/kernel are also executed by official Debian
76	# kernel packages, as well as kernel packages built using make-kpkg.
77	# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
78	# so do we; recent versions of dracut and initramfs-tools will obey this.
79	debhookdir=${KDEB_HOOKDIR:-/etc/kernel /usr/share/kernel}
80	for script in postinst postrm preinst prerm; do
81		mkdir -p "${pdir}/DEBIAN"
82		cat <<-EOF > "${pdir}/DEBIAN/${script}"
83		#!/bin/sh
84
85		set -e
86
87		# Pass maintainer script parameters to hook scripts
88		export DEB_MAINT_PARAMS="\$*"
89
90		# Tell initramfs builder whether it's wanted
91		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
92
93		# run-parts will error out if one of its directory arguments does not
94		# exist, so filter the list of hook directories accordingly.
95		hookdirs=
96		for dir in ${debhookdir}; do
97			test -d "\$dir/${script}.d" || continue
98			hookdirs="\$hookdirs \$dir/${script}.d"
99		done
100		hookdirs="\${hookdirs# }"
101		test -n "\$hookdirs" && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" \$hookdirs
102		exit 0
103		EOF
104		chmod 755 "${pdir}/DEBIAN/${script}"
105	done
106}
107
108install_linux_image_dbg () {
109	pdir=debian/$1
110
111	# Parse modules.order directly because 'make modules_install' may sign,
112	# compress modules, and then run unneeded depmod.
113	if is_enabled CONFIG_MODULES; then
114		while read -r mod; do
115			mod="${mod%.o}.ko"
116			dbg="${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/kernel/${mod}"
117			buildid=$("${READELF}" -n "${mod}" | sed -n 's@^.*Build ID: \(..\)\(.*\)@\1/\2@p')
118			link="${pdir}/usr/lib/debug/.build-id/${buildid}.debug"
119
120			mkdir -p "${dbg%/*}" "${link%/*}"
121			"${OBJCOPY}" --only-keep-debug "${mod}" "${dbg}"
122			ln -sf --relative "${dbg}" "${link}"
123		done < modules.order
124	fi
125
126	# Build debug package
127	# Different tools want the image in different locations
128	# perf
129	mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
130	cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
131	# systemtap
132	mkdir -p ${pdir}/usr/lib/debug/boot/
133	ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE}
134	# kdump-tools
135	ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE}
136}
137
138install_kernel_headers () {
139	pdir=debian/$1
140	version=${1#linux-headers-}
141
142	CC="${DEB_HOST_GNU_TYPE}-gcc" "${srctree}/scripts/package/install-extmod-build" "${pdir}/usr/src/linux-headers-${version}"
143
144	mkdir -p $pdir/lib/modules/$version/
145	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
146}
147
148install_libc_headers () {
149	pdir=debian/$1
150
151	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr
152
153	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
154	# used by Debian-based distros (to support multi-arch)
155	mkdir "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
156	mv "$pdir/usr/include/asm" "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
157}
158
159package=$1
160
161case "${package}" in
162*-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