linux内核模块管理,内核及模块管理

内核及模块管理基础

查询程序的依赖库

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...] [] 

(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=   add the kernel module  to the initramfs.

除了默认的模块之外需要装在至initramfs中

--preload=   initramfs所提供的模块需要预先加载的模块。

preload the kernel 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...] [ []]

[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/.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.178linux.com/47814

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值