Linux Kernel – Linux Hint https://linuxhint.com Exploring and Master Linux Ecosystem Mon, 01 Mar 2021 00:17:41 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.2 How to Check the Linux Kernel Version? https://linuxhint.com/check-the-linux-kernel-version/ Tue, 02 Feb 2021 19:48:51 +0000 https://linuxhint.com/?p=88842

The Kernel is the essential component of any operating system because it manages the processes, resources, and provides a mechanism for communication between software and hardware. There are many Kernel versions available; you could face situations where you’ll have to check the version of the installed Linux kernel on your Linux system. For example, if you want to debug the hardware issue, then you will be interested in checking the Linux Kernel version.

This post presents various commands to check the installed Linux Kernel version on your Linux system. I am using Ubuntu 20.04 for preparing this article and executing the command. The commands used in this article are generic and can be used on other Linux based operating systems like Linux Mint, Fedora, Debian, etc.

Check the Linux Kernel version with the uname command

uname command displays the system information. To check the Linux Kernel installed version, type the below-given command:

$ uname -r

The output shows that the Linux Kernel version 5.10.0-051000 is installed on my Ubuntu 20.04 system. The explanation of the Kernel version is as follows:

5- Kernel version

10-Major revision

0-Minor revision

051000-Patch number

Whereas, the generic confirms that I am using the desktop version of Kernel.

Check Linux Kernel version using the dmesg command

The basic purpose of the dmesg command is to write the Kernel messages. It is also used for checking the Kernel version. Run the dmesg commands with the sudo privileges in the following way to check the Kernel version:

$ dmesg | grep Linux

Check Linux Kernel version using the hostnamectl command

The hostnamectl is a very useful command that is primarily used to change the system hostname. Additionally, it also shows the Kernel version.

$ hostnamectl

Specifically, to only check the Kernel version, use the grep command with hostnamectl:

$ hostnamectl | grep -i kernel

Check Linux Kernel version using the /proc/version file

The proc file system is a virtual file system that is created when the system is started, and it vanishes when the system shutdown. In the /proc file system, the Linux Kernel information is stored in the version file. The cat command can be used for printing the version file system content.

Type the below-given command to check the Linux Kernel version:

$ cat /proc/version

Conclusion

The article explains the various commands that we can use to check the installed Linux Kernel version. These commands work on various Linux distributions like Debian, CentOS, Fedora, Linux Mint, etc.

]]>
Managing Linux Kernel Modules https://linuxhint.com/managing-linux-kernel-modules/ Mon, 01 Feb 2021 11:43:04 +0000 https://linuxhint.com/?p=88163

Understanding the Linux kernel

The Linux kernel is the core of the Linux operating system. It contains the main components to address the hardware and allows both communication and interaction between the user and the hardware. The Linux kernel is not a monolithic system but quite flexible, and the kernel is extended by so-called kernel modules.

What is a kernel module?

In general, a kernel module is a “piece of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system” [1]. This leads to very great flexibility during operation.

Furthermore, “a kernel module can be configured as built-in or loadable. To dynamically load or remove a module, it has to be configured as a loadable module in the kernel configuration” [1]. This is done in the kernel source file /usr/src/linux/.config [2]. Built-in modules are marked with “y” and loadable modules with “m”. As an example, listing 1 demonstrates this for the SCSI module:

Listing 1: SCSI module usage declaration

CONFIG_SCSI=y   # built-in module

CONFIG_SCSI=m   # loadable module

# CONFIG_SCSI   # variable is not set

We do not recommend editing the configuration file directly, but to use either the command “make config”, “make menuconfig”, or “make xconfig” to define the usage of the corresponding module in the Linux kernel.

Module commands

The Linux system comes with a number of different commands to handle kernel modules. This includes listing the modules currently loaded into the Linux kernel, displaying module information, as well as loading and unloading kernel modules. Below we will explain these commands in more detail.

For the current Linux kernels, the following commands are provided by the kmod package [3]. All the commands are symbolic links to kmod.

The list currently loaded modules with lsmod

We start with the lsmod command. lsmod abbreviates “list modules” and displays all modules currently loaded into the Linux kernel by nicely formatting the contents of the file /proc/modules. Listing 2 shows its output that consists of three columns: module name, the size used in memory, and other kernel modules that use this specific one.

Listing 2: Using lsmod

$ lsmod

Module                  Size  Used by

ctr                 12927  2

ccm                 17534  2

snd_hrtimer         12604  1

snd_seq             57112  1

snd_seq_device      13132  1 snd_seq

...

$

Find available modules for your current kernel

There might be kernel modules available that you are not aware of yet. They are stored in the directory /lib/modules. With the help of find, combined with the uname command, you can print a list of these modules. “uname -r” just prints the version of the currently running Linux kernel. Listing 3 demonstrates this for an older 3.16.0-7 Linux
kernel, and shows modules for IPv6 and IRDA.

Listing 3: Displaying available modules (selection)

$ find /lib/modules/$(uname -r) -name '*.ko'

/lib/modules/3.16.0-7-amd64/kernel/net/ipv6/ip6_vti.ko

/lib/modules/3.16.0-7-amd64/kernel/net/ipv6/xfrm6_tunnel.ko

/lib/modules/3.16.0-7-amd64/kernel/net/ipv6/ip6_tunnel.ko

/lib/modules/3.16.0-7-amd64/kernel/net/ipv6/ip6_gre.ko

/lib/modules/3.16.0-7-amd64/kernel/net/irda/irnet/irnet.ko

/lib/modules/3.16.0-7-amd64/kernel/net/irda/irlan/irlan.ko

/lib/modules/3.16.0-7-amd64/kernel/net/irda/irda.ko

/lib/modules/3.16.0-7-amd64/kernel/net/irda/ircomm/ircomm.ko

/lib/modules/3.16.0-7-amd64/kernel/net/irda/ircomm/ircomm-tty.ko

...

$

Display module information using modinfo

The command modinfo tells you more about the requested kernel module (“module information”). As a parameter, modinfo requires either the full module path or simply the module name. Listing 4 demonstrates this for the IrDA kernel module dealing with the Infrared Direct Access protocol stack.

Listing 4: Display module information

$ /sbin/modinfo irda

filename:       /lib/modules/3.16.0-7-amd64/kernel/net/irda/irda.ko

alias:          net-pf-23

license:        GPL

description:    The Linux IrDA Protocol Stack

author:         Dag Brattli <dagb@cs.uit.no> & Jean Tourrilhes <jt@hpl.hp.com>

depends:        crc-ccitt

vermagic:       3.16.0-7-amd64 SMP mod_unload modversions

$

The output contains different information fields such as the full path for the kernel module, its alias name, software license, module description, authors, as well as kernel internals. The field “depends” shows which other kernel modules it depends on.

The information fields differ from module to module. In order to limit the output to a specific information field, modinfo accepts the parameter “-F” (short for “–field”) followed by the field name. In Listing 5, the output is limited to the license information made available using the license field.

Listing 5: Display a specific field only.

$ /sbin/modinfo -F license irda

GPL

$

In newer Linux kernels, a useful security feature is available. This covers cryptographically signed kernel modules. As explained on the Linux kernel project website [4], “this allows increased kernel security by disallowing the loading of unsigned modules or modules
signed with an invalid key. Module signing increases security by making it harder to load a malicious module into the kernel. The module signature checking is done by the kernel so that it is not necessary to have “trusted userspace bits.” The figure below shows this for the
parport_pc module.

Show module configuration using modprobe

Every kernel module comes with a specific configuration. The command modprobe followed by the option “-c” (short for “–showconfig”) lists the module configuration. In combination with grep, this output is limited to a specific symbol. Listing 6 demonstrates this for IPv6 options.

Listing 6: Show module configuration

$ /sbin/modprobe -c | grep ipv6

alias net_pf_10_proto_0_type_6 dccp_ipv6

alias net_pf_10_proto_33_type_6 dccp_ipv6

alias nf_conntrack_10 nf_conntrack_ipv6

alias nf_nat_10 nf_nat_ipv6

alias nft_afinfo_10 nf_tables_ipv6

alias nft_chain_10_nat nft_chain_nat_ipv6

alias nft_chain_10_route nft_chain_route_ipv6

alias nft_expr_10_reject nft_reject_ipv6

alias symbol:nf_defrag_ipv6_enable nf_defrag_ipv6

alias symbol:nf_nat_icmpv6_reply_translation nf_nat_ipv6

alias symbol:nft_af_ipv6 nf_tables_ipv6

alias symbol:nft_reject_ipv6_eval nft_reject_ipv6

$

Show module dependencies

The Linux kernel is designed to be modular, and functionality is distributed over a number of modules. This leads to several module dependencies that can be displayed using modprobe again. Listing 7 uses the option “–show-depends” in order to list the dependencies for the i915 module.

Listing 7: Show module dependencies

$ /sbin/modprobe --show-depends i915

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/i2c/i2c-core.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/i2c/algos/i2c-algo-bit.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/thermal/thermal_sys.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/gpu/drm/drm.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/gpu/drm/drm_kms_helper.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/acpi/video.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/acpi/button.ko

insmod /lib/modules/3.16.0-7-amd64/kernel/drivers/gpu/drm/i915/i915.ko

$

In order to display the dependencies as a tree similar to the “tree” or “lsblk” command, the modtree project [5] can help (see figure below for the i915 module tree). Although it is freely available on GitHub, it requires some adaptations to comply with the rules for free software and to become part of a Linux distribution as a package.

Loading modules

Loading a module to a running kernel can be done by two commands — insmod (“insert module”) and modprobe. Be aware that there is a slight but important difference between these two: insmod does not resolve module dependencies, but modprobe is cleverer and does that.

Listing 8 shows how to insert the IrDA kernel module. Please note that insmode works with the full module path, whereas modprobe is happy with the name of the module and looks it up itself in the module tree for the current Linux kernel.

Listing 8: Inserting a kernel module

# insmod /lib/modules/3.16.0-7-amd64/kernel/net/irda/irda.ko

...

# modprobe irda

Unloading modules

The last step deals with unloading modules from a running kernel. Again, there are two commands available for this task — modprobe and rmmod (“remove module”). Both commands expect the module name as a parameter. Listing 9 shows this for removing the IrDA module from the running Linux kernel.

Listing 9: Removing a kernel module

# rmmod irda

...

# modprobe -r irda

...

Conclusion

Handling Linux kernel modules is not big magic. Just a few commands to learn, and you are the master of the kitchen.

Thank you

The author would like to thank Axel Beckert (ETH Zürich) and Saif du Plessis (Hothead Studio Cape Town) for their help while preparing the article.

Links and References

]]>
Linux Kernel Makefile Explained https://linuxhint.com/linux-kernel-makefile-explained/ Mon, 04 Jan 2021 02:22:28 +0000 https://linuxhint.com/?p=84092 In software development, the process of creating and managing large code repositories can become very complex easily.

To manage and reduce this complexity, software developers organize code in small files that link to specific modules. Developers can compile each of these files separately and then link them together to create a final software executable.

An example of this is C projects made up of source code files in .c extensions and software interfaces in .h extensions. Each source file gets compiled together with the header files to create. o objects linked together using libraries, thereby creating executable files.

To perform this process, software developers use tools, such as Make, to automate the build process and required file dependencies. Make uses Makefiles to manage the behavior of the compilation process.

The GNU Make tools provide a set of rules and conventions used to create Makefiles and reduce the complexity in improving efficiency.

In this tutorial, we will discuss the Linux Kernel Makefiles, specifically Kconfig and Kbuild.

Before we begin, it is good to note that this article does not pretend to teach everything about the Kernel Build system. However, we provide a high-level overview of building a vmlinux image and modules.

If you’d like information beyond the scope of this tutorial, we recommend the following resource for better information:

https://linkfy.to/goMakefilesDocs

https://linkfy.to/gnuMake

Kernel Makefiles: An Overview

The Kernel Build System, also called the config system, is an essential tool—for those who need it—that has been around for a while. However, not everyone will use this system; even drivers and other low-level software developers rarely use it. Since you’re reading this, it means you want to know more about the Kernel Build System.

Thus, we’ll discuss how the Kernel gets compiled and discuss the Kbuild and Kconfig system so you can understand them better.

The Kernel Makefile has five core components:

  1. Makefile: This is the top make file located in the source root.
  2. arch/$(ARCH) Makefile: This is the arch Makefile; it acts as a supplement to the top Makefile.
  3. .config: This is the Kernel configuration file.
  4. Scripts/Makefile.*: This defines set rules for all kbuild Makefiles.
  5. Kbuild Makefiles: There are about 500 kbuild Makefiles, and they are not very easy to read. Consider a file such as:

https://elixir.bootlin.com/linux/latest/source/scripts/Kbuild.include

Kconfig

The Kconfig file contains modules that assist when using the make *config. It helps the Kernel make selective configurations, creating modularity and customizability for the Kernel build process.

There are various config targets specified by the Kconfig system. You can use the make help to view the available targets. These targets are processed by various programs provided by the Kernel during the build process.

Some of Kconfig targets include:

  • Config: This is used to update the kernel config file using the line program.
  • Menuconfig: This is a Kconfig feature or mechanism that offers menu-based access to Kernel options. To launch menuconfig and other Kconfig features, you should be inside the platform project directory. You can use the following to launch the Kconfig menuconfig feature. However, you can also launch menuconfig with other GUI Linux Kernel config features such as xconfig and gconfig.
  • make linux-windriver.menuconfig – Executes menuconfig in a separate terminal session.
  • gconfig and xconfig: Gconfig activates GUI-based Linux Kernel features. Gconfig employs the GTK or (X based) based UI. On the other hand, Xconfig utilizes Qt-based UI. Use the following commands to launch gconfig and xconfig, respectively:
make linux-windriver.gconfig

make linux-windriver.xconfig

NOTE: To use gconfig and xconfig, you should have the QT development tools installed on the host system.

  • Nconfig: Nconfig feature runs the current configuration (Buildtools) and applies for the Ncurses menu-driven program. This allows you to select the packages to build, such as CPU, drivers, and filesystem when building the Kernel. Use the command: make nconfig.
  • Oldconfig: The oldconfig feature allows you to apply newer .config files to older kernel config files. For instance, an old .config file and a newer .config file (newer kernel version) will have differences, meaning you need to update the current config before the kernel build. You can use make oldconfig to update the old config interactively by applying options missing in the old config file.
  • Defconfig: This feature allows the kernel build system to add a new config supplied by defconfig to the .config file. More precisely, the Kbuild system checks all the Kconfig files. If defconfig specifies an option in the file, the Kbuild system uses the specified value to add the option to the .config. If the defconfig does not mention the option, Kbuild uses default values in the .config.

Consider the following:

Defconfig code snapshot from the following resource:

https://elixir.bootlin.com/linux/v5.9/source/scripts/kconfig/Makefile#L98

1.  defconfig: $(obj)/conf
2.  ifneq ($(wildcard $(srctree)/arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG)),)
3.      @$(kecho) "*** Default configuration is based on '$(KBUILD_DEFCONFIG)'"
4.      $(Q)$< $(silent) --defconfig=arch/$(SRCARCH)/configs/$(KBUILD_DEFCONFIG) $(Kconfig)
5else
6.      @$(kecho) "*** Default configuration is based on target '$(KBUILD_DEFCONFIG)'"
7.      $(Q)$(MAKE) -f $(srctree)/Makefile $(KBUILD_DEFCONFIG)
8.  endif
9.   
10. %_defconfig: $(obj)/conf
11.     $(Q)$< $(silent) --defconfig=arch/$(SRCARCH)/configs/$@ $(Kconfig)
12.  
13. configfiles=$(wildcard $(srctree)/kernel/configs/$@ $(srctree)/arch/$(SRCARCH)/configs/$@)
14.  
15. %.config: $(obj)/conf
16.     $(if $(call configfiles),, $(error No configuration exists for this target on this architecture))
17.     $(Q)$(CONFIG_SHELL) $(srctree)/scripts/kconfig/merge_config.sh -m .config $(configfiles)
18.     $(Q)$(MAKE) -f $(srctree)/Makefile olddefconfig

Oldconfig code snapshot from the following resource:

https://elixir.bootlin.com/linux/v5.9/source/scripts/kconfig/conf.c#L694

1.      case olddefconfig:
2.      default:
3.          break;
4.      }
5.   
6.      if (input_mode == savedefconfig) {
7.          if (conf_write_defconfig(defconfig_file)) {
8.              fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
9.                  defconfig_file);
10.             return 1;
11.         }
12.     } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
13.         if (!no_conf_write && conf_write(NULL)) {
14.             fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
15.             exit(1);
16.         }
17.  
18.         /*
19.          * Create auto.conf if it does not exist.
20.          * This prevents GNU Make 4.1 or older from emitting
21.          * "include/config/auto.conf: No such file or directory"
22.          * in the top-level Makefile
23.          *
24.          * syncconfig always creates or updates auto.conf because it is
25.          * used during the build.
26.          */
27.         if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
28.             fprintf(stderr,
29.                 "\n*** Error during sync of the configuration.\n\n");
30.             return 1;
31.         }
32.     }
33.     return 0;
34. }

  • Savedefconfig: This rule saves the current .config in the form of ./defconfig, which is regarded as a minimal config file. Use the command: make savedefconfig
  • Listnewconfig: This is used to list new options.
  • Kvmconfig: This enables options for KVM support. Use the command: make kvm_guest.config
  • Allyesconfig: This builds a new kernel config file with all options set to yes. It’s the opposite of allnoconfig.
  • Allmodconfig: This builds a new kernel config with which modules are enabled by default.
  • Randconfig: This builds a new kernel config file with random answers to all options.
  • Tinyconfig: This makes the tiniest Kernel possible.

There are a lot of targets in the Kconfig system. Some common ones include config and menuconfig.

As mentioned, the targets are processed by various programs in the host systems, either providing a GUI or command line. You can find Kconfig tools in /scripts/Kconfig in the kernel source.

https://elixir.bootlin.com/linux/latest/source/scripts/kconfig

https://elixir.bootlin.com/linux/latest/source/scripts/kconfig/Makefile

The first process is usually to read the Kconfig file in the root directory, which is used to build an initial config database. As the process continues, the database is updated when reading files in the following order:

.config

/lib/modules/$(shell,uname-r)/.config

/etc/kernel-config

/boot/config-$(shell,uname-r)

ARCH_DEFCONFIG

arch/$(ARCH)/defconfig

.config file is then dropped to syncconfig, which accepts the .config file as input. It processes the file and outputs files, which are then classified into various categories such as:

  • autoconf.h: This is used for C language source files.
  • auto.conf and tristate.conf: These are used for Makefile text processing.
  • /includes/config: These are empty header files used in dependency tracking.

Kbuild Files

Almost all Kernel files are Kbuild Makefiles that use the Kbuild infrastructure, which is a recursive make feature. Recursive Make is a way of using the Make tool as a command in a Makefile. Recursion is very useful when compiling a large project.

Kbuild works by referring to all the files we mentioned in the above section.

The Kbuild system builds its components using the top Makefile that includes the arch Makefiles with the name arch/$(ARCH)/Makefile in the config files. It recursively descends into subdirectories invoking Make on the components using the routines in scripts/Makefile.*. Kbuild then builds upon the adjacent object and links them into objects, creating vmlinux.

To learn more about the syntax used in Kbuild Makefiles, refer to the documentation.

Consider the following script.

https://github.com/torvalds/linux/blob/master/scripts/link-vmlinux.sh

The o object files used to create the vmlinux are compiled first in their respective built-in .a files as the var KBUILD_VMLINUX_INIT,MAIN,LIBS. These are composed in vmlinux.

https://github.com/torvalds/linux/blob/master/scripts/Makefile.build

Conclusion

In this guide, we took a look at Kbuild and Kconfig systems in the Kernel build system and how it works. As we mentioned at the beginning of the tutorial, the topics discussed are broad and cannot be covered in a single tutorial.

]]>
How To Change the Swappiness of Your Linux System https://linuxhint.com/customize-linux-swappiness/ Mon, 28 Dec 2020 08:15:05 +0000 https://linuxhint.com/?p=83402 Memory is a critical part of any system. The Linux kernel knows this and tries to ensure there’s enough memory at the system’s disposal. One way the Kernel manages memory is by using a swap system to create additional memory space.

In this tutorial, we will talk about what a swap is, how it works, and finally, how to customize the swappiness of your Linux system.

What Is Swap?

A Swap system is defined as space allocated on the disk by the operating system and used when RAM is insufficient. In Linux, the Swap system can be a partitioned disk space dedicated to acting as a swap or a swap file. The swap system can also be a combination of both swap partition and swap files.

When a system is out of physical memory, the Kernel swaps inactive pages from the main memory into the swap system. That frees up some space in the main memory for use by other processes.

Although you can get away without a swap system, if you have a system with less than 4 GB of RAM or a system with critical performance requirements, a swap can come in very handy—and is recommended. Otherwise, if your system requires more memory than the system can provide, the system will crash, which can have grave repercussions.

NOTE: Do not assume that a swap system is a replacement or an alternative to additional memory. A Swap system is nothing more than a partition or a file stored in the disk; it cannot fully function as physical memory. If your system is always running out of sufficient memory, consider adding more physical memory because increasing your swap system size won’t help much.

What is Swappiness?

Swappiness is a kernel parameter that allows system administrators to adjust how often the system utilizes the swap system. Swappiness defines the relationship between swapping out inactive pages from the RAM to the swap system.

To avoid confusion, Here is the official swappiness definition from Linux manual pages.

“This control is used to define how aggressive (sic) the kernel will swap memory pages. Higher values will increase aggressiveness; lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The swappiness feature first appeared in Linux kernel version 2.6; it ranges from value 0 to 100. The higher the swappiness value, the more the inactive pages are swapped from main memory to swap space. Lower values will lead to inactive pages remaining in the main memory.

How to Display the Swappiness Value Of Your System

By default, major Linux distributions will have the value of swappiness set to 60. You can view the swappiness value of your system using the sysctl command.

sudo sysctl vm.swappiness

If you do not have a sysctl command in your system, use the command below to install it on Debian based systems.

apt-get install procps

https://man7.org/linux/man-pages/man8/sysctl.8.html

How to Change the Swappiness Value

The default swappiness value is usually the appropriate values for most use cases. However, you may want to adjust this value for various reasons.

There is no best or recommended swappiness value. However, you can use various tools such as free to monitor how your system memory performs and adjust the swappiness value until you find your system’s optimal value.

We can adjust the swappiness value by editing the configuration file. This method preserves the swappiness value even after a reboot.

To do this, open the file /etc/sysctl.conf with your text editor and change the value of the following entry vm.swappiness to your suitable value. (Add the entry if it does not exist).

To change your system’s swappiness value during runtime, use the sysctl command as shown: Change the swappiness value to fit your optimal value.

sudo sysctl vm.swappiness=10

NOTE: Accessing swap space is considered much slower compared to accessing physical memory. Therefore, setting your swappiness value to 100 will not guarantee an increase in speed.

Conclusion

In this guide, we discussed what swap is and how it works. Using this guide, you can optimize your system space requirements to fit your usage needs.

]]>
Check and Update Ubuntu Kernel Version on Ubuntu 20.04 https://linuxhint.com/update_ubuntu_kernel_20_04/ Sat, 16 May 2020 11:54:15 +0000 https://linuxhint.com/?p=60076 The Linux kernel is the core of the Linux OS. It is responsible for interfacing the computer hardware and software applications in this operating system. Like the continual updates to Linux OS and applications, new versions of Linux kernel will also be released. It is recommended to update the kernel version to have the latest functionalities, improved stability, and security fixes.

In this article, we will explain three ways by which you might update your Linux kernel to the latest available version. We have explained the procedure on Ubuntu 20.04 LTS, which comes with kernel version 5.4. More or less the same procedure can be followed using previous versions of Ubuntu OS.

Check Current Kernel Version

Before updating the kernel version, it is better to check the current version. It will help you to decide which new version to update the kernel to. Open the command line by using the Ctrl+Alt+T keyboard shortcut.

Now, check the current kernel version directly from the command line, as follows:

$ uname -r

You can also check the kernel version with the following command:

$ cat /proc/version

Using either of the above commands will show you the kernel version of your OS. From the above output, you can see that the kernel version used in this article is 5.4.0-28-generic.

Update Ubuntu Kernel Version via Command Line

In the following methods, I will show you how to update the kernel version using the Command line.

Method # 1: Ubuntu Kernel Team Site

In the following method, we will update the kernel version by first downloading the latest version from the Ubuntu kernel team site and then installing it.

For this method, follow these steps to update the kernel version:

  1. Open the Ubuntu kernel team site to download the latest Linux kernel for Ubuntu. Select the version you wish to download and then download the .deb files for the generic version.
  • linux-headers-<version-number >-all.deb
  • linux-mage-unsigned-<version-number>-amd64.deb
  • linux-modules-<version-number>-amd64.deb

We want to update our kernel to latest stable kernel version 5.6.10. So, we have downloaded the following .deb files.

linux-headers-5.6.10-050610_5.6.10-050610.202005052301_all.deb

linux-image-unsigned-5.6.10-050610-generic_5.6.10-050610.202005052301_amd64.deb

linux-modules-5.6.10-050610-generic_5.6.10-050610.202005052301_amd64.deb

Alternatively, you can use the wget command to download the following mainline packages for kernel version 5.6.10:

$ wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v5.6.10/linux-headers-
5.6.10-050610_5.6.10-050610.202005052301_all.deb
$ wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v5.6.10/linux-image-unsigned-
5.6.10-050610-generic_5.6.10-050610.202005052301_amd64.deb
$ wget https://kernel.ubuntu.com/~kernel-ppa/mainline/v5.6.10/linux-modules-5.6.10-
050610-generic_5.6.10-050610.202005052301_amd64.deb
  1. Once all packages are downloaded, you can verify them using the “ls –l” command:
  2. The next step is to install all the downloaded packages. If you have downloaded the files on a directory other than the Home directory, then first navigate to it using the cd command:
    $ cd /path/to/directory

    Now, install all the downloaded .deb files. You can do so via the following command:

    $ sudo dpkg –i *.deb

    Wait for a while until installation of all packages is complete.

    1. Reboot your system. Once the reboot is completed, log in to your system and check the kernel version with the uname command, as follows:
    $ uname –r

    The output will show the updated kernel version that is 5.6.10.

Method # 2: Using the Bash Script

In the next method, we will use the bash script in order to install a new kernel version. Follow these simple steps to do so:

1. Download the bash script using the following command:

$ wget https://raw.githubusercontent.com/pimlie/ubuntu-mainline-kernel.sh/
master/ubuntu-mainline-kernel.sh

2. Copy the script to the “/usr/local/bin/” directory. You can do so with the following command:

$ sudo install ubuntu-mainline-kernel.sh /usr/local/bin/

3. Now, you can update the kernel version as follows:

$ Ubuntu-mainline-kernel.sh –i

It will search for and provide you with the latest kernel version number, e.g. v5.6.10, in the following output. If you want to install this version, hit B, upon which the installation will begin.

Once installed, reboot the system. Then, check the kernel version with the uname command as follows:

$ uname –r

The output will show the updated kernel version

Method # 3: Update Ubuntu Kernel Version via GUI

In the following method, we will discuss the GUI method to update the Linux kernel. We will use the graphical Mainline tool for installing the latest version of the kernel. Here are the simple steps to do so:

1. The Mainline tool is not available in official Ubuntu repositories, so we will need to manually add its PPA to the sources.list file in our system. Use the apt-add-repository command for this purpose:

$ sudo apt-add-repository -y ppa:cappelikan/ppa

2. Once you have added the mainline repository, the following step is to update the system’s repository index:

$ sudo apt update

3. Next, install the Mainline tool with this command:

$ sudo apt install mainline

The system might ask for confirmation by providing you with a Y/N option. Hit Y to continue, after which the Mainline tool will be installed on your system.

4. Launch the mainline tool from the Ubuntu search bar as follows:

5. When the Mainline tool is launched, you will see the following installer window with a list of new and available kernel versions. You may install a new version by selecting the version from the list and clicking the Install button on the bar to the right.

After this step, the following Authentication dialog will appear. Enter the super user password and click the Authenticate button.

The installation of the selected kernel version will begin, and you will be able to see the installation process in a new window.

Now, reboot your system and verify the new kernel version:

$ uname –r

In this article, we have explained three different methods to update the kernel version on Ubuntu 20.04 LTS. After updating the kernel, sometimes you may face issues in booting the system. In this case, switch back to the old kernel by hitting the Shift key at boot, and select the older version from the list.

]]>
How to view and browse the linux kernel source? https://linuxhint.com/browse_linux_kernel_source/ Fri, 24 May 2019 08:06:43 +0000 https://linuxhint.com/?p=40796 The Linux Kernel is the operating system (OS) core which allows it to support and manage the hardware, protocols and more. In this tutorial the kernel source structure will be briefly explained. The kernel’s root folder located at /usr/src/, usually /usr/src/linux* contains it’s source like the one you can browse at https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/?h=v5.1.3

The source directory contains the following subdirectories called kernel tree, learning the structure of the kernel tree and the function of each path is the first step to start browsing the kernel.

Kernel tree root directory:

Documentation/: kernel source documentation.

LICENSES/: It contains the licenses to be applied to the kernel source and individual source files which may have different licenses. Inside this directory you will find 3 subdirectories: preferred (GNU GPL), exceptions listed in the COPYING file and other.

arch/: source codes for specific architectures (e.g powerpc, x86,etc). Inside this directory you’ll find subdirectories belonging to each supported architecture lie i386, sparc, arm, etc.

block/: block I/O layer, contains code for the management of block devices (such as hard disks, dvd, floppy disks, etc.) and their requests.

Certs/:
certificates and sign files to enable module signature to make the kernel load signed modules, this can be useful to prevent malicious code from running with kernel modules like rootkits.

Crypto/: Crypto API. Contains cryptographic ciphers which handles cryptographic and compression tasks.

drivers/: Hardware device drivers. Contains code of device drivers to support hardware, inside this directory you’ll find subdirectories for each hardware such as video, bluetooth,etc (any hardware supported).

fs/: code for the Virtual File System and additional filesystems. This directory contains the code to support, read and write filesystems.

include/: kernel headers. This directory contains C headers for kernel files such as functions to compile code.

init/: kernel boot. Contains source code related to the initialization of the kernel. The source code is stored in a file called  mainc.c within the directory /init. The code initializes the kernel and some initial processes.

ipc/: Inter-Process Communication such as signals and pipes.

kernel/: Core subsystems, such as the scheduler signal handling code, etc.

lib/: library routines common string operations, hardware dependent operations, debugging routines and command line parsing code.

mm/:  Memory management and virtual memory. The kernel manages both the hardware and virtual memory (swap). This directory stores code for the memory management.

net/: Network stack. Contains code related to communication protocols such as IP, TCP,UP, etc.

samples/: sample code and configuration files.

scripts/: scripts to build the kernel.

security/: Linux Security Module (LSM)  is a framework to enable security policies to access modules control.

sound/: The sound subsystem, here you’ll find sound drivers and code related to sound such as ALSA,


tools/:
tools for compressed kernel development such as ACPI, cgroup, USB testing tools, vhost test module, GPIO, IIO and spi tools, Inter energy policy tool among more.

usr/: initramfs which roots the filesystem and init in the kernel memory cache.

virt/: Virtualization, this directory contains the KVM (Kernel Virtual Machine) module for hypervisor.

These are the main subdirectories of the kernel tree and their function, hope its helpful for you.

]]>
Introduction to Linux Kernel https://linuxhint.com/introduction_linux_kernel/ Sat, 04 May 2019 08:01:56 +0000 https://linuxhint.com/?p=39548 The Kernel of an Operating System is the program which makes the hardware and the software work together. With the help of device drivers, the Linux kernel acts as a translator allowing communication between the software and the user interface with the hardware.

This article will focus on Linux kernel, which is a Monolithic Kernel based on Unix.

Other kernels may be MicroKernels delegating some functions to other programs such as external drivers.

A monolithic kernel is a kernel monopolizing all hardware and driver operations as parts of itself, in contrast with microkernels which deal with a few basic tasks while leaving the rest as external tasks, due the lack of popular operating systems using microkernels we’ll ignore their existence for now.We can think about the kernel as the first level or lower level of communication between our OS (operating system) and our physical device.

Being a “hardware-software translator” the kernel’s main task is allocating hardware resources to software processes, mainly memory and processor. The kernel also can limit memory allocation to failing devices to prevent crashes.

Let’s say we installed a new Linux system and some of our devices fail to work properly, like an undetected wifi card. After doing some checks we realize the operating system we just installed does not support our wifi card. Before such scenario we may edit our kernel’s configuration adding support for the device we need. On a previous article on LinuxHint we took this scenario to teach how to compile the Slackware Linux kernel to add hardware support. If we want we can also remove hardware support we don’t need from our kernel to make it lighter.

While modifying a kernel we can edit the hardware support as native or we can load support as a loadable module called upon need, and of course you can write your own modules if needed.
Kernel modules have advantages over regular applications with the same utility (such as external drivers) because modules only work upon need. In contrast with applications kernel modules release hardware resources terminating all it’s activity while regular applications may retain them. On the other hand it’s disadvantage is regular applications are less likely to make the system crash.

Since the Linux kernel administrates every interaction between our hardware and software, it can also regulate functions of our system. By editing the Linux kernel we can remove or add support for internet protocols as IPv6, firewall tables and every way of interaction between our hardware and software.

It is critical to keep the core of our system, the kernel updated in order to avoid security flaws such as 2018-14634 which allows privileges escalation within a system. On LinuxHint we already explained how to keep your kernel upgraded, check this article to keep your Debian’s kernel upgraded, this for your Ubuntu kernel and this one to upgrade Linux Mint kernel.

See Also this additional article written on LinuxHint: Linux Kernel Tutorial For Beginners.

I hope you found this introduction to the Linux kernel useful to understand it’s functions and potential in an easy way. Keep following us on LinuxHint for more tips and updates on Linux.

]]>
Upgrade Kernel on Linux Mint https://linuxhint.com/upgrade_kernel_linux_mint/ Wed, 06 Feb 2019 10:38:24 +0000 https://linuxhint.com/?p=36261 For using any computer, it’s a must to have a suitable operating system that will operate the machine for you. Now, if we go deeper into the hierarchy, for any operating system, the most important part of the operating system is the “kernel”.The question arises naturally – what the heck is the kernel? The kernel is the part of the OS that’s responsible for performing the hardware-level interaction. It sits in-between the hardware and the OS + other software. In fact, kernel is the piece of software that holds the ultimate power over the hardware of the system.For understanding the concept of kernel, let’s consider ourselves – the humans. Assume that the OS + all the other software is your brain and your mind while your body is the hardware. You think something using your brain, your body does the task. But the signal of doing that task has to be carried to the hardware in a way, right? In our case, we got the nervous system. The kernel in an OS holds the same significance.

Linux kernel

Each of the operating systems use their own kernels. For example, Windows uses its proprietary kernel whereas most other operating systems, especially the open-source ones use the well-known, tested and proven Linux kernel.

The Linux kernel is a living marvel of the software world. It’s one of the FINEST pieces of software that’s both INCREDIBLY complex and so, INCREDIBLY powerful with an INCREDIBLE community backing it up. It’s the core of all the Linux distros. Started by Linus Torvalds, Linux kernel v1.0.0 came out back in 1994. Now, it’s 2019 and the latest version is 4.20.5 (stable).

Throughout its course, the Linux kernel is getting advanced day by day. The kernel update is released every couple months with improved performance, stability, bug fixes, patches and additional features.

However, not all the Linux distros instantly incorporate the latest update right away. After the release, it usually takes time for getting it into your system officially.

That’s why I love to manage the kernel by myself.

Before we proceed any further, it’s worth to mention that unless you’re sure that you need that next-gen kernel, it’s not really worth making the leap. The kernel that comes with the distro is well-maintained by the developers as well.

Manage kernel on Linux Mint

If you’re a user of Linux Mint, then it’s way easier to perform “proven” actions on the kernel without worrying about something going wrong. Of course, things can go completely out of whack if you become too much adventurous. There are 2 available ways for managing a newer kernel version – using a GUI tool named “Ukuu” and installing the kernel manually.

Changing kernel using Ukuu

Ukuu is available directly for all the Debian/Ubuntu-based distros via the PPA.

Fire up a terminal –

Add the Ukuu repository –

sudo add-apt-repository ppa:teejee2008/ppa

Now, update the APT repository cache –

sudo apt update

Finally, install Ukuu –

sudo apt install ukuu

Launch Ukuu –

As you can see, the ones with the Ubuntu mark are the installed ones. The others are stable kernel releases throughout the journey.

It’s clear that my Linux Mint system is using Linux Kernel 4.15. It’s a quite old model.

Let’s install Kernel v4.18.20. Select the kernel and hit “Install”.

The installation process will start immediately.

Now, reboot your system for completing the installation.

Voila! The new kernel is all set!

Changing kernel manually

Note that this is a more risky process and might end up with some unwanted issues with your system.

  • Downloading the kernel

At first, download the latest Linux kernel package.

  • Verify the package

The process of downloading isn’t complete YET, unless you’ve ensured that the file is the verified package. Don’t you DARE skipping this step! Because it’s the kernel, even the slightest bit change can cause serious trouble!

Verify the downloaded package –

unxz linux-4.20.5.tar.xz
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.20.5.tar.sign

Upon trying to verify the package,

gpg --verify linux-4.20.5.tar.sign

See? It’s not possible. Grab the public key from the PGP keyserver –

gpg --recv-keys <key_string>

Here, the key string is going to be the key that the previous command showed.

Once the download is complete,

You can successfully verify the signature of the downloaded file.

The warning should be fine as long as you don’t get “BAD signature”. Now, it’s safe to proceed with the rest of the installation process.

tar -xvf linux-4.20.5.tar

  • Configuring kernel features and modules

Now, before you install the new kernel, you must configure its features. You also must specify all the necessary kernel modules that your system needs.

This can be overwhelming, so we’ll be using the existing config file. Run the following command –

cd linux-4.20.5/
cp -v /boot/config-$(uname -r) .config

  • Install the necessary compiler tools

For building the kernel package, you need the necessary dependencies available. Otherwise, the building step is going to be a mess.

sudo apt-get install build-essential libncurses-dev bison flex libssl-dev libelf-dev

  • Configuring the kernel for build

There are 3 available options –

  • make menuconfig
    Text-based radio lists, color menus and dialogs. Useful for remote servers.
  • make xconfig
    X Windows (Qt) based config tool. It works the best under KDE desktop.
  • make gconfig
    X Windows (GTK) based config tool. It works the best under GNOME destkops.
  • make oldconfig
    A series of y/N questions that you’re free to skip by pressing Enter.

For my case, I’ll be going with “make oldconfig”.

Just hold Enter until the nightmare ends!

  • Building the kernel

This is going to take a while, folks. Let’s concentrate on something else! It’s also going to take a lot of disk space and by that, I mean A LOT!

make -j4
  • Installing the kernel

Start installing the kernel with the modules.

sudo make modules_install

Then, it’s time to install the kernel itself.

sudo make install
  • Update GRUB config

Run the following commands –

sudo update-initramfs -c -k 4.20.5
sudo update-grub

For taking the result into effect, reboot your system.

]]>
Resources to Learn About the Linux Kernel https://linuxhint.com/resources_learn_linux_kernel/ Sun, 04 Nov 2018 13:59:19 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=31878

The Linux kernel is complex, large and feature-rich. Learning about it is not an easy task, not just because of its complexity and enormity but because it is constantly under development. This makes Linux a bit of a moving target.

Moreover, there’s one key difference between Linux and other operating system. Linux gets the definition of operating system wrong. Basic jobs that a kernel is actually responsible for involve stuff like scheduling, memory management, etc. Drivers and libraries actually just interface with the kernel thanks to the interface (ABI) that the kernel exposes. Which has led to the popular phrase, “Its not Linux, its GNU/Linux”. GNU being the rest of the stack, libraries etc and Linux being the kernel.

But misinformed folks will often wander into the internals of the Linux Kernel whereas their intention was something completely different starting out. With out of the way, here are some of the resources, that will help you learn about operating systems in the abstract along with the specificity of the Linux Kernels.

1. Operating System concepts

This books digs deep into the idea of what an Operating system ought to be, in the abstract. At the same time, it has a solid foothold into the practicalities of real world. The companion website offers a Linux VM that can be used to actually work through some of the challenges that the book offers. So even the abstract ideas will mostly be implemented in Linux by the reader.

Furthermore, it has elaborate sections where the case studies for popular operating systems are done and you get to see the distinction between Linux (which is just the kernel) and full-fledged operating systems like Windows 7.

Level: Intermediate

2. How Linux Works

Released by No Starch Press, this is one of the finest book to show you… well, how Linux works. Everything from Kernel’s management of filesystem and memory to the detailed description of I/O, networking and storage are discussed within this book.

The boundary where Linux ends and your Ubuntu, Arch, CentOS, etc begin will be made clear as well. If you run Linux you have all the prerequisites to go through this book.

Level: Beginner

3. The Linux Kernel Documentation

So far we have stuck to books. However, books get outdated quickly and can only help us so much. The Linux kernel documentation can guide you through your journey into the heart of the matter.

There is something for everyone. The documentation is neatly divided into sections for Users, Application developers and Kernel developers. You can get a practical gist of how bugs discovered and reported, and feature requests made. Of course, everything is centered around the Linux kernel, so chances are, you won’t be allowed to stray away from your aim, in this website.

This is an indispensable resource when it comes to the staying up-to-date as well. Since the documentation is where you will see the first sign of patches and new features.

Level: N/A

4. LFD 420 and LFD 440

Although quite expensive, these are some of the highest rated courses from the Linux foundation. My recommendation, here, is from anecdote rather than personal experience, but people have benefitted enormously from these courses.

The LFD 420 course is for intermediate level whereas the next course is for much more advanced users. The courses are provided by the Linux Foundation and delves deep into the topics of kernel architecture, workings, security and much more.

Honourable Mention: Design and Implementation of FreeBSD

There’s a lot that Linux actually gets wrong, and learning about the Kernel as if it is an entity of the natural world waiting to be studied is a wrong approach. It is not for nothing, that Linux users are accused of fanaticism. To avoid getting trapped into the echo chamber of Linux, this book would help you immensely.

The book doesn’t only talks about the operating system, FreeBSD but also about its kernel. I chose FreeBSD to contrast against Linux for a multitude of reasons. First, FreeBSD is used quite a lot in the real-world. It has enabled companies like iXsystems, Netflix, Delphix and WhatsApp to run their servers. About 25% of the entire US’ internet traffic flows through FreeBSD, thanks to the popularity of Netflix alone. So it can be used for some serious heavy-lifting.

Secondly, FreeBSD is proper UNIX. With clear distinction between userland and kernel and various other intelligent design choices, that seasoned Linux kernel developers can appreciate and adopt.

Level: Intermediate to Advanced

Conclusion

When learning about anything as enormous as the Linux Kernel, you first need to define your goals. This maybe involve understanding how it works, how to debug it, how to write kernel modules, etc.

Once the goal is well-defined, you will find it hard to stray way into unnecessary topics and heated debates that Linux community is infamous for. Its okay if the goal changes as you understand more about the system. It is far better to nativagate by yourself than to get adrift by opinions and authority.

]]>
Debian modprobe Tutorial https://linuxhint.com/debian_modprobe_tutorial/ Sun, 02 Sep 2018 17:16:28 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=30112 Modules are a piece of code that extends the functionality of the Linux kernel. A Module on Linux is also known as Linux Kernel Module. The best thing about modules in Linux is that they can be added or removed on the fly. That means, you can add or remove modules in Linux without the need to reboot your computer.

There are modules for your graphics hardware (GPU), different filesystems (such as ext4, zfs, btrfs), network hardwares, display, keyboard and different input devices and many more.

In this article, I am going to show you how to use modprobe to manage kernel modules on Debian GNU/Linux distribution. I am going to use Debian 9 Stretch for the demonstration. But it should work on any version of Debian GNU/Linux. So let’s get started.

The Default Modules Directory:

All the modules in Debian is stored is the /lib/modules/$(uname -r) directory.

The kernel modules are stored in a sub directory kernel/. The kernel modules are stored in a modular hierarchical order as you can see in the screenshot below.

$ ls /lib/modules/$(uname -r)/kernel

Listing All the Available Kernel Modules:

You can list all the kernel modules in a nicely formatted tree structure with the following command:

$ tree /lib/modules/$(uname -r)/kernel

NOTE: The tree command is not installed by default. You will have to install it manually with sudo apt install tree

As you can see, the directories and kernel modules are listed as a tree.

It’s a very long list. You can also pass it to a pager such as less as follows to make navigation through the list easier:

$ tree /lib/modules/$(uname -r)/kernel | less

Now you can press the <Up> and <Down> arrow keys to navigate through the list.

You can search for modules in the less pager. Just press / and type in your search keyword.

Now press <Enter> and your search result should be displayed as you can see in the marked section of the screenshot below.

You can also press n and p to go to the next result or previous result respectively.

To exit the pager, press q.

Listing All the Loaded Kernel Modules:

You can list all the kernel modules that are used by your Debian operating system with the following command:

$ lsmod

It’s a long list. On the Module column (1), you can see the kernel module name, on the Size column (2), you can see the kernel module size in bytes.

The Used by column (3) is interesting. This column starts with a number and optionally a comma separated list of module names.

The number determines how many modules depend on this module. If it is 0, then it means, no other modules depend on this module. For example, the pppdev module is not used by any other module. So you can safely remove or add pppdev kernel module anytime.

If it’s something other than 0, then many other modules depend on this module. For example, 2 other modules depend on snd_pcm module as you can see in the marked section of the screenshot below. So you can’t remove snd_pcm module unless you remove the modules that depend on this module. In this case, the snd_ac97_codec and snd_ens1371 modules must be removed in order to remove snd_pcm module.

Getting Information About Specific Kernel Module:

Now that you know how to list all the available kernel modules and the loaded kernel modules. In this section, I am going to show you how to get information about any kernel module.

On Debian, you can use the modinfo command to get information about any module.

For example, to get information about the snd_pcm module we saw earlier, run the following command:

$ sudo modinfo snd_pcm

As you can see, the module file path, license, a short description about the module snd_pcm, the modules snd_pcm depends on and some other information is listed.

These are essential bits of information you need to know in order to work with modules on Deban. For example, if you want to enable snd_pcm module, then you must also enable to snd and snd-timer modules before you do so. As snd_pcm depends on snd and snd-timer. Otherwise, it won’t work.

Loading New Kernel Module Using modprobe:

You can load new kernel modules using the modprobe command. For example, let’s say, you want to enable the btrfs filesystem kernel module.

First check what modules the btrfs kernel modules depends on with modinfo as follows:

$ sudo modinfo btrfs

As you can see, the btrfs kernel module depends on raid6_pg and xor kernel modules.

Let’s check whether they are enabled as follows:

$ lsmod | grep raid6
$ lsmod | grep xor

As you can see, none of the modules are loaded.

Now run the following commands to load raid6_pq and xor kernel modules:

$ sudo modprobe raid6_pq
$ sudo modprobe xor

Now let’s check whether the dependency kernel modules are loaded again:

$ lsmod | grep raid6
$ lsmod | grep xor

As you can see, the kernel modules are loaded.

Now you can load the btrfs kernel module with the following command:

$ sudo modprobe btrfs

Now let’s verify whether the btrfs kernel module is loaded with the following command:

$ lsmod | grep btrfs

As you can see, the btrfs kernel module is loaded and it depends on the raid6_pq and the xor kernel modules.

Most of the time modprobe command will resolve dependencies for you automatically. But I showed you how to do it manually because at times you may need to do it this way. It’s best to know how to solve a problem as you may have to deal with it someday.

Removing Modules with modprobe:

You can also remove modules with modprobe command. If no other modules depend on the module that you want to remove, then the process is straightforward.

For example, to remove the btrfs module I loaded earlier, run the following command:

$ sudo modprobe -r btrfs

The module should be removed.

If other modules depend on the module you want to remove, then you will have to remove these modules first before you can remove your desired module.

That’s basically all you need to know about managing kernel modules on Debian. Thanks for reading this article.

]]>
Understanding vm.swappiness https://linuxhint.com/understanding_vm_swappiness/ Tue, 07 Aug 2018 05:01:42 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=29204 The Linux kernel is a rather complex piece of software with a long list of components such as modules, interfaces and configuration files [1]. These components can be configured with specific values in order to achieve a desired behaviour or mode of operation of the component [2,3,4]. Subsequently, this setup directly influences both the behaviour and the performance of your Linux system as a whole.

The current values of the Linux kernel and its components are made accessible using a special interface — the /proc directory [5]. This is a virtual file system in which the single files are filled with values in real time. The values represent the actual state the Linux kernel is in. You can access the individual files in the /proc directory using the cat command as follows:

$ cat /proc/sys/net/core/somaxconn
128
$

One of these kernel parameters is called vm.swappiness. It “controls the relative weight given to swapping out of runtime memory, as opposed to dropping memory pages from the system page cache” [6]. Starting with Linux kernel releases 2.6 this value was introduced. It is stored in the file /proc/sys/vm/swappiness .

Using Swap

The use of swap [6] was an essential part of using smaller UNIX machines in the early 1990s. It is still useful (like having a spare tire in your vehicle) when nasty memory leaks interfere with your work. The machine will slow down but in most cases will still be usable to finish its assigned task. Free software developers have been making great strides to reduce and eliminate program errors so before changing kernel parameters consider updating to a newer version of your application and related libraries first.

If you run numerous tasks, then the inactive tasks will be swapped out to disk, making better use of memory with your active tasks. Video editing and other large memory consuming applications often have recommended amounts of memory and disk space. If you have an older machine which cannot have a memory upgrade, then making more swap available might be a good temporary solution for you (see [6] on how to learn more about that).

The swapping can happen on a separate partition or on a swap file. The partition is faster and favored by many database applications. The file approach is more flexible (see the dphys-swapfile package in Debian GNU/Linux [7]). Having more than one physical device for swapping allows the Linux kernel to choose the device that is most rapidly available (lower latency).

vm.swappiness

The default value of vm.swappiness is 60 and represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory.

The value of 60 is a compromise that works well for modern desktop systems. A smaller value is a recommended option for a server system, instead. As the Red Hat Performance Tuning manual points out [8], a smaller swappiness value is recommended for database workloads. For example, for Oracle databases, Red Hat recommends a swappiness value of 10. In contrast, for MariaDB databases, it is recommended to set swappiness to a value of 1 [9].

Changing the value directly influences the performance of the Linux system. These values are defined:

* 0: swap is disable
* 1: minimum amount of swapping without disabling it entirely
* 10: recommended value to improve performance when sufficient memory exists in a system
* 100: aggressive swapping

As shown above the cat command helps to read the value. Also, the sysctl command gives you the same result:

# sysctl vm.swappiness
vm.swappiness = 60
#

Keep in mind that the sysctl command is only available to an administrative user. To set the value temporarily set the value in the /proc file system as follows:

# echo 10 > /proc/sys/vm/swappiness

As an alternative you may use the sysctl command as follows:

# sysctl -w vm.swappiness=10

To set the value permanently, open the file /etc/sysctl.conf as an administrative user and add the following line:

vm.swappiness = 10

Conclusion

More and more linux users are running virtual machines. Each one has its own kernel in addition to the hypervisor that actually controls the hardware. Virtual machines have virtual disks created for them, so changing the setting inside the virtual machine will have indeterminate results. Experiment first with changing the values of the hypervisor kernel, as it actually controls the hardware in your machine.

For older machines which can no longer be upgraded (already have maximum supported memory) you can consider placing a small solid state disk in the machine to use it as an additional swap device. This will obviously become a consumable as memory cells fail from lots of writes, but can extend the life of a machine for a year or more for very low cost. The lower latency and quick reads will give much better performance than swapping to an ordinary disk, giving intermediate results to RAM. This should allow you to use somewhat lower vm.swappiness values for optimal performance. You will have to experiment. SSD devices are changing rapidly.

If you have more than one swap device, consider making it a RAID device to stripe data across the available devices.

You can make changes in swappiness without rebooting the machine, a major advantage over other operating systems.

Try to include only the services you need for your business. This will reduce memory requirements, improve performance and keep everything simpler.

A final note: You will be adding load to your swap devices. You will want to monitor the temperatures of them. An overheated system will lower its CPU frequency and slow down.

Acknowledgements

The author would like to say a special thanks to Gerold Rupprecht and Zoleka Hatitongwe for their critical remarks and comments while preparing this article.

Links and References

* [1] Linux Kernel Tutorial for Beginners, https://linuxhint.com/linux-kernel-tutorial-beginners/

* [2] Derek Molloy: Writing a Linux Kernel Module — Part 1: Introduction, http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/

* [3] Derek Molloy: Writing a Linux Kernel Module — Part 2: A Character Device, http://derekmolloy.ie/writing-a-linux-kernel-module-part-2-a-character-device/

* [4] Derek Molloy: Writing a Linux Kernel Module — Part 3: Buttons and LEDs, http://derekmolloy.ie/kernel-gpio-programming-buttons-and-leds/

* [5] Frank Hofmann: Commands to Manage Linux Memory, https://linuxhint.com/commands-to-manage-linux-memory/

* [6] Frank Hofmann: Linux Kernel Memory Management: Swap Space, https://linuxhint.com/linux-memory-management-swap-space/

* [7] dphys-swapfile package for Debian GNU/Linux, https://packages.debian.org/stretch/dphys-swapfile

* [8] Red Hat Performance Tuning Guide, https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/performance_tuning_guide/s-memory-tunables

* [9] Configuring MariaDB, https://mariadb.com/kb/en/library/configuring-swappiness/ ]]> Best Linux Books https://linuxhint.com/best-linux-books/ Fri, 25 May 2018 08:07:18 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=26669 (This post contains affiliate links. It is a way for this site to earn advertising fees by advertising or linking to certain products and/or services.)

It can be daunting to learn how to use Linux from scratch or become more familiar with its many intricacies, especially when you don’t know where to start and what to learn.

A good book can be an indispensable guide on your journey for knowledge, laying down a clear path for you to follow. Our selection of the best Linux books offers something to beginners and seasoned Linux veterans alike and presents a great, if somewhat old-school, alternative to online courses and YouTube explainer videos.

Linux for Beginners by Jason Cannon

In his book, Jason Cannon presents an approachable introduction to the Linux operating system, clearly and concisely describing many concepts, commands, and nuances that are typically glanced over in less detailed introductions to Linux. The book doesn’t focus on any specific distribution. Instead, it talks about Linux systems in general and teaches how to use the GNU Core Utilities, which are the basic file, shell and text manipulation tools present in virtually all Linux distributions.

The Linux Command Line by William Shotts

The Linux Command Line by William Shotts is a must-read book for somewhat experienced Linux users who would like to become power users and move away from the shiny, point-and-click surface of their Linux distributions of choice and explore the depths of the command line. By the time you’ve finished the book, you’ll know how to write powerful bash scripts that accomplish what would take mouse-dependent Linux neophytes a few hours in a few seconds, and you’ll also understand the reasoning behind many seemingly strange design decisions of core Linux developers.

Linux Bible by Christopher Negus

Considering its length of over 800 pages, this step-by-step tutorial for mastering Linux has definitely earned its title. Its 25 chapters cover everything from using the shell to managing running processes to learning system administration to configuring a web server to securing Linux on a network. Those who’ve finished this book from cover to cover are said to have the ability to telepathically channel the almighty rms himself, but we can’t confirm such claims.

How Linux Works by Brian Ward

If you’d like to learn how Linux really works under the hood, Brian Ward has you covered. On its 350 or so pages, the book explains basic commands and directory hierarchy, devices, disks, filesystems, how the Linux kernel boots, how userspace starts, system configuration, processes and resource utilization, networking, shell scripts, Linux development tools, and more. The book has been written with regular Linux users in mind, but it also works as an introductory course for aspiring Linux system administrators.

UNIX and Linux System Administration Handbook

The last book on our list is different than the previous ones because its purpose is to provide a definitive guide to installing, configuring, and maintaining any UNIX or Linux system, including systems that supply core Internet and cloud infrastructure. You might be wondering why you should keep around a book like this when you can just google any problem you come across. The reason is simple: the answers you find in this book are always 100 percent accurate and written by experts who understand UNIX and Linux in depth and know when one change may have unexpected consequences.

]]>
Upgrade Kernel on Ubuntu 18.04 https://linuxhint.com/upgrade-kernel-ubuntu-1804/ Wed, 04 Apr 2018 09:06:51 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=24546 On Ubuntu operating systems, Ubuntu Kernel Update Utility (UKUU) is used to manage and update kernels. UKUU has a graphical and a command line frontend. It is really easy to use. In this article I will show you how to update the kernel version on Ubuntu 18.04 LTS codename Bionic Beaver using UKUU graphically and through the command line. Let’s get started.

Checking the Currently Used Kernel Version

It’s a good idea to check the kernel version you’re using before performing kernel update. So you will be able to find out whether the kernel update worked.

Run the following command to check the kernel version you’re currently using:

$ uname -r

As you can see, I am using kernel version 4.15 right now.

You can also see that I am using Ubuntu 18.04 Bionic Beaver. It’s in development as of this writing. But it should be released this month.

$ lsb_release -a

Installing UKUU

Ubuntu Kernel Update Utility also known as UKUU is not installed on Ubuntu 18.04 Bionic Beaver by default. It is not available in the official package repository as well. But it is available on UKUU PPA.

You can add UKUU PPA to your Ubuntu 18.04 machine with the following command:

$ sudo add-apt-repository ppa:teejee2008/ppa

Once you run the command, UKUU PPA should be added as you can see in the screenshot below.

As of Ubuntu 18.04, adding a PPA will also auto update the package repository cache. You won’t have to run sudo apt-get update manually anymore. It is a great feature to have.

Now you can install UKUU with the following command:

$ sudo apt-get install ukuu

Press y and then press <Enter> to continue.

UKUU should be installed.

Upgrading the Kernel Graphically with UKUU

UKUU has a graphical application to update the kernel of Ubuntu 18.04 LTS operating system. To start UKKU Graphical User Interface (GUI), go to the Application Menu of Ubuntu 18.04 LTS, and search for ukuu. Now click on the UKUU icon as shown in the screenshot below.

You should see the following window. UKUU is generating a list of all the available kernels.

Once all the kernel information is downloaded, you should see the following window. As of this writing, the latest version of the kernel is 4.16. You can of course select any version you want.

I am going to install the latest version 4.16. So I will select that. Once you select your desired version, click on Install as marked blue in the screenshot below.

Now enter your password and click on Authenticate.

UKUU should start downloading and installing the new kernel.

Once it’s done, click on Close as marked in the screenshot below.

UKUU may give you a warning like this. Simply click on OK as this is the only thing you can do.

Now restart your computer for the changes to take effect.

Once your computer starts, you can run the following command to check the kernel version again:

$ uname -r

As you can see from the screenshot below, the kernel is updated to the latest version.

Upgrading the Kernel using UKUU Command Line Utility

UKUU also has a command line utility which you can use to update the kernel of your Ubuntu operating system.

The reason why you might want to use the command line utility is that it is more stable that the GUI version of UKUU. Also if you’re using a headless Ubuntu Server or Ubuntu without graphical user interface, then you can’t use the UKUU GUI even if you want to without installing several gigabytes of GNOME dependencies, which is really unnecessary. You can use UKUU command line utility on Ubuntu headless servers.

I personally prefer the command line utility.

In this section I will show you how to use the UKUU command line utility to update the kernel of your Ubuntu 18.04 machine.

First run the following command to list all the available kernels:

$ ukuu --list

UKKU should list all the available kernels as you can see in the screenshot below. I am going to install v4.16 kernel using UKUU.

Before you can install disable access control with the following command:

$ xhost +

The access control should be disabled.

Now run the following command to install the desired kernel version (in my case v4.16):

$ sudo ukuu install --v4.16

UKUU should download and install the new kernel as you can see from the screenshot below.

The kernel v4.16 is installed.

Now restart your computer with the following command:

$ sudo reboot

Once your computer starts, you can run the following command to check the currently used kernel version again:

$ uname -r

As you can see the kernel is updated.

That’s how you upgrade the kernel on Ubuntu 18.04. Thanks for reading this article.

]]>
Upgrade Kernel on Arch Linux https://linuxhint.com/upgrade-kernel-on-arch-linux/ Sat, 10 Feb 2018 05:42:34 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=22285 Arch is a rolling release Linux distro. It means you always get up to date software packages and kernel updates on Arch Linux. But that doesn’t mean you can’t manually install an updated version of Kernel on Arch Linux. Of course you can.

In this article, I will show you how to update the kernel of Arch Linux using the package manager. I will also show you how to compile the kernel from source and use it on Arch Linux. Let’s get started.

Updating Kernel using Package Manager:

First check the version of kernel you’re currently using with the following command:

$ uname -r

Now run the following command to perform a system update with pacman:

$ sudo pacman -Syu

As you can see from the marked section in the screenshot below, the kernel package will also be updated. Now press ‘y’ and then press <Enter> to continue.

Pacman package manager should start downloading and installing the updates.

All the packages including the kernel is updated at this point.

Now restart your Arch Linux machine with the following command:

$ sudo reboot

Once your computer starts, run the following command to check for the kernel version again:

$ uname -r

As you can see from the screenshot below, the kernel is updated to 4.15.1.

Compiling Kernel from Source:

You can also download and compile an updated version of kernel from the official website of Linux kernel at https://www.kernel.org

I am going to show you how in this section.

First go to https://www.kernel.org and you should see the following page as shown in the screenshot below.

Now click on the marked section as shown in the screenshot below.

The latest version of Linux kernel as of the time of writing is 4.15.2. Your web browser should prompt you to save it. Click on “Save File” and then click on “OK” as marked in the screenshot below.

The Linux kernel archive file should start downloading.

Once the download is complete, navigate to the directory where you downloaded the file. In my case it is the Downloads/ directory in my USER’s home directory.

$ cd Downloads/

I listed the directory contents with ls command and as you can see, linux-4.15.2.tar.xz file is there.

Now extract the archive file with the following command:

$ tar xvf linux-4.15.2.tar.xz

The file should be extracted.

NOTE: To compile a Linux kernel, you need more than 20GB of free space. You can check how much space you have left with df -h command.

Once the file is extracted, a new directory should be created. In my case it is linux-4.15.2/ directory as shown in the screenshot below.

Now navigate to the directory with the following command:

$ cd linux-4.15.2

Before starting the kernel compilation process, make sure you run the following command to install ncurses, make, gcc, bc, and openssl packages:

$ sudo pacman -S ncurses make gcc bc openssl

Press ‘y’ and then press <Enter> to continue.

The required packages should be installed.

Now copy the configuration file that the current kernel is using to the linux-4.15.2 directory with the following command:

$ zcat /proc/config.gz > .config

Now run the following command to prepare the configuration file for the new version of kernel.

$ make menuconfig

It should start the following terminal based graphical interface. You can press <Up>, <Down>, <Left> and <Right> arrow keys to navigate and <Enter> and <ESC> to select or go back one step respectively.

From here you can enable or disable specific kernel features. If you don’t know what it is, just leave the defaults.

Once you’re satisfied with the configuration file, go to <Save> option and press <Enter>

Then you should see the following window. Press <Enter> again.

Then you should see the following window. Press <Enter> again.

You should be back to the main configuration window.

Go to <Exit> and press <Enter>

You should be back to the terminal as shown in the screenshot below.

Now run the following command to start the compilation process:

$ make

The kernel compilation process should start.

It should take a long time for the kernel compilation process to finish. Once it’s done, you should see the following window as shown in the screenshot below.

Now install all the compiled kernel modules with the following command:

$ sudo make modules_install

All the kernel modules should be installed.

Now copy the vmlinuz file for your architecture to the /boot directory. For 32-bit operating system, run the following command:

$ sudo cp -v arch/x86/boot/bzImage /boot/vmlinuz-4.15.2

For 64-bit operating system, run the following command:

$ sudo cp -v arch/x86_64/boot/bzImage /boot/vmlinuz-4.15.2

The file should be copied.

Now generate an initramfs image and save it to /boot directory with the following command:

$ sudo mkinitcpio -k 4.15.2-ARCH -g /boot/initramfs-4.15.2.img

The initramfs file should be generated.

Now copy the System.map file to /boot directory with the following command:

$ sudo cp -v System.map /boot/System.map-4.15.2

Now make a symbolic link of the System.map-4.15.2 file to /boot/System.map with the following command:

$ sudo ln -sf /boot/System.map-4.15.2 /boot/System.map

Now generate a grub.cfg file with the following command:

$ sudo grub-mkconfig -o /boot/grub/grub.cfg

A new grub.cfg file should be generated.

Now reboot your computer with the following command:

$ sudo reboot

When your computer shows the GRUB menu, select the “Advanced options for Arch Linux” option and press <Enter>.

Then select the menu for your newly installed kernel from the list and press <Enter>.

Once your computer boot, run the following command to check for the kernel version:

$ uname -r

The kernel should be updated as you can see from the screenshot below.

That’s how you upgrade the kernel of Arch Linux. Thanks for reading this article.

]]>
Linux Kernel 4.15 Release https://linuxhint.com/linux-kernel-4-15-release/ Fri, 26 Jan 2018 05:05:19 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=21900

Linux Kernel 4.15 – The Latest Casualty of Spectre and Meltdown

The real meltdown from the CPU fiasco came this week with Linus Torvalds rant against Intel. In true Linus form, he didn’t mince words about Intel.  Honestly – their decision in continuing to ship flawed chips and marketing their software protection as a feature, is kind of sketchy. Of course, it does allow them to continue to ship chips without a serious reduction in performance.

Regardless of the genius behind the marketing spin, it potentially puts the average user at risk.  Those not savvy enough to ensure they set the bypass flag, will continue to be vulnerable to the flaw.  So will those who choose to risk vulnerability for the added performance boost.  Without getting philosophical, This thread from the Kernel mailing list shows Linus’ disapproval of Intel’s decision.

That said – the flaw has added workload to the Linux Kernel development team which may or may not have been the reason for the release candidate 9, the highest release candidate for any kernel version since 2011.  Instead of shipping his latest release, Linus decided to incubate what he considered an unfinished product.

So, what can we expect from the 4.15 when it is released next week?  There are a few notable additions.

The kernel adds support for the Radeon RX Vega cards.  This is probably a huge win for die-hard gamers.  From the opinion of a non-gamer, this represents a noticeable performance boost for Ethereum miners.

The next release could also offer support for RISC-V architecture.  This introduces out-of-the-box support for a wide range of new devices.

4.15 adds temperature support for CPUs and graphics.  It adds code for the AMD Zen temperature monitoring for Ryzen / Threadripper / EPYC processor(s).  Another big addition for AMD followers.  As well as support for the NVIDIA Pascal graphics cards.

This release will also include significant changes to the XFS file system.  While 4.14 fixed a number of persistent bugs, 4.15 should have some new code added to the release.

So, while the release delay was overshadowed by Linus’ entertaining outburst, it has some exciting features.  RC9 should be delivered as 4.15 this week, barring any further delays.  Truth be told, I would pass on the release to listen to another rant from Linus.

]]>
Linux Kernel Memory Management: Swap Space https://linuxhint.com/linux-memory-management-swap-space/ Fri, 19 Jan 2018 08:39:50 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=21681

Part One. The Swap space

In order to work properly a computer depends on having an adequate amount of memory. Simply saying that there can never be enough. The more physical memory is installed the more costly it is. Mostly, the result is a clever compromise between costs and speed to access the memory cells.

To achieve this compromise UNIX/Linux systems combine two types of memory — physical memory (RAM), and swap space. Altogether this is called the virtual memory of a computing system. Physical memory is rather expensive but fast and accessible within nanoseconds. In contrast, swap memory is rather cheap, but slow, and accessible within milliseconds.

There exist a few reasons why swap memory is useful. First, sometimes single processes need more memory than the system physically owns and can provide more to the processes that demands it. As a result, all data that is kept in physical memory cannot be stored there any longer. Now, the swap space comes into play, and a selection of memory pages are transferred to the swap space to free physical memory.

Second, not all the data is needed in memory at the same time. That’s why less used memory pages are parked on swap space to have as much free physical memory available as possible. This method is named the Least Recently Used Page Replacement Algorithm (LRU) [1].

Types of swap

Swap space exists in two variants. Version 1 is a separate disk partition which is the so-called swap partition. There are no files stored onto that partition but memory information (dumps). Simply, version 2 is a file on a disk that resides in the file system on your harddisk. Version 1 is very common on UNIX/Linux systems, BSD and OS X, whereas version 2 exists on systems that run Microsoft Windows. Version 2 can also be enabled on UNIX/Linux systems (see below).

To see which swap space is active on your UNIX/Linux system run the following command in a terminal:

$ /sbin/swapon -s
Filename      Type       Size        Used      Priority
/dev/dm-3   partition  16150524   316484  -1
$

As an alternative you may send a request to the proc file system, and run the command cat /proc/swaps

This Linux system has a swap partition with a size of about 15 GB in which over 300M are in use, currently. The Priority column shows which swap space to use first. The default value is -1. The higher the priority value, the earlier this swap space is taken into account. The option -s is the short version of –summary. This option is deprecated, and it is recommended to use the option –show as follows, instead:

$ /sbin/swapon --show=NAME,TYPE,SIZE,USED,PRIO
NAME       TYPE       SIZE   USED PRIO
/dev/dm-3 partition 15,4G 307,1M   -1
$

The option –show accepts a list of values that represent the column headers. In order to achieve a specific output order choose the desired column headers and its sequence.

Swap size

As a general rule the size of the swap space it is recommended to be twice as much as the system has physical memory. Keep this in mind for general-purpose setups and desktop machines. For UNIX/Linux servers with much more physical memory you may lower the size of swap space to 50% of RAM. Laptops that can hibernate need to be slightly larger than the physical memory.

Installation

For a swap partition, it is recommended to think of swap space right from the beginning of splitting the disk into single partitions, or to leave enough unused disk space to use it later, eventually. Usually, during the configuration of the disks to be used the setup routine asks you about the size of the swap space. As an example, on Debian GNU/Linux this looks as follows:

As mentioned above, as long as you have space for new partitions on your harddisk you can create and include swap partitions with the use of commands like fdisk, and swapon.

Alternatively, swap space can also be enabled later on as a swap file. Linux supports this way so that you can create, prepare, and mount it in a fashion similar to that of a swap partition. The advantage of this way is that you do not need to repartition a disk to add additional swap space.

As an example, we create a file named /swapfile with a size of 512M, and enable this as additional swap space. First, with the help of the dd command we create an empty file. Second, mkswap uses this file to transform it into swap style. You may notice that the contents of the file is treated like a partition, and a corresponding UUID is assigned. Third, we enable this using swapon. Finally, the command swapon –show displays two swap entries — a partition, and the newly created file.

# dd if=/dev/zero of=/swapfile bs=1024 count=524288
524288+0 datasets in
524288+0 datasets out
536870912 bytes (537 MB) copied, 0,887744 s, 605 MB/s

# mkswap /swapfile
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=e47ab7fe-5efc-4175-b287-d0e83bc10f2e

# swapon /swapfile
# swapon --show=NAME,TYPE,SIZE,USED,PRIO
NAME      TYPE       SIZE   USED PRIO
/dev/dm-3 partition 15,4G 288,9M   -1
/swapfile file       512M      0B   -2
#

To use this swap file at boot time add, as administrator, the following line to the file /etc/fstab:

/swapfile  none   swap  sw   0   0

Disabling a swap space

Least but not last there is one command to disable the swap file, again. The command is called swapoff. It requires a single parameter that indicates the swap device to be disabled. This command disables the previously activated swap file:

# swapoff /swapfile

Also, swapoff can work with the UUID of a file system. To make swapoff act this way use the option -U followed by the UUID of the according file system. In case it is needed to disable all the swap spaces at once the option -a (long option –all) is quite handy. The full command is swapoff -a.

Tuning the swap ecosystem

Starting with Linux kernel releases 2.6 a new value was introduced. This is stored in the variable /proc/sys/vm/swappinessand controls the relative weight given to swapping out of runtime memory, as opposed to dropping memory pages from the system page cache [2]. The default value is 60 (percent of memory free before activating swap). The lower the value the less swapping is used, and the more memory pages are kept in physical memory.

  • 0: swap is disabled
  • 1: minimum amount of swapping without disabling it entirely
  • 10: recommended value to improve performance when sufficient memory exists in a system
  • 100: aggressive swapping

To set the value temporarily set the value in the /proc file system as follows:

# echo 10 > /proc/sys/vm/swappiness

As an alternative you may use the sysctl command as follows:

# sysctl -w vm.swappiness=10

To set the value permanently add the following line to the file /etc/sysctl.conf:

vm.swappiness = 10

Is swap still up to date?

You may ask why we deal with that topic. Modern computers have enough physical memory — so why do we have to care about that? There are a few reasons why this technology is worth more than a thought.

Keep in mind that you stick with your machine for a while, but may update the software you use on it from time to time. Currently, both the hardware and the software suit to each other. In the future it may change, and you need more memory than you have now. Unless upgrading or buying new hardware a Swap partition could save you a bit of money.

You may have heard about a feature called suspend to disk, or hibernate mode [3]. Your machine is going to sleep. Before doing that it has to store its current state somewhere. Now the swap space comes into play, and acts as a container to keep this data. As soon as the machine wakes up the next time the entire data is read from the Swap space, loaded into memory, and you can continue working where you have stopped before.

The system, if having only one permanent storage device, will have to read and write your files while swapping on the same device. You will see a huge improvement if you have a second device and can separate the swap device from conflicting file accesses.

The swap file must pass data through the file system. This adds a layer of indirection, to make it appear that there is a contiguous logical address space for the kernel to work with. This adds additional memory overhead and cpu cycles. You will get best results using a raw swap partition.

Conclusion

Even today the knowledge regarding Swap is essential. This topic is part of the knowledge that is required to pass the Linux Professional Institute Certificate Level 1 (LPIC 1). Most of the exams contain one or two questions about this topic.

Swap space helps your Linux system (kernel) to quickly organize memory if there is need for it. To be open with you, Swap space is not absolutely necessary in case your system has tons of RAM. In case of emergencies it helps your system to survive. That’s why I would never leave the path of a traditional setup without Swap space.

The combination of Swap and SSD is discussed in a controversial way because the number of disc writes on an SSD is quite limited. Both Swap and temporary files are built to write lots of data. On the other hand, modern SSDs have more than enough additional space (7%) to cope with sector failures. To be on the safe side: if possible have a separate Swap on a conventional hard drive — don’t use ramdisk, nor an SSD, at least for swap [4]. Your Linux system will thank you for this decision.

To avoid putting the swap space on your SSD you could use ZRAM, instead [5,6]. This is Virtual Swap Compressed in RAM, also named zSwap. This technology enables a compressed block device in memory. As soon as there is no more memory left memory pages are transferred to this block device. This results in less swap usage, and helps to extend the life of your harddisk, too.

Links and References

Linux Memory Management Series

Acknowledgements

The author would like to thank Mandy Neumeyer and Gerold Rupprecht for their support while preparing this article.

]]>
Linux Kernel Watchdog Explained https://linuxhint.com/linux-kernel-watchdog-explained/ Mon, 15 Jan 2018 18:09:23 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=21595

Linux Kernel Watchdog

The Linux kernel watchdog is used to monitor if a system is running. It is supposed to automatically reboot hanged systems due to unrecoverable software errors. The watchdog module is specific to the hardware or chip being used. Personal computer users don’t need watchdog as they can reset the system manually. However, it is useful for systems that are mission critical and need the ability to reboot themselves without human intervention. For example, servers on a remote location or embedded equipment on a spacecraft that need automatic hardware reset capabilities.

Warning: Proceed with Caution

Wrong configurations of a watchdog on your system can cause problems like:

  • Endless reboot loop
  • File corruption due to hard reset
  • Unpredictable random reboots

So avoid using live servers to test Linux kernel watchdog.

Watchdog Module

Watchdog functionality on the hardware side sets up a timer that times out after a predetermined period. The watchdog software then periodically refreshes the hardware timer. If the software stops refreshing, then after the predetermined period, the timer performs a hardware reset of the device. In order for a watchdog timer to be functional, the motherboard manufacturer has to use the chip’s watchdog functionality. Often the documentation from the manufacturer is not clear about whether the functionality was implemented. In that case, you have to test it out.

Also, you need the right watchdog kernel module to be loaded in your Linux system. Different chips use different modules. For example:

  • Intel chipsets might use the “iTCO_wdt” module
  • HP hardware might use “hpwdt”
  • IBM mainframes might use “vmwatchdog”
  • Xen VM might use “xen_wdt”

After the module is loaded, you can check /dev/watchdog on the Linux system. If this file is present, that means the watchdog kernel device driver or module was loaded. The system periodically keeps writing to /dev/watchdog. It is also called “kicking or feeding the watchdog”. If the system fails to kick or feed the watchdog, then after a while the system is hard reset.

Watchdog Daemon

The watchdog daemon opens the device and provides the necessary refresh to keep the system from resetting. It can test process table space, memory usage, file accessibility, work overload, file table overflow, IP address ping, network interface traffic, temperature, running processes and more. If the tests fail, then watchdog causes a shutdown.

Starting and Stopping Watchdog

Watchdog daemon should start at boot time and put itself in the background. You can check if it is running:

ps -af | grep watch*

If the kernel is NOT compiled with CONFIG_WATCHDOG_NOWAYOUT, then if you close the /dev/watchdog properly, it will not cause a reboot. You can write the character V into /dev/watchdog and then close the file. This should stop the watchdog.

Testing the Watchdog

If you want to test if the hardware watchdog is working, you can do the following from your administrator command prompt:

cat >> /dev/watchdog

And press “enter” twice and wait. The prompt will not come back. After awhile depending on your kernel’s setting, the system should perform the hard reboot.

References:

]]>
Compile Linux Kernel on CentOS7 https://linuxhint.com/compile-linux-kernel-centos7/ Fri, 05 Jan 2018 16:24:34 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=21277

Compile the Latest Linux Kernel from Source on CentOS 7

In this article I will show you how to download the latest Linux kernel source from the official website of Linux kernel, compile Linux kernel from source and use the compiled kernel on CentOS 7. Let’s get started.

Checking Currently Used Kernel:

You can see from the following screenshot that I am using CentOS 7.

And the current kernel version is 3.10

Installing the prerequisites:

To compile the latest Linux kernel from source on CentOS 7, you must have a build tool and some other packages installed on your CentOS 7 operating system.

Before you install anything, run the following command to update package cache:

$ sudo yum makecache

Now you can install the compilers and libraries required for compiling the kernel with the following command:

$ sudo yum install ncurses-devel make gcc bc openssl-devel

Press ‘y’ and then press <Enter> to continue.

The build tools should be installed.

Now you have to install elfutils package. Run the following command to install it:

$ sudo yum install elfutils-libelf-devel

Press ‘y’ and press <Enter> to continue.

‘elfutils’ should be installed.

Now you have to install rpm-build with the following command:

$ sudo yum install rpm-build

Press ‘y’ and then press <Enter> to continue.

‘rpm-build’ package should be installed.

Downloading the Linux Kernel Source:

Go to the official website of Linux Kernel at https://www.kernel.org and you should be the following page.

Click on the “Latest Stable Kernel” button as marked on the screenshot below.

Your browser should prompt you to save the file. Just click on “Save File” and click on “OK”.

Your download should begin.


Compiling the Kernel:

Once the download is complete, navigate to the directory where you downloaded the file. In my case it is the Downloads directory in my user’s HOME directory.

$ cd ~/Downloads

From the output of ‘ls’ you can see that the downloaded file is ‘linux-4.14.10.tar.xz’. Which is a compressed tar file.

Now extract the compressed tar file with the following command:

$ tar xvf linux-4.14.10.tar.xz

tar is extracting the compressed file.

Once the file is extracted, you should see the following window.

After extraction, you should see a new directory as marked red in the screenshot below. Navigate to the directory with the following command.

$ cd linux-4.14.10

If you run the following command, you should see a list of config files used by the kernels that are installed on your system. You can run ‘uname -r’ command to find the one that you need. The filename should match with the output of the ‘uname -r’ command.

Then copy the config file to the linux-4.14.10 directory with the following command:

$ sudo cp -v /boot/config-3.10.0-693.el7.x86_64 .config

Now run the following command:

$ make menuconfig

You should see the following window. This is where you enable or disable certain kernel features. If you don’t know what to do here, then just leave the defaults.

Once you’re done, press <Left Arrow> button several times and go to “< Save >” as shown in the screenshot below. Then press <Enter>.

Then press <Enter> again.

Press <Enter> again.

Now navigate to “< Exit >” and press <Enter>

The .config file is updated for the new kernel.

Before you start compiling the new kernels, make sure you have more than 20GB of free space on the filesystem where you’re compiling the kernel.

You can check how much space you have available with the following command:

$ df -h

 

Now run the following command to start the compilation process:

$ make rpm-pkg

The kernel should be compiling just fine. It should take a long time.

On completion, you should see the following window. Some rpm package file was created on the user’s home directory as you can see from the screenshot.

The generated rpm package files.

Now you can run the following command to install the rpm packages:

$ sudo rpm -iUv ~/rpmbuild/RPMS/x86_64/*.rpm

Once the installation is complete, run the following command to restart your computer.

$ reboot

Once your computer starts, you can run the following command to check the version of the kernel that you’re currently using.

$ uname -r

You should see that it’s the version that you just installed. For me, it is ‘4.14.10’.

So that’s how you compile the latest kernel from source and use it on CentOS 7. Thanks for reading this article.

]]>
How to Upgrade Kernel of Debian 9 Stretch from Source https://linuxhint.com/how-to-upgrade-kernel-of-debian-9-stretch-from-source/ Tue, 19 Dec 2017 13:03:49 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=20852 In this article, I will show you how to upgrade the kernel of Debian 9 stretch. I will download a kernel source from the official website of Linux kernel and compile it on Debian 9 stretch. Let’s get started.

Checking the Installed Kernel Version:

You can check the current version of the kernel that is installed on your Debian 9 operating system with the following command:

$ uname -r

From the output of the ‘uname’ command, you can see that the kernel I have installed on my Debian 9 operating system is 4.9.0


Downloading the Kernel:

Go to the official website of Linux Kernel at https://www.kernel.org from any web browser of your choice. You should see the following page:

You can see that source code of different kernel versions is listed on the website. You can download the kernel sources as compressed tar file.

There are mainline, stable, longterm kernels that you can download. If you’re upgrading the kernel of a production computer system running Debian, you should download the longterm or stable release. If you’re just testing something, you may download the mainline release if you want. Be warned, the mainline release may have bugs. If you care about stability, you should always get the stable or longterm releases.

In this article, I will show you how to upgrade the default Debian 9 kernel to stable 4.14.7 kernel. So click on the link as shown in the screenshot to download the source code for kernel 4.14.7

Your browser should prompt you to download the file. Click on “Save File” and click on “OK”. Your download should start.


Installing Required Tools for Building the Kernel:

Before you can compile a kernel on Debian 9, you need to install some additional packages, basically the compiler and the required dependencies.

First update the package repository cache with the following command:

$ sudo apt-get update

Now run the following command to install the required packages:

$ sudo apt-get install build-essential libncurses5-dev gcc libssl-dev bc

Just press ‘y’ and press <Enter> to continue.

Once the installation is complete, we can start the kernel compilation process.


Compiling the Kernel:

Now we can compile the kernel from source. Before you go any further, make sure you have more than 18GB-20GB of free space where you are going to compile the kernel.

First go to the directory where you downloaded the linux kernel source. In my case, it is the Downloads directory on my HOME directory.

Run the following command to navigate to the Downloads directory:

$ cd ~/Downloads

You can see from the output of ‘ls’ that the name of the file I downloaded is ‘linux-4.14.7.tar.xz’.

Now we have to extract the tar archive.

Run the following command to extract the tar.xz archive:

$ tar xvf linux-4.14.7.tar.xz

You can see that a new directory ‘linux-4.14.7’ was created.

Now navigate to the directory with the following command:

$ cd linux-4.14.7

Now we have to copy the boot configuration into the ‘linux-4.14.7’ directory. We are doing these because it’s a lot of work to figure out what kernel module to enable, what to disable to get a working system. So we can just use the configuration that the current kernel is using.

From the output of the following ‘ls’ command, you can see a config file marked black in the screenshot. This is the file we are interested in.

Run the following command to copy the configuration file:

$ cp -v /boot/config-4.9.0-3-amd64 .config

The new kernel may have a lot of new features that the old kernel didn’t have. So it’s a good idea to run the following command to convert the old configuration file to a new one. Otherwise we will be asked a lot of questions that might not make sense to you.

$ make menuconfig

You will be presented with the following window. From here you can enable and disable specific kernel features. If you don’t know what to do here, just leave the defaults.

Once you’re done deciding what you want to install, press the <Right Arrow> key to navigate to “Save” and press <Enter>

Press <Enter>

Press <Enter>

Navigate to “Exit” and press <Enter>. You’re done.

Now run the following command to start the kernel compilation process:

$ make deb-pkg

The compilation process takes a very long time to finish.

Once the compilation is complete, you should see the following window:

4 debian package files (.deb) will be generated outside of the ‘linux-4.14.7’ directory as you can see in the screenshot.

Now all we have to do to update the kernel is to install all the generated .deb files.

Run the following command to update the kernel:

$ sudo dpkg -i ../linux-*.deb

Now restart your computer with the following command:

$ sudo reboot

Once the computer boots, run the following command to check the kernel version:

$ uname -r

You can see from the output of the ‘uname’ command that the kernel version is updated to 4.14.7

So that’s how you update the kernel of Debian 9 stretch. Thanks for reading this article.

]]>
History of the Linux Kernel https://linuxhint.com/history-linux-kernel/ Wed, 13 Dec 2017 17:02:13 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=20772 Even though most people have heard of Linux, they still associate it primarily with various operating system distributions built around it. In this article, we describe the history of Linux as an open source operating system kernel, which is the central component of most computer operating systems that acts as a bridge between applications and the actual data processing done at the hardware level. The history of the Linux kernel is both fascinating and educational as it can teach us a lot about the underlying motivations of Linux developers and help us understand the direction the kernel is headed.

Introduction

What started as one man’s humble idea has grown to become the most important open source project ever created. The Linux kernel currently has over 20 million lines of code, and it runs on all of the world’s 500 most powerful supercomputers. It also runs on servers, desktops, laptops, TV boxes, routers, tablets, smartphones, wearable devices, and it powers much of the rapidly growing network of connected devices known as the Internet of Things.

Over 12,000 programmers from more than 1,200 companies have contributed to the project, including Intel, Red Hat, Linaro, Samsung, SUSE, IBM, and Microsoft. In other words, the Linux kernel is hugely important, and its future is looking brighter than ever.

Creation of the Linux Kernel

But it wasn’t always like this. Not too long ago, in 1991, the Linux kernel was nothing but an announcement made by Linus Torvalds, at the time a 21-year-old computer science student at the University of Helsinki, Finland.

“I’m doing a (free) operating system (just a hobby, won’t be big and professional like GNU) for 386 (486) AT clones. This has been brewing since April, and is starting to get ready. I’d like any feedback on things people like/dislike in MINIX, as my OS resembles it somewhat (same physical layout of the file-system (due to practical reasons) among other things),” Linus posted to comp.os.minix, a newsgroup on Usenet, a worldwide distributed discussion system that predates current Internet forums.

In his historic announcement, Linus mentioned two other important projects: GNU and MINIX. The latter is a Unix-like computer operating system that was initially released in 1987 by Andrew S. Tanenbaum for educational purposes. Unix-like computer operating systems are inspired by Bell Labs’ original Unix computer operating system, often emulating its features and architecture. GNU is also a Unix-like operating system, initiated by Richard Stallman and first announced in 1983, but it differs from Unix in two important aspects: it’s free, and it doesn’t contain any Unix code.

Linus had been using MINIX during the time he spent as a student at the University of Helsinki in Finland. After he had become frustrated with MINIX’s licensing model, he decided to develop his own free alternative to Unix, one that would embrace the concept of free software that had only just started to become popular at the time thanks to Richard Stallman and his GNU General Public License (GPL), which guarantees end users the freedom to run, study, share and modify the software.

Linus started by porting some essential GNU components, and it remains true to this day that many Linux distributions heavily rely on GNU. “I’ve currently ported bash (1.08) [a Unix shell and command language written by Brian Fox] and gcc (1.40) [a compiler system produced by the GNU Project supporting various programming languages], and things seem to work. This implies that I’ll get something practical within a few months, and I’d like to know what features most people would want. Any suggestions are welcome, but I won’t promise I’ll implement them.”

In September 1991, version 0.01 of the Linux kernel was released on the FTP server of FUNET, the Finnish University and Research Network, containing 10,239 lines of code. When Linus announced version 0.02 on October 5, 1991, the Linux kernel still needed MINIX to operate, but the number of volunteers from around the world who decided to contribute to the project without expecting anything in return had been steadily increasing. In December of the same year, Linux kernel 0.11 was released as the first version that could be compiled by a computer running the same kernel version. With Linux kernel 0.12, released in February 1992, Linux officially adopted the GNU General Public License (GPL).

Release of Linux kernel 1.0.0

In March 1992, Linux kernel 0.95 became the first version of the Linux kernel capable of running the X Window System, which is a windowing system for bitmap displays that offers a basic framework for a GUI environment by providing a way for windows to be drawn on a display device and interacted with using a mouse and keyboard. The massive version-jump from 0.12 to 0.95 reflected the fact that the Linux kernel had matured and evolved into a full-featured system.

To cement this notion further, Linux kernel 1.0.0 was released on March 14, 1994. It had 176,250 lines of code, and you can still study the original code and read the original release notes, which state that the Linux kernel 1.0 “has all the features you would expect in a modern fully-fledged Unix, including true multitasking, virtual memory, shared libraries, demand loading, shared copy-on-write executables, proper memory management, and TCP/IP networking.”

Modern-Day Development of Linux kernel

The Linux kernel continued to be heavily improved through the 1990’s, with version 2.0 released on June 6, 1996, and version 2.2.13, which allowed the Linux kernel to run on enterprise-class machines thanks to IBM mainframe patches, released on December 18, 1999.

After the arrival of the new millennium, Linux evolved to a world-wide development project with countless contributors from around the world. You can see the complete changelog of everything that happened from December 17, 2001 to the present day by visiting this website. According to recent estimations, “The average number of changes accepted into the kernel per hour is 7.71, which translates to 185 changes every day and nearly 1,300 per week.”

Considering that Linus never intended for his pet project to become so big, the Linux kernel is a true testament to the power of open source development and the ingenuity and skill of independent developers motivated by the desire to collectively create something great.

]]>
Linux Device Driver Tutorial https://linuxhint.com/linux-device-driver-tutorial/ Mon, 27 Nov 2017 04:19:25 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=20343 Linux Device Driver Tutorial For Beginners

Linux operating system contains 3 main sections: Root File System, Kernel and Bootloader.

Root File System:

This part of the OS contains application binaries, libraries, scripts, config files and kernel loadable module files etc..

Kernel:

This part is the heart of OS, the Kernel is responsible for handling all the operations needed to run the OS such as memory management, process management, and input/output hardware operations etc..

Bootloader:

This is the first part to be executed by the CPU on boot. Bootloader contains the source code to initialize the system and start execute the kernel and contains commands for debugging and modifying the kernel environment, it also contains the commands to download and update the kernel and system images into the flash memory.

Drivers act as a bridge between hardware and a user application, the Kernel provides a mechanism called system calls to talk to the kernel.  In Linux, drivers can be implemented in two ways, one is drivers can be compiled as part of the kernel and another one is drivers can be compiled as modules and loaded at runtime.

Let us start with a simple hello world kernel module. Here is the source code for a simple hello world kernel module.

hello.c

#include<linux/module.h>	//needed for module_init and module_exit
#include<linux/kernel.h>	//needed for KERN_INFO
#include<linux/init.h>		//needed for the macros

int __init hw_init(void) {
        printk(KERN_INFO"Hello World\n");
        return 0;
}

void __exit hw_exit(void) {
        printk(KERN_INFO"Bye World\n");
}

MODULE_LICENSE("GPL");

module_init(hw_init);
module_exit(hw_exit);

Makefile

obj-m := hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Create a folder named hello and then place the hello.c and Makefile inside it. Open the terminal application and change directory to hello. Now run the command make and if it is successful then it should generate loadable kernel module file called hello.ko.

When you run make if you get output make: Nothing to be done for `all’.  Then please make sure that in the Makefile you have entered tab(no spaces) before make -C. If make is successful you should get the output as shown below.

make[1]: Entering directory `/usr/src/linux-headers-3.13.0-128-generic'
  CC [M] /home/John/Desktop/hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC /home/John/Desktop/hello/hello.mod.o
  LD [M]  /home/John/Desktop/mvs/pers/kern/hello/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-3.13.0-128-generic'

Now let us test the module by loading it into the kernel. For loading and unloading kernel modules we need to have superuser permission. Use the following command to load the kernel module into the kernel.

sudo insmod hello.ko

To see the printk message you need to check the kernel log, to check the kernel log use the following command.

dmesg

This command will output the kernel log messages, at the end you should see that our message Hello World printed.

To unload the module use the following command.

sudo rmmod hello

To see the printk message use dmesg command again and in the kernel log you can see our message Bye World.

Now let us understand the source code.

hello.c

To start writing the kernel driver you can use any editor or ide of your choice but most commonly kernel developers prefer using vi editor.

Every kernel module should include the header file linux/module.h this has the declarations and macros for kernel functions such as module_init and module_exit etc.  The two most necessary functions for a kernel driver are module_init and module_exit functions. The function whose pointer is passed to module_init will be executed when we load the module into the kernel, and the function whose pointer is passed to module_exit will be called when we unload or remove the module from the kernel.

Inside the kernel for debugging and printing the log, we use printk function which is similar to printf function which we use in the application. You can use the macros such as KERN_INFO, KERN_ERR etc.. to specify a log level.

If we are writing a driver to talk to a specific hardware then the init function should have the code to initialize the hardware before we start using it and exit function should have a code to clean up resources(Dynamic Memory etc) we used in the driver before we exit from the kernel.

Here in this example, we are just printing debug messages in init and exit functions.

Makefile

For building the kernel module we need to write a Makefile which will guide make  utility how to compile the module. The syntax obj-m is used to tell the kernel makefile that the driver needs to be compiled as module using the specified object file. When you just run command make then the control comes to the all: section of the Makefile and if you run command make clean then the control goes to the clean: section of Makefile. From this Makefile we are actually running make inside the kernel source directory using option -C. Please make sure you have kernel source directory installed in your system. Here in this example we used command uname -r  to find the current version of your system’s linux kernel.

We have used option M=$(PWD) to indicate in the kernel makefile that the source for driver is in present working directory and we are specifying the word modules to tell kernel makefile to just build modules and not to build the complete kernel source code. In clean: section of Makefile we are telling kernel makefile to clean the object files generated to build this module.

This should you get started compiling and running your first kernel module.

]]>
How to upgrade the kernel on Ubuntu 17.10 https://linuxhint.com/how-to-upgrade-the-kernel-on-ubuntu-17-10/ Thu, 23 Nov 2017 05:09:32 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=20267 Ubuntu 17.10 Artful Aardvark uses kernel 4.13.0 by default. It is pretty new. Most of the people will not need to update it. But if someone really need something that is only available in a later version of the kernel and feel like updating the kernel, then this article is for you.  In this article, I will show you how to update the default kernel 4.13.0 on Ubuntu 17.10 Artful Aardvark. Let’s get started.

You can check the version of the currently installed kernel on Ubuntu 17.10 using the following command:

$ uname -r

You can update the kernel of your Ubuntu 17.10 operating system in 2 ways. You can either use graphical user interface or the terminal. You can use Ubuntu Kernel Update Utility (UKUU), which is used to update the kernel of Ubuntu operating systems. UKUU has a graphical user interface version called ukuu-gtk and a command line utility. The problem is, UKUU GTK doesn’t work on Wayland very well, it only works on X11. So on Ubuntu 17.10, we can update the kernel only through the command line interface. I will show you how you can update the kernel from the command line with UKUU.


Installing UKUU:

Ubuntu Kernel Update Utility (UKUU) is not installed on Ubuntu by default and it’s also not available on Ubuntu software repository. So we have to install UKUU from UKUU PPA.

To add the UKUU PPA, run the following command and press <Enter>:

$ sudo add-apt-repository ppa:teejee2008/ppa

Now run the following command to update the package repository cache of your Ubuntu 17.10 operating system:

$ sudo apt-get update

How to upgrade the kernel on Ubuntu 17.10How to upgrade the kernel on Ubuntu 17.10How to upgrade the kernel on Ubuntu 17.10

Now we are ready to install UKUU, Run the following command to install UKUU:

$ sudo apt-get install ukuu

Press ‘y’ and press <Enter> to continue. UKUU should be downloaded and installed.


Updating the Kernel with UKUU:

Once UKUU is installed, you can open up a terminal (Ctrl+Alt+T on Ubuntu) and update the kernel.

You can run the following command to see what you can do with UKUU.

$ ukuu –help

You can see that, we can check for kernel updates, install and remove kernels with UKUU.

Now run the following command to see what kernels are available right now for download.

$ ukuu –list

UKUU will download a list of kernels that are available. It may take a while. You will have to wait till it’s done.

Once the download is complete, ukuu should show you a very long list of kernels. But we are interested on the first items of the list, as the list is sorted in descending order which means the latest versions are listed above the old versions. You can see that the current kernel version is 4.13.0. I want to install 4.14.1, the latest version as of this writing.

To install kernel 4.14.1 with UKUU, run the following command:

$ xhost +
$ sudo ukuu --install v4.14.1

It will take several minutes to download and install everything depending on your internet connection.

Once the download and installation is complete, reboot your computer with the following command:

$ reboot

Now run the following command to see the current kernel version:

$ uname -r

You can see that the kernel is updated to 4.14.1.  So that’s how you update the kernel of Ubuntu 17.10 Artful Aardvark. Thanks for reading this article.

]]>
How to Upgrade the Kernel on CentOS 7 https://linuxhint.com/upgrade-kernel-centos-7/ Thu, 23 Nov 2017 04:32:56 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=20253 By default, CentOS 7 uses an old version of kernel, which is 3.10.x. But the old kernel doesn’t support some new hardwares that we have today. So it’s necessary to update the old kernel for better hardware support.  In this article I will show you how to update the kernel of CentOS 7. Let’s get started.


Preparing for the Kernel Upgrade:

We must add ELRepo repository to CentOS 7 to update the kernel of CentOS 7. For more information, check the official website of ELRepo at http://elrepo.org/tiki/tiki-index.php.  First we have to add the GPG key for ELRepo. To do that, run the following command:

$ sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

Now we can add the ELRepo repository on CentOS 7. To do that, run the following command:

$ sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm

The name of the ELRepo kernel repository is called elrepo-kernel.  There are 2 kernels in ELRepo. One is called kernel-lt and the other is called kernel-ml. The difference between these 2 are, kernel-lt package provides the Long Term Support version of the latest Linux kernel, and the kernel-ml package provides Mainline Stable version of the latest Linux kernel. The kernel provided by kernel-ml is more updated than kernel-lt. Both of these kernels are safe. You can use any of them.

I will show you how to install both of them, but I will install kernel-ml in this article.

You can check the version of the currently installed kernel on CentOS 7 with the following command:

$ uname -r

You can see that; the kernel CentOS 7 is using right now is 3.10.0. We will soon update that. Let’s continue.


Installing Latest Long Term Support Kernel:

You can easily install long term support kernel or kernel-lt package provided by ELRepo on CentOS 7. At the time of this writing, the version of the kernel provided by kernel-lt package is 4.4.100.

To install kernel-lt package on CentOS 7 from ELRepo, run the following command:

$ sudo yum --enablerepo=elrepo-kernel install kernel-lt

Press ‘y’ and press <Enter> to continue.

Once the installation is complete, just restart your computer. When it boots, select the new kernel from the GRUB menu. Your CentOS 7 operating system should use the new kernel afterwards.


Installing Mainline Stable Kernel:

You can easily install mainline stable kernel or kernel-ml package provided by ELRepo on CentOS 7. At the time of this writing, the version of the kernel provided by kernel-ml package is 4.14.1.

To install kernel-ml package on CentOS 7 from ELRepo, run the following command:

$ sudo yum --enablerepo=elrepo-kernel install kernel-ml



Now press ‘y’ and then press <Enter> to continue:

It should take a while to download and install the kernel.  Once the installation is complete, run the following command to restart your computer.

$ sudo reboot

Once your computer boots, select the new kernel from the GRUB menu, it should be using the latest kernel that you just installed.

You can check and verify that its using the latest kernel with the following command:

$ uname -r

You can see that the kernel is updated.

So That’s how you update/upgrade the kernel of your CentOS 7 operating system. Thanks for reading this article.

]]>
Linux Kernel Tutorial for Beginners https://linuxhint.com/linux-kernel-tutorial-beginners/ Fri, 22 Sep 2017 20:16:40 +0000 https://linuxhint-com.zk153f8d-liquidwebsites.com/?p=19547

Background info on Operating Systems

An Operating System, abbreviated as OS, is a piece of software that controls the hardware components of a system, be it a phone, laptop or a desktop. It is in charge of the communication between the software and the hardware. Windows XP, Windows 8, Linux and Mac OS X are all examples of operating systems. The operating system consists of:

  • The bootloader: software in charge of the boot process of your device.
  • The kernel: the core of the system and manages the CPU, memory and peripheral devices.
  • Daemons: background services.
  • Networking: communications systems for sending and retrieving data between systems.
  • The shell: comprises a command process that allows manipulation of the device through commands entered into a text interface.
  • Graphical Server: the sub-system that shows the graphics on your screen.
  • Desktop Environment: this is what the users usually interact with.
  • Applications: are programs that perform the user’s tasks such as word processors.

Kernel space and Userspace

Kernel Space: the kernel is found in an elevated system state, which includes a protected memory space and full access to the device’s hardware. This system state and memory space is altogether referred to as kernel-space. Within kernel space the core access to the hardware and system services are managed and provided as a service to the rest of the system.

User Space: the user’s applications are carried out in the user-space, where they can reach a subset of the machine’s available resources via kernel system calls. By using the core services provided the kernel, a user level application can be created like a game or office productivity software for example.

Linux

Linux has gained in popularity over the years due it being open source hence, based on a UNIX like design, and ported to more platforms compared to other competing operating systems. It is an operating system, as indicated, that resembles a UNIX OS – a stable multi-user multi-tasking operating system, and that has been assembled as a free and open-source software for development and distribution. Meaning that any individual or company has the permission to use, imitate, study and alter the Linux operating system in any way they desire.

The Linux Kernel

From its first release on September 17, 1991, the Linux kernel has defied all odds to be Linux’s defining component. It was released by Linus Torvalds and makes use of the GNU/Linux to describe the operating system.  Linux kernel-based Android OS on smartphones has made Linux beat its competition to be the largest installed OS base of all general-purpose operating systems.  History of Linux Kernel can be found here.

A kernel can either be monolithic, microkernel or hybrid (like the OS X and Windows 7). The Linux kernel is a monolithic computer operating system kernel that resembles the UNIX system. The Linux line of Operating Systems commonly referred to as Linux Distributions are based on this kernel.  The monolithic kernel, unlike the microkernel, not only encompasses the Central Processing Unit, memory and IPC but also has device drivers, system server calls and file system management. They are best at communicating with hardware and performing several tasks simultaneously. It is for this reason that processes here react at a fast rate.

However, the few setbacks are the huge install and memory footprint needed and inadequate security as everything operates in a supervisor mode. In contrast, a microkernel may react slowly to application calls as user services and the kernel are separated. They are thus smaller in size when compared to the monolithic kernel.  Microkernels are easily extensible, but more code is needed to write a microkernel. The Linux kernel is written in the C and Assembly programming languages.

The Linux kernel relationship with the Hardware

The kernel can manage the system’s hardware through what is referred to as interrupts. When the hardware wants to interface with the system, an interrupt is issued that interrupts the processor that in turn does the same to the kernel. To provide synchronization, the kernel can disable interrupts, be it a single one or all of them. In Linux, however, the interrupt handlers do not run in a process context, they instead run in an interrupt context not associated with any process.This particular interrupt context exists solely to let an interrupt handler quickly respond to an individual interrupt and then finally exit.

What makes the Linux Kernel different from other Classic Unix Kernels?

Significant differences exist between the Linux kernel and the Classic Unix kernels; as listed below:

  1. Linux supports dynamic loading of kernel modules.
  2. The Linux kernel is preemptive.
  3. Linux has a symmetrical multiprocessor support.
  4. Linux is free due to its open software nature.
  5. Linux ignores some standard Unix features that the kernel developers call “poorly designed.”
  6. Linux provides an object-oriented device model with device classes, hot-pluggable events, and a user-space device file-system
  7. The Linux kernel fails to differentiate between threads and normal processes.

Architechture

Components of the Linux Kernel

A kernel is simply a resource manager; the resource being managed may be a process, memory or hardware device. It manages and arbitrates access to the resource between multiple competing users. The Linux kernel exists in the kernel space, below the userspace, which is where the user’s applications are executed. For the user space to communicate with the kernel space, a GNU C Library is incorporated which provides a forum for the system call interface to connect to the kernel space and allow transition back to the userspace.

The Linux kernel can be categorized into three primary levels:

  1. The system call interface; this is the topmost and undertakes the basic actions such as read and write.
  2. The kernel code; is located below the system call interface, it is common to all of the processor architectures supported by Linux, it is sometimes defined as architecture-independent kernel code.
  3. The architecture-dependent code; it is under the architecture-independent code, forms what is usually referred to as a Board Support Package (BSP) – this contains a small program called the bootloader that places the Operating System and device drivers into memory.

The architectural perspective of the Linux kernel consists of: System call interface, Process Management, the Virtual File system, Memory Management, Network Stack, Architecture and the Device Drivers.

  1. System call interface; is a thin layer that is used to undertake function calls from user space into the kernel. This interface may be architecture dependent
  2. Process management; is mainly there to execute the processes. These are referred to as the thread in a kernel and are representing an individual virtualization of the particular processor
  3. Memory management; memory is managed in what are known as pages for efficiency. Linux includes the methods in which to manage the available memory as well as the hardware mechanisms for physical and virtual mappings.  Swap space is also provided
  4. Virtual file system; it provides a standard interface abstraction for the file systems. It provides a switching layer between the system call interface and the file systems supported by the kernel.
  5. Network stack; is designed as a layered architecture modeled after the particular protocols.
  6. Device drivers; a significant part of the source code in the Linux kernel is found in the device drivers that make a particular hardware device usable.  Device driver tutorial
  7. Architecture-dependent code; those elements that depend on the architecture on which they run, hence must consider the architectural design for normal operation and efficiency.

Interfaces

System calls and Interrupts

Applications pass information to the kernel through system calls. A library contains functions that the applications work with. The libraries then, through the system call interface, instruct the kernel to perform a task that the application wants.  What is a Linux System Call?

Interrupts offer a way through which the Linux kernel manages the systems’ hardware. If hardware has to communicate with a system, an interrupt on the processor does the trick, and this is passed on to the Linux kernel.

Linux kernel interfaces

The Linux kernel offers various interfaces to the user space applications that perform a variety of tasks and have different properties. Two distinct Application Programming Interface (API) exist; the kernel-user space and the kernel internal.  The Linux API is the kernel-userspace API; it gives access to programs in the user space into the system resources and services of the kernel. It is made up of the System Call Interface and the subroutines from the GNU C Library.

Linux ABI

This refers to the kernel-user space ABI (Application Binary Interface). This is explained as the interface that exists between program modules. When comparing API and ABI, the difference is that ABI’s are used to access external codes that are already compiled while API are structures for managing software.  Defining an important ABI is majorly the work of Linux distributions than it is for the Linux kernel. A specific ABI should be defined for each instruction set, for example, x86-64. End-users of Linux products are interested in the ABIs rather than the API.

System Call Interface

As earlier discussed, this plays a more prominent role in the kernel. It is a denomination of the whole part of all existing system calls.

The C standard library

All the system calls of the kernel are within the GNU C Library whereas, the Linux API is comprised of the system call interface and the GNU C Library, also called glibc.

Portable Operating System Interface(POSIX)

POSIX is a collective term of standards for maintaining compatibility among the operating systems. It declares the API together with utility interfaces and command line shells. The Linux API, not only has the usable features defined by the POSIX but also has additional features in its kernel:

  1. Cgroups subsystem.
  2. The Direct Rendering Manager’s system calls.
  3. A readahead feature.
  4. Getrandom call that is present in V 3.17.
  5. System calls such as futex, epoll, splice, dnotify, fanotify and inotify.

More information about POSIX Standard is here.

The modular kernel

Previous versions of the Linux kernel were in such a way that all their parts were statically fixed into one, monolithic. However, modern Linux kernels have most of their functionality contained in modules that are put into the kernel dynamically. This in contrast to monolithic types, is referred to as modular kernels. Such a setup allows a user to load or replace modules in a running kernel without the need of rebooting.

The Linux Loadable Kernel Module (LKM)

The basic way of adding code in the Linux kernel is through the introduction of source files to the kernel source tree. However, you may want to add a code while the kernel is running. The code added this way is referred to as a loadable kernel module. These particular modules perform various tasks but are specified into three: device drivers, file system drivers and system calls.

The loadable kernel module can be compared to the kernel extensions in other operating systems. You can put a module into the kernel by either loading it as an LKM or binding it into the base kernel.

The benefits of LKMs over binding into the base kernel:

  • Rebuilding your kernel often is not necessary, saving time and avoids errors.
  • They assist in figuring out system problems such as bugs.
  • LKMs save you space as you only have them loaded when you need to use them.
  • Give much faster maintenance and debugging time.

Uses of LKMs

  1. Device drivers; the kernel exchanges information with hardware through this. A kernel must have a device’s driver before using it.
  2. Filesystem drivers; this translates the contents of a filesystem
  3. System calls; programs in the user space utilize system calls to acquire services from the kernel.
  4. Network drivers; interprets a network protocol
  5. Executable interpreters; loads and manages an executable.

Compiling the Linux Kernel

Unlike what most people say, compiling the Linux kernel is a simple task. The following is a step-by-step illustration of the process using one of the Linux distributions: Fedora 13 KDE. (It is advisable to backup your data and grub.conf just in case something goes wrong)

  1. From http://kernel.org website, download the source.
  2. While in your downloads directory, extract the kernel source from the archive by entering the following command in terminal:
    tar xvjf Linux-2.6.37.tar.bz2
  3. Use the command make mrproper to clear the build area prior to any compilation.
  4. Use a configuration say xconfig, These configurations are designed to make it easier to run any program in Linux.
  5. Specify the modules and features you wish your kernel to contain.
  6. After acquiring the .config file, the next step is to go to Makefile
  7. Run the make command and wait for the compilation to go through.
  8. Install the modules using the command make modules_install
  9. Copy your kernel and the system map to /boot.
  10. Run the new-kernel-pkg to build the list of module dependencies and stuff like grub.conf

Upgrading the kernel

It is possible to upgrade a Linux kernel from an older version to a more recent one, while retaining all of the configuration options from the earlier version. To achieve this, one has to first back up the .config file in the kernel source directory; this is in case something goes wrong when trying to upgrade your kernel. The steps are:

  1. Get the latest source code from the main kernel.org website
  2. Apply the variations to the old source tree to bring it up to the latest version.
  3. Reconfigure the kernel based on the preceding kernel configuration file you had backed up.
  4. Build the new kernel.
  5. Now you can install the new build the kernel.

Downloading the new source; the Linux kernel developers understand that some users may not want to download the full source code for the kernel updates, as this would waste time and bandwidth. Therefore, a patch is made available which can upgrade an older kernel release. Users only need to know which patch applies to a particular version, since a kernel patch file will only update the source code from one specific release. The different patch files can be applied through the following ways;

  1. Stable kernel patches which apply to the base kernel version.
  2. Base kernel release patches only apply to the previous base kernel version
  3. Incremental patch upgrade from a particular release to the next release. This allows developers avoid the hustle of downgrading then upgrading their kernel. Instead, they can switch from their current stable release to the next stable release.

Here are more detailed steps for the process to update your kernel from source on Debian, and from pre-built binaries on CentOS and Ubuntu.

Conclusion

The Linux kernel mainly acts as a resource manager acting as an abstract layer for the applications. The applications have a connection with the kernel which in turn interacts with the hardware and services the applications. Linux is a multitasking system allowing multiple processes to execute concurrently. The Linux kernel is popular due to its open source nature that allows users to alter the kernel to what is suitable for them and their hardware. Therefore it can be used in a variety of devices, unlike other operating systems.

The modular characteristic of the Linux kernel adds more thrill to its users. This is because of the wide variety of modifications that can be made here without rebooting the system. The flexibility gives its users a big room to actualize their imaginations.

Moreover, the monolithic nature of the kernel is a great advantage as it has a high processing ability than the microkernel. The main setback with the Linux type of kernel is that if any of its services fail, then the whole system goes down with it. Latest versions have been designed in a way that if a new service is added, there is no need of modifying the whole operating system. This is an improvement when compared to previous versions.

Sources

  1. Wikipedia Linux Kernel
  2. Wikipedia Linux Kernel Interfaces
  3. Linux Loadable Kernel Module How To
  4. linux.com beginners guide
  5. https://www.quora.com/What-are-good-tutorials-to-learn-Linux-Kernel
  6. https://unix.stackexchange.com/questions/1003/linux-kernel-good-beginners-tutorial
  7. http://www.linux-tutorial-tutorial.info/modules.php?name=MContent&pageid=82
  8. https://www.howtogeek.com/howto/31632//what-is-the-linux-kernel-and-what-does-it-do/
]]>