builddeb 7.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
#!/bin/sh
#
3
# builddeb 1.3
L
Linus Torvalds 已提交
4 5 6
# Copyright 2003 Wichert Akkerman <wichert@wiggy.net>
#
# 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
L
Linus Torvalds 已提交
8
# is left to other scripts and packages: they can install scripts in the
9 10 11
# /etc/kernel/{pre,post}{inst,rm}.d/ directories (or an alternative location
# specified in KDEB_HOOKDIR) that will be called on package install and
# removal.
L
Linus Torvalds 已提交
12 13 14

set -e

15
is_enabled() {
16
	grep -q "^$1=y" include/config/auto.conf
17 18 19 20 21 22 23 24 25 26
}

if_enabled_echo() {
	if is_enabled "$1"; then
		echo -n "$2"
	elif [ $# -ge 3 ]; then
		echo -n "$3"
	fi
}

27 28 29
create_package() {
	local pname="$1" pdir="$2"

30 31
	mkdir -m 755 -p "$pdir/DEBIAN"
	mkdir -p "$pdir/usr/share/doc/$pname"
32
	cp debian/copyright "$pdir/usr/share/doc/$pname/"
33 34
	cp debian/changelog "$pdir/usr/share/doc/$pname/changelog.Debian"
	gzip -9 "$pdir/usr/share/doc/$pname/changelog.Debian"
F
FEJES Jozsef 已提交
35 36
	sh -c "cd '$pdir'; find . -type f ! -path './DEBIAN/*' -printf '%P\0' \
		| xargs -r0 md5sum > DEBIAN/md5sums"
37

38 39 40
	# Fix ownership and permissions
	chown -R root:root "$pdir"
	chmod -R go-w "$pdir"
41 42
	# in case we are in a restrictive umask environment like 0077
	chmod -R a+rX "$pdir"
43

44
	# Create the package
45
	dpkg-gencontrol -p$pname -P"$pdir"
46
	dpkg-deb ${KDEB_COMPRESS:+-Z$KDEB_COMPRESS} --build "$pdir" ..
47 48
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
deploy_kernel_headers () {
	pdir=$1

	rm -rf $pdir

	(
		cd $srctree
		find . arch/$SRCARCH -maxdepth 1 -name Makefile\*
		find include scripts -type f -o -type l
		find arch/$SRCARCH -name module.lds -o -name Kbuild.platforms -o -name Platform
		find $(find arch/$SRCARCH -name include -o -name scripts -type d) -type f
	) > debian/hdrsrcfiles

	{
		if is_enabled CONFIG_STACK_VALIDATION; then
			echo tools/objtool/objtool
		fi

		find arch/$SRCARCH/include Module.symvers include scripts -type f

		if is_enabled CONFIG_GCC_PLUGINS; then
			find scripts/gcc-plugins -name \*.so
		fi
	} > debian/hdrobjfiles

	destdir=$pdir/usr/src/linux-headers-$version
	mkdir -p $destdir
	tar -c -f - -C $srctree -T debian/hdrsrcfiles | tar -xf - -C $destdir
	tar -c -f - -T debian/hdrobjfiles | tar -xf - -C $destdir
	rm -f debian/hdrsrcfiles debian/hdrobjfiles

	# copy .config manually to be where it's expected to be
	cp $KCONFIG_CONFIG $destdir/.config

	mkdir -p $pdir/lib/modules/$version/
	ln -s /usr/src/linux-headers-$version $pdir/lib/modules/$version/build
}

L
Linus Torvalds 已提交
87
version=$KERNELRELEASE
88 89 90
tmpdir=debian/linux-image
libc_headers_dir=debian/linux-libc-dev
dbg_dir=debian/linux-image-dbg
91
packagename=linux-image-$version
92
libc_headers_packagename=linux-libc-dev
93
dbg_packagename=$packagename-dbg
94

95
if [ "$ARCH" = "um" ] ; then
96 97
	packagename=user-mode-linux-$version
fi
L
Linus Torvalds 已提交
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112
# Not all arches have the same installed path in debian
# XXX: have each arch Makefile export a variable of the canonical image install
# path instead
case $ARCH in
um)
	installed_image_path="usr/bin/linux-$version"
	;;
parisc|mips|powerpc)
	installed_image_path="boot/vmlinux-$version"
	;;
*)
	installed_image_path="boot/vmlinuz-$version"
esac

113
BUILD_DEBUG=$(if_enabled_echo CONFIG_DEBUG_INFO Yes)
114

L
Linus Torvalds 已提交
115
# Setup the directory structure
116
rm -rf "$tmpdir" "$libc_headers_dir" "$dbg_dir" debian/files
117
mkdir -m 755 -p "$tmpdir/DEBIAN"
118
mkdir -p "$tmpdir/lib" "$tmpdir/boot"
L
Linus Torvalds 已提交
119

120
# Install the kernel
121
if [ "$ARCH" = "um" ] ; then
122
	mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin" "$tmpdir/usr/share/doc/$packagename"
123
	cp System.map "$tmpdir/usr/lib/uml/modules/$version/System.map"
124
	cp $KCONFIG_CONFIG "$tmpdir/usr/share/doc/$packagename/config"
125
	gzip "$tmpdir/usr/share/doc/$packagename/config"
126
else
127
	cp System.map "$tmpdir/boot/System.map-$version"
128
	cp $KCONFIG_CONFIG "$tmpdir/boot/config-$version"
129
fi
130
cp "$($MAKE -s -f $srctree/Makefile image_name)" "$tmpdir/$installed_image_path"
L
Linus Torvalds 已提交
131

132
if is_enabled CONFIG_OF_EARLY_FLATTREE; then
133
	# Only some architectures with OF support have this target
134
	if [ -d "${srctree}/arch/$SRCARCH/boot/dts" ]; then
135
		$MAKE -f $srctree/Makefile INSTALL_DTBS_PATH="$tmpdir/usr/lib/$packagename" dtbs_install
136 137 138
	fi
fi

139
if is_enabled CONFIG_MODULES; then
140
	INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_install
141 142
	rm -f "$tmpdir/lib/modules/$version/build"
	rm -f "$tmpdir/lib/modules/$version/source"
143
	if [ "$ARCH" = "um" ] ; then
144 145 146
		mv "$tmpdir/lib/modules/$version"/* "$tmpdir/usr/lib/uml/modules/$version/"
		rmdir "$tmpdir/lib/modules/$version"
	fi
147
	if [ -n "$BUILD_DEBUG" ] ; then
148 149 150 151 152 153 154 155 156 157
		for module in $(find $tmpdir/lib/modules/ -name *.ko -printf '%P\n'); do
			module=lib/modules/$module
			mkdir -p $(dirname $dbg_dir/usr/lib/debug/$module)
			# only keep debug symbols in the debug file
			$OBJCOPY --only-keep-debug $tmpdir/$module $dbg_dir/usr/lib/debug/$module
			# strip original module from debug symbols
			$OBJCOPY --strip-debug $tmpdir/$module
			# then add a link to those
			$OBJCOPY --add-gnu-debuglink=$dbg_dir/usr/lib/debug/$module $tmpdir/$module
		done
158 159

		# resign stripped modules
160
		if is_enabled CONFIG_MODULE_SIG_ALL; then
161
			INSTALL_MOD_PATH="$tmpdir" $MAKE -f $srctree/Makefile modules_sign
162
		fi
163
	fi
L
Linus Torvalds 已提交
164 165
fi

166
if [ "$ARCH" != "um" ]; then
167
	$MAKE -f $srctree/Makefile headers
168
	$MAKE -f $srctree/Makefile headers_install INSTALL_HDR_PATH="$libc_headers_dir/usr"
169 170 171 172 173
	# move asm headers to /usr/include/<libc-machine>/asm to match the structure
	# used by Debian-based distros (to support multi-arch)
	host_arch=$(dpkg-architecture -a$(cat debian/arch) -qDEB_HOST_MULTIARCH)
	mkdir $libc_headers_dir/usr/include/$host_arch
	mv $libc_headers_dir/usr/include/asm $libc_headers_dir/usr/include/$host_arch/
174
fi
175

L
Linus Torvalds 已提交
176
# Install the maintainer scripts
177
# Note: hook scripts under /etc/kernel are also executed by official Debian
178 179 180
# kernel packages, as well as kernel packages built using make-kpkg.
# make-kpkg sets $INITRD to indicate whether an initramfs is wanted, and
# so do we; recent versions of dracut and initramfs-tools will obey this.
181
debhookdir=${KDEB_HOOKDIR:-/etc/kernel}
L
Linus Torvalds 已提交
182
for script in postinst postrm preinst prerm ; do
183
	mkdir -p "$tmpdir$debhookdir/$script.d"
L
Linus Torvalds 已提交
184 185 186 187 188
	cat <<EOF > "$tmpdir/DEBIAN/$script"
#!/bin/sh

set -e

189
# Pass maintainer script parameters to hook scripts
190
export DEB_MAINT_PARAMS="\$*"
191

192
# Tell initramfs builder whether it's wanted
193
export INITRD=$(if_enabled_echo CONFIG_BLK_DEV_INITRD Yes No)
194

195
test -d $debhookdir/$script.d && run-parts --arg="$version" --arg="/$installed_image_path" $debhookdir/$script.d
L
Linus Torvalds 已提交
196 197 198 199 200
exit 0
EOF
	chmod 755 "$tmpdir/DEBIAN/$script"
done

201
if [ "$ARCH" != "um" ]; then
202 203 204
	deploy_kernel_headers debian/linux-headers
	create_package linux-headers-$version debian/linux-headers

205 206 207
	create_package "$libc_headers_packagename" "$libc_headers_dir"
fi

208
create_package "$packagename" "$tmpdir"
L
Linus Torvalds 已提交
209

210 211 212 213 214 215 216 217 218 219 220 221 222 223
if [ -n "$BUILD_DEBUG" ] ; then
	# Build debug package
	# Different tools want the image in different locations
	# perf
	mkdir -p $dbg_dir/usr/lib/debug/lib/modules/$version/
	cp vmlinux $dbg_dir/usr/lib/debug/lib/modules/$version/
	# systemtap
	mkdir -p $dbg_dir/usr/lib/debug/boot/
	ln -s ../lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/boot/vmlinux-$version
	# kdump-tools
	ln -s lib/modules/$version/vmlinux $dbg_dir/usr/lib/debug/vmlinux-$version
	create_package "$dbg_packagename" "$dbg_dir"
fi

L
Linus Torvalds 已提交
224
exit 0