kernel-module-split.bbclass 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. pkg_postinst:modules () {
  5. if [ -z "$D" ]; then
  6. depmod -a ${KERNEL_VERSION}
  7. else
  8. # image.bbclass will call depmodwrapper after everything is installed,
  9. # no need to do it here as well
  10. :
  11. fi
  12. }
  13. pkg_postrm:modules () {
  14. if [ -z "$D" ]; then
  15. depmod -a ${KERNEL_VERSION}
  16. else
  17. depmodwrapper -a -b $D ${KERNEL_VERSION}
  18. fi
  19. }
  20. autoload_postinst_fragment() {
  21. if [ x"$D" = "x" ]; then
  22. modprobe %s || true
  23. fi
  24. }
  25. PACKAGE_WRITE_DEPS += "kmod-native depmodwrapper-cross"
  26. do_install:append() {
  27. install -d ${D}${sysconfdir}/modules-load.d/ ${D}${sysconfdir}/modprobe.d/
  28. }
  29. KERNEL_SPLIT_MODULES ?= "1"
  30. PACKAGESPLITFUNCS:prepend = "split_kernel_module_packages "
  31. KERNEL_MODULES_META_PACKAGE ?= "${@ d.getVar("KERNEL_PACKAGE_NAME") or "kernel" }-modules"
  32. KERNEL_MODULE_PACKAGE_PREFIX ?= ""
  33. KERNEL_MODULE_PACKAGE_SUFFIX ?= "-${KERNEL_VERSION}"
  34. KERNEL_MODULE_PROVIDE_VIRTUAL ?= "1"
  35. python split_kernel_module_packages () {
  36. import re
  37. modinfoexp = re.compile("([^=]+)=(.*)")
  38. def extract_modinfo(file):
  39. import tempfile, subprocess
  40. tempfile.tempdir = d.getVar("WORKDIR")
  41. compressed = re.match( r'.*\.(gz|xz|zst)$', file)
  42. tf = tempfile.mkstemp()
  43. tmpfile = tf[1]
  44. if compressed:
  45. tmpkofile = tmpfile + ".ko"
  46. if compressed.group(1) == 'gz':
  47. cmd = "gunzip -dc %s > %s" % (file, tmpkofile)
  48. subprocess.check_call(cmd, shell=True)
  49. elif compressed.group(1) == 'xz':
  50. cmd = "xz -dc %s > %s" % (file, tmpkofile)
  51. subprocess.check_call(cmd, shell=True)
  52. elif compressed.group(1) == 'zst':
  53. cmd = "zstd -dc %s > %s" % (file, tmpkofile)
  54. subprocess.check_call(cmd, shell=True)
  55. else:
  56. msg = "Cannot decompress '%s'" % file
  57. raise msg
  58. cmd = "%sobjcopy -j .modinfo -O binary %s %s" % (d.getVar("HOST_PREFIX") or "", tmpkofile, tmpfile)
  59. else:
  60. cmd = "%sobjcopy -j .modinfo -O binary %s %s" % (d.getVar("HOST_PREFIX") or "", file, tmpfile)
  61. subprocess.check_call(cmd, shell=True)
  62. # errors='replace': Some old kernel versions contain invalid utf-8 characters in mod descriptions (like 0xf6, 'ö')
  63. f = open(tmpfile, errors='replace')
  64. l = f.read().split("\000")
  65. f.close()
  66. os.close(tf[0])
  67. os.unlink(tmpfile)
  68. if compressed:
  69. os.unlink(tmpkofile)
  70. vals = {}
  71. for i in l:
  72. m = modinfoexp.match(i)
  73. if not m:
  74. continue
  75. vals[m.group(1)] = m.group(2)
  76. return vals
  77. def frob_metadata(file, pkg, pattern, format, basename):
  78. vals = extract_modinfo(file)
  79. dvar = d.getVar('PKGD')
  80. # If autoloading is requested, output /etc/modules-load.d/<name>.conf and append
  81. # appropriate modprobe commands to the postinst
  82. autoloadlist = (d.getVar("KERNEL_MODULE_AUTOLOAD") or "").split()
  83. autoload = d.getVar('module_autoload_%s' % basename)
  84. if autoload and autoload == basename:
  85. bb.warn("module_autoload_%s was replaced by KERNEL_MODULE_AUTOLOAD for cases where basename == module name, please drop it" % basename)
  86. if autoload and basename not in autoloadlist:
  87. bb.warn("module_autoload_%s is defined but '%s' isn't included in KERNEL_MODULE_AUTOLOAD, please add it there" % (basename, basename))
  88. if basename in autoloadlist:
  89. name = '%s/etc/modules-load.d/%s.conf' % (dvar, basename)
  90. f = open(name, 'w')
  91. if autoload:
  92. for m in autoload.split():
  93. f.write('%s\n' % m)
  94. else:
  95. f.write('%s\n' % basename)
  96. f.close()
  97. postinst = d.getVar('pkg_postinst:%s' % pkg)
  98. if not postinst:
  99. bb.fatal("pkg_postinst:%s not defined" % pkg)
  100. postinst += d.getVar('autoload_postinst_fragment') % (autoload or basename)
  101. d.setVar('pkg_postinst:%s' % pkg, postinst)
  102. # Write out any modconf fragment
  103. modconflist = (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()
  104. modconf = d.getVar('module_conf_%s' % basename)
  105. if modconf and basename in modconflist:
  106. name = '%s/etc/modprobe.d/%s.conf' % (dvar, basename)
  107. f = open(name, 'w')
  108. f.write("%s\n" % modconf)
  109. f.close()
  110. elif modconf:
  111. bb.error("Please ensure module %s is listed in KERNEL_MODULE_PROBECONF since module_conf_%s is set" % (basename, basename))
  112. files = d.getVar('FILES:%s' % pkg)
  113. files = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (files, basename, basename)
  114. d.setVar('FILES:%s' % pkg, files)
  115. conffiles = d.getVar('CONFFILES:%s' % pkg)
  116. conffiles = "%s /etc/modules-load.d/%s.conf /etc/modprobe.d/%s.conf" % (conffiles, basename, basename)
  117. d.setVar('CONFFILES:%s' % pkg, conffiles)
  118. if "description" in vals:
  119. old_desc = d.getVar('DESCRIPTION:' + pkg) or ""
  120. d.setVar('DESCRIPTION:' + pkg, old_desc + "; " + vals["description"])
  121. rdepends = bb.utils.explode_dep_versions2(d.getVar('RDEPENDS:' + pkg) or "")
  122. modinfo_deps = []
  123. if "depends" in vals and vals["depends"] != "":
  124. for dep in vals["depends"].split(","):
  125. on = legitimize_package_name(dep)
  126. dependency_pkg = format % on
  127. modinfo_deps.append(dependency_pkg)
  128. for dep in modinfo_deps:
  129. if not dep in rdepends:
  130. rdepends[dep] = []
  131. d.setVar('RDEPENDS:' + pkg, bb.utils.join_deps(rdepends, commasep=False))
  132. # Avoid automatic -dev recommendations for modules ending with -dev.
  133. d.setVarFlag('RRECOMMENDS:' + pkg, 'nodeprrecs', 1)
  134. # Provide virtual package without postfix
  135. providevirt = d.getVar('KERNEL_MODULE_PROVIDE_VIRTUAL')
  136. if providevirt == "1":
  137. postfix = format.split('%s')[1]
  138. d.setVar('RPROVIDES:' + pkg, pkg.replace(postfix, ''))
  139. kernel_package_name = d.getVar("KERNEL_PACKAGE_NAME") or "kernel"
  140. kernel_version = d.getVar("KERNEL_VERSION")
  141. metapkg = d.getVar('KERNEL_MODULES_META_PACKAGE')
  142. splitmods = d.getVar('KERNEL_SPLIT_MODULES')
  143. postinst = d.getVar('pkg_postinst:modules')
  144. postrm = d.getVar('pkg_postrm:modules')
  145. if splitmods != '1':
  146. etcdir = d.getVar('sysconfdir')
  147. d.appendVar('FILES:' + metapkg, '%s/modules-load.d/ %s/modprobe.d/ %s/modules/' % (etcdir, etcdir, d.getVar("nonarch_base_libdir")))
  148. d.appendVar('pkg_postinst:%s' % metapkg, postinst)
  149. d.prependVar('pkg_postrm:%s' % metapkg, postrm);
  150. return
  151. module_regex = r'^(.*)\.k?o(?:\.(gz|xz|zst))?$'
  152. module_pattern_prefix = d.getVar('KERNEL_MODULE_PACKAGE_PREFIX')
  153. module_pattern_suffix = d.getVar('KERNEL_MODULE_PACKAGE_SUFFIX')
  154. module_pattern = module_pattern_prefix + kernel_package_name + '-module-%s' + module_pattern_suffix
  155. 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))
  156. if modules:
  157. d.appendVar('RDEPENDS:' + metapkg, ' '+' '.join(modules))
  158. # If modules-load.d and modprobe.d are empty at this point, remove them to
  159. # avoid warnings. removedirs only raises an OSError if an empty
  160. # directory cannot be removed.
  161. dvar = d.getVar('PKGD')
  162. for dir in ["%s/etc/modprobe.d" % (dvar), "%s/etc/modules-load.d" % (dvar), "%s/etc" % (dvar)]:
  163. if len(os.listdir(dir)) == 0:
  164. os.rmdir(dir)
  165. }
  166. do_package[vardeps] += '${@" ".join(map(lambda s: "module_conf_" + s, (d.getVar("KERNEL_MODULE_PROBECONF") or "").split()))}'