Changed the question to be more specific, in the hope it will be reopened.

On Ubuntu, how does one apply a patch (using the patch command) to apply a patch to a kernel (this patch, to get btrfs to accept my replacement disk, of all things). I found on the web and here some instructions (Ubuntu's BuildYourOwnKernel), but not the full recipe I am looking for. With this question, I hope someone will provide an easy recipe for all to follow. Following is what is needed:

  • How to fetch the source of the currently running kernel.

  • How to apply the patch on it.

  • How to compile it using the configuration which is usually used for my distribution.

  • How to change the version of the kernel, so I can install it without replacing the current official kernel. Adding '-tmp' or some initials to differentiate it from the official one, if possible.

  • How to get grub recognize the newly built kernel and have it shown in its menu, but not having it loading it by default; previous official kernel should be loaded by default. I want to select explicitely my custom kernel.

  • How to reboot the system such as upon reboot, grub allows one to select the kernel to boot from.

  • Once my usage for the kernel finished, how to remove all traces of it from the filesystem and grub.

  • 1
    The recommended way to do it is actually not distro-agnostic, which is to create a deb file. Feb 24, 2017 at 9:18

1 Answer 1


You'll need to be the root user for most/all of this so just use

sudo -i

Install some utilities and libraries -

apt-get install build-essential fakeroot kernel-package libncurses5-dev zlib1g-dev libssl-dev xz-utils

Then extract the kernel source tree, patch it, and (maybe) copy a working config from /boot to /path/to/source/.config

Then configure the kernel with

make oldconfig


make menuconfig

Once it is configured, then build it. The build command is where you append to the version -

make-kpkg clean 
fakeroot make-kpkg --initrd --append-to-version=-MyPatchedKernel kernel_image kernel_headers

This will generate 2 deb files for you - one for the kernel, one for headers. If you are working in /usr/src/linux then the deb files will be in /usr/src. Install them using dpkg and you should be good to go.

  • Your answer is missing some of my points, such as adding the patch and configuring grub2 properly. But it is a good start. Thanks. Feb 25, 2017 at 13:44
  • 1
    The patch will depend on how the author of the patch created the patch file, if it is for hte whole directory tree or one particular file, etc. I'd expect that wherever you download it from will have some sort of directions. For grub updating, when you install the deb files it will happen automagically. As may things like building new modules for virtualbox, etc (anything using DKMS). Dependign on how you current grub is configured it may present a menu or not, but that is a grub preferences thing...
    – ivanivan
    Feb 25, 2017 at 15:30
  • For grub updating, when you install the deb files it will happen automagically. Meaning what? That it will install the new kernel but it will not be the default loading kernel, like I wish? Mar 4, 2017 at 8:50
  • Both - it will add the new kernel to your grub menu, and make it the default entry (unless you've changed things in grub to dual boot, etc)
    – ivanivan
    Mar 4, 2017 at 14:49

Not the answer you're looking for? Browse other questions tagged .