From e63780b710bc8e4f3eb7cd8c315f7f47407d6143 Mon Sep 17 00:00:00 2001 From: mayfrost Date: Sat, 3 Mar 2018 08:24:49 +0000 Subject: [PATCH] Update KERNEL.md --- KERNEL.md | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/KERNEL.md b/KERNEL.md index e109a0e..adda2c5 100644 --- a/KERNEL.md +++ b/KERNEL.md @@ -119,6 +119,153 @@ _NOTE_: The vmlinuz can be any name but that exact name has to be added to the c `update-initramfs -u` +## COMPILE A SINGLE MODULE + +* Only compile a module +`cd linux-/` +* Create files required for compiling external modules +`make modules_prepare` +* Compile modules +`make M=` +* Move module to +`cp /.ko /lib/modules/$(uname -r)/` +* Or +`cp /.ko /lib/modules/` +* Update the modules +`depmod -a` + + +## DKMS +To update modules automatically when changing a kernel use DKMS. Requires the dkms package and the module source code. +* Create a directory +`mkdir /usr/src/-/` +* Copy the source code of the module +`cp -a * ` +* Create a dkms.conf file in the directory +`vi /usr/src/-/dkms.conf` +* Edit +`PACKAGE_NAME=""` +`PACKAGE_VERSION=""` +`BUILT_MODULE_NAME[0]=""` +`DEST_MODULE_LOCATION[0]="/kernel/
//"` +`AUTOINSTALL="yes"` +* Add and to DKMS +`dkms add -m -v ` +* Compile the module with DKMS +`dkms build -m -v ` +* Install the module with DKMS +`dkms install -m -v ` + + +## KERNEL PATCH + +You can find patch files on the Linux Kernel Mailing List https://lkml.org/. + +* Go to kernel source tree +`cd /usr/src/linux-` +* Download the patch file there. +* Following options assume is gzipped. + +* OPTION 1: Git can be used to fallback from the patch and revert to the upatched source. +* Uncompress patch file +`gunzip .gz` +* Apply patch +`git am ` +* To revert a single patch +`git apply -R ` +* To revert several patches first see the git log +`git log --pretty=oneline --abbrev-commit` +* Select the proper number from the first column +`git reset --hard ` + +* OPTION 2: Patch command +* Uncompress patch file +`gunzip .gz` +* The -p flag specifies a number of leading directories to remove, location is at the top of the patch file filename and is relative to the current directory. +`patch -p1 < ` + +* OPTION 3: Use patch with zcat to use the uncrompressed file directly +`zcat .gz | patch -p1` + + +* Compile kernel with the patch now included. + +`make localmodconfig` + +* OPTION 4: Create a minimal kernel config file which necessarily needs to manually enable options afterwards to have a working system. Avaiable from kernel version 3.17-rc1 and up. +`make tinyconfig` + + +## CHANGING THE CONFIGURATION (OPTIONAL) +* Depending on your tastes this part can take minutes, hours, or days to enable proper options. +* The Y (or "*" in menuconfig) flag in kernel configuration compiles options as part of vmlinuz. +* The M flag in kernel configuration compiles options as separate modules. +* The N (or a blank space in menuconfig) flag in kernel configuration will not build selected option. + + +* List hardware and kernel modules in use +`lspci -k` +`lspci -v` +`cat /proc/cpuinfo` +`cat /proc/meminfo` +`lsmod` + +* OPTION 1: Start a menu and browse options. Requires curses library but likely is already on your computer. Press "H" or "?" to see help. You can use the space bar to cycle between the available choices or press the appropriate key mentioned above. Pressing "/" to search for keywords +make menuconfig + +* OPTION 2: Will make questions on what kernel options to support when it finds new kernel options not marked on an existing config file (takes time and and if you are using a striped down config file it requires you knowledge) +`make oldconfig` + +* OPTION 3: Will fill questions with default answers on what kernel options to support beyond your provided config file +`make olddefconfig` + + +## COMPILATION +Can take minutes to hours. + +* Clean directory (wont touch the config file just remove all compiled files from the kernel tree) +`make clean` + +_NOTE_: The "-j" flag, where is the number of cores +1, is OPTIONAL and only goes if you have a processor with multiple cores. If you install ccache (OPTIONAL) you can speed up subsequent compilations by including CC="ccache gcc" after the "-j" flag. +`make -j ` + +* OPTION A: Compile and move everything to its place. The "all" flag makes modules AND the bzImage at the same time (replaces "bzImage" and "modules" flags). +`make -j all modules_install install` + +* OPTION B: One by one + +* If your configuration does not contain answers for all of the options, especially if they are new and not currently included in your running kernel, you will need to answer the prompts for these options. +`make -j bzImage` + +## MOVE KERNEL IMAGE +* OPTION B1: Copy the new kernel to /boot, create the initrd file and change the GRUB menu +`cp arch//boot/bzImage /boot/vmlinuz` +* Not required for booting but some processes need it +`cp System.map /boot` + +* OPTION B2: Automatically move kernel to /boot (and in certain distros, symlink the new kernel to /boot/vmlinuz and update the bootloader configuration) +`make install` + + +## BUILD MODULES +* Compile individual files for each question you answered M during kernel config. The object code is linked against your freshly built kernel. (For questions answered Y, these are already part of vmlinuz, and for questions answered N they are skipped). Modules end with .ko. +`make -j modules` + +## MOVE MODULES +* Copy generated kernel modules to /lib/modules// +`make modules_install` + +_NOTE_: The vmlinuz can be any name but that exact name has to be added to the configuration file of your bootloader of choice. Usually a version number is appended to the new kernel image. This has the advantage to avoid replacing a current kernel and having a fallback as backup to boot. You can set to boot from any image in the bootloader once you configure them in the bootloader. + +## INITRD +* The initrd is used only while booting, unless you compile the kernel with the filesystem it resides on (initfs) +* OPTION 1: Compile the kernel with an initfs. +* OPTION 2: Use mkinitrd +`mkinitrd -o /boot/initrd.img` +* OPTION 3: Use initramfs +`update-initramfs -u` + + ## COMPILE A SINGLE MODULE * Only compile a module