Gentoo Archives: gentoo-dev

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

Replies