Linux 内核是一种开源的类 Unix 操作系统宏内核。
Linux 内核是 Linux 操作系统的主要组件,也是计算机硬件与其进程之间的核心接口。它负责两者之间的通信,还要尽可能高效地管理资源。之所以称为内核,是因为它在操作系统中就像果实硬壳中的种子一样,并且控制着硬件的所有主要功能。内核的用途主要有以下 4 项工作:
在正确实施的情况下,内核对于用户是不可见的,它在自己的小世界(称为内核空间)中工作,并从中分配内存和跟踪所有内容的存储位置。用户所看到的内容则被称为用户空间。这些应用通过系统调用接口(SCI)与内核进行交互。
Linux系统内核概述
单内核体系设计、但充分借鉴了微内核设计体系的优点,为内核引入模块化机制。
Linux 内核的重要组成部分,主要有以下几部分:
使用格式
参数解释
- # uname -m
- i686
-
- # uname -r
- 2.6.32-573.22.1.el6.i686
-
- # uname -a
- Linux MyServer 2.6.32-573.22.1.el6.i686 ... i686 i386 GNU/Linux
显示由核心已经装载的内核模块
命令定义
- # cat /proc/modules
- iptable_filter 2173 0 - Live 0xed9b2000
- ip_tables 9567 1 iptable_filter, Live 0xed9a9000
- ext3 203718 1 - Live 0xed962000
- jbd 65315 1 ext3, Live 0xed904000
- xenfs 4360 1 - Live 0xed8e6000
- ipv6 271097 14 - Live 0xed88e000
- xen_netfront 15871 0 - Live 0xed7d9000
- ext4 339812 2 - Live 0xed764000
- jbd2 75927 1 ext4, Live 0xed6d9000
- mbcache 6017 2 ext3,ext4, Live 0xed6b7000
- xen_blkfront 19209 5 - Live 0xed69f000
- dm_mirror 11969 0 - Live 0xed68d000
- dm_region_hash 9644 1 dm_mirror, Live 0xed67e000
- dm_log 8322 2 dm_mirror,dm_region_hash, Live 0xed672000
- dm_mod 84711 11 dm_mirror,dm_log, Live 0xed64e000
- # lsmod | grep ext4
- ext4 339812 2
- jbd2 75927 1 ext4
- mbcache 6017 2 ext3,ext4
字段含义
- # lsmod
- Module Size Used by
- iptable_filter 2173 0
- ip_tables 9567 1 iptable_filter
- ext3 203718 1
- jbd 65315 1 ext3
- xenfs 4360 1
- ipv6 271097 14
- xen_netfront 15871 0
- ext4 339812 2
- jbd2 75927 1 ext4
- mbcache 6017 2 ext3,ext4
- xen_blkfront 19209 5
- dm_mirror 11969 0
- dm_region_hash 9644 1 dm_mirror
- dm_log 8322 2 dm_mirror,dm_region_hash
- dm_mod 84711 11 dm_mirror,dm_log
显示模块的详细描述信息
命令定义
语法
选项
实战演示
- # modinfo ext4
- filename: /lib/modules/2.6.32-573.22.1.el6.i686/kernel/fs/ext4/ext4.ko
- license: GPL
- description: Fourth Extended Filesystem
- author: Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore and others
- srcversion: CB1B990F5A758DFB0FB12F1
- depends: mbcache,jbd2
- vermagic: 2.6.32-573.22.1.el6.i686 SMP mod_unload modversions 686
-
- # modinfo btrfs
- filename: /lib/modules/2.6.32-573.22.1.el6.i686/kernel/fs/btrfs/btrfs.ko
- license: GPL
- alias: devname:btrfs-control
- alias: char-major-10-234
- srcversion: B412C18B0F5BF7F1B3C941A
- depends: libcrc32c,zlib_deflate,lzo_compress,lzo_decompress
- vermagic: 2.6.32-573.22.1.el6.i686 SMP mod_unload modversions 686
装载或卸载内核模块
命令定义
bash
- # modules.dep为解决依赖的配置文件,modules.dep.bin二进制文件运行
- # ls /lib/modules/2.6.32-358.6.1.el6.i686/
- build modules.block modules.ieee1394map modules.ofmap modules.symbols.bin weak-updates
- extra modules.ccwmap modules.inputmap modules.order modules.usbmap
- kernel modules.dep modules.isapnpmap modules.pcimap source
- modules.alias modules.dep.bin modules.modesetting modules.seriomap updates
- modules.alias.bin modules.drm modules.networking modules.symbols vdso
语法
选项
内核模块依赖关系文件及系统信息映射文件的生成工具
语法
参数
装载或卸载内核模块
不解决依赖关系,需要自己手动卸载
insmod命令
rmmod命令
内核把自己内部状态信息及统计信息,以及可配置参数通过 proc 伪文件系统加以输出。
- # ls /proc/
- 1 1173 22 29855 35 47 60 973 filesystems loadavg scsi version
- 10 12 23 3 36 48 600 buddyinfo fs locks self vmallocinfo
- 1071 13 232 30 37 49 61 bus interrupts mdstat slabinfo vmstat
- 1082 14 234 31 38 5 62 cgroups iomem meminfo softirqs xen
- 1085 15 24 31314 39 528 7 cmdline ioports misc stat zoneinfo
- 11 16 25 317 4 531 739 cpuinfo irq modules swaps
- 1150 17 252 318 40 543 8 crypto kallsyms mounts sys
- 1162 18 253 32 41 56 808 devices kcore mtd sysrq-trigger
- 1163 19 26 320 42 566 830 diskstats keys net sysvipc
- 1165 1908 27 33 43 567 853 dma key-users pagetypeinfo timer_list
- 1167 2 28 330 44 57 9 driver kmsg partitions timer_stats
- 1169 20 29 334 45 59 94 execdomains kpagecount sched_debug tty
- 1171 21 29853 34 46 6 95 fb kpageflags schedstat uptime
语法格式
命令参数
默认配置文件
命令使用方式
参数说明
两种修改方式
配置文件中常用的几个参数
- # cat /etc/sysctl.conf
- # Kernel sysctl configuration file for Red Hat Linux
-
- # Controls IP packet forwarding
- net.ipv4.ip_forward = 0
-
- # Controls source route verification
- net.ipv4.conf.default.rp_filter = 1
-
- # Do not accept source routing
- net.ipv4.conf.default.accept_source_route = 0
-
- # Controls the System Request debugging functionality of the kernel
- kernel.sysrq = 0
-
- # Controls whether core dumps will append the PID to the core filename.
- # Useful for debugging multi-threaded applications.
- kernel.core_uses_pid = 1
-
- # Controls the use of TCP syncookies
- net.ipv4.tcp_syncookies = 1
-
- # Disable netfilter on bridges.
- net.bridge.bridge-nf-call-ip6tables = 0
- net.bridge.bridge-nf-call-iptables = 0
- net.bridge.bridge-nf-call-arptables = 0
-
- # Controls the default maxmimum size of a mesage queue
- kernel.msgmnb = 65536
-
- # Controls the maximum size of a message, in bytes
- kernel.msgmax = 65536
-
- # Controls the maximum shared segment size, in bytes
- kernel.shmmax = 4294967295
-
- # Controls the maximum number of shared memory segments, in pages
- kernel.shmall = 268435456
- # Auto-enabled by xs-tools:install.sh
- net.ipv4.conf.all.arp_notify = 1
- # 查看所有可读变量
- sysctl -a
-
- # 修改对应参数
- sysctl -w kernel.sysrq=0
- sysctl -w kernel.core_uses_pid=1
- sysctl -w net.ipv4.conf.default.accept_redirects=0
-
- # 如果希望屏蔽别人 ping 你的主机,配置文件修改
- net.ipv4.icmp_echo_ignore_all = 1
-
- # 编辑完成后,请执行以下命令使变动立即生效
- /sbin/sysctl -p
- /sbin/sysctl -w net.ipv4.route.flush=1
sysfs 伪文件系统,输出内核识别出的各硬件设备的相关属性信息,也有内核对硬件特性的设定信息。有些参数是可以修改的,用于调整硬件工作特性。
点击这里查看 ==> Arch Wiki udev
方法一
- # 移动ramdisk文件到/root目录下
- mv /boot/initramfs-2.6.32...img /root
-
- # 为当前正在使用的内核重新制作ramdisk文件
- mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)
方法二
- # 移动ramdisk文件到/root目录下
- mv /boot/initramfs-2.6.32...img /root
-
- # 为当前正在使用的内核重新制作ramdisk文件
- dracut /boot/initramfs-$(uname -r).img $(uname -r)
- # 使用file命令查看ramdisk文件发现是以gz压缩存放的
- file /boot/initramfs-2.6.32-504.el6.x86_64.img
-
- # 改名称,解压
- cd /boot/
- mv initramfs-2.6.32-504.el6.x86_64.img initramfs-2.6.32-504.el6.x86_64.img.gz
- gzip -d initramfs-2.6.32-504.el6.x86_64.img.gz
-
- # 使用file命令查看发现是以cpio存放的文本文件
- file initramfs-2.6.32-504.el6.x86_64.img
-
- # 解压这个文本文件
- # 之后会在initrd目录下生成相应的文件,一个微型的/root
- mkdir initrd
- cd initrd
- cpio -id < ../initramfs-2.6.32-504.el6.x86_64.img
-
- # 这个时候就可以查看init脚本文件了
- cat init
-
- # 在sbin文件中存放着相关的命令
- ls sbin
(1) 准备好开发环境
(2) 获取目标主机上硬件设备的相关信息
(3) 获取到目标主机系统功能的相关信息
(4) 获取内核源代码包
简易安装
简单依据模板文件的制作内核
- # 下载对应的Linux内核版本进行解压缩
- # 会在/usr/src目录下创建debug、kernels和linux-3.10.67目录
- tar xf linux-3.10.67.tar.xz -C /usr/src
-
- # 为了方便多内核共存,使用连接指向
- # 会在当前目录下创建一个链接文件 linux -> linux-3.10.67
- cd /usr/src
- ln -sv linux-3.10.67 linux
-
- # 创建模板
- cd linux
- # 查看链接指向的文件内容
- ls
- # 拷贝系统自带的模板文件
- cp /boot/config-$(uname -r) .config
-
- # 打开图形界面配置内核选项,选择添加、删除内核模块
- # 添加的默认选项来自.config配置文件
- make menuconfig
-
- # 使用screen来不中断安装
- screen
- # 采用几个线程进行编译
- make -j n
-
- # 安装内核
- make modules_install
- # make install中将会安装内容
- # 安装bzImage为/boot/vmlinuz-VERSION-RELEASE
- # 生成initramfs文件
- # 编辑grub的配置文件
- make install
-
- # 重启系统,并测试使用新内核,不是默认启动内核
- init 6
(1) 配置内核选项
(2) 编译 - make [-j #]
- # (a)只编译某子目录中的相关代码
- cd /usr/src/linux
- make dir/
-
- # (b)只编译一个特定的模块
- cd /usr/src/linux
- make dir/file.ko
-
- # 例如:只为e1000编译驱动
- make drivers/net/ethernet/intel/e1000/e1000.ko
- # 编译的目标平台与当前平台不相同;
- make ARCH=arch_name
-
- # 要获取特定目标平台的使用帮助
- make ARCH=arch_name help
- # 事先清理操作
-
- # 清理大多数编译生成的文件,但会保留config文件等
- make clean
-
- # 清理所有编译生成的文件、config及某些备份文件
- make mrproper
-
- # mrproper、patches以及编辑器备份文件
- make distclean
文章作者: Escape
文章链接:
https://www.escapelife.site/posts/3acaa44c.html