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.

closed as too broad by jasonwryan, Jeff Schaller, GAD3R, mdpc, countermode Feb 25 '17 at 7:43

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    The recommended way to do it is actually not distro-agnostic, which is to create a deb file. – Rui F Ribeiro Feb 24 '17 at 9:18

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. – Hans Deragon Feb 25 '17 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 '17 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? – Hans Deragon Mar 4 '17 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 '17 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.