Introduction to Linux kernel

Time:2020-6-19

Kernel system design: single kernel, micro kernel

Windows is a microkernel design.

Linux is a single kernel design, but it fully draws on the advantages of microkernel system and introduces modularization mechanism for kernel.

Components of the kernel

  • Kernel: kernel core, usually BZ compressed image file, usually located in / boot directory, named vmlinuz-version-release

  • Kernel object: the kernel object, which is the kernel module, is generally placed in the directory / lib / modules / version release

    The version numbers of kernel and kernel object must be exactly the same before they can be used normally.

    []: do not compile, do not use

    [M] : compiled into module, not into kernel

    [*]: directly compiled into kernel

  • Ramdisk: auxiliary file, not necessary. It depends on whether the kernel can directly drive the device where rootfs is located.

    In it are:

    • Target device drivers, such as SCSI device drivers;
    • Logical device drivers, such as LVM device drivers
    • File systems, such as XFS

    It is a simple version of the root file system

Command to get kernel version information: uname

  • Location of kernel file / boot / vmlinuz- VERSION.release

  • Get the release number of the kernel:

    # uname -r
    2.6.32-754.el6.x86_64

    Get host name:

    # uname -n
    c610
    # hostname
    c610

Get which modules the kernel has loaded: lsmod

The information displayed is from the / proc / modules file

Module: module name

Size: size

Used by: which module has been used and the number of times it has been referenced

# lsmod
Module                  Size  Used by
xt_CHECKSUM            12549  1
ipt_MASQUERADE         12678  3
nf_nat_masquerade_ipv4    13412  1 ipt_MASQUERADE
tun                    31740  1

Get module details: modinfo name

Information can be obtained even if the module is not loaded, similar torpm -qlCommand.

# modinfo xfs
filename:       /lib/modules/3.10.0-957.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
license:        GPL
description:    SGI XFS with ACLs, security attributes, no debug enabled
author:         Silicon Graphics, Inc.
alias:          fs-xfs
retpoline:      Y
rhelversion:    7.6
srcversion:     799C7EBA4C499822FD1E465
depends:        libcrc32c
intree:         Y
vermagic:       3.10.0-957.el7.x86_64 SMP mod_unload modversions
signer:         CentOS Linux kernel signing key
sig_key:        B7:0D:CF:0D:F2:D9:B7:F2:91:59:24:82:49:FD:6F:E8:7B:78:14:27
sig_hashalgo:   sha256

Common options:

  • Specify the fields to display: – f filed name

    # modinfo -F filename xfs
    /lib/modules/3.10.0-957.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
  • Display the path of the module file: – n

    # modinfo -n xfs
    /lib/modules/3.10.0-957.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
  • If there are multiple kernels, display the information of the specified kernel: – K kernel

Load / unload module: modprobe

  • Loading module:modprobe mode_name

    # modprobe btrfs
  • Uninstall module:modprobe -r mode_name

    # modprobe -r btrfs

Another set of commands to load / unload modules:

  • Loading module:insmod file_name

    Use the module file path when loading.

    # insmod /lib/modules/3.10.0-957.el7.x86_64/kernel/fs/xfs/xfs.ko.xz
    # insmod `modinfo -n xfs`

    Insmod is similar to rpm and cannot automatically resolve dependencies. When the module you are loading depends on has not been loaded, the loading fails. Error message: unknown symbol in module. Modeprobe can automatically resolve dependencies.

  • Uninstall module:rmmod mode_name

    When uninstalling, specify the name of the module.

    # rmmod xfs

Generate module dependency file:depmod

Management of ramdisk files

Accidentally put initramfs-3.10.0-957.el7.x86 under the / boot directory_ 64. If the IMG file is deleted, the system will not be able to start next time, so you need to make the ramdisk file manually.

Command to create a ramdisk file:

centos5,6,7:mkinitrd

# mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)

/Boot / initramfs – $(uname – R). Img: Specifies the filename of the ramdisk.

$(uname – R): Specifies the release number of the kernel

Common options:

  • In addition to the default module, install other modules into the ramdisk: — with=
  • Pre loaded modules before the kernel loads the modules in the ramdisk: — preload=

centos6,7:dracut

Kernel parameter management

The function of modifying kernel parameters: if the parameters of kernel are modified, the behavior of kernel will change.

The parameter management of kernel is managed by pseudo file system, because everything is file, and the kernel parameter is also abstracted into file.

1、 The directory / proc is the pseudo file system where the kernel parameters are stored

  • Therefore, to view the value of the kernel parameter, you can use cat and other commands to view the corresponding files in the / proc directory.
  • So change the value of the kernel parameter, that is, use echo to redirect the corresponding file in the / proc directory.
  • There are also special commands to view and modify kernel parameters.

View the parameters of the currently running kernel

To view all parameters of the kernel:sysctl -a

  • Use the special command to view:

    # sysctl abi.vsyscall32
    abi.vsyscall32 = 1
  • Use the command of the file system to view:

    # cat /proc/sys/abi/vsyscall32
    1

It can be seen that the parameters are separated by dots. If you change the dots to / you can find the path of the file where this parameter is stored.

For example, parameters net.ipv4 .ip_ The path of the forward file is: / proc / sys / net / IPv4 / IP_ forward

parameter abi.vsyscall32 The path of the corresponding file is: / proc / sys / ABI / vsyscall32

So all the kernel parameters are stored in the directory / proc / sys /.

# ll /proc/sys
dr-xr-xr-x 0 root root 0 Feb 23 07:34 abi
dr-xr-xr-x 0 root root 0 Feb 23 07:31 crypto
dr-xr-xr-x 0 root root 0 Feb 23 07:34 debug
dr-xr-xr-x 0 root root 0 Feb 23 07:34 dev
dr-xr-xr-x 0 root root 0 Feb 23 07:31 fs
dr-xr-xr-x 0 root root 0 Feb 23 07:31 kernel
dr-xr-xr-x 0 root root 0 Feb 23 07:31 net
dr-xr-xr-x 0 root root 0 Feb 23 07:31 vm

Modify the parameters of the currently running kernel

  • Modify with special command:

    # sysctl -w net.ipv4.ip_forward=1
    net.ipv4.ip_forward = 1
    # sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 1
  • Use the command of the file system to modify:

    # echo 0 > /proc/sys/net/ipv4/ip_forward
    # sysctl net.ipv4.ip_forward
    net.ipv4.ip_forward = 0
  • The above modification is only valid at the current time, and the restart is invalid. So the profile comes on the scene

  • Modify the configuration file to make the kernel parameters permanent.

    • Centos6: modify / etc/ sysctl.conf

      After modifying the document, it cannot take effect immediately. To take effect immediately, use:sysctl -p [filename]

      After – P is selected, the file path will not be activated, that is, the file / etc will be loaded/ sysctl.conf Documents.

      Modify / etc/ sysctl.conf File:
      net.ipv4.ip_forward = 1
      # cat /proc/sys/net/ipv4/ip_forward
      0
      [[email protected] ~]# sysctl -p
      net.ipv4.ip_forward = 1
      [[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward
      1
    • Centos7: modify the / etc / sysctl. D / *. Conf file

      # emacs -nw /etc/sysctl.d/test1.conf
      net.ipv4.ip_forward = 0
      # cat /proc/sys/net/ipv4/ip_forward
      1
      # sysctl -p /etc/sysctl.d/test1.conf
      net.ipv4.ip_forward = 0
      # cat /proc/sys/net/ipv4/ip_forward
      0
  • Kernel parameters: net.ipv4 .icmp_ echo_ ignore_ All

    • 0: I respond to the Ping request sent by others, so others can ping me
    • 1: I don’t respond to the Ping request sent to me by others, so others can’t ping me, but I can still Ping others.

2、 Directory / sys is also a pseudo file system

  • Input the attribute information of each hardware device recognized by the kernel, as well as the parameters of the kernel control hardware. Modify this parameter to customize the working characteristics of the hardware device.

  • Udev creates a driver (device file: / dev / xxx) for the hardware by reading the hardware device information in the / sys directory. Udev is a user space program, the tool isdevadminhotplug(hot plug)

    Kernel doesn’t know what hardware users add after power on, so it can’t create hardware drivers on demand. Therefore, the way of kernel 2.4 before was to put all the known hardware device driver files in / dev directory. In this way, one is to waste space, the other is that users cannot judge which hardware devices are on the current system through the files in the / dev directory. /The appearance of sys directory can make the device appear in / dev directory on demand.

  • When udev creates a driver for the device, it reads the pre-defined rule file

    • Centos7: usually in the directory / etc / udev / rules. D /, and / usr / lib / udev / rules. D /.
    • Centos6: usually in / etc / udev / rules. D /

QQ group of mutual learning in C / C + +: 877684253

Introduction to Linux kernel

My wechat: xiaoshitou5854

Recommended Today

What knowledge points have I gained by using vue3.0 (1)

The front-end development is full of flowers, and one skill is not yet mature.At a loss, I don’t know what to do. I pay close attention to the small editor’s success. I feel very busy recently. I don’t have much time to write articles. Today’s article is mainly about my early studyVue3.0Some of the notes […]