內核及模塊管理基礎
查詢程序的依賴庫
ldd命令
ldd [OPTION]…FILE…
[root@centos6 ~]# ldd /bin/ls linux-vdso.so.1 => (0x00007ffcef1ee000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00000034d0200000) librt.so.1 => /lib64/librt.so.1 (0x00000034cf200000) libcap.so.2 => /lib64/libcap.so.2 (0x00000034d5600000) libacl.so.1 => /lib64/libacl.so.1 (0x00000034d9600000) libc.so.6 => /lib64/libc.so.6 (0x00000034cea00000) libdl.so.2 => /lib64/libdl.so.2 (0x00000034ce600000) /lib64/ld-linux-x86-64.so.2 (0x00000034ce200000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00000034cee00000) libattr.so.1 => /lib64/libattr.so.1 (0x00000034d9200000)
取出庫的路徑
[root@centos6 ~]# ldd /bin/ls |grep -o "/lib[^[:space:]]*" /lib64/libselinux.so.1 /lib64/librt.so.1 /lib64/libcap.so.2 /lib64/libacl.so.1 /lib64/libc.so.6 /lib64/libdl.so.2 /lib64/ld-linux-x86-64.so.2 /lib64/libpthread.so.0 /lib64/libattr.so.1
內核設計體系:單內核,微內核
linux:單內核設計,充分借鑒了微內核體系的設計優點為內核引入了模塊化機制 內核的組成部分 kernel:內核核心,一般為bzimage,通常位于/boot/目錄下,名稱vmlinuz-VERSION-release kernel object:內核對象 即內核模塊,一般放置于/lib/modules/VSERSION-release/ 內核模塊與內核核心版本一定要嚴格匹配。 [ ]:N [M]:Module [*]:Y,編譯進內核核心,只有所有人都會用到的功能才會編譯進核心 內核:支持動態裝載和卸載 ramdisk:是個輔助性文件,并非必須的,這取決于內核是否能直接驅動rootfs所在的設備 目標設備驅動,例如SCSI設備驅動; 邏輯設備驅動,例如LVM設備驅動 文件系統,例如xfs文件系統 ramdisk:是一個簡裝版的根文件系統
內核信息的查看
uname 命令
-a, --all print all information, in the following order, except omit -p and -i if unknown: -s, --kernel-name print the kernel name -n, --nodename ###主機名與hostmane效果是一樣的 print the network node hostname -r, --kernel-release print the kernel release -v, --kernel-version ###指的是編譯版本 print the kernel version -m, --machine print the machine hardware name -p, --processor print the processor type or "unknown" -i, --hardware-platform print the hardware platform or "unknown" -o, --operating-system print the operating system
-v, –kernel-version ###指的是編譯版本
-n, –nodename ###主機名與hostmane效果是一樣的
-r, –kernel-release ###發行版本號
-a, 顯示所有信息
模塊信息獲取
lsmod
program to show the status of modules in the Linux Kernel
lsmod is a trivial program which nicely formats the contents of the /proc/modules, showing what kernel modules are currently loaded.
[root@centos6 ~]# cat /proc/modules
rfcomm 71079 4 – Live 0xffffffffa0498000
sco 17493 2 – Live 0xffffffffa048e000
bridge 85674 0 – Live 0xffffffffa0470000
bnep 16370 2 – Live 0xffffffffa0468000
…
………
查詢模塊詳細信息
modinfo
modinfo – program to show information about a Linux Kernel module
[root@centos7 ~]# modinfo ext4 filename: /lib/modules/3.10.0-327.el7.x86_64/kernel/fs/ext4/ext4.ko license: GPL description: Fourth Extended Filesystem author: Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others alias: fs-ext4 alias: ext3 alias: fs-ext3 alias: ext2 alias: fs-ext2 rhelversion: 7.2 srcversion: DB48BDADD011DE28724EB21 depends: mbcache,jbd2 ##依賴關系 intree: Y vermagic: 3.10.0-327.el7.x86_64 SMP mod_unload modversions signer: CentOS Linux kernel signing key sig_key: 79:AD:88:6A:11:3C:A0:22:35:26:33:6C:0F:82:5B:8A:94:29:6A:B3 sig_hashalgo: sha256
modinfo 通過讀取/lib/modules/VERSION目錄下的文件,獲取模塊信息,依賴關系,映射表,別名等等。通過讀取這些元數據文件加以顯示。類似rpm包的元數據。
默認只顯示當前系統運行的內核的模塊信息。可以-k加以指定。
-F 顯示指定字段的內容
詳細內容左側一列為指定字段。filename、depends….
/lib/modules/VERSION下的普通文件都是兩份文件名差不多的。
元數據是文本格式,經過哈希算法變成bin(數據庫格式的一種),加快讀取速度。
動態裝載或卸載模塊
modprobe
modprobe – program to add and remove modules from the Linux Kernel
modprobe [-r] module name
帶-r 是卸載模塊
不帶-r是加載模塊
注意:對于正在使用的模塊不要卸載。一般不要卸載模塊,除非明確知道自己要干什么。
生成模塊依賴關系映射表
depmod
depmod – program to generate modules.dep and map files.
內核模塊依賴關系文件,及系統信息映射文件的生成工具。
模塊的裝在和卸載的另一組命令
insmod命令:
insmod - simple program to insert a module into the Linux Kernel insmod [filename] [module options...] filename:模塊的文件路徑 模塊間存在依賴關系,有可能加載不上,modprobe類似yum。insmod類似rpm
rmmod命令:
移除模塊,只需要指明模塊名稱即可
rmmod [moduname]
ramdisk文件的管理
1、mkinitrd centos5使用 為當前正在使用的內核重新制作ramdisk文件 mkinitrd [OPTION...] [<initrd-image>] <kernel-version> (ex: mkinitrd /boot/initramfs-2.6.32-642.el6.x86_64.img 2.6.32-642.el6.x86_64) [root@centos6 ~]# mkinitrd /boot/initramfs-$(uname -r).img $(uname -r) --with=<module> add the kernel module <module> to the initramfs. 除了默認的模塊之外需要裝在至initramfs中 --preload=<module> initramfs所提供的模塊需要預先加載的模塊。 preload the kernel module <module> in the initramfs before any other kernel modules are loaded. This can be used to ensure a certain device naming, which should in theory be avoided and the use of symbolic links in /dev is encouraged. 2、dracut dracut - low-level tool for generating an initramfs image dracut [OPTION...] [<image> [<kernel version>]] [root@centos6 ~]# dracut /boot/initramfs-$(uname -r).img $(uname -r) centos6、7使用 centos6、7也能使用initrd
內核信息輸出的偽文件系統
系統性能調優 主要調整 /proc/sys /sys
/proc:內核狀態及統計信息的輸出接口;同時還提供了一個配置接口,/proc/sys
參數: 只讀:信息輸出;列入/proc/數字命名的目錄下的文件 可寫:可接受用戶指定一個“新值”來實習那對內核某功能或特性的配置;/proc/sys 僅是管理員才有寫權限。
修改內核參數proc
能修改的文件,但是不能使用vim修改。(因為是偽文件系統)只能使用重定向。 1、sysctl sysctl - configure kernel parameters at runtime sysctl variable ... 查詢 sysctl -w variable=value ... 修改 sysctl -a 查詢所有 sysctl -p 默認讀取/etc/sysct.conf,也可指定某個文件(自己創建的) /proc/sys/net/ipv4/ip_forward [root@centos6 net]# sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 0 [root@centos6 net]# sysctl -w net.ipv4.ip_forward=1 net.ipv4.ip_forward = 1 注意:net.ipv4.ip_forward net/ipv4/ip_forward 2、文件系統命令,cat,echo cat /proc/sys/*/*.. echo /porc/sys/*/*.. [root@centos6 net]# cat /proc/sys/net/ipv4/ip_forward 1
配置文件proc
上述兩種方式的設定僅 當前運行內核有效;
配置文件
centos6
/etc/sysctl.conf
[root@centos6 net]# cat /etc/sysctl.conf # Kernel sysctl configuration file for Red Hat Linux # # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and # sysctl.conf(5) for more details. # # Use '/sbin/sysctl -a' to list all possible parameters. centos7 /etc/sysctl.d/*.conf [root@centos7 ~]# cat /etc/sysctl.conf 用戶在此 自己修改的設置 # System default settings live in /usr/lib/sysctl.d/00-system.conf. # To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file # # For more information, see sysctl.conf(5) and sysctl.d(5). [root@centos7 ~]# cat /lib/sysctl.d/00-system.conf 系統默認設置 # Kernel sysctl configuration file # # For binary values, 0 is disabled, 1 is enabled. See sysctl(8) and # sysctl.conf(5) for more details. # 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 [root@centos7 ~]# cat /lib/sysctl.d/50-default.conf (關于systemd的默認設置) # This file is part of systemd. # # systemd is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation; either version 2.1 of the License, or # (at your option) any later version. # See sysctl.d(5) and core(5) for for documentation. # To override settings in this file, create a local file in /etc # (e.g. /etc/sysctl.d/90-override.conf), and put any assignments # there. # System Request functionality of the kernel (SYNC) # # Use kernel.sysrq = 1 to allow all keys. # See http://fedoraproject.org/wiki/QA/Sysrq for a list of values and keys. kernel.sysrq = 16
修改配置文件,使用sysctl讀取配置文件,并寫入內核參數。
token = value EXAMPLE # sysctl.conf sample # kernel.domainname = example.com ; this one has a space which will be written to the sysctl! kernel.modprobe = /sbin/mod probe 注意:等號兩邊的空格
常用參數
net.ipv4.ip_forward 核心ip轉發 vm.drop.caches 清緩存 kernel.hostname 主機名 net.ipv4.icmp_echo_ignore_all 是否響應ping
/sys 主要與硬件相關。
/sys sysfs:輸出內核識別出的個硬件設備的相關屬性信息,也有內核對硬件特性的可設置參數;對此參數修改,可定制硬件設備的工作特性。 udev:通過讀取/sys/目錄下的硬件信息,按需為各硬件設備創建設備文件。 專用工具:devadmin,hotplug udev為設備創建文件時,會讀取其事先定義好的規則文件,一般在/etc/udev/rules.d目錄下,以及/usr/lib/rules.d目錄下。
注意:內核在啟動過程中,會探測硬件設備,并將其以sysfs形式輸出,系統啟動以后,udev才可以讀取其參數,并跟據需要創建設備文件。(udev是用戶空間程序)
例如有兩個網卡,想要調換其設備名稱
可以修改/etc/udev/rules.d/70-persistent-net.rules 文件
[root@centos6 ~]# cat /etc/udev/rules.d/70-persistent-net.rules # This file was automatically generated by the /lib/udev/write_net_rules # program, run by the persistent-net-generator.rules rules file. # # You can modify it, as long as you keep each rule on a single # line, and change only the value of the NAME= key. # PCI device 0x8086:0x100f (e1000) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:83:fa:77", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" # PCI device 0x8086:0x100f (e1000) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:83:fa:8b", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
只需要將NAME修改即可,再將/etc/sysconfig/network-script/ifcfg-eth* 配置文件修改。
將網卡模塊卸載再裝載就可以了。
原創文章,作者:yyw,如若轉載,請注明出處:http://www.www58058.com/47814