You are not logged in.
Getting and compiling a Linux kernel need not be hard or complex. The following tutorial is long, but not difficult.
This tutorial assumes -
you've installed ( or upgraded to) Devuan
you are compiling the kernel on the machine that will use it. Other configurations are do-able, just not part of this tutorial
you are compiling a i386 or amd64 system. This tutorial doesn't cover other architectures
you have a userid installed on the base system, if you are going to use userid 'root' then skip the 'setup sudo' part
you know the 'root' password
you are ok with Grub. Grub update is completed by the 'make' install step. Tutorial uses only this method
Task list -
Setup 'sudo' so that you can do Administrator level work without being 'root'
Edit apt-get 'sources.list' to comment out the CD-ROM source, or move the cdrom definition to last place in the file
Use apt-get to install the several prerequisites
Make a directory for the linux source files
Use git to clone the Linux branch (all of it to start)... roughly 3 GiB
Check latest linux version
Checkout the latest linux kernel version, or checkout a specific version
Get a copy of the kernel config from your current linux install
Use 'menuconfig' to prep the kernel .config file
Use 'make' to compile the kernel
Use 'make' to compile the various modules and to install everything
Reboot to effect the new kernel
Check to see that your machine is using the new kernel version
Party !!
Detail task steps -
Switch User (su) to root to add your userid to "Sudo" so that you can do Admin work without being administrator
Open a Terminal window/screen, at prompt type
su
and press [return/enter key], then provide root password (default devuan pswd is: toor )
Add 'Sudo' to your userid
visudo then press [return/enter key]
Add your userid (ie: bkm ) just below the entry for "root" with same priviliges, etc..
root ALL=(ALL:ALL) ALL
bkm ALL=(ALL:ALL) ALL
Press control-x to exit, select "Y" when prompted to save changes, then [enter] key to exit
Back at root prompt, type in
exit
Edit sources.list to comment out the CD-ROM source, or move the line with the definition to last place in file
sudo nano /etc/apt/sources.list
Comment out (place a hash symbol # at beginning of line) "deb cdrom:..."
Ctrl-x, then "Y" to save and exit
Use apt-get to install these several software prerequisites.
sudo apt-get update
sudo apt-get install libncurses5-dev build-essential libssl-dev libelf-dev git bison flex
press "Y" at "Do you want to continue? [Y/n]" prompt
Make a directory for the linux source files, then change directory into your new source directory.
mkdir ~/src
cd ~/src
Use git to clone the Linux branch (all of it to start)
git clone [url]https://github.com/torvalds/linux.git[/url]
Check latest linux version, cd into the git linux directory
cd ~/src/linux
git tag -l (that"s a lower case "L" for list )
check the listing for the highest level version, remember that 4.11 comes after 4.1
Checkout the latest linux kernel version after reviewing the git tag list (above)
git checkout -f
OR if you want/know of a specific version then ie:
git checkout v4.11-rc6
Get a copy of the kernel config... from our current linux install
cp /boot/conf* ~/src/linux/.config (note the .config )
Validate the .config using 'menuconfig'
make menuconfig
once in 'menuconfig' (text based kernel config menu) then use arrow-keys to select <Exit>
select <Yes> at the save .config prompt
Kick off the kernel compile, use 6 job threads to do the compile
make -j 6
Kick off the kernel modules prepare, then install the modules
make modules_prepare
sudo make modules_install install
After the kernel is compiled, the large number of modules is installed, and GRUB2 is updated, then reboot to move to the new kernel version.
sudo reboot
Once the reboot occurs you"ll see the GRUB boot menu,
and if you do nothing (for the 5 seconds you"re allowed) then the latest kernel version will be used to boot the machine
Check to see if the machine is using the new kernel version
Open a terminal session and at terminal prompt, type in:
uname -a
you should see something like...
"Linux devuan 4.11.0-rc4+ #1 SMP Thu Mar 30.. x86_64 GNU/Linux"
Party!!
-------------------------------------------------------------------------------------------------------
Appendix
- maintaining your Linux kernel, housekeeping...
- assumes you start where you left off (above)
To go a bit further, you might want to customize your kernel a bit, or to recycle and compile another kernel. Some helpful tips.
Gather information about your machine, helps when you want to customize your kernel. The more you know about your machine, the finer you can tune it.
List and capture machine specific information. Run these commands, then store/print the output for reference.
List PCI information
lspci > myMachine.info
or lspci -v for more verbose information
List USB information
lsusb >> myMachine.info
or lsusb -v for more info
List CPU info
lscpu >> myMachine.info
List Modules loaded and running on your machine
lsmod >> myMachine.info
Print out the myMachine.info file and keep for reference.
Clean or Reset your kernel build environment.
Change to your Linux source directory, list the current kernel version, then save your existing .config file to something meaningful... ie: your kernel version!
cd ~/src/linux
uname -a
cp .config .config-4.11-rc2
Clean up and reset the kernel build env.
make clean
make mrproper
Copy your saved kernel configuration to .config
cd ~/src/linux
cp .config-4.11-rc2 .config
OR list the current /boot/configs and copy the one you want. ie: latest
cd ~/src/linux
ls -la /boot/config
cp /boot/config-4.11-rc2 .config
Get the latest kernel updates from https://github.com/torvalds/linux.git
git fetch
List out the kernel versions.... remember 4.1, 4.11, 4.2...
git tag -l
Checkout the latest kernel version (reset the master head) or select a specific kernel version (see way above).
git checkout -f
Use steps below to 'make menuconfig' to validate the kernel .config
Now compile your kernel... and reboot.
Tune your kernel .config
Should you want to start tuning your kernel (shrinking it to the smallest possible size with the most flexibility) then you must modify the kernel .config file by using the 'make menuconfig' utility. This is where your earlier inventory of machine hardware (above) is important.
Ensure you've saved off your build environment .config or use the instructions above to make a copy of the exiting /boot/configxxx file.
An easy way to make a succinct kernel configuration, based on the currently loaded kernel modules
cd ~/src/linux
make localmodconfig
check the listed modules against your machine inventory list (the myMachine.info listing).
Firmware for USB, networking cards, or wifi things are where most problems occur.
If it's ok with you then update your '/etc/apt/sources.list' for contrib and non-free repositories.
firmware-linux-free comes with the standard install, but other firmware installs such as: firmware-linux, firmware-linux-nonfree, firmware-iwlwifi, firmware-ralink, firmware-realtek, and many others, are in the 'contrib' and 'non-free' repositories.
The 'make' wizard will ask you to decide on the fate of new/updated modules.
For new or modified modules you'll be prompted to decide [Yes/No/Module/New] (New): or similar. Typically the default is in parens ie: '(New)', and that is probably a safe selection...select [enter] key for default option.
ie:
* PCI GPIO expanders
*
AMD 8111 GPIO driver (GPIO_AMD8111) [N/m/y/?] n
BT8XX GPIO abuser (GPIO_BT8XX) [N/m/y/?] (NEW)
go thru each module prompt till you're at the shell prompt again, then use 'make menuconfig' (way above) to validate it.
Explore your kernel .config file
Explore your kernel .config via the 'make menuconfig' menu.
Remember to refer to your previously printed/saved machine info and make intelligent decisions on what to remove, what to leave in.
cd ~/src/linux
cp .config-4.11-rc2 .config
make menuconfig
<exit> and <save> to effect the .config changes, then use 'make' to compile your new, tuned kernel. Reboot to enact it.
If your new, tuned, kernel doesn't boot
Don't panic... we have the previous kernel remember?
Reboot again, and at the restart Kernel Selection menu then use the arrow keys to select the 'Advanced...' options and select a previous kernel.
Last edited by batmore (2018-06-03 04:18:58)
Offline
Thanks for your guide! I have a question though: does Devuan/Debian ship with vanilla kernel? No custom patches?
Offline
Devuan RC currently ships with standard kernel. On my machine...
Linux devuan 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux
Kernel as provided by Debian. So no "custom" patches that Devuan put in (that I'm aware of...).
Last edited by batmore (2017-05-01 20:09:06)
Offline
I see... I think the original package is this one. The page mentions "linux_3.16.39-1+deb8u2.debian.tar.xz" which seems to contain patches collected by Debian against the vanilla kernel. I think this answer my question, thanks
Offline
Hi batmore!
This was my first "selfmade" kernel...
Thanks to you it was relatively easy. I'm now running on kernel version 4.9; until now no
problems occured.
Very nice HOWTO!
Cheers
EDIT:
I forgot to mention, but
make menuconfig
did not work for me, so I tried
make nconfig
that way I could configue my kernel.
I did some searching, for menuconfig I obviously need the packages "ncurses-devel" or "libncurses5-dev";
those I did not find in the repositories.
Now I get on systemstart a failure-message on screen like "cpu freq kernel modules could not be loaded".
Otherwise everything seems to work well.
When I got time, I will start a new post!
Greetings
Last edited by angus71 (2017-05-06 17:30:40)
Offline
I've updated the list of resources/prerequisits to install for make: removed module-init-tools, and added bison and flex
Offline
This is a very useful tutorial and I have a couple of updates/suggestions. Since kernel version 4.14 (I believe) the package "libelf-dev" is also required to compile. And with the package "debhelper" installed you can instead use
make -j 6 deb-pkg
to create a set of .deb packages to install, rather than using the make install steps. This makes it easier to copy your newly built kernel to other computers, or to uninstall it.
Offline
You are so right... I missed that library. I'm glad you mentioned it, and I've updated the list of prerequisites. I've not tried the "debhelper" deb-pkg route yet, so I'm off to give that a shot to see what it gains.
great catch.
Offline
Hi everyone! May be not the right thread, but does anybody know where is the info regarding Devuan kernel rebuilding how-to? Can't find anywhere.
Offline
If you're refering to the production Devuan kernel, and how it's built, etc.. then I recommend going to relay chat https://webchat.freenode.net/ #Devuan and ask the question there... maybe from FSmithRed.
Offline
Sorry but with the next line:
cp /boot/conf* ~/src/linux/.config
I have the error: ".config is not a directory" and I must step this. After I finish all the others steps all is OK but the ~/src directory is empty. It's ok?
Thanks in advance,
Mikel
Last edited by mikel (2018-08-18 15:32:40)
Offline
Sorry but with the next line:
cp /boot/conf* ~/src/linux/.configI have the error: ".config is not a directory" and I must step this. After I finish all the others steps all is OK but the ~/src directory is empty. It's ok?
That error means you have more than one config* file in /boot, so the shell doesn't know which to use. You'll want to use something like:
cp /boot/config-4.9.0-7-amd64 ~/src/linux/.config
That version is the correct one for my ascii installations. You need the version that matches your current kernel, which you can see by typing:
uname -a
Offline
Way back before Devuan existed I'd build my own kernels. So much so, I created a script for it:
#! /bin/bash
#
# Small script for building a new 3.x kernel on Debian and derivatives
# Written by <private>
# Version: 1.0 @ May 6, 2014
# Requires minor editing for latest stable kernel version
# The make install and mkinitramfs steps requires root permissions.
# This script is released under the GPL: http://www.gnu.org/copyleft/gpl.html
# Preparing your system:
# First, we'll install the build environment with some extra's to aid configuration of the new kernel
apt-get install build-essential libncurses5-dev wget tar
# To keep things tidy a new directory for the kernel is created. Remove or comment out both lines if you don't want this.
# If you comment these line out or remove them, the kernel will be downloaded and installed in the current working directory.
mkdir ~/kernel
cd ~/kernel
# Getting the source and preparing it for use:
# edit these lines to represent the latest (longterm support) stable kernel from the kernel.org team
wget -q https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.12.18.tar.xz
tar -xvf linux-3.12.18.tar.xz
cd linux-3.12.18
# Configuring the new kernel:
# Run manually if this part fails and accept all defaults, then comment out the next line with a # in front.
# Alternatively, use "make oldconfig" but this will ask a lot of questions you'd need to answer individually.
# Provided you have installed the correct packages, you can also use the "make xconfig" tool if you're working
# in a graphical environment.
make menuconfig
# Building the kernel and putting everything in place to use:
# This is the part that does the actual work:
make && make modules && make modules_install && make install && mkinitramfs -o /boot/initrd.img-3.12.18 3.12.18
# The last portion of the above line should be edited if another kernel version is used!
# Now you need to update your bootloader to add the new kernel. Most Linux distributions now use Grub2 by default.
# If yours uses the old Grub or Lilo, comment out this line and configure/run manually.
update-grub
# After you've done that, and checked thoroughly twice, it's time to reboot into your new kernel.
exit 0
As you've noticed, this is aimed at the 3.x series kernel, I guess with minor editing this script can also be used for new 4.x and perhaps even the upcoming 5.x series kernel. Unless the tool-chain or its syntax has significantly changed since, I haven't had to build a kernel in several years, due to using a (cutting-edge) source-based distro. (Funtoo, in my case) This script was also intended as a teaching tool for those wishing to compile their own kernels but hadn't tried yet, hence the copious amounts of comments.
HTH!
[edit: I've also found an earlier, undated version of this script, dealing with a 2.6.38 kernel (that's telling! ) last modified Spring 2011... [/edit]
Last edited by Dutch_Master (2019-05-21 04:01:30)
Offline
Hi everyone
Does anyone managed to compile a version 5.X Kernel in Devuan?
I've just installed Devuan Desktop in VirtualBox. But somewhere with an Kernel Update around 4.3.X and 4.8.X the Guest Addition Tools failed to run out of the box. Even with the suggested Kernel module compilation. It seams to be not a Devuan problem because of the same issue in Ubuntu. 18.X, 20.X, Debian 20.X, Raspberry Desktop.
So I would like to try a Kernel 5.10.30 and see if it works and to have a more recent Kernel version..
I was following the instruction on the Debian page:
"4.5. Building a custom kernel from Debian kernel source" at
https://kernel-team.pages.debian.net/ke … tasks.html
Which is almost identical with the above described process. Unfortunately I struggled in configuration using nconfig with error message:
/bin/sh: 1: flex: not found. See below.
root@devuan:/usr/src/linux-5.10.30# make nconfig
UPD scripts/kconfig/nconf-cfg
HOSTCC scripts/kconfig/nconf.o
HOSTCC scripts/kconfig/nconf.gui.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
/bin/sh: 1: flex: not found
make[1]: *** [scripts/Makefile.host:9: scripts/kconfig/lexer.lex.c] Fehler 127
make: *** [Makefile:603: nconfig] Fehler 2
root@devuan:/usr/src/linux-5.10.30#
Any help is appreciated.
This is also an important step for me to learn compile Kernel and Programs, because I'm not willing to use the monstrous systemd distributions and like to contribute in development with Devuan. My be we start to continue this documentation with Major Kernel Updates.
Best regards Hans
Last edited by Sailor17 (2021-04-14 18:57:23)
Offline
Does anyone managed to compile a version 5.X Kernel in Devuan?
Yes, compiled linux-libre-5.9.9 some time ago:
http://packages.gnuinos.org/gnuinos/pool/main/l/linux/
Unfortunately I struggled in configuration using nconfig with error message:
/bin/sh: 1: flex: not found. See below.root@devuan:/usr/src/linux-5.10.30# make nconfig
UPD scripts/kconfig/nconf-cfg
HOSTCC scripts/kconfig/nconf.o
HOSTCC scripts/kconfig/nconf.gui.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
/bin/sh: 1: flex: not found
make[1]: *** [scripts/Makefile.host:9: scripts/kconfig/lexer.lex.c] Fehler 127
make: *** [Makefile:603: nconfig] Fehler 2
root@devuan:/usr/src/linux-5.10.30#Any help is appreciated.
This is also an important step for me to learn compile Kernel and Programs, because I'm not willing to use the monstrous systemd distributions and like to contribute in development with Devuan. My be we start to continue this documentation with Major Kernel Updates.
Best regards Hans
That's right, you need to install both flex and bison. But..., what's the problem installing flex?
If you work systematically, things will come by itself (Lev D. Landau)
Offline
All,
I ran thru this install/config steps by installing Devuan 4.0 Chimera Alpha Desktop as the base and then installing/compiling the v5.12-rc7 linux kernel.
it worked well, yielding the below changes... please note.
> That's right, you need to install both flex and bison. But..., what's the problem installing flex?
Aitor is right, need to install FLEX to make this work. What issue/error did you see when installing flex? Or any of the other essentials software?
Also, the essential install list needs to include 'bc'
so updated full list of needed essential installs is -
sudo apt-get install libncurses5-dev build-essential libssl-dev libelf-dev git bison flex bc
Just so everyone is aware, the size of the linux branch is ~8 GiB
If you can't install or get the menuconfig to work then the nconfig works just fine and yields the same result.
And, after you get this working, then do try the "debhelper" deb-pkg that Untimely suggested. It's very good that it will generate a .deb (and associated) files that you can transport/copy to another Devuan install, and just install the .deb package to update everything.
Also, yes, I'll come back on occasion when major Devuan builds change or major Linux kernel changes to run thru this with latest builds to validate it still works, or if not post updates.
cheers all.
Offline
Hi
I try to compile my own (hopefully slimmer) kernel now, but I miss the point . At
make menuconfig / oldconfig / localoldconfig / xconfig. 146 options are shown from tab completion, but these are not.
which packages provide these make targets ?
I need to reduce kernel size and weed unneeded out.
The devil, you know, is better than the angel, you don't know. by a British Citizen, I don't know too good.
One generation abandons the enterprises of another like stranded vessels. By Henry David Thoreau, WALDEN, Economy. Line 236 (Gutenberg text Version)
broken by design :
https://bugs.debian.org/cgi-bin/bugrepo … bug=958390
Offline
When you use the MAKE script, it will accept a number of options... if you type make -? or make -help then you can use the TAB completion to list a number of different configurations for the make script.
e.g.
Configuration targets:
config - Update current config utilising a line-oriented program
nconfig - Update current config utilising a ncurses menu based program
menuconfig - Update current config utilising a menu based program
xconfig - Update current config utilising a QT based front-end
gconfig - Update current config utilising a GTK based front-end
oldconfig - Update current config utilising a provided .config as base
localmodconfig - Update current config disabling modules not loaded
localyesconfig - Update current config converting local mods to core
silentoldconfig - Same as oldconfig, but quietly, additionally update deps
defconfig - New config with default from ARCH supplied defconfig
savedefconfig - Save current config as ./defconfig (minimal config)
allnoconfig - New config where all options are answered with no
allyesconfig - New config where all options are accepted with yes
allmodconfig - New config selecting modules when possible
alldefconfig - New config with all symbols set to default
randconfig - New config with random answer to all options
listnewconfig - List new options
olddefconfig - Same as silentoldconfig but sets new symbols to their default value
kvmconfig - Enable additional options for guest kernel support
tinyconfig - Configure the tiniest possible kernel
Each of these options provide a differnt set of interfaces to select options for a user to explore.
I typically use make menuconfig
so that have an easily traversed menu based interface to select, or not, certain Kernel Modules or characteristics.
I select these with some thought and then compile the kernel.
Sometimes it works out great.
Other times it does not. In those cases I revert back to the previous kernel option and try again.....
In any case, I explore the differing options using the menuconfig, or the nconfig, or other HELP > TAB options....
Or I create my own CUSTOM .config by selecting various options using 'make menuconfig' and then editing the .config file with a text editor.
All of those options (146?) are available for you to explore. They are various ways to present the various kernel interfaces and/or options... or to limit them.
Part of the fun/beauty is exploring the kernel options given what your hardware/software supports when defining what to use for the kernel, and what to leave out.... that is what 'Tuning' the kernel is all about.
To be safe, select the make menuconfig or the make nconfig options.... and then just exit out to format the .config for that next command = make
Offline
Also, now that I think about it...
You can select make localyesconfig or make localmodconfig options which will only compile or make available those options/modules that exist for your 'running' machine state.
In other words you will create a .config that has only those modules that are currently running on your machine.
This make a great, small, compact kernel based on what is running currently...
But, beware!
This might not include certain firmware that is needed for your machine to run successfully.
Often the network card, or other hardware driver is not included, and after you compile/reboot.... your machine might complain or not start successully until you modify the .config to include that firmware or driver.
Offline
Notes from going thru this guide following the Chimeara stable upgrade.
1. I did a fresh install of Devuan Chimeara using the netinstall.iso install image, downloaded and burned to USB thrumb drive. This is a non-UEFI install and I installed the default Xfce4 desktop. That all went well.
2. I did an update and then full-upgrade to the installation
3. To upgrade to the latest Linux Kernel, 5.16-rc3 I followed the guide steps above, and needed to do two changes.
a. I copied the previous /boot/config-5.10.0-9-amd64 to my ...src/linux/.config and changed the CONFIG_SYSTEM_TRUSTED_KEYS="debian/certs/debian-uefi-certs.pem" to CONFIG_SYSTEM_TRUSTED_KEYS="" else the make fails
b. I needed to install apt install dwarves as a prerequisit tool to get the make to finish successfully. make failed with 'pahole not installed...'
4. after a successfull make , make modules_prepare, sudo make modules_install install then I rebooted.
Whoot, whoot! system booted into Linux kernel 5.16-rc3
bkm@optiplex:/boot$ uname -a
Linux optiplex 5.16.0-rc3+ #2 SMP PREEMPT Thu Dec 2 07:11:06 PST 2021 x86_64 GNU/Linux
Offline