How Kernel Works in Operating System | Linux in Pakistan
Tutorials

How Kernel Works in Operating System

How Kernel Works in Operating System

In this lecture we’re going to talk a little bit more about the Linux kernel architecture well look at where the kernel actually resides on your system where the kernel modules reside on the system talk a little more bit more about what makes the Linux kernel, the Linux kernel in other words what’s its role in the system. Then we’ll go ahead and we’ll actually download the source code for the Linux kernel take a look at its different options that can be configured within the Linux kernel. The goal here is not to give you the ability to compile your own custom kernel instead it’s just to give you a little bit more information about what this really important piece of the operating system does. I mean the reason it’s called Linux is because it uses the Linux kernel so let’s take a look at a couple things. First thing what is the Linux kernel well according to Wikipedia the Linux kernel is a computer program that manages input/output requests from software and translates them into data processing instructions for the CPU and other electronic components of a computer. There are many types of kernel.

Linux Kernel

In other words you know the director of everything at the lowest level and it handles talking to the hardware and it knows how to communicate with you know expansion cards and knows how to time slice on the CPU and kind of manage all your system resources. It does all of the grunt work under the hood so that you can do the fun stuff in user space with the applications that you want so it’s really again the lowest level part of our computer that allows us to interact with and utilize the hardware that we have available. where does the kernel actually sit if you watched a video lecture a little bit on how the computer goes into a boots from pressing the power button all the way up to a command prompt we talked a little bit about ages the boot loaders job to go ahead and load at the Linux kernel. So I’m going to go into a directory on the Linux system called slash boot and if I take a look at what’s in this directory you’re going to see a couple of things but the one that’s most important or in this case I actually have two kernels on my system are these items that are called vmlinuz and these are actually the system kernels there’s a bunch of other files around here that are useful with booting the system but the actual compiled kernels are these two files here. And you’ll notice that it’s possible to have multiple kernels on the system in this case. I guess in case the update goes bad you can always revert to the old known best kernel. Sometimes you might have a specialized kernel for testing and you may want to boot into that testing kernel maybe you have an older piece of hardware that only works with an older piece of we’re sorry an older Linux kernel and you might want to keep that older kernel around. How do you select or choose to boot into a one kernel over another that’s a little bit beyond the scope of this lecture but you would actually configure that utilizing your system’s bootloader and in this case on ubunru – it uses grub – and at the time your system boots just like you can choose to boot into say single user mode, recovery mode you would be able to select from that menu you could configure it. So you could say OK at boot time. I want to choose this kernel or that kernel but right now by default we are actually booting into the most update kernel in our system and remember you can always see this information by using the uname command on Linux and uname -a will show me that my current running kernel is the 3.5.0-39 generic kernel. So even though I have two available on my system this is the one I’m running remember that kernels have drivers associated with them or modules in the case of the Linux kernel and so those are actually going to be stored in the directory Lib modules. So I’ll go over there I’ll just leave this full screen so everything cut on one case one screen and you’ll notice that there are directories that correspond with the names of our compiled kernels and so it’s important to realize that every kernel has corresponding modules and those modules only correspond to a specific version of the kernel. So if you change kernel versions you need to go ahead and change and recompile and have new modules that are compatible with that new kernel. So in this case we can take a peek in this directory and if I look in that 3.5.0-39-generic directory you’ll notice that there’s a bunch of different files in here but essentially. If we dug around enough we would find the actual kernel modules that we can load into our kernel and in a previous lecture I had discussed how that happens but if you want to see what the installed kernel modules are you can use ls mod and this will show you all the modules that are currently installed into your running kernel. In most cases you’re going to go ahead and get your kernel updates through your distributions main repositories. So in other words you usually don’t ever have to worry about building your own kernel but there are times where you might need to do this like. If you need to fix a problem with a driver or you are provided with you know some customized hardware that maybe you know whatever the vendor does not have a release driver for you that you would need to build against the specific version of the kernel that you’re using. There’s a bunch of reasons you might just want to even try to do this yourself so I will say that the normal path for even building your own custom kernel is going to be to check with your distribution through the website see what the provider of your via Linux distribution recommends. Ubuntu has a really nice way that you can download all of the sources for your kernel using apt-get and you can build your own DB file and manage your kernel installation the same way you would install and manage software using apt-get. So in this case though what I want to do is I want to show you that you can actually go ahead and download the Linux kernel source code and show you how to explore that source code and talk a little bit about you know. What you find in there and we will actually start into the configuration process for building a custom kernel but we’re not really going to go through the whole process of doing that so the place you go to get your kernel source code is it kernel.org and this is the official location to download kernel sources again you don’t really need to do this because the maintainer of your distribution are already taking care of this. You’ll see older versions of the kernel available and you’ll see upcoming release candidates for new versions and basically it’s good to know what is a stable kernel and again you don’t really want to go ahead and be changing your kernel without really giving it a lot of research and thought. But for now we’re just going to explore so I’m going to grab the latest stable kernel. I’m going to save it to my desktop because I had already done this once before so I will put my kernel sources on my desktop and you’re going to notice that it downloads a fairly large file the kernel sources are last time I checked about 72 mega bytes from my previous download now that my kernel sources are downloaded you’ll notice that I’ve got a tar.xz file. So I’m going to go ahead and use tar to uncompress this and in this case I can just let tar figure out what the compression algorithm is and give it the name of the file and it will go ahead and uncompress my kernel it might take a little bit of time because we’re going it’s about 8 to 1 compression ratio. We’re going from about 70 to Meg’s to around 600 Meg’s of data the last time I did this so it might take a little bit time to get that prompt back sort of been a good process to run in the background but for now we can take a look and note that a lot of stuff is happening. So what you realize is that there’s a lot that goes into the source code that is the Linux kernel. When that’s done I’ve now got a folder and in that folder is my uncompressed Linux sources and I’m going to cd into that directory clear and ls. You’ll see a bunch of other directories and files that give you some information about how to navigate around this the most important one being the readme file and it’s important that you go and take a look at the readme file so you can see information about how to build the kernel who maintains it and information about licenses and all other components. Another you know it’s basically the place to start awesome source if you’re curious about writing driver or what source could the drivers looks like the modules within your kernel you can go and dig around and start to look through some of these directories. So for example if we go into the drivers directory I notice that there are a number of other directories here that demonstrate or include information about all these different areas for where you could have various drivers probably the best place to go or what I was most interested in was to take a look at some of the networking stuff you know and those would be in it so if I cd Internet do an ls and then from there I can go take a look at Ethernet drivers so for me taking a look at some of these network drivers is kind of interesting. You know maybe find like the network driver you have on your machine and you can go ahead and learn some more about that definitely worth. Taking a look at some of these files they’re all written in C and you get an idea of how the level drivers are written. If you’re interested but these are all of the source code files oh cool and I went right back up to what I want it to be so you get this ability to look around now. I want to go ahead and start the process for configuring the kernel because I want to get I want you to try to get an appreciation for all of the things and all the knowledge that the kernel has built into it so to do that we’re going to jump into what’s called the configuration menu. The configuration menu is how you tell the KERNEL if you were to create a new one what information it should have built into it. In other words what knowledge is built directly into the kernel what various drivers should be loaded as module so in other words what information does the kernel need to load just in time to use a certain resource and you get to see all the different things that it can do auto blunt. There’s a few things that we need to do to get this set up if you don’t have it already installed I’m going to actually utilize what’s called ncurses or the basic menu config to demonstrate some of the features that the kernel has if you don’t already have it installed the package to install is called Lib-curses5-dev and in my case I already have it installed but if you do not have it installed it would go through and install the dependencies that are necessary. So the reason why I’m able to demonstrate what I’m about to demonstrate is because I’ve installed the ncurses library there are other ways to do this that they can manage using X windows but in with this specific build of ubuntu that I’m using and the virtual machine I’m using I was having trouble with those so I’m going to use this as a way to demonstrate what the kernel can do so. While I am in the kernel source directory I am going to run the command make menu config and what this is going to do is go ahead and do some compiling of things and by the time this is done. What it should do is present me with a text-based gooey system which is really what anchors is provides that allows me to go through and select and deselect and options that I want installed into my kernel. If I would like to have modules installed I can install them as or have certain pieces installed as modules so I’m not going to do too much more beyond this but what I would like to do is just quick demonstrate that when I jump into this configuration menu it shows you all the things that the the kernel has information about and so a really good place to start looking is if I scroll down. Here you’ll notice that one of the selection options here is file systems and I think this is a really great place to just kind of talk a little bit about what the kernel does and things that it and services that it provides. So I’m going to use the arrows to go down to that I’m going to hit enter to select it and what’s going to notice is in this submenu is that there are a number of file systems that the Linux system can support. You’ll notice that anytime, if for example the operating system needs to read a disk that you plug into it that file system that is on that disk needs to be understood by the system and the kernel has knowledge built in. If we look for ext 2 3 4, ReiserFS and a bunch of other things JFS and if you scroll down you’ll notice lots and lots of file systems XFS, btrfs and so on lots of file systems. If there was a new file system or file system that was not included in the kernel you know you would be able to go ahead and add those source code files build those modules or build those directly into the kernel yourself. Something that you could add by I guess patching the kernel source tree which totally is beyond the scope of this but in this case you can kind of review what items are available for you to you know what type of disks you can read and one of the items all the way at the bottom is DOS/FAT/NTFS filesystem. So I’m going to hit enter there and what you’ll notice is the reason why my Linux system can interact with disks that are formatted on a Windows system is because the kernel has information about how to interact with fat systems and dos systems. You’ll notice that it even has read support for NTFS notice that write support for NTFS is off as far as I know still to this day Linux can read NTFS but not write to it a couple other things to point out while we’re in this menu is you’ll notice that msdos support there’s a letter M there so if I hit the spacebar that turns to a star if I hit it again there’s nothing there if I hit it a bit again I go back to module this allows me when I’m configuring my kernel to load that knowledge into the kernel as a module. So only loaded into the kernel when it’s needed if there’s an M there if there’s a star there it will only load that or I’m sorry it will always have information about how to read a kernel embedded into it and if there’s nothing there then the kernel has no idea how to interact interact with ms-dos disks. There’s really not a huge hit between having something loaded as a module or embedded directly in the kernel so it’s really up to you and just about if you don’t think you’re going to be using dos disks all the time but there’s like that one rare occasion where you might use it you know maybe a module is the best way to go I’m gonna use the right arrow key to select exit and hit enter to go back up a level hit exit again and go back up a level and you’ll notice I can do a bunch of stuff in here I can and I would encourage you to kind of just look through here it’s pretty easy too the first time you start to build your own kernels to break things. Because you’ll be like I’ll try this and not realize that that’s dependent on something else or whatever but I encourage you definitely walk through this and I think do this and then the reason why doing this in this lecture just kind of show you that Wow the kernel has a lot of knowledge of a lot of hardware systems. So definitely take some time to look through this menu look through the things that it can support when you’re done you can use the right arrow to exit. I’m going to save it first it will save a file called dot config hit exit and then I will exit out of this menu and if I look it’s now created a file in my directory called dot config and if we look at that file that file contains all of the settings for my kernel so that when I would go to compile a new kernel it would use this file to figure out what should and shouldn’t be created. I’m sorry included in that kernel build how do you build a kernel well making sure I’m in that directory once I’ve run make menu config you’re going to run make all which would go ahead and compile the kernel and that can take a while depending on the speedier system. When you’ve made everything and everything is built all your modules and your kernel itself you can run make modules install which will copy your modules over to the appropriate folder and live modules and then you can do make install to install your kernel. After you’ve done that you’ll need to go edit your boot loader so that it’s aware of the new kernel and knows that it’s there. Then you can try to reboot your system and see that if you’ve built a good kernel really long process goal of this lecture definitely download the Linux source code take a look at the code that’s written in there take some time to run the menu config and look through the different things that the kernel knows about again you can know pretty much everything about a Linux system everything that the kernel knows you can know there shouldn’t be any mysteries as a Linux administrator. You should be able to know every file in your system why it’s there everything that the kernel you have installed supports these are achievable things and I think that those make for secure systems as well.

More on topic Linux Kernel Tutorial, Linux kernel android and linux kernel pdf formate available soon.

My next article is about difference between kernel and os.