xref: /linux/scripts/package/builddeb (revision 159956f34ede363e67a87bea840937e242293e91)
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: they can install scripts in the
9# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
10# specified in KDEB_HOOKDIR) that will be called on package install and
11# removal.
12
13set -e
14
15is_enabled() {
16	grep -q "^$1=y" include/config/auto.conf
17}
18
19if_enabled_echo() {
20	if is_enabled "$1"; then
21		echo -n "$2"
22	elif [ $# -ge 3 ]; then
23		echo -n "$3"
24	fi
25}
26
27create_package() {
28	local pname="$1" pdir="$2"
29	local dpkg_deb_opts
30
31	mkdir -m 755 -p "$pdir/DEBIAN"
32	mkdir -p "$pdir/usr/share/doc/$pname"
33	cp debian/copyright "$pdir/usr/share/doc/$pname/"
34	cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
35	gzip -n -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
36	sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
37		| xargs -r0 md5sum > DEBIAN/md5sums"
38
39	# a+rX in case we are in a restrictive umask environment like 0077
40	# ug-s in case we build in a setuid/setgid directory
41	chmod -R go-w,a+rX,ug-s "$pdir"
42
43	# Create the package
44	dpkg-gencontrol -p$pname -P"$pdir"
45	dpkg-deb --root-owner-group ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
46}
47
48install_linux_image () {
49	pdir=$1
50	pname=$2
51
52	rm -rf ${pdir}
53
54	# Only some architectures with OF support have this target
55	if is_enabled CONFIG_OF_EARLY_FLATTREE && [ -d "${srctree}/arch/${SRCARCH}/boot/dts" ]; then
56		${MAKE} -f ${srctree}/Makefile INSTALL_DTBS_PATH="${pdir}/usr/lib/linux-image-${KERNELRELEASE}" dtbs_install
57	fi
58
59	${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${pdir}" modules_install
60	rm -f "${pdir}/lib/modules/${KERNELRELEASE}/build"
61
62	# Install the kernel
63	if [ "${ARCH}" = um ] ; then
64		mkdir -p "${pdir}/usr/lib/uml/modules"
65		mv "${pdir}/lib/modules/${KERNELRELEASE}" "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}"
66		mkdir -p "${pdir}/usr/bin" "${pdir}/usr/share/doc/${pname}"
67		cp System.map "${pdir}/usr/lib/uml/modules/${KERNELRELEASE}/System.map"
68		cp ${KCONFIG_CONFIG} "${pdir}/usr/share/doc/${pname}/config"
69		gzip "${pdir}/usr/share/doc/${pname}/config"
70	else
71		mkdir -p "${pdir}/boot"
72		cp System.map "${pdir}/boot/System.map-${KERNELRELEASE}"
73		cp ${KCONFIG_CONFIG} "${pdir}/boot/config-${KERNELRELEASE}"
74	fi
75
76	# Not all arches have the same installed path in debian
77	# XXX: have each arch Makefile export a variable of the canonical image install
78	# path instead
79	case "${SRCARCH}" in
80	um)
81		installed_image_path="usr/bin/linux-${KERNELRELEASE}";;
82	parisc|mips|powerpc)
83		installed_image_path="boot/vmlinux-${KERNELRELEASE}";;
84	*)
85		installed_image_path="boot/vmlinuz-${KERNELRELEASE}";;
86	esac
87	cp "$(${MAKE} -s -f ${srctree}/Makefile image_name)" "${pdir}/${installed_image_path}"
88
89	# Install the maintainer scripts
90	# Note: hook scripts under /etc/kernel are also executed by official Debian
91	# kernel packages, as well as kernel packages built using make-kpkg.
92	# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
93	# so do we; recent versions of dracut and initramfs-tools will obey this.
94	debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
95	for script in postinst postrm preinst prerm; do
96		mkdir -p "${pdir}${debhookdir}/${script}.d"
97
98		mkdir -p "${pdir}/DEBIAN"
99		cat <<-EOF > "${pdir}/DEBIAN/${script}"
100
101		#!/bin/sh
102
103		set -e
104
105		# Pass maintainer script parameters to hook scripts
106		export DEB_MAINT_PARAMS="\$*"
107
108		# Tell initramfs builder whether it's wanted
109		export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
110
111		test -d ${debhookdir}/${script}.d && run-parts --arg="${KERNELRELEASE}" --arg="/${installed_image_path}" ${debhookdir}/${script}.d
112		exit 0
113		EOF
114		chmod 755 "${pdir}/DEBIAN/${script}"
115	done
116}
117
118install_linux_image_dbg () {
119	pdir=$1
120	image_pdir=$2
121
122	rm -rf ${pdir}
123
124	for module in $(find ${image_pdir}/lib/modules/ -name *.ko -printf '%P\n'); do
125		module=lib/modules/${module}
126		mkdir -p $(dirname ${pdir}/usr/lib/debug/${module})
127		# only keep debug symbols in the debug file
128		${OBJCOPY} --only-keep-debug ${image_pdir}/${module} ${pdir}/usr/lib/debug/${module}
129		# strip original module from debug symbols
130		${OBJCOPY} --strip-debug ${image_pdir}/${module}
131		# then add a link to those
132		${OBJCOPY} --add-gnu-debuglink=${pdir}/usr/lib/debug/${module} ${image_pdir}/${module}
133	done
134
135	# re-sign stripped modules
136	if is_enabled CONFIG_MODULE_SIG_ALL; then
137		${MAKE} -f ${srctree}/Makefile INSTALL_MOD_PATH="${image_pdir}" modules_sign
138	fi
139
140	# Build debug package
141	# Different tools want the image in different locations
142	# perf
143	mkdir -p ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
144	cp vmlinux ${pdir}/usr/lib/debug/lib/modules/${KERNELRELEASE}/
145	# systemtap
146	mkdir -p ${pdir}/usr/lib/debug/boot/
147	ln -s ../lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/boot/vmlinux-${KERNELRELEASE}
148	# kdump-tools
149	ln -s lib/modules/${KERNELRELEASE}/vmlinux ${pdir}/usr/lib/debug/vmlinux-${KERNELRELEASE}
150}
151
152install_kernel_headers () {
153	pdir=$1
154	version=$2
155
156	rm -rf $pdir
157
158	"${srctree}/scripts/package/install-extmod-build" "${pdir}/usr/src/linux-headers-${version}"
159
160	mkdir -p $pdir/lib/modules/$version/
161	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
162}
163
164install_libc_headers () {
165	pdir=$1
166
167	rm -rf $pdir
168
169	$MAKE -f $srctree/Makefile headers
170	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH=$pdir/usr
171
172	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
173	# used by Debian-based distros (to support multi-arch)
174	mkdir "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
175	mv "$pdir/usr/include/asm" "$pdir/usr/include/${DEB_HOST_MULTIARCH}"
176}
177
178rm -f debian/files
179
180packages_enabled=$(dh_listpackages)
181
182for package in ${packages_enabled}
183do
184	case ${package} in
185	*-dbg)
186		# This must be done after linux-image, that is, we expect the
187		# debug package appears after linux-image in debian/control.
188		install_linux_image_dbg debian/linux-image-dbg debian/linux-image;;
189	linux-image-*|user-mode-linux-*)
190		install_linux_image debian/linux-image ${package};;
191	linux-libc-dev)
192		install_libc_headers debian/linux-libc-dev;;
193	linux-headers-*)
194		install_kernel_headers debian/linux-headers ${package#linux-headers-};;
195	esac
196done
197
198for package in ${packages_enabled}
199do
200	case ${package} in
201	*-dbg)
202		create_package ${package} debian/linux-image-dbg;;
203	linux-image-*|user-mode-linux-*)
204		create_package ${package} debian/linux-image;;
205	linux-libc-dev)
206		create_package ${package} debian/linux-libc-dev;;
207	linux-headers-*)
208		create_package ${package} debian/linux-headers;;
209	esac
210done
211
212exit 0
213