Jump to content

Linux kernel: Difference between revisions

1,862 bytes added ,  20 August 2023
Add instructions for patching In-tree kernel modules without rebuilding the entire kernel
imported>Khoi
imported>Lelgenio
(Add instructions for patching In-tree kernel modules without rebuilding the entire kernel)
Line 451: Line 451:
</syntaxHighlight>
</syntaxHighlight>


== Patching a single In-tree kernel module ==
If you wish to patch a single kernel module, you can avoid rebuilding the entire linux kernel
by packaging that module applying patches to it.
=== Packaging a single kernel module ===
Here is an example for how to package the <code>amdgpu</code> kernel module.
<syntaxHighlight lang=nix>
{ pkgs
, lib
, kernel ? pkgs.linuxPackages_latest.kernel
}:
pkgs.stdenv.mkDerivation {
  pname = "amdgpu-kernel-module";
  inherit (kernel) src version postPatch nativeBuildInputs;
  kernel_dev = kernel.dev;
  kernelVersion = kernel.modDirVersion;
  modulePath = "drivers/gpu/drm/amd/amdgpu";
  buildPhase = ''
    BUILT_KERNEL=$kernel_dev/lib/modules/$kernelVersion/build
    cp $BUILT_KERNEL/Module.symvers .
    cp $BUILT_KERNEL/.config        .
    cp $kernel_dev/vmlinux          .
    make "-j$NIX_BUILD_CORES" modules_prepare
    make "-j$NIX_BUILD_CORES" M=$modulePath modules
  '';
  installPhase = ''
    make \
      INSTALL_MOD_PATH="$out" \
      XZ="xz -T$NIX_BUILD_CORES" \
      M="$modulePath" \
      modules_install
  '';
  meta = {
    description = "AMD GPU kernel module";
    license = lib.licenses.gpl3;
  };
}
</syntaxHighlight>
=== Replacing default kernel modules ===
After packaging the kernel module you can add it to your system just like an out-of-tree kernel module, it will replace the default module provided by the linux package because it has the same name:
<syntaxHighlight lang=nix>
{pkgs, config, ...}:
let
  amdgpu-kernel-module = pkgs.callPackage ./amdgpu-kernel-module.nix {
    # Make sure the module targets the same kernel as your system is using.
    kernel = config.boot.kernelPackages.kernel;
  };
in
{
  boot.extraModulePackages = [
    (amdgpu-kernel-module.overrideAttrs (_: {
      patches = [ ./patches/amdgpu-foo-bar.patch ];
    })
  ];
}
</syntaxHighlight>


== Troubleshooting ==
== Troubleshooting ==
Anonymous user