Gentoo Archives: gentoo-commits

From: "Michał Górny" <mgorny@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
Date: Thu, 09 Jan 2020 16:10:27
Message-Id: 1578586215.6c42ffb977082bd8bc38e1cf3d851580796bfec8.mgorny@gentoo
1 commit: 6c42ffb977082bd8bc38e1cf3d851580796bfec8
2 Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
3 AuthorDate: Sat Jan 4 16:19:50 2020 +0000
4 Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
5 CommitDate: Thu Jan 9 16:10:15 2020 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=6c42ffb9
7
8 kernel-install.eclass: Install logic for dist-kernels
9
10 Introduce a new eclass that contains common logic needed to test
11 and install distribution kernels. This is the eclass common both
12 to kernels built from source and installed from binary packages.
13
14 Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
15
16 eclass/kernel-install.eclass | 309 +++++++++++++++++++++++++++++++++++++++++++
17 1 file changed, 309 insertions(+)
18
19 diff --git a/eclass/kernel-install.eclass b/eclass/kernel-install.eclass
20 new file mode 100644
21 index 00000000000..f44fc3ebfa9
22 --- /dev/null
23 +++ b/eclass/kernel-install.eclass
24 @@ -0,0 +1,309 @@
25 +# Copyright 2020 Gentoo Authors
26 +# Distributed under the terms of the GNU General Public License v2
27 +
28 +# @ECLASS: kernel-install.eclass
29 +# @MAINTAINER:
30 +# Distribution Kernel Project <dist-kernel@g.o>
31 +# @AUTHOR:
32 +# Michał Górny <mgorny@g.o>
33 +# @SUPPORTED_EAPIS: 7
34 +# @BLURB: Installation mechanics for Distribution Kernels
35 +# @DESCRIPTION:
36 +# This eclass provides the logic needed to test and install different
37 +# kinds of Distribution Kernel packages, including both kernels built
38 +# from source and distributed as binaries. The eclass relies on the
39 +# ebuild installing a subset of built kernel tree into
40 +# /usr/src/linux-${PV} containing the kernel image in its standard
41 +# location and System.map.
42 +#
43 +# The eclass exports src_test, pkg_postinst and pkg_postrm.
44 +# Additionally, the inherited mount-boot eclass exports pkg_pretend.
45 +# It also stubs out pkg_preinst and pkg_prerm defined by mount-boot.
46 +
47 +if [[ ! ${_KERNEL_INSTALL_ECLASS} ]]; then
48 +
49 +case "${EAPI:-0}" in
50 + 0|1|2|3|4|5|6)
51 + die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
52 + ;;
53 + 7)
54 + ;;
55 + *)
56 + die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
57 + ;;
58 +esac
59 +
60 +inherit mount-boot
61 +
62 +TCL_VER=10.1
63 +SRC_URI+="
64 + test? (
65 + amd64? (
66 + https://dev.gentoo.org/~mgorny/dist/tinycorelinux-${TCL_VER}-amd64.qcow2
67 + )
68 + x86? (
69 + https://dev.gentoo.org/~mgorny/dist/tinycorelinux-${TCL_VER}-x86.qcow2
70 + )
71 + )"
72 +
73 +SLOT="${PV}"
74 +IUSE="+initramfs test"
75 +RESTRICT+=" !test? ( test ) test? ( userpriv )"
76 +
77 +# install-DEPEND actually
78 +# note: we need installkernel with initramfs support!
79 +RDEPEND="
80 + || (
81 + sys-kernel/installkernel-gentoo
82 + sys-kernel/installkernel-systemd-boot
83 + )
84 + initramfs? ( >=sys-kernel/dracut-049-r3 )"
85 +BDEPEND="
86 + test? (
87 + dev-tcltk/expect
88 + sys-kernel/dracut
89 + amd64? ( app-emulation/qemu[qemu_softmmu_targets_x86_64] )
90 + x86? ( app-emulation/qemu[qemu_softmmu_targets_i386] )
91 + )"
92 +
93 +# @FUNCTION: kernel-install_build_initramfs
94 +# @USAGE: <output> <version>
95 +# @DESCRIPTION:
96 +# Build an initramfs for the kernel. <output> specifies the absolute
97 +# path where initramfs will be created, while <version> specifies
98 +# the kernel version, used to find modules.
99 +kernel-install_build_initramfs() {
100 + debug-print-function ${FUNCNAME} "${@}"
101 +
102 + [[ ${#} -eq 2 ]] || die "${FUNCNAME}: invalid arguments"
103 + local output=${1}
104 + local version=${2}
105 +
106 + ebegin "Building initramfs via dracut"
107 + dracut --force "${output}" "${version}"
108 + eend ${?} || die "Building initramfs failed"
109 +}
110 +
111 +# @FUNCTION: kernel-install_get_image_path
112 +# @DESCRIPTION:
113 +# Get relative kernel image path specific to the current ${ARCH}.
114 +kernel-install_get_image_path() {
115 + case ${ARCH} in
116 + amd64|x86)
117 + echo arch/x86/boot/bzImage
118 + ;;
119 + *)
120 + die "${FUNCNAME}: unsupported ARCH=${ARCH}"
121 + ;;
122 + esac
123 +}
124 +
125 +# @FUNCTION: kernel-install_install_kernel
126 +# @USAGE: <version> <image> <system.map>
127 +# @DESCRIPTION:
128 +# Install kernel using installkernel tool. <version> specifies
129 +# the kernel version, <image> full path to the image, <system.map>
130 +# full path to System.map.
131 +kernel-install_install_kernel() {
132 + debug-print-function ${FUNCNAME} "${@}"
133 +
134 + [[ ${#} -eq 3 ]] || die "${FUNCNAME}: invalid arguments"
135 + local version=${1}
136 + local image=${2}
137 + local map=${3}
138 +
139 + ebegin "Installing the kernel via installkernel"
140 + # note: .config is taken relatively to System.map;
141 + # initrd relatively to bzImage
142 + installkernel "${version}" "${image}" "${map}"
143 + eend ${?} || die "Installing the kernel failed"
144 +}
145 +
146 +# @FUNCTION: kernel-install_update_symlink
147 +# @USAGE: <target> <version>
148 +# @DESCRIPTION:
149 +# Update the kernel source symlink at <target> (full path) with a link
150 +# to <target>-<version> if it's either missing or pointing out to
151 +# an older version of this package.
152 +kernel-install_update_symlink() {
153 + debug-print-function ${FUNCNAME} "${@}"
154 +
155 + [[ ${#} -eq 2 ]] || die "${FUNCNAME}: invalid arguments"
156 + local target=${1}
157 + local version=${2}
158 +
159 + if [[ ! -e ${target} ]]; then
160 + ebegin "Creating ${target} symlink"
161 + ln -f -n -s "${target##*/}-${version}" "${target}"
162 + eend ${?}
163 + else
164 + local symlink_target=$(readlink "${target}")
165 + local symlink_ver=${symlink_target#${target##*/}-}
166 + if [[ ${symlink_target} == ${target##*/}-* && \
167 + -z ${symlink_ver//[0-9.]/} ]]
168 + then
169 + local symlink_pkg=${CATEGORY}/${PN}-${symlink_ver}
170 + # if the current target is either being replaced, or still
171 + # installed (probably depclean candidate), update the symlink
172 + if has "${symlink_ver}" ${REPLACING_VERSIONS} ||
173 + has_version -r "~${symlink_pkg}"
174 + then
175 + ebegin "Updating ${target} symlink"
176 + ln -f -n -s "${target##*/}-${version}" "${target}"
177 + eend ${?}
178 + fi
179 + fi
180 + fi
181 +}
182 +
183 +# @FUNCTION: kernel-install_get_qemu_arch
184 +# @DESCRIPTION:
185 +# Get appropriate qemu suffix for the current ${ARCH}.
186 +kernel-install_get_qemu_arch() {
187 + debug-print-function ${FUNCNAME} "${@}"
188 +
189 + case ${ARCH} in
190 + amd64)
191 + echo x86_64
192 + ;;
193 + x86)
194 + echo i386
195 + ;;
196 + *)
197 + die "${FUNCNAME}: unsupported ARCH=${ARCH}"
198 + ;;
199 + esac
200 +}
201 +
202 +# @FUNCTION: kernel-install_test
203 +# @USAGE: <version> <image> <modules>
204 +# @DESCRIPTION:
205 +# Test that the kernel can successfully boot a minimal system image
206 +# in qemu. <version> is the kernel version, <image> path to the image,
207 +# <modules> path to module tree.
208 +kernel-install_test() {
209 + debug-print-function ${FUNCNAME} "${@}"
210 +
211 + [[ ${#} -eq 3 ]] || die "${FUNCNAME}: invalid arguments"
212 + local version=${1}
213 + local image=${2}
214 + local modules=${3}
215 +
216 + local qemu_arch=$(kernel-install_get_qemu_arch)
217 +
218 + dracut \
219 + --conf /dev/null \
220 + --confdir /dev/null \
221 + --no-hostonly \
222 + --kmoddir "${modules}" \
223 + "${T}/initrd" "${version}" || die
224 + # get a read-write copy of the disk image
225 + cp "${DISTDIR}/tinycorelinux-${TCL_VER}-${ARCH}.qcow2" \
226 + "${T}/fs.qcow2" || die
227 +
228 + cd "${T}" || die
229 + cat > run.sh <<-EOF || die
230 + #!/bin/sh
231 + exec qemu-system-${qemu_arch} \
232 + -m 256M \
233 + -display none \
234 + -no-reboot \
235 + -kernel '${image}' \
236 + -initrd '${T}/initrd' \
237 + -serial mon:stdio \
238 + -hda '${T}/fs.qcow2' \
239 + -append 'root=/dev/sda console=ttyS0,115200n8'
240 + EOF
241 + chmod +x run.sh || die
242 + # TODO: initramfs does not let core finish starting on some systems,
243 + # figure out how to make it better at that
244 + expect - <<-EOF || die "Booting kernel failed"
245 + set timeout 900
246 + spawn ./run.sh
247 + expect {
248 + "Kernel panic" {
249 + send_error "\n* Kernel panic"
250 + exit 1
251 + }
252 + "Entering emergency mode" {
253 + send_error "\n* Initramfs failed to start the system"
254 + exit 1
255 + }
256 + "Core 10.1" {
257 + send_error "\n* Booted successfully"
258 + exit 0
259 + }
260 + timeout {
261 + send_error "\n* Kernel boot timed out"
262 + exit 2
263 + }
264 + }
265 + EOF
266 +}
267 +
268 +# @FUNCTION: kernel-install_src_test
269 +# @DESCRIPTION:
270 +# Boilerplate function to remind people to call the tests.
271 +kernel-install_src_test() {
272 + debug-print-function ${FUNCNAME} "${@}"
273 +
274 + die "Please redefine src_test() and call kernel-install_test()."
275 +}
276 +
277 +# @FUNCTION: kernel-install_pkg_preinst
278 +# @DESCRIPTION:
279 +# Stub out mount-boot.eclass.
280 +kernel-install_pkg_preinst() {
281 + debug-print-function ${FUNCNAME} "${@}"
282 +
283 + # (no-op)
284 +}
285 +
286 +# @FUNCTION: kernel-install_pkg_postinst
287 +# @DESCRIPTION:
288 +# Build an initramfs for the kernel, install it and update
289 +# the /usr/src/linux symlink.
290 +kernel-install_pkg_postinst() {
291 + debug-print-function ${FUNCNAME} "${@}"
292 +
293 + if [[ -z ${ROOT} ]]; then
294 + mount-boot_pkg_preinst
295 +
296 + if use initramfs; then
297 + # putting it alongside kernel image as 'initrd' makes
298 + # kernel-install happier
299 + kernel-install_build_initramfs \
300 + "${EROOT}/usr/src/linux-${PV}/initrd" "${PV}"
301 + fi
302 +
303 + kernel-install_install_kernel "${PV}" \
304 + "${EROOT}/usr/src/linux-${PV}/$(kernel-install_get_image_path)" \
305 + "${EROOT}/usr/src/linux-${PV}/System.map"
306 + fi
307 +
308 + kernel-install_update_symlink "${EROOT}/usr/src/linux" "${PV}"
309 +}
310 +
311 +# @FUNCTION: kernel-install_pkg_prerm
312 +# @DESCRIPTION:
313 +# Stub out mount-boot.eclass.
314 +kernel-install_pkg_prerm() {
315 + debug-print-function ${FUNCNAME} "${@}"
316 +
317 + # (no-op)
318 +}
319 +
320 +# @FUNCTION: kernel-install_pkg_postrm
321 +# @DESCRIPTION:
322 +# No-op at the moment. Will be used to remove obsolete kernels
323 +# in the future.
324 +kernel-install_pkg_postrm() {
325 + debug-print-function ${FUNCNAME} "${@}"
326 +
327 + # (no-op at the moment)
328 +}
329 +
330 +_KERNEL_INSTALL_ECLASS=1
331 +fi
332 +
333 +EXPORT_FUNCTIONS src_test pkg_preinst pkg_postinst pkg_prerm pkg_postrm