blob: a29c294810e1ab9d3fa7e28a50e5a69b4fd775de [file] [log] [blame]
Patrick Williams213cb262021-08-07 19:21:33 -05001pkg_postinst:modules () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002if [ -z "$D" ]; then
3 depmod -a ${KERNEL_VERSION}
4else
5 # image.bbclass will call depmodwrapper after everything is installed,
6 # no need to do it here as well
7 :
8fi
9}
10
Patrick Williams213cb262021-08-07 19:21:33 -050011pkg_postrm:modules () {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012if [ -z "$D" ]; then
13 depmod -a ${KERNEL_VERSION}
14else
15 depmodwrapper -a -b $D ${KERNEL_VERSION}
16fi
17}
18
19autoload_postinst_fragment() {
20if [ x"$D" = "x" ]; then
21 modprobe %s || true
22fi
23}
24
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025PACKAGE_WRITE_DEPS += "kmod-native depmodwrapper-cross"
26
Patrick Williams213cb262021-08-07 19:21:33 -050027do_install:append() {
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028 install -d ${D}${sysconfdir}/modules-load.d/ ${D}${sysconfdir}/modprobe.d/
29}
30
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050031KERNEL_SPLIT_MODULES ?= "1"
Patrick Williams213cb262021-08-07 19:21:33 -050032PACKAGESPLITFUNCS:prepend = "split_kernel_module_packages "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
Brad Bishop316dfdd2018-06-25 12:45:53 -040034KERNEL_MODULES_META_PACKAGE ?= "${@ d.getVar("KERNEL_PACKAGE_NAME") or "kernel" }-modules"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
Patrick Williamsc0f7c042017-02-23 20:41:17 -060036KERNEL_MODULE_PACKAGE_PREFIX ?= ""
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037KERNEL_MODULE_PACKAGE_SUFFIX ?= "-${KERNEL_VERSION}"
38KERNEL_MODULE_PROVIDE_VIRTUAL ?= "1"
Patrick Williamsc0f7c042017-02-23 20:41:17 -060039
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040python split_kernel_module_packages () {
41 import re
42
43 modinfoexp = re.compile("([^=]+)=(.*)")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45 def extract_modinfo(file):
46 import tempfile, subprocess
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 tempfile.tempdir = d.getVar("WORKDIR")
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050048 compressed = re.match( r'.*\.(gz|xz|zst)$', file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050049 tf = tempfile.mkstemp()
50 tmpfile = tf[1]
Brad Bishopc342db32019-05-15 21:57:59 -040051 if compressed:
52 tmpkofile = tmpfile + ".ko"
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050053 if compressed.group(1) == 'gz':
Brad Bishopc342db32019-05-15 21:57:59 -040054 cmd = "gunzip -dc %s > %s" % (file, tmpkofile)
55 subprocess.check_call(cmd, shell=True)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050056 elif compressed.group(1) == 'xz':
Brad Bishopc342db32019-05-15 21:57:59 -040057 cmd = "xz -dc %s > %s" % (file, tmpkofile)
58 subprocess.check_call(cmd, shell=True)
Andrew Geisslerd159c7f2021-09-02 21:05:58 -050059 elif compressed.group(1) == 'zst':
60 cmd = "zstd -dc %s > %s" % (file, tmpkofile)
61 subprocess.check_call(cmd, shell=True)
Brad Bishopc342db32019-05-15 21:57:59 -040062 else:
63 msg = "Cannot decompress '%s'" % file
64 raise msg
65 cmd = "%sobjcopy -j .modinfo -O binary %s %s" % (d.getVar("HOST_PREFIX") or "", tmpkofile, tmpfile)
66 else:
67 cmd = "%sobjcopy -j .modinfo -O binary %s %s" % (d.getVar("HOST_PREFIX") or "", file, tmpfile)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068 subprocess.check_call(cmd, shell=True)
Brad Bishop19323692019-04-05 15:28:33 -040069 # errors='replace': Some old kernel versions contain invalid utf-8 characters in mod descriptions (like 0xf6, 'รถ')
70 f = open(tmpfile, errors='replace')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 l = f.read().split("\000")
72 f.close()
73 os.close(tf[0])
74 os.unlink(tmpfile)
Brad Bishopc342db32019-05-15 21:57:59 -040075 if compressed:
76 os.unlink(tmpkofile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077 vals = {}
78 for i in l:
79 m = modinfoexp.match(i)
80 if not m:
81 continue
82 vals[m.group(1)] = m.group(2)
83 return vals
84
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 def frob_metadata(file, pkg, pattern, format, basename):
86 vals = extract_modinfo(file)
87
Brad Bishop6e60e8b2018-02-01 10:27:11 -050088 dvar = d.getVar('PKGD')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089
90 # If autoloading is requested, output /etc/modules-load.d/<name>.conf and append
91 # appropriate modprobe commands to the postinst
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092 autoloadlist = (d.getVar("KERNEL_MODULE_AUTOLOAD") or "").split()
93 autoload = d.getVar('module_autoload_%s' % basename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094 if autoload and autoload == basename:
95 bb.warn("module_autoload_%s was replaced by KERNEL_MODULE_AUTOLOAD for cases where basename == module name, please drop it" % basename)
96 if autoload and basename not in autoloadlist:
97 bb.warn("module_autoload_%s is defined but '%s' isn't included in KERNEL_MODULE_AUTOLOAD, please add it there" % (basename, basename))
98 if basename in autoloadlist:
99 name = '%s/etc/modules-load.d/%s.conf' % (dvar, basename)
100 f = open(name, 'w')
101 if autoload:
102 for m in autoload.split():
103 f.write('%s\n' % m)
104 else:
105 f.write('%s\n' % basename)
106 f.close()
Patrick Williams213cb262021-08-07 19:21:33 -0500107 postinst = d.getVar('pkg_postinst:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 if not postinst:
Patrick Williams213cb262021-08-07 19:21:33 -0500109 bb.fatal("pkg_postinst:%s not defined" % pkg)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500110 postinst += d.getVar('autoload_postinst_fragment') % (autoload or basename)
Patrick Williams213cb262021-08-07 19:21:33 -0500111 d.setVar('pkg_postinst:%s' % pkg, postinst)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112
113 # Write out any modconf fragment
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 modconflist = (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()
115 modconf = d.getVar('module_conf_%s' % basename)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500116 if modconf and basename in modconflist:
117 name = '%s/etc/modprobe.d/%s.conf' % (dvar, basename)
118 f = open(name, 'w')
119 f.write("%s\n" % modconf)
120 f.close()
121 elif modconf:
122 bb.error("Please ensure module %s is listed in KERNEL_MODULE_PROBECONF since module_conf_%s is set" % (basename, basename))
123
Patrick Williams213cb262021-08-07 19:21:33 -0500124 files = d.getVar('FILES:%s' % pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125 files = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (files, basename, basename)
Patrick Williams213cb262021-08-07 19:21:33 -0500126 d.setVar('FILES:%s' % pkg, files)
Andrew Geissler09209ee2020-12-13 08:44:15 -0600127
Patrick Williams213cb262021-08-07 19:21:33 -0500128 conffiles = d.getVar('CONFFILES:%s' % pkg)
Andrew Geissler09209ee2020-12-13 08:44:15 -0600129 conffiles = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (conffiles, basename, basename)
Patrick Williams213cb262021-08-07 19:21:33 -0500130 d.setVar('CONFFILES:%s' % pkg, conffiles)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131
132 if "description" in vals:
Patrick Williams213cb262021-08-07 19:21:33 -0500133 old_desc = d.getVar('DESCRIPTION:' + pkg) or ""
134 d.setVar('DESCRIPTION:' + pkg, old_desc + "; " + vals["description"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135
Patrick Williams213cb262021-08-07 19:21:33 -0500136 rdepends = bb.utils.explode_dep_versions2(d.getVar('RDEPENDS:' + pkg) or "")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600137 modinfo_deps = []
138 if "depends" in vals and vals["depends"] != "":
139 for dep in vals["depends"].split(","):
140 on = legitimize_package_name(dep)
141 dependency_pkg = format % on
142 modinfo_deps.append(dependency_pkg)
143 for dep in modinfo_deps:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 if not dep in rdepends:
145 rdepends[dep] = []
Patrick Williams213cb262021-08-07 19:21:33 -0500146 d.setVar('RDEPENDS:' + pkg, bb.utils.join_deps(rdepends, commasep=False))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147
148 # Avoid automatic -dev recommendations for modules ending with -dev.
Patrick Williams213cb262021-08-07 19:21:33 -0500149 d.setVarFlag('RRECOMMENDS:' + pkg, 'nodeprrecs', 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500150
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500151 # Provide virtual package without postfix
152 providevirt = d.getVar('KERNEL_MODULE_PROVIDE_VIRTUAL')
153 if providevirt == "1":
154 postfix = format.split('%s')[1]
Patrick Williams213cb262021-08-07 19:21:33 -0500155 d.setVar('RPROVIDES:' + pkg, pkg.replace(postfix, ''))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156
Brad Bishop316dfdd2018-06-25 12:45:53 -0400157 kernel_package_name = d.getVar("KERNEL_PACKAGE_NAME") or "kernel"
158 kernel_version = d.getVar("KERNEL_VERSION")
159
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500160 metapkg = d.getVar('KERNEL_MODULES_META_PACKAGE')
161 splitmods = d.getVar('KERNEL_SPLIT_MODULES')
162 postinst = d.getVar('pkg_postinst:modules')
163 postrm = d.getVar('pkg_postrm:modules')
164
165 if splitmods != '1':
166 etcdir = d.getVar('sysconfdir')
167 d.appendVar('FILES:' + metapkg, '%s/modules-load.d/ %s/modprobe.d/ %s/modules/' % (etcdir, etcdir, d.getVar("nonarch_base_libdir")))
168 d.appendVar('pkg_postinst:%s' % metapkg, postinst)
169 d.prependVar('pkg_postrm:%s' % metapkg, postrm);
170 return
171
172 module_regex = r'^(.*)\.k?o(?:\.(gz|xz|zst))?$'
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600173
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500174 module_pattern_prefix = d.getVar('KERNEL_MODULE_PACKAGE_PREFIX')
175 module_pattern_suffix = d.getVar('KERNEL_MODULE_PACKAGE_SUFFIX')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400176 module_pattern = module_pattern_prefix + kernel_package_name + '-module-%s' + module_pattern_suffix
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500177
Brad Bishop316dfdd2018-06-25 12:45:53 -0400178 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))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 if modules:
Patrick Williams213cb262021-08-07 19:21:33 -0500180 d.appendVar('RDEPENDS:' + metapkg, ' '+' '.join(modules))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500181
182 # If modules-load.d and modprobe.d are empty at this point, remove them to
183 # avoid warnings. removedirs only raises an OSError if an empty
184 # directory cannot be removed.
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185 dvar = d.getVar('PKGD')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 for dir in ["%s/etc/modprobe.d" % (dvar), "%s/etc/modules-load.d" % (dvar), "%s/etc" % (dvar)]:
187 if len(os.listdir(dir)) == 0:
188 os.rmdir(dir)
189}
190
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500191do_package[vardeps] += '${@" ".join(map(lambda s: "module_conf_" + s, (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()))}'