Building a Custom Kernel on Arch Linux

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to linux mate thank you for watching today we're going to take a look at the process of compiling and installing a custom kernel on arch linux and its derivatives such as arco linux manjaro endeavor os etc oftentimes when people decide to build a custom linux kernel it's because their distribution is using an older kernel that doesn't have support for a specific piece of hardware built in they need to compile a new module or something like that uh this is really a problem on arch because kernel updates are usually available within a week or two of being released so you're generally up to date with the latest kernel however that's not the only reason why you might want to build a custom kernel arch actually ships with quite a large kernel with tons of features and hardware support built in that you'll never use this is nice in a way because when you install arch you can be pretty sure that if linux supports your hardware most the time that support will be part of the the kernel the stock kernel but the the downside is you end up with a very large kernel and lots of stuff compiled in that you just never actually use it's also compiled for generic hardware so uh no hardware specific optimizations or anything have been um have been done so if you want a kernel that is built specifically for your system um you want a small memory footprint uh that sort of thing you're looking for more of a minimalist system and a minimalist kernel then building your own kernel is definitely a good option today we will demonstrate how to download the latest kernel sources uh how to configure your custom kernel how to build it and finally how to install it on your arch linux system today we will be focused specifically on the process of building the kernel but we will take a brief look at configuring kernel features as well so let's get started before we begin uh we need to make sure that we have all the dependencies installed that are required to build the kernel um specifically if you've never built a kernel um on this box before um so we will need to sudo pacman-s and we need the base devel package we also need xml2 kmod uh inet utils bc lib elf and we'll go ahead and throw git on there as well i already have these installed on my system so i don't need to go through the process but if you do not then you'll want to make sure these packages are installed prior to proceeding any further with building your kernel now that we have our dependencies installed we can go and download the source for the kernel we want to build so we will grab that from kernel.org this is the site that hosts the linux kernel sources you can download the sources of the kernels for the various versions that are still supported you see 5.8.1 was just released we want 5.7.15 the virtual machine that i'm upgrading in this video is currently on 5.7.12 so i will go with 5.7.15. we need to download the tarball so i will just grab the location and we need to grab this file that will give us all the files we need the sources necessary to successfully build the kernel so now that we have the url for the source file we need to download we need to create a temporary directory where we can actually perform the build and i'm going to call this kernel build and we'll hop over there into the directory and then we can perform the download this should just take a minute and so i'm going to stop the video we'll be back in a minute all right so now that we've downloaded the kernel source we can extract it into the current kernel build directory and we do that with tar xv jf and as you can see the sources are being extracted see a ton of different uh device drivers in the source code i think the linux uh source code is currently uh at least it's about 30 device drivers so um so now we have a new directory um and we will hop in there is linux-5.7.15 and our next step is to prepare the kernel source tree for building and we do this by using the command make mr proper this doesn't make clean and does some other things to clean up the tree just takes a second and our next task is to configure the kernel to tell it which features and modules to build in order to get a base configuration to use as a starting point we can extract the configuration from the currently running kernel this is stored in the proc directory which is a virtual file system that exposes information about your kernel and running processes so the file we're looking for is called config dot gz so let's hop over to proc and take a look um config.gz is right here if you do not have a config.gz file that means that your kernel was built not to expose its config fortunately the arch kernel does expose config.gz and so we have no problems there so let's go back to our build directory and we're going to use a tool called zcat which is similar to cat except it performs extraction at the same time and we're going to redirect its output to a file called dot config and dot config is the file that the the build process looks for in order to know how to configure kernel so now let's take a look at the dot config file as you can see here this is the file the compiler needs to build your custom kernel uh each option has an individual line in this file um and um most of the options will be set equal to either a y and n or an m a y indicates that the feature will be compiled into the kernel directly an n means that the feature will be excluded and an m means that the feature will be compiled as a loadable module so it won't be part of the main kernel but it will be loadable when is needed so i'm going to quickly disable a few uh debug features that are not needed which can generate some messy output during boot and so you can comment out features in the file by just using the pound sign so i am looking for config d bug and let's have a look here i'm looking for i'm going to comment out this one here this one here as well and this one i think we're good so we will save that and since we are upgrading to a new kernel version we need to make sure that all configuration options have values all the options that are required and we can do that by using the command make old def config this just inspects the config file and makes sure that everything that's required is needed if there are options that require values and it needs your input it will ask you it will prompt you for each of the features that needs to be configured but uh it will also provide you with default values so you don't necessarily have to have to know um exactly what you need you just need to accept the default values and continue so before we proceed uh any further i would uh just like to mention uh that you can also generate a kernel config based on your current hardware and running devices if you don't want to use the config that comes with your kernel you're currently running kernel or if your kernel does not provide that and you can do that by using the command make local mod config i'm not going to do this because i've got a configuration that i want to keep but you could use this command if if you want to have the system to [Music] automatically detect your hardware and create a kernel that just uses the running devices the downside is that you won't have any support compiled into your kernel for anything that's not currently connected to your system so if you want to use a usb drive for example and you don't have it plugged in at the time that you run the make local mod config that support for that device will not be compiled into your kernel so something you might want to be careful about or at least at least be aware of but it is an option there if if you use something that you would like to use so now that we have a kernel config file in place we can proceed with customizing it to our needs instead of editing the dot config file by hand we can use a menu driven system to make the config changes easier there are several different utilities we can use to accomplish this but i'm going to use the tried and true menu config utility we can execute that by doing a make menu config and another advantage of using menu driven kernel configuration utilities is that editing the file directly opens you up to possibly making some errors that are not going to that are going to prevent your kernel from building so if you look through here you can see there's a ton of different options that you can use to configure your kernel i'm not going to spend a whole lot of time customizing the kernel um i'll take a closer look at configuring a custom kernel in a later video but today i just want to make a couple of changes just to demonstrate how it's done so we've got let's say look at device drivers there are certain things that i know that i don't need and specifically i'm doing this in a vm and i'm certainly not doing it on a macintosh so i can hit the space bar there to remove the macintosh device drivers from the build and what else do we want to do uh graphic support i know i don't have an ati radeon so i'm going to remove the amd support don't have nvidia remove that as well um and then once you've made the configuration changes that you want you can go over to save and it will give you an option to specify the file name we want to save it to our dot config file so we'll just accept the default there and exit oh is there anything else i want to do um i know i'm using um ext4 file system so i can remove ryzer fs i can remove jfs xfs support um butterfs i think that's enough so let's save the config again and hop out and our kernel is now configured with our desired specifications so now we're ready to build the kernel we initiate the build process by using the make command we can use the dash j flag to instruct the compiler to use multiple cpus for compilation i am working in a virtual machine that has four cores so i will compile using all four cores so we'll do make dash j4 and hit enter and the build process starts so this process will certainly take some time and depending on the features that you've selected to build and the number of cores you're using and the speed of your cpu the build process uh time could vary significantly anywhere from 20 to 30 minutes to several hours so i will now pause the video and we will come back when the process is completed so now that the build process is complete the rest of the steps that we need to perform require root access so let's switch over to the root account and so now uh we need to build the kernel modules unless that you've uh configured your kernel to build everything internally this will be a necessary step building the modules can be done by using make modules install now this will build any modules that do not exist otherwise it will copy the modules to your kernel modules directory so now let me pause the video and we'll be back when this is finished now that the kernel modules have been built successfully we need to install the kernel itself so the first thing we need to do is copy the kernel to the boot partition so the kernel file is located at arch x 86 64. boot and the name is bz image and we want to copy that to the boot partition and we're going to call it vm linus dash i'm called linux and my version number so it's 5.7.15. all right the kernel is in place so now we need to generate the init ram fs file and we do this by using pk init cpio the dash k flag the version of the kernel that we've just built it's 5.7.15 and we need to save this in boot i'm gonna call it init ram fs dash linux 5715 to match the name of the kernel dot img and the edit remfs has been generated so the last file we need to copy over is our system.map file and so let's see let's copy system.map and that's going to go into boot system dot map dash linux 5715 so now we just have to let the grub bootloader know about our new kernel so we do this by generating a new grub config file and i will do this by using grub mk config and we want to output this file to boot slash grub slash grub dot config and the mk another grub mk config program will look in the boot partition and find any kernels and configure them to be listed in the boot uh in the grub boot menu uh startup so now for the moment of truth let's reboot and see how our custom kernel can boot system i'll just do a reboot here and all right let's log in open up terminal and we can check our kernel version by going unnamed dash a and as you can see now we have uh kernel 5.7.15. installed on the system so we have successfully downloaded configured built and installed a new kernel and we are up and running there's obviously a lot more to configuring and optimizing a kernel than what we've gone through today we've basically just scratched the surface but this should be enough to get you started if there's interest i'll consider making a follow-up video that focuses on the kernel config itself and how to go about optimizing and building a kernel that's specific to your system so thank you very much for watching if you enjoyed this video and found it useful please consider liking and subscribing we will be looking at other interesting linux topics and posting more tutorials soon see you later
Info
Channel: Linux Mate
Views: 9,466
Rating: undefined out of 5
Keywords: linux, arch, kernel, programming, build, compile, artix, manjaro, module, gcc, minimalist, unix
Id: VVunP3yDgm4
Channel Id: undefined
Length: 24min 18sec (1458 seconds)
Published: Sat Aug 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.