06 - Intro to LinuxOutlineKernel DevelopmentConfigurationBuild ProcessAnnouncementsReading: MOS 10.2, LDD 1Kernel DevelopmentAfter obtaining kernel source (see last lecture).4 steps for basic development:Configure kernel features- a lot of code for different hardware, don't enable all of it- `.config` file to enable/disable feature flagsmake menuconfigsudo make INSTALL_MOD_STRIP=1 modules_installsudo make installmake menuconfigmake -j $(nproc)/lib/modules-- compile with max possible cores:- modules need to be compiled for specific kernel versions- modules installed separately in ---- very long wait (~1.5 hr 4 cores; 5.5 hrs 8 cores emulated)Compile kernelInstall kernel modules (as root)Install kernel (as root)A `.config` file is needed in the kernel directory.Steps:Easier to copy the config file for the currentrunning kernelconfig files have thousands of non-obviousfeature flags. It's easier to configure via TUI:- Each config file for corresponding built kernels are available in `/boot`- Select based on current running versionKernel Module DevelopmentQuiz 1 will be on Friday, first 10 min of class.10 MCQ online, no cheat sheet.Don't be late!PA 2 released, but later portions require updates.You can still start the first 2 parts after thislecture.1)1)2)2)3)4)Copy current kernel configcp /boot/config-$(uname -r) .configmake menuconfigmake -j $(nproc) allsudo make INSTALL_MOD_STRIP=1 modules_installsudo make installsudo update-grub/boot---- wait hours...-- copies compiled kernel and config to- select new kernel from GRUB menu- must reboot to switch kernels or if kernel crashes--Modify kernel configCompile kernelInstall kernel modules (as root)Install kernel (as root)Update bootloader (distro-dependent)Reboot with new kernel1)2)4)3)5)6)7)Basic configurations are available in `arch/`,but these are missing a lot of details.ls arch/x86/configs/ls /boot/config-*uname -aKernel Module DevelopmentWorking with Kernel ModulesKernel Module ExampleDynamically loadable modules (kernel extensions)Kernel modules run in kernel-mode.They can touch everything.Often used for:Kernel modules are version specific.Statically compiled kernel modules- loaded manually, requires root permissions- don't need to recompile entire kernel -> faster kernel-mode development- "linux kernel module" usually refers to this- crashes can crash the whole kernel- potential security risk- device drivers- file system drivers- networking protocols (except TCP/IP)- compiled as part of kernel- often necessary for kernel functionCodeDevelopment:We'll do an example "hello world" module.To build it, we need to create a Makefile to invokethe kernel build system, since the module needs tobe linked against a specific kernel version.In action:Kernel Module Commands- loads kernel module by path- unloads kernel module by name- lists loaded modules (/proc/modules)- print info about a kernel module- description, version, parameters, authors, etc.- advanced module loading- modules from kernel sources are at /lib/modules/<kversion>/- handles module dependencies (we probably won't be using it)- generates dependency list at /lib/modules/<kversion>/modules.dep- remove with `-r` flaginsmod <module_path>rmmod <module_name>lsmodmodinfo <module_path>modprobe <module_name>Requires Linux kernel to link againstDrastically shortens development cycle- compiled kernel source tree- header files and libraries- quickly load/unload, no reboot required- no need to recompile huge portions of kernelCompileLoadUseUnload1)2)4)3)5)Available Kernel FunctionsModules are linked against the full kernelKernel symbol table available at:C standard library is not generally available- kernel's printf equivalent (same function args)- messages must start with a- does not print to stdout, but to kernel log - filtered by log level - don't forget `\n`, otherwise output buffered.Kernel Module LibrariesprintkKernel modules can expose symbols for othermodules to use module functionality.- KERN_EMERG- KERN_ALERT- KERN_CIRT- KERN_ERR- KERN_WARNING- KERN_NOTICE- KERN_INFO- KERN_DEBUG- EXPORT_SYMBOL(name);- EXPORT_SYMBOL_GPL(name);- all kernel functions in-scope, including - global variables - exported functions- similar functions available in helper libraries- /proc/kallsyms- /boot/System.map-*int printk(const char *fmt, ...)log level