blob: 9487365eb7224940368359e5e232cc61d5e9c75a [file] [log] [blame]
Patrick Williams92b42cb2022-09-03 06:53:57 -05001#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7pkg_postinst:modules () {
8if [ -z "$D" ]; then
9 depmod -a ${KERNEL_VERSION}
10else
11 # image.bbclass will call depmodwrapper after everything is installed,
12 # no need to do it here as well
13 :
14fi
15}
16
17pkg_postrm:modules () {
18if [ -z "$D" ]; then
19 depmod -a ${KERNEL_VERSION}
20else
Andrew Geissler87f5cff2022-09-30 13:13:31 -050021 depmodwrapper -a -b $D ${KERNEL_VERSION} ${KERNEL_PACKAGE_NAME}
Patrick Williams92b42cb2022-09-03 06:53:57 -050022fi
23}
24
25autoload_postinst_fragment() {
26if [ x"$D" = "x" ]; then
27 modprobe %s || true
28fi
29}
30
31PACKAGE_WRITE_DEPS += "kmod-native depmodwrapper-cross"
32
Andrew Geissler8f840682023-07-21 09:09:43 -050033modulesloaddir ??= "${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '${nonarch_libdir}', '${sysconfdir}', d)}/modules-load.d"
34modprobedir ??= "${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '${nonarch_base_libdir}', '${sysconfdir}', d)}/modprobe.d"
Patrick Williams92b42cb2022-09-03 06:53:57 -050035
36KERNEL_SPLIT_MODULES ?= "1"
Andrew Geissler517393d2023-01-13 08:55:19 -060037PACKAGESPLITFUNCS =+ "split_kernel_module_packages"
Patrick Williams92b42cb2022-09-03 06:53:57 -050038
39KERNEL_MODULES_META_PACKAGE ?= "${@ d.getVar("KERNEL_PACKAGE_NAME") or "kernel" }-modules"
40
41KERNEL_MODULE_PACKAGE_PREFIX ?= ""
42KERNEL_MODULE_PACKAGE_SUFFIX ?= "-${KERNEL_VERSION}"
43KERNEL_MODULE_PROVIDE_VIRTUAL ?= "1"
44
45python split_kernel_module_packages () {
46 import re
47
48 modinfoexp = re.compile("([^=]+)=(.*)")
49
50 def extract_modinfo(file):
51 import tempfile, subprocess
52 tempfile.tempdir = d.getVar("WORKDIR")
53 compressed = re.match( r'.*\.(gz|xz|zst)$', file)
54 tf = tempfile.mkstemp()
55 tmpfile = tf[1]
56 if compressed:
57 tmpkofile = tmpfile + ".ko"
58 if compressed.group(1) == 'gz':
59 cmd = "gunzip -dc %s > %s" % (file, tmpkofile)
60 subprocess.check_call(cmd, shell=True)
61 elif compressed.group(1) == 'xz':
62 cmd = "xz -dc %s > %s" % (file, tmpkofile)
63 subprocess.check_call(cmd, shell=True)
64 elif compressed.group(1) == 'zst':
65 cmd = "zstd -dc %s > %s" % (file, tmpkofile)
66 subprocess.check_call(cmd, shell=True)
67 else:
68 msg = "Cannot decompress '%s'" % file
69 raise msg
Patrick Williams03514f12024-04-05 07:04:11 -050070 cmd = "%s -j .modinfo -O binary %s %s" % (d.getVar("OBJCOPY"), tmpkofile, tmpfile)
Patrick Williams92b42cb2022-09-03 06:53:57 -050071 else:
Patrick Williams03514f12024-04-05 07:04:11 -050072 cmd = "%s -j .modinfo -O binary %s %s" % (d.getVar("OBJCOPY"), file, tmpfile)
Patrick Williams92b42cb2022-09-03 06:53:57 -050073 subprocess.check_call(cmd, shell=True)
74 # errors='replace': Some old kernel versions contain invalid utf-8 characters in mod descriptions (like 0xf6, 'รถ')
Andrew Geissler8f840682023-07-21 09:09:43 -050075 with open(tmpfile, errors='replace') as f:
76 l = f.read().split("\000")
Patrick Williams92b42cb2022-09-03 06:53:57 -050077 os.close(tf[0])
78 os.unlink(tmpfile)
79 if compressed:
80 os.unlink(tmpkofile)
81 vals = {}
82 for i in l:
83 m = modinfoexp.match(i)
84 if not m:
85 continue
86 vals[m.group(1)] = m.group(2)
87 return vals
88
89 def frob_metadata(file, pkg, pattern, format, basename):
90 vals = extract_modinfo(file)
91
92 dvar = d.getVar('PKGD')
93
Andrew Geissler8f840682023-07-21 09:09:43 -050094 # If autoloading is requested, output ${modulesloaddir}/<name>.conf and append
Patrick Williams92b42cb2022-09-03 06:53:57 -050095 # appropriate modprobe commands to the postinst
96 autoloadlist = (d.getVar("KERNEL_MODULE_AUTOLOAD") or "").split()
97 autoload = d.getVar('module_autoload_%s' % basename)
98 if autoload and autoload == basename:
99 bb.warn("module_autoload_%s was replaced by KERNEL_MODULE_AUTOLOAD for cases where basename == module name, please drop it" % basename)
100 if autoload and basename not in autoloadlist:
101 bb.warn("module_autoload_%s is defined but '%s' isn't included in KERNEL_MODULE_AUTOLOAD, please add it there" % (basename, basename))
102 if basename in autoloadlist:
Andrew Geissler8f840682023-07-21 09:09:43 -0500103 conf = '%s/%s.conf' % (d.getVar('modulesloaddir'), basename)
104 name = '%s%s' % (dvar, conf)
105 os.makedirs(os.path.dirname(name), exist_ok=True)
106 with open(name, 'w') as f:
107 if autoload:
108 for m in autoload.split():
109 f.write('%s\n' % m)
110 else:
111 f.write('%s\n' % basename)
112 conf2append = ' %s' % conf
113 d.appendVar('FILES:%s' % pkg, conf2append)
114 d.appendVar('CONFFILES:%s' % pkg, conf2append)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500115 postinst = d.getVar('pkg_postinst:%s' % pkg)
116 if not postinst:
117 bb.fatal("pkg_postinst:%s not defined" % pkg)
118 postinst += d.getVar('autoload_postinst_fragment') % (autoload or basename)
119 d.setVar('pkg_postinst:%s' % pkg, postinst)
120
121 # Write out any modconf fragment
122 modconflist = (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()
123 modconf = d.getVar('module_conf_%s' % basename)
124 if modconf and basename in modconflist:
Andrew Geissler8f840682023-07-21 09:09:43 -0500125 conf = '%s/%s.conf' % (d.getVar('modprobedir'), basename)
126 name = '%s%s' % (dvar, conf)
127 os.makedirs(os.path.dirname(name), exist_ok=True)
128 with open(name, 'w') as f:
129 f.write("%s\n" % modconf)
130 conf2append = ' %s' % conf
131 d.appendVar('FILES:%s' % pkg, conf2append)
132 d.appendVar('CONFFILES:%s' % pkg, conf2append)
133
Patrick Williams92b42cb2022-09-03 06:53:57 -0500134 elif modconf:
135 bb.error("Please ensure module %s is listed in KERNEL_MODULE_PROBECONF since module_conf_%s is set" % (basename, basename))
136
Patrick Williams92b42cb2022-09-03 06:53:57 -0500137 if "description" in vals:
138 old_desc = d.getVar('DESCRIPTION:' + pkg) or ""
139 d.setVar('DESCRIPTION:' + pkg, old_desc + "; " + vals["description"])
140
141 rdepends = bb.utils.explode_dep_versions2(d.getVar('RDEPENDS:' + pkg) or "")
142 modinfo_deps = []
143 if "depends" in vals and vals["depends"] != "":
144 for dep in vals["depends"].split(","):
145 on = legitimize_package_name(dep)
146 dependency_pkg = format % on
147 modinfo_deps.append(dependency_pkg)
148 for dep in modinfo_deps:
149 if not dep in rdepends:
150 rdepends[dep] = []
151 d.setVar('RDEPENDS:' + pkg, bb.utils.join_deps(rdepends, commasep=False))
152
153 # Avoid automatic -dev recommendations for modules ending with -dev.
154 d.setVarFlag('RRECOMMENDS:' + pkg, 'nodeprrecs', 1)
155
156 # Provide virtual package without postfix
157 providevirt = d.getVar('KERNEL_MODULE_PROVIDE_VIRTUAL')
158 if providevirt == "1":
159 postfix = format.split('%s')[1]
160 d.setVar('RPROVIDES:' + pkg, pkg.replace(postfix, ''))
161
162 kernel_package_name = d.getVar("KERNEL_PACKAGE_NAME") or "kernel"
163 kernel_version = d.getVar("KERNEL_VERSION")
164
165 metapkg = d.getVar('KERNEL_MODULES_META_PACKAGE')
166 splitmods = d.getVar('KERNEL_SPLIT_MODULES')
167 postinst = d.getVar('pkg_postinst:modules')
168 postrm = d.getVar('pkg_postrm:modules')
169
170 if splitmods != '1':
Andrew Geissler8f840682023-07-21 09:09:43 -0500171 d.appendVar('FILES:' + metapkg, '%s %s %s/modules' %
172 (d.getVar('modulesloaddir'), d.getVar('modprobedir'), d.getVar("nonarch_base_libdir")))
Patrick Williams92b42cb2022-09-03 06:53:57 -0500173 d.appendVar('pkg_postinst:%s' % metapkg, postinst)
174 d.prependVar('pkg_postrm:%s' % metapkg, postrm);
175 return
176
177 module_regex = r'^(.*)\.k?o(?:\.(gz|xz|zst))?$'
178
179 module_pattern_prefix = d.getVar('KERNEL_MODULE_PACKAGE_PREFIX')
180 module_pattern_suffix = d.getVar('KERNEL_MODULE_PACKAGE_SUFFIX')
181 module_pattern = module_pattern_prefix + kernel_package_name + '-module-%s' + module_pattern_suffix
182
183 modules = do_split_packages(d, root='${nonarch_base_libdir}/modules', file_regex=module_regex, output_pattern=module_pattern, description='%s kernel module', postinst=postinst, postrm=postrm, recursive=True, hook=frob_metadata, extra_depends='%s-%s' % (kernel_package_name, kernel_version))
184 if modules:
185 d.appendVar('RDEPENDS:' + metapkg, ' '+' '.join(modules))
Patrick Williams92b42cb2022-09-03 06:53:57 -0500186}
187
188do_package[vardeps] += '${@" ".join(map(lambda s: "module_conf_" + s, (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()))}'