2

While learning how to build and install a custom kernel (for kernel hacking), I came across a contradictory statement.

In this StackExchange answer, the author states:

in the following instructions, paths inside the source tree take the form [src]/whatever, where [src] is the directory you installed the source into, e.g. /usr/src/linux-3.13.3. You probably want to do this stuff su root as the source tree should remain secure in terms of write permissions (it should be owned by root).

While in the reference book, that he mentioned: Linux Kernel in a nutshell, Greg Kroah-Hartman says:

This warning is the most important thing to remember while working through the steps in this book. Everything in this book — downloading the kernel source code,uncompressing it, configuring the kernel, and building it — should be done as a normal user on the machine. Only the two or three commands it takes to install a new kernel should be done as the superuser (root).

and

The kernel source code should also never be placed in the/usr/src/linux/directory, as that is the location of the kernel that the system libraries were built against, not your new custom kernel. Do not do any kernel development under the /usr/src/directory tree at all, but only in a local user directory where nothing bad can happen to the system.

Both sources are quite old, what is the correct approach to this nowadays?

  • What does "custom kernel" mean to you? Please edit your question and elaborate a bit on what you're doing. Are you modifying the kernel, or are you selecting an alternative set of kernel sources for your system? – Andy Dalton Mar 29 '20 at 17:52

1 Answers1

1

/usr is the wrong place for anything "custom":

man hier:

   /usr/src
          Source  files  for different parts of the system, included with some packages for reference purposes.  Don't work
          here with your own projects, as files below /usr should be read-only except when installing software (optional).

   /usr/src/linux
          This was the traditional place for the kernel source.  Some distributions put here the  source  for  the  default
          kernel they ship.  You should probably use another directory when building your own kernel.

man file-hierarchy:

   /usr/
       Vendor-supplied operating system resources. Usually read-only, but this is not required. Possibly shared between
       multiple hosts. This directory should not be modified by the administrator, except when installing or removing
       vendor-supplied packages.

/usr/include was relying on /usr/src/linux before:

   /usr/include/linux
          This  contains  information which may change from system release to system release and used to be a symbolic link
          to /usr/src/linux/include/linux to get at operating-system-specific information.

So the kernel sources only belong in /usr/src to be referenced, not modified.

The Documentation/admin-guide/README.rst shows the O= option so you can turn a build into a read-only affair in /usr/src/linux-VERSION

   cd /usr/src/linux-4.X
   make O=/home/name/build/kernel menuconfig
   make O=/home/name/build/kernel

Like that also the .config file is created under /home.

  • Thank you for that answer! What is your comment about the advice to do everything as root or the opposite as stated by Greg KH? – initBasti Mar 30 '20 at 15:33
  • Q is more how are you going to run and test the new kernel. I would prefer a separate installation for testing. Greg's footnote tells a lot. He is right as a rule, but when you're hacking, you're hacking. Me, I cannot stand this switching between sysadmin and user. –  Mar 30 '20 at 17:10