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