xref: /OK3568_Linux_fs/yocto/poky/meta/classes/kernel-module-split.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1pkg_postinst:modules () {
2if [ -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
11pkg_postrm:modules () {
12if [ -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
25PACKAGE_WRITE_DEPS += "kmod-native depmodwrapper-cross"
26
27do_install:append() {
28	install -d ${D}${sysconfdir}/modules-load.d/ ${D}${sysconfdir}/modprobe.d/
29}
30
31KERNEL_SPLIT_MODULES ?= "1"
32PACKAGESPLITFUNCS:prepend = "split_kernel_module_packages "
33
34KERNEL_MODULES_META_PACKAGE ?= "${@ d.getVar("KERNEL_PACKAGE_NAME") or "kernel" }-modules"
35
36KERNEL_MODULE_PACKAGE_PREFIX ?= ""
37KERNEL_MODULE_PACKAGE_SUFFIX ?= "-${KERNEL_VERSION}"
38KERNEL_MODULE_PROVIDE_VIRTUAL ?= "1"
39
40python split_kernel_module_packages () {
41    import re
42
43    modinfoexp = re.compile("([^=]+)=(.*)")
44
45    def extract_modinfo(file):
46        import tempfile, subprocess
47        tempfile.tempdir = d.getVar("WORKDIR")
48        compressed = re.match( r'.*\.(gz|xz|zst)$', file)
49        tf = tempfile.mkstemp()
50        tmpfile = tf[1]
51        if compressed:
52            tmpkofile = tmpfile + ".ko"
53            if compressed.group(1) == 'gz':
54                cmd = "gunzip -dc %s > %s" % (file, tmpkofile)
55                subprocess.check_call(cmd, shell=True)
56            elif compressed.group(1) == 'xz':
57                cmd = "xz -dc %s > %s" % (file, tmpkofile)
58                subprocess.check_call(cmd, shell=True)
59            elif compressed.group(1) == 'zst':
60                cmd = "zstd -dc %s > %s" % (file, tmpkofile)
61                subprocess.check_call(cmd, shell=True)
62            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)
68        subprocess.check_call(cmd, shell=True)
69        # errors='replace': Some old kernel versions contain invalid utf-8 characters in mod descriptions (like 0xf6, 'ö')
70        f = open(tmpfile, errors='replace')
71        l = f.read().split("\000")
72        f.close()
73        os.close(tf[0])
74        os.unlink(tmpfile)
75        if compressed:
76            os.unlink(tmpkofile)
77        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
85    def frob_metadata(file, pkg, pattern, format, basename):
86        vals = extract_modinfo(file)
87
88        dvar = d.getVar('PKGD')
89
90        # If autoloading is requested, output /etc/modules-load.d/<name>.conf and append
91        # appropriate modprobe commands to the postinst
92        autoloadlist = (d.getVar("KERNEL_MODULE_AUTOLOAD") or "").split()
93        autoload = d.getVar('module_autoload_%s' % basename)
94        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()
107            postinst = d.getVar('pkg_postinst:%s' % pkg)
108            if not postinst:
109                bb.fatal("pkg_postinst:%s not defined" % pkg)
110            postinst += d.getVar('autoload_postinst_fragment') % (autoload or basename)
111            d.setVar('pkg_postinst:%s' % pkg, postinst)
112
113        # Write out any modconf fragment
114        modconflist = (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()
115        modconf = d.getVar('module_conf_%s' % basename)
116        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
124        files = d.getVar('FILES:%s' % pkg)
125        files = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (files, basename, basename)
126        d.setVar('FILES:%s' % pkg, files)
127
128        conffiles = d.getVar('CONFFILES:%s' % pkg)
129        conffiles = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (conffiles, basename, basename)
130        d.setVar('CONFFILES:%s' % pkg, conffiles)
131
132        if "description" in vals:
133            old_desc = d.getVar('DESCRIPTION:' + pkg) or ""
134            d.setVar('DESCRIPTION:' + pkg, old_desc + "; " + vals["description"])
135
136        rdepends = bb.utils.explode_dep_versions2(d.getVar('RDEPENDS:' + pkg) or "")
137        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:
144            if not dep in rdepends:
145                rdepends[dep] = []
146        d.setVar('RDEPENDS:' + pkg, bb.utils.join_deps(rdepends, commasep=False))
147
148        # Avoid automatic -dev recommendations for modules ending with -dev.
149        d.setVarFlag('RRECOMMENDS:' + pkg, 'nodeprrecs', 1)
150
151        # Provide virtual package without postfix
152        providevirt = d.getVar('KERNEL_MODULE_PROVIDE_VIRTUAL')
153        if providevirt == "1":
154           postfix = format.split('%s')[1]
155           d.setVar('RPROVIDES:' + pkg, pkg.replace(postfix, ''))
156
157    kernel_package_name = d.getVar("KERNEL_PACKAGE_NAME") or "kernel"
158    kernel_version = d.getVar("KERNEL_VERSION")
159
160    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))?$'
173
174    module_pattern_prefix = d.getVar('KERNEL_MODULE_PACKAGE_PREFIX')
175    module_pattern_suffix = d.getVar('KERNEL_MODULE_PACKAGE_SUFFIX')
176    module_pattern = module_pattern_prefix + kernel_package_name + '-module-%s' + module_pattern_suffix
177
178    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))
179    if modules:
180        d.appendVar('RDEPENDS:' + metapkg, ' '+' '.join(modules))
181
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.
185    dvar = d.getVar('PKGD')
186    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
191do_package[vardeps] += '${@" ".join(map(lambda s: "module_conf_" + s, (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()))}'
192