This is an old revision of the document!
Table of Contents
Building the Linux kernel using git
Author's note: I am writing this in as impartial a way as I can, because I think the user should decide what is best for them.
Where to build
There are two common places to build the kernel. It is important that wherever you build the kernel, it must be a location that does not change nor risk deletion or corruption. For example do NOT build it in /tmp
.
1. You can build the kernel in /usr/src
and then you will have to be root in order to build the kernel. This has the advantage that it works well for multi-user systems.
mkdir -p /usr/src cd /usr/src su
2. You can build the kernel in your home directory to a directory that you should not delete. For example you can use ~/.local/src
or something similar. You can build the kernel as a regular user. The disadvantage is that other users on a multi-user system will not have access to the source.
mkdir -p ~/.local/src cd ~/.local/src
Where to get the kernel source
There are two ways to get the kernel source. We will assume the version is as follows.
version=3.10.24 base=3.10
1. You can use git to get the kernel source. This has the advantage that it will avoid issues if linux.org is cracked. A disadvantage is that the source directory may get large as new releases are pulled.
git clone --depth 1 git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git # this only needs to be run once at the very beginning cd linux-stable git pull # run this every time to update the source git checkout v$version # you MUST checkout a kernel version or you will get the base version, i.e. 3.10 git log # just check to make sure the top of the log says the version you want git tag -v v$version # this is not necessary, but it validates the source using gpg public key, which you need to import
RSA key ID 0000000E
. Run gpg --search 0000000E
and choose the key you want to import, or if that doesn't work then run
gpg --keyserver wwwkeys.pgp.net --recv-keys 0000000E
The key here is just an example.
2. You can download the source tarball from http://kernel.org/. The best way to stay up-to-date is to download the base version tarball and then download the individual version patch that you want, and apply it before compiling.
kernelsite="ftp://www.kernel.org/pub/linux/kernel/v3.x" lftp -c "open $kernelsite; get linux-$base.tar.xz linux-$base.tar.sign" unxz -k "linux-$base.tar.xz" gpg --verify "linux-$base.tar.sign" # the following is done with every new kernel update tar -xf "linux-$base.tar.xz" mv "linux-$base" "linux-$version" cd "linux-$version" lftp -c "open $kernelsite; get patch-$version.xz patch-$version.sign" unxz "patch-$version.xz" gpg --verify "patch-$newver.sign" patch -p1 < "patch-$newver"
Here is a script that downloads the latest patch for the current running kernel. It assumes you already have an xz compressed tarball in the kernel build directory.
- kernupd
#!/bin/bash error () # error { echo "$1" exit 1 } kerneldir="$HOME/.local/src" kernelsite="ftp://www.kernel.org/pub/linux/kernel/v3.x" currentver="$(uname -r)" basever="$(uname -r | rev | cut -d. -f1 --complement | rev)" newver="$(lftp -c "open $kernelsite; ls" | awk '{ print $9 }' | grep "patch-$basever.*\.xz" | sort -V | tail -n1 | sed 's/patch-//' | sed 's/\.xz//')" if test "$currentver" = "$newver" then echo 'Kernel is up to date.' else echo "Updating kernel to $newver" cd "$kerneldir" tar -xf "linux-$basever.tar.xz" || exit 1 mv "linux-$basever" "linux-$newver" || exit 1 cd "linux-$newver" lftp -c "open $kernelsite; get patch-$newver.xz patch-$newver.sign" || exit 1 unxz "patch-$newver.xz" gpg --verify "patch-$newver.sign" || exit 1 patch -p1 < "patch-$newver" || exit 1 make mrproper zcat /proc/config.gz > .config make oldconfig echo "cd $PWD" fi exit 0
How to build
First run the following to make sure everything is setup properly for a build.
make clean make mrproper # note that this deletes any .config file in the current directory
You have many choices when it comes to how to build the kernel. It is best to read the README
file in the kernel source directory to get all the current options, as well as Documentation/kbuild/kconfig.txt
for details on all the options.
- README
- Alternative configuration commands are: "make config" Plain text interface. "make menuconfig" Text based color menus, radiolists & dialogs. "make nconfig" Enhanced text based color menus. "make xconfig" X windows (Qt) based configuration tool. "make gconfig" X windows (Gtk) based configuration tool. "make oldconfig" Default all questions based on the contents of your existing ./.config file and asking about new config symbols. "make silentoldconfig" Like above, but avoids cluttering the screen with questions already answered. Additionally updates the dependencies. "make olddefconfig" Like above, but sets new symbols to their default values without prompting. "make defconfig" Create a ./.config file by using the default symbol values from either arch/$ARCH/defconfig or arch/$ARCH/configs/${PLATFORM}_defconfig, depending on the architecture. "make ${PLATFORM}_defconfig" Create a ./.config file by using the default symbol values from arch/$ARCH/configs/${PLATFORM}_defconfig. Use "make help" to get a list of all available platforms of your architecture. "make allyesconfig" Create a ./.config file by setting symbol values to 'y' as much as possible. "make allmodconfig" Create a ./.config file by setting symbol values to 'm' as much as possible. "make allnoconfig" Create a ./.config file by setting symbol values to 'n' as much as possible. "make randconfig" Create a ./.config file by setting symbol values to random values. "make localmodconfig" Create a config based on current config and loaded modules (lsmod). Disables any module option that is not needed for the loaded modules. To create a localmodconfig for another machine, store the lsmod of that machine into a file and pass it in as a LSMOD parameter. target$ lsmod > /tmp/mylsmod target$ scp /tmp/mylsmod host:/tmp host$ make LSMOD=/tmp/mylsmod localmodconfig The above also works when cross compiling. "make localyesconfig" Similar to localmodconfig, except it will convert all module options to built in (=y) options. You can find more information on using the Linux kernel config tools in Documentation/kbuild/kconfig.txt.
Yet another option is to start with one of the slackware kernel configs found in the source/k
directory of your slackware install media. Just copy the config file in, rename it to .config
and run one of the configuration commands, typically make oldconfig
if you are compiling a kernel version newer than the config file you copied in.
You can also use the kernel config of your currently running kernel located at /proc/config.gz
by running
zcat /proc/config.gz > .config
Note, however, that /proc/config.gz
may not exist if the kernel was not configured to expose it.
/usr/src
and use xconfig
or gconfig
please read howtos:slackware_admin:kernelbuilding#x_and_su
What to build
Kernel Panic-not syncing: VFS: unable to mount root fs on unknown block(8,2)
error.
/etc/lilo.conf
.
Highlights of the kernel config
What follows is a documentation of some of the more important or interesting features of the kernel. This will not be comprehensive, because there are tons and tons of options to configure. It will be just highlights of the options considered to be more interesting or important than the rest. Read the Help on each option for more info. They are presented in the same order as they are in the kernel config along with where to find them.
General setupLocal version - append to kernel release
This option lets you add a string to the end of the kernel version, in case you want to install more that one kernel of the same version.
Kernel compression mode
This lets you select the kernel compression mode. Gzip is the good default choice. Higher compression (LZMA/XZ) requires more RAM and processor usage, but is faster to load from disk to RAM. Lower compression (Gzip/LZO) requires less RAM and processor usage, but is slower to load from disk to RAM.
Kernel .config supportEnable access to .config through /proc/config.gz
Make sure to enable this so you can have easy access to your current kernel config at /proc/config.gz
Automatic process group scheduling
This option can greatly improve the performance of responsiveness of multi-threaded machines. So you can run make -j4
without causing other programs to stutter.
Initial RAM filesystem and RAM disk (initramfs/initrd) support
Make sure to enable this if you are going to make an initrd.
Disable heap randomization
This option should be disabled on machines make after the year 2000 for security reasons.
Optimize very unlikely/likely branches
Can increase kernel performance.
Enable the block layerIO Schedulers —>Deadline I/O scheduler
Causes performance improvement if you use the JFS filesystem.
CFQ I/O schedulerCFQ Group Scheduling support
This option allows CFQ to recognize task groups and control disk bandwidth allocation, and can improve performance.
cat /sys/block/sd?/queue/scheduler echo deadline > /sys/block/sd?/queue/scheduler echo cfq > /sys/block/sd?/queue/scheduler
Processor type and featuresTickless System (Dynamic Ticks)
This causes timer interrupts to only trigger on an as-needed basis both when the system is busy and idle.
Symmetric multi-processing support
This should be enabled for multi-core and multi-processor machines.
Processor family
For maximum performance you should choose the right processor family.
cat /proc/cpuinfo
If you still cannot tell what processor family to choose, then look up the cpu family
and model
online or on the gentoo wiki:IntelAMD
Maximum number of CPUs
Set this to the number of CPUs you have.
SMT (Hyperthreading) scheduler support
This is for Hyperthreading machines, i.e. virtual cores.
Multi-core scheduler support
This is for multi-core machines, i.e. real cores.
Preemption Model
Your selection depends on how you use your computer, and will make a difference performance-wise.
No Forced Preemption (Server)
High latency, Maximum throughput. Good for data servers.
Voluntary Kernel Preemption (Desktop)
Medium latency, Medium throughput. Apps will still run smoothly when the system is under load.
Preemptible Kernel (Low-Latency Desktop)
Low latency, Low throughput. Good for media servers, or low-latency embedded systems.
Reroute for broken boot IRQs
Enable if you have problems with spurious interrupts.
Machine Check / overheating reporting
Make sure to enable this along with Intel or AMD. This allows the kernel to respond to system overheating.
Low address space to protect from user allocation
65536 for most architectures, 32768 for ARM. This can help with NULL pointer deference bugs.
Transparent Hugepage Support
This can increase the performance of apps that require lots of RAM at once, such as p7zip, LZMA, LZMA2 (xz).
Transparent Hugepage Support sysfs defaults
Selecting always will allow hugepages to work without needing to mount hugepages or configure the application to use them, unlike madvise.
Enable cleancache driver
Can increase performance by reducing disk I/O. However, I don't know how stable it is.
MTRR cleanup support
Can improve performance.
MTRR cleanup enable value (0-1)
1 to enable it
MTRR cleanup spare reg num
This is the number of unused/spare MTRR registers.
dmesg
and look for
MTRR variable ranges enabled: 0 base 000000000 mask F80000000 write-back 1 base 07EF00000 mask FFFF00000 uncachable 2 base 07F000000 mask FFF000000 uncachable 3 disabled 4 disabled 5 disabled 6 disabled
Here, registers 3,4,5,6 are unused, so MTRR cleanup spare reg num = 4 total unused registers.
Enable -fstack-protector buffer overflow detection
Can prevent buffer overflows on systems with gcc version 4.2 and up.
Timer frequency
Always pick 1000Hz for systems that need to run multimedia. This number is proportional to interactive responsiveness. You want lower frequencies on servers and higher frequencies on desktops. However, if you have Tickless System enabled timer interrupts will only trigger as-needed, so it may be best to round up.
Compat VDSO support
Disable for glibc version 2.3.3 or later.
Power management and ACPI optionsACPI
Always enable: Button, Fan, Processor, Thermal Zone. Without these, your computer (especially laptops) may overheat because ACPI cannot access thermal monitoring or fans.
Bus optionsMessage Signaled Interrupts (MSI and MSI-X)
This can offload IRQ interrupts by using MSI instead. However, if your BIOS is buggy this may need to be disabled.
Executable file formats / EmulationsIA32 Emulation
Allows 32-bit emulation via multi-lib on 64-bit systems.
Device DriversBlock devicesNormal floppy disk support
Should be module or the floppy drive may malfuction.
Loopback device support
Required by some disk encryption methods.
SCSI device supportAsynchronous SCSI scanning
Will speed up booting, but all SCSI drivers must be built-in for it to work properly.
Serial ATA and Parallel ATA driversAHCI SATA support
If you enable AHCI in the BIOS this must be built-in or part of initrd. If you don't use AHCI, then do the same but for your SATA or PATA driver.
Multiple devices driver support (RAID and LVM)Device mapper support
Crypt target support
Required by cryptsetup.
Graphics support/dev/agpgart (AGP Support)
Only old graphics cards use this. Typically it will be auto-selected by the driver.
Direct Rendering ManagerNouveau (nVidia) cards
If you use nouveau, you only need this driver, because it has framebuffer support, you do NOT need:
Support for frame buffer devicesnVidia Framebuffer Support
Again this is NOT needed by nouveau.
Sound card supportAdvanced Linux Sound ArchitecturePCI sound devicesIntel HD AudioPre-allocated buffer size for HD-audio driver
A larger buffer (e.g. 2048) is preferred for systems using PulseAudio.
USB supportEHCI HCD (USB 2.0) support
Improved Transaction Translator scheduling
Can greatly improve transfer speeds over USB 2.0 in some cases.
Real Time ClockPC-style 'CMOS'
Enable this or you won't be able to set the clock from Linux. It is often accidentally omitted.
Staging drivers —>
Avoid drivers in this section if you value system stability.
File systems
You MUST either build-in the driver for the filesystem on which the kernel modules reside OR create an initrd, or the kernel will NOT boot.
CD-ROM/DVD FilesystemsUDF file system support
You need this if you plan on reading or writing disks with the UDF filesystem.
DOS/FAT/NT FilesystemsNTFS file system support
NTFS write support
You need this if you want to write to NTFS filesystems.
Kernel hackingMagic SysRq key
Enable if you want to use SysRq REISUB (p.cogx on dvorak) to safely shutdown a hung system.
Write protect kernel read-only data structures
Helps catch kernel bugs
Allow gcc to uninline functions marked 'inline'
Enable for gcc 4.x but not for gcc 3.x
Security OptionsRestrict unprivileged access to the kernel syslog
Disable this if you want users to be able to use the 'dmesg' command.
Cryptographic API
Make sure to build-in all algorithms you plan to use for cryptography using cryptsetup, especially if you plan on full disk encryption, otherwise you won't be able to decrypt your disk and thus will not be able to boot. Note that there are optimized and 64-bit versions to choose from.
Parallel crypto engine
Converts an arbitrary crypto algorithm into a parallel algorithm that executes in kernel threads. It allows multi-threading of any crypto algorithm.
lsmod /sbin/lspci -k lsusb dmesg
You may want to consider disabling Staging drivers and EXPERIMENTAL and OBSOLETE drivers if you want a stable, modern kernel. Some can be left on for good reason.
Building
To speed up building, you can use the -j
option for make. The maximum it can be set to is the number of cores or processors plus one. However, if you want to do something while the kernel is building, like browse the web, you may want to use just the number of cores or processors.
cores=4
make -j$cores
Installing
First, you should make sure to remove any previously installed kernel modules at /lib/modules/$version
.
su rm -r /lib/modules/$version # make sure you know what is being removed
Now, you can install the new modules.
make modules_install
Next you should install the kernel itself. This script will install the kernel and make sure it is installed properly.
- installkernel.sh
#!/bin/sh # installs kernel only, this should be run only from the kernel source directory # make sure we are root if test $HOME != '/root' then echo 'ERROR: This script must be run as root' >&2 exit 1 fi # remove the old rm -f /boot/config.old rm -f /boot/System.map.old rm -f /boot/vmlinuz.old # rename the present mv /boot/config /boot/config.old mv /boot/System.map /boot/System.map.old mv /boot/vmlinuz /boot/vmlinuz.old # copy in the new cp arch/x86/boot/bzImage /boot/vmlinuz cp System.map /boot cp .config /boot/config # change permissions of vmlinuz chmod a-rwx,u+r /boot/vmlinuz # check install echo if cmp arch/x86/boot/bzImage /boot/vmlinuz then echo 'Kernel installed correctly' else echo 'Kernel install failed' exit 1 fi if cmp System.map /boot/System.map then echo 'System.map installed correctly' else echo 'System.map install failed' exit 1 fi if cmp .config /boot/config then echo 'Kernel config installed correctly' else echo 'Kernel config install failed' exit 1 fi echo echo 'Kernel install completed successfully' echo 'Remember to run lilo if you use it' echo exit 0
lilo
If you use lilo, which is the default bootmanager on Slackware, you should edit /etc/lilo.conf
and then run
lilo
# append options here if you need any kernel parameters on boot append=" vt.default_utf8=0" # this should point to the device you want to boot boot = /dev/sda # not necessary, but lilo complains if it is not here lba32 # the compact option speeds up boot time significantly, but may not work on all systems compact # This is needed if you want lilo to prompt you for what to boot prompt # This is given in tenths of a second, so 600 for every minute. You can comment this out if you single-boot. timeout = 1200 # Override dangerous defaults that rewrite the partition table: change-rules reset # Normal VGA console, the safest choice. # You can choose others if you want a framebuffer console, but you must have framebuffer support or the screen will go black. vga = normal # End LILO global section # Linux bootable partition config begins # the path to the Linux kernel boot image image = /boot/vmlinuz # the partition where the Linux kernel is located root = /dev/sda1 # how this entry is shown on the boot screen label = Linux read-only # Linux bootable partition config ends
Sources
- The howto was written by H_TeXMeX_H
- The README is an excerpt from the README included with the kernel source.
- The installkernel.sh script was written by H_TeXMeX_H
- The lilo.conf excerpt is from the default lilo.conf that comes with Slackware plus a few options.
- Thanks to Alien Bob for his kernel building howto. It inspired me to write a restructured and updated kernel building guide.