460 likes | 578 Views
Linux 操作系统 软件模块和内核安装配置. 周炯 上海艾基信息技术有限公司. 内容提要. 1 软件安装与配置 2 开机和关机 3 系统服务 4 内核管理 5 模块管理 6 核心参数. 1 软件安装与配置. 本部分将讨论两种最常用的软件安装方法:使用 Red Hat Package Manager 软件包管理工具软件( RPM )和自行编译源代码。 本部分中所有需要输入的命令都是以根用户的身份输入的。因此最好是直接以根用户身份登录进入系统。. RPM Package Manager. Manage software packages
E N D
Linux操作系统软件模块和内核安装配置 周炯 上海艾基信息技术有限公司 Acegene IT Co. Ltd.
内容提要 • 1 软件安装与配置 • 2 开机和关机 • 3 系统服务 • 4 内核管理 • 5 模块管理 • 6 核心参数 Acegene IT Co. Ltd.
1 软件安装与配置 • 本部分将讨论两种最常用的软件安装方法:使用Red Hat Package Manager软件包管理工具软件(RPM)和自行编译源代码。 • 本部分中所有需要输入的命令都是以根用户的身份输入的。因此最好是直接以根用户身份登录进入系统。 Acegene IT Co. Ltd.
RPM Package Manager • Manage software packages • Install, upgrade, remove, verify, query, build • Package files referred to as RPMs • Distributed by the vendor • Include files to be installed plus some install scripts • Source RPMs contain the source code • e.g., kernel-2.4.9-e.24.src.rpm • Binary RPMs contain the pre-built binaries • e.g., kernel-2.4.9-e.24.i686.rpm • Choose the highest architecture the machine can use • e.g., i686, i586, i486, i386 (uname -m) Acegene IT Co. Ltd.
1 Red Hat Package Manager • 一些可以获得到rpm软件的站点 • http://www.rpmfind.net • ftp://ftp.redhat.com/pub/contrib • http://www.linuxapps.com • rpm的老家 • http://www.rpm.org Acegene IT Co. Ltd.
1 安装新的软件包 • 普通安装: • #rpm -i bc-1.05a-4.i386.rpm • 升级软件: • #rpm -U bc-1.05a-4.i386.rpm • 强行安装: • #rpm -i --force -nodeps packagename.rpm • 其他参数 • -h 使用符号“#”指示安装进度,与-v参数一起使用时显示效果更好 • -v 告诉RPM报告每一步操作的情况 • --test 这个参数并不进行真正的安装;它只是用来检查安装能否成功地完成。用于检查依赖关系是否安全。 • --percent 显示已完成的百分比指示安装进度。如果从另外一个程序(比如某个Perl脚本程序)中来运行RPM,并且想了解安装的进度时,这个参数就很方便。 Acegene IT Co. Ltd.
1 查询软件包 • 列出已经安装的全部软件包 • #rpm -qa • #rpm -qa | grep -i 'name' • 找出某个特定的文件到底是属于哪个软件包的 • #rpm -qf filename • 查出某个已经安装的软件包的功能 • #rpm -qi packagename • 查出软件包中的文件 • #rpm -qlp packagename • 其中的packagename是准备要了解其组成文件的软件包名称 • 列出已经安装的软件包中的文件: • #rpm –ql <pkg_name> Acegene IT Co. Ltd.
1 反安装(清除)软件包 • RPM反安装(清除)软件包 • #rpm -e packagename • 其中的packagename是rpm -qa命令清单中列出的软件包名称。 Acegene IT Co. Ltd.
1 gnorpm工具 • 那些喜欢使用GUI工具简化操作的人们可以使用gnorpm软件。虽然它是被设计运行于GNOME环境的,但在KDE环境中也运行得相当好。它可以完成通过命令行完成的全部操作,但是用户就不必费力记住那些命令行参数了。当然,这样做是要增加一些系统开销的,这也是命令行版本依然存在的原因。 Acegene IT Co. Ltd.
RPM Builds • Builds consist of multiple phases: • Prep – extracts and patches sources • Build – make individual binaries • Install – creates an install staging area • Doesn’t install it for use on the local system! • Additional actions come after Install phase: • Create a source RPM • Create a binary RPM Acegene IT Co. Ltd.
RPM Work Area • Work area is used for building RPMs • Location depends on vendor: • Red Hat: /usr/src/redhat • UnitedLinux: /usr/src/packages • Directory structure contents: • SPECS – RPM spec file • SOURCES – source archives and patches • BUILD – extracted source, objects, binaries • RPMS – generated binary RPMs • SRPMS – generated source RPMs Acegene IT Co. Ltd.
RPM Build Procedure • First, install source RPM • Populates SPECS and SOURCES • Run build phases • Populates BUILD and creates staging area • Generate binary and/or source RPMs • Populates RPMS and SRPMS, respectively • Clean up the build area • Install the new binary RPM if desired Acegene IT Co. Ltd.
RPM Build Example • All in one go: • rpm ––rebuild stat-2.5-3.src.rpm • Step-by-step: • rpm –i stat-2.5-3.src.rpm • cd /usr/src/redhat/SPECS • rpm –bp stat.spec • rpm –bc stat.spec • rpm –bi stat.spec • rpm –bl stat.spec • rpm –ba stat.spec ––clean • rpm –i ../RPMS/i386/stat-2.5-3.i386.rpm Acegene IT Co. Ltd.
2 开机和关机 • 2.1 系统引导 • 2.2 启动文件 • 2.3 内核加载 • 2.4 init进程 Acegene IT Co. Ltd.
2.1 启动引导 - Boot Loaders • Grub • New and improved boot loader • Configured via /boot/grub/menu.lst • Not necessary to rerun after config changes • Grub Configuration default=0 timeout=10 splashimage=(hd0,0)/boot/grub/splash.xpm.gz title Red Hat Ent Linux AS (2.4.21-9.EL) root (hd0,0) kernel /boot/vmlinuz-2.4.21-9.EL root=LABEL=/ initrd /boot/initrd-2.4.21-9.EL.img • LILO • Original Linux boot loader • Configured via /etc/lilo.conf • Must run /sbin/lilo after any change to lilo.conf or binary images (/boot/*) Acegene IT Co. Ltd.
2.2 Boot Files • Kernel binary: • IA-32 (compressed): /boot/vmlinuz-$LABEL • Other architectures: /boot/vmlinux-$LABEL • initrd images: • /boot/initrd-$LABEL • System mapfile: • /boot/System.map-$LABEL By convention kernel binary, initrd and system mapfile should share the same $LABEL Acegene IT Co. Ltd.
2.2 Initrd Images • Kernel can’t mount root fs until appropriate disk drivers are loaded (except IDE) • Initrd is loaded into memory by Grub/LILO • Contains drivers needed to mount real root fs • Initrd must be rebuilt if a disk driver changes • To rebuild: • RH: Use /sbin/mkinitrd • SUSE: Use /sbin/mk_initrd Acegene IT Co. Ltd.
2.3 开机引导的步骤 • 加载内核 • 内核就必须完整地加载到可用RAM的第一个兆字节之内。为了实现这个目的,内核是被压缩了的。 • 这个文件的头部包含着必要的代码,先设置CPU进入安全模式(以此解除内存限制)再对内核的剩余部分进行解压缩。 • 执行内核 • 内核在内存中解压缩之后,它就可以开始运行了 • 一旦内核启动运行,对硬件的检测就会决定需要对哪些设备驱动程序进行初始化。 • 内核就能够挂装根文件系统,内核挂装了根文件系统之后, • 启动并运行一个叫做init的程序。 Acegene IT Co. Ltd.
Run Level Description 0 Halt 1 Single User 2 No Network 3 MultiUser 4 (Unused) 5 GUI 6 Reboot 2.4 开机引导的步骤 • init进程是所有进程的发起者和控制者。 • init是第一个运行的进程, • init的进程编号永远是1。 • 如果init出现问题,系统随之垮掉。 • init进程有两个作用: • 扮演终极父进程的角色。 • 失去了父进程的子进程就都会以init作为它们的父进程。 • 快速执行一下ps -af 命令可以列出许多父进程ID(parent process ID,PPID)为1的进程来。 • 特定运行级别( runlevel)时运行相应的程序, • init读取配置文件/etc/inittab,决定启动的运行级别(runlevel)。 • 缺省的运行级别 • 取决于配置文件 /etc/inittab id:3:initdefault: Acegene IT Co. Ltd.
2.4 开机引导的步骤 • 命令脚本程序 • 执行 rc.sysinit • 执行脚本在各个运行级别的子目录中/etc/rc{RUNLEVEL}.d • 都建立有到/etc/rc.d/init.d子目录中的符号链接。 • 但是这些符号链接并不使用命令脚本程序在/etc/rc.d/init.d子目录中原来的名字, • 如果命令脚本程序是用来启动一个服务的,其符号链接的名字就以字母S打头; • 如果命令脚本程序是用来关闭一个服务的,其符号链接的名字就以字母K打头 • 运行级别3和5中,在字母S后面使用了数字99,这就保证了这个命令脚本程序将作为引导过程的组成部分,其最后阶段被执行。 Acegene IT Co. Ltd.
2.4 编写自己的rc命令脚本程序 • 有两种方法可以用来实现修改开机或者关机命令脚本的目的: • /etc/rc.d/rc.local • 如果所做的修改只在引导开机的时候起作用,并且改动不大的话,可以考虑简单地编辑一下/etc/rc.d/rc.local脚本。 • 这个命令脚本程序是在引导过程的最后一步被执行的。 • /etc/rc.d/init.d子目录中添加一个命令脚本程序 • 适合修改比较细致,或者还要求关闭进程的操作必须使之明确地停止运行 • 这个命令脚本程序必须可以接受start和stop参数并完成相应的操作。 Acegene IT Co. Ltd.
3 系统服务管理 • 用于在激活或停用启动时的服务 • 会修改/etc/rcX.d到 /etc/init.d的(符号联接symbolic links) • 配置服务 chkconfig • chkconfig --list • chkconfig [--level 2345] [service] on|off • 服务管理 service • service [servicename] start|stop|restart Acegene IT Co. Ltd.
使用 chkconfig # See where sendmail is running chkconfig --list sendmail sendmail 0:off 1:off 2:off 3:on 4:off 5:on 6:off # Turn off sendmail at levels 3 & 5 # too many spammers out there! chkconfig --level 35 sendmail off Acegene IT Co. Ltd.
4 内核 • 4.1 核心服务 • 4.2 内核编译和安装 • 4.3 模块加载卸载 Acegene IT Co. Ltd.
4.1 核心级系统服务 • 核心系统服务:init、inetd、syslog和cron。 • 本部分将对每一个核心系统服务、相应的配置文件以及推荐的应用方式(如果有的话)逐个进行介绍。 Acegene IT Co. Ltd.
4.2 安装新内核 • Distributed by vendors in binary RPM format • Red Hat: kernel-{smp|enterprise}-*.i686.rpm • UnitedLinux: ??? • RPM contents: • /boot/vmlinuz-$LABEL • /boot/System.map-$LABEL • /lib/module/$LABEL/… • RPM script generates a new initrd-$LABEL and automatically adds a Grub entry • If using LILO instead of Grub, then add lilo.conf entry manually and re-run /sbin/lilo Acegene IT Co. Ltd.
4.2 编译或者不编译内核的原因 • 编译的原因 • 运行一个程序,但是操作系统不支持该程序需要的功能。 • 某个用户需要的内核功能—比如说用户的机器中装有1GB到2GB容量之间的RAM系统内存,只能重新编译经过改动的内核才能够激活它。 • 用户希望对内核进行优化。 • 内核升级(驱动、BUG、意愿、功能、安全、有时间进行升级) • 为什么不编译 • 如果只是想给内核加上很少数量的功能的话,就没有必要进行编译 • 编译内核可能会遇到的困难:面对众多的选项 Acegene IT Co. Ltd.
4.2 编译(或升级)内核 • 确定内核版本 • uname –r • 确定内核是测试版还是正式版 • 按照下面的方法确定内核是测试版还是正式版: • 1) 忽略句号”.”前的第一个数字。 • 2) 查看紧跟在句号”.”后面的第二个数字。 • 在实验性版本中,紧跟在第一个句号后面的第一个数字是一个奇数。 • 在成熟版本中,紧跟在第一个句号后面的第一个数字是一个偶数。 • 举例来说,内核版本2.2.5-15是成熟版本,而版本2.3.4-12是实验性版本 Acegene IT Co. Ltd.
4.2 为内核源代码打补丁 • 内核经常需要小规模地升级。每次升级修复一个特定的bug,增加一个新的驱动程序或者功能,或者实现其他任务。 • 1) 下载主版本的源代码和升级到新版本需要的每一个补丁的源代码。 • 2) 使用c d命令把路径切换到/usr/src目录。 • 3) 使用l s命令列出目录中的内容清单。如果其中有一个/usr/src/linux-version-ext目录已经存在的话,一般情况这就是正在已经运行的内核源代码。 • 4) 输入“mkdir linux-newversion-ext”命令建立一个新的源代码目录。 • 5) 把下载的源代码和补丁都拷贝到这个目录中。 • 6) 使用tar命令对源代码进行解包操作。 • 7) 每一个补丁都应该分别并按顺序来处理。使用tar命令对第一个补丁进行解包操作。 • 8) 使用patch命令把这个补丁打到源代码中去。输入“patch -p0 original <patchname”命令。 • 9) 依次打补丁 • 10) 准备编译 • 注意:在源代码中有时候会包括有一个名为kernel-patch的命令脚本程序,该程序的功能是按顺序打所有的补丁。 Acegene IT Co. Ltd.
4.2 编译内核 • 1) 获取需要安装的内核源代码。 • 2) 使用tar命令把源代码解包到/usr/src/linux-version-ext目录 • 3) 使用cd命令把路径切换到这个目录。 • 4) 选择你想使用的内核配置工具程序: • 图形化功能最少,又很乏味的工具是config程序 • 一个图形化的工具是menuconfig程序 • 用于对内核进行配置的X工具总是最流行的,它叫做xconfig • Make [config|menuconfig|xconfig] • 保存并退出内核配置程序之后,就已经有了用来配置内核设置值了。 • 5) 输入“make dep”命令建立依赖关系表,它定义了包括在内核源代码中的某个特定文件如果发生了变化,就需要修改哪一个文件。 • 6) 输入“make clean”命令删除安装过程中产生的大量临时文件。 • 7) 输入“make bzImage”命令完成建立新内核的过程。 • 8) 输入“make modules”命令编译内核模块并把它们存放到相应的位置。这是那些需要加载或者卸载的内核模块,另外还有一些固定加载的。 • 9) 安装内核 Acegene IT Co. Ltd.
4.2 安装内核 • 简单(自动)方法: • make • install • make • module_install • reboot Acegene IT Co. Ltd.
4.2 安装内核 • 手工方法: • 1) 使用ls命令列出/boot目录中的内容。 • 2) 备份/boot/vmlinuz文件。改名为vmlinuz-version-ext。 • 3)拷贝编译过程中生成的bzImage文件到/boot目录中。/usr/src/LINUX_PATH/arch/i386/boot/目录中找到。 • 4) 把/boot目录中的bzImage文件改名为“vmlinuz”。 • 5) 把/usr/src/linux/System.map文件拷贝到/boot目录。 • 6) 把路径切换回/usr/src/LINUX_PATH目录。 • 7) “make module_install”命令把内核模块存放到相应的位置。 • 8) 如果SCSI硬盘要确保SCSI模块能够被及时加载。输入“mkinitrd /boot/initrd-version-ext version-ext”命令确保SCSI模块在引导过程中能够被及时加载。 • 9) 拷贝initrd-version.img到/boot目录,输入“mkinitrd /boot/initrd-version.img version-ext”命令建立一个RAM盘,它将在开机引导启动过程中加载,然后再从机器中被删除 • 10) 配置LILO Acegene IT Co. Ltd.
4.2 使用RPM包升级内核的步骤 • 从Red Hat公司的Web升级站点下载内核软件包,按照下面的方法进行操作: • 1) 以根用户身份登录进入系统。 • 2) 使用rpm命令升级下载的非内核文件,输入“rpm -Uvh packagename”命令。警告千万要在手边准备好一张引导启动盘以防新的内核出现问题。 • 3) 为确保不出现问题,最好安装一个新内核,而不是简单的升级。安装内核文件需要输入“rpm -ivh packagename”命令。 • 4) 从/boot目录中查看当前内核的名称是什么。格式应该是“/boot/vmlinuz-version-ext” • 5) 输入“mkinitrd /boot/initrd-version.img version-ext”命令建立一个RAM盘,它将在开机引导启动过程中加载,然后再从机器中被删除。 • 7) 配置LILO Acegene IT Co. Ltd.
4.2 配置LILO • 1) 以根用户身份登录进入系统。 • 2) 使用“vi /etc/lilo.conf”之类的命令打开/etc/lilo.conf文件。 • 3) 除了被升级的部分,文件中新的数据段必须和用户当前运行的旧版本的有关内容保持一致。举例来说,用户原来的数据可能是如下所示的样子: • image = /boot/vmlinuz-2.2.5-15 • label =linux • root = /dev/hda1 • initrd = /boot/initrd-2.2.5 • 4) 把光标移动到以“image”字样开头的那一行。 • 5) 输入“4yy”拷贝4行文本到缓冲区。 • 6) 按下“G”键前进到文件末尾。 • 7) 按下“o”键在文件末尾插入一个新的文本行。 • 8) 按下ESC键返回到命令模式。 • 9) 按下“p”键把拷贝的文本复制到/etc/lilo.conf文件的末尾。 • 10) 在新的语句中做必要的修改。比如说,把2.2.5-15修改为2.2.5-25等等。initrd项的数据根据版本之间差异的大小可以修改也可以不改。 • 11) 再回到原始数据段部分,把它的label项改为“linuxold”之类的东西,这样用户就可以把这两个数据段区分开了。 • 12) 输入“:wq”保存文件并退出编辑器程序。 • 13) 输入“/sbin/lilo -v”命令执行LILO程序,让新设置生效。 • 14) 重新启动 Acegene IT Co. Ltd.
5 加载核心模块 • 安装模块: • insmod <module name>.o • modprobe <module name> • 列出已经安装的模块: • lsmod • cat /proc/modules • 每一个已经安装的内核版本都应该有一个对应的目录。把路径切换到当前运行内核对应的目录中。 Acegene IT Co. Ltd.
5 卸载模块 • 卸载模块 • rmmod <module name> • 只对idle和不再使用的模块起作用 • 如果机器警告有其他的模块依赖于想要删除的这个,但你还是想删除它的话,可以使用 • rmmod -r modulename Acegene IT Co. Ltd.
5 检查坏的模块(Tainted Kernel) • Use the /sbin/lsmod command to see whether the kernel is tainted: # /sbin/lsmod Module Size Used by Not tainted nfs 87936 0 (autoclean) lockd 60224 0 (autoclean) [nfs] sunrpc 79952 0 (autoclean) [nfs lockd] iptable_filter 2912 0 (autoclean) (unused) ip_tables 14080 1 [iptable_filter] ad1848 23968 0 [cs4232] ext3 70240 5 jbd 51004 5 [ext3]
6 核心参数 • Control dynamic kernel configuration / tuning • Most parameters can be changed on the fly! • Can be set multiple ways: • In /etc/sysctl.conf: (Recommended) • fs.aio-max-size=1048576 • In /etc/rc.local (RH) or /etc/boot.local (SUSE): • echo 1048576 > /proc/sys/fs/aio-max-size • Using sysctl: • sysctl –w fs.aio-max-size=1048576 • Will be lost on reboot if not in /etc/sysctl.conf Acegene IT Co. Ltd.
6 shmfs • Mount tmpfs on /dev/shm via fstab • Option “size=8g” means 8GB • Option “size=??” can be larger than physical RAM • Required by Oracle for VLM mode • Only used for buffer cache • Never used in non-VLM mode Acegene IT Co. Ltd.
6 IA-32系统上使用超过4g内存 • Bigpages (x86) • Separate memory pool using large hw pages • Non-swappable • Must be set aside at boot time • Boot with kernel parameter “bigpages=8192MB” • Use Workaround in bug 3080838 • To prevent Kernel panic in sshd_config set: • UsePrivilegeSeparation no -OR- • Compression no (preferred) • Hugetlbfs (RHEL) • Similar to bigpages but, configured at runtime, no need to reboot: • Shutdown instance (free memory) • echo 600 > /proc/sys/vm/hugetlb_pool • Ask for 600MB • cat /proc/sys/vm/hugetlb_pool • Verify you got 600MB, if not might need to reboot • Startup instance • Verify usage in /proc/meminfo Acegene IT Co. Ltd.
6 参数: 异步IO - async io requests • /proc/sys/fs/aio-max-size • Max size of data passed to driver at once • Requests larger than this will be broken up • Next chunk will not be submitted until the previous one has completed • Default value: 131072 • Unit: bytes • /proc/sys/fs/aio-max-nr • Max number of aio requests system-wide • Limits number of structures allocated, not the number of requests in flight! • Each Oracle process allocates 1024 • Default value: 65536 • Unit: count Acegene IT Co. Ltd.
6 参数: 共享内存 • /proc/sys/kernel/shmmax • Max size of a single shared memory segment • Smaller values mean more segments needed • Default value: 33554432 • Unit: bytes • Maximum allowed: 4GB - 1 (4294967295) • /proc/sys/kernel/shmall • Max amount of shared memory system-wide • Default: 2097152 • Unit: ? • /proc/sys/kernel/shm-use-bigpages • Allowed values: • 0: don’t use bigpages pool for shared memory • 1: use bigpages pool for SysV shared memory • 2: use bigpages pool for SysV and shmfs • Default value: 0 • Use Workaround in bug 3080838 • To prevent Kernel panic in sshd_config set: • UsePrivilegeSeparation no -OR- • Compresson no (preferred) Acegene IT Co. Ltd.
6 RHEL Kernel Types (IA-32) • Choose the right kernel • “smp”: 2-level page tables • HW can only address 4GB of RAM • 4kB pages and 4MB “large” pages • “enterprise”: 3-level page tables • HW can address up to 64GB of RAM • 4kB pages and 2MB “large” pages • RHEL2.1: max 16GB RAM supported • SLES8: 64GB officially supported Acegene IT Co. Ltd.
Default After Relink 0xFFFFFFFF 0xFFFFFFFF Reserved for kernel Reservedfor kernel 0xC0000000 0xC0000000 Variable SGA Variable SGA DB Buffers (SGA) DB Buffers (SGA) sga_base (relink Oracle) 0x50000000 mapped_base (/proc/<pid>/mapped_base) 0x40000000 Code, etc. 0x15000000 0x10000000 Code, etc. 0x00000000 0x00000000 6 进程地址空间 (IA-32) • 3GB user/1GB kernel address space • 0x00000000-0xbfffffff user • 0xc0000000-0xffffffff kernel • Stack grows down from 0xbfffffff • mapped_base • Address where .so libraries are loaded • Default value: 0x40000000 • Configure via /proc/$PID/mapped_base Acegene IT Co. Ltd.
Q & Q U E S T I O N S A N S W E R S A
练习 • 为虚拟机安装vmware-tools软件 • 安装 • 确认是否安装 • 卸载 • 配置系统服务 • 启动服务: • rsh rexec rlogin telnet sshd vsftpd rawdevices • 禁用服务: • cups sendmail smartd apmd • chkconfig service • 配置自动启动服务 • /etc/rc.d/rc.local, • 启动时自动授权oracle用户文件权限 • /dev/rawctl • /dev/raw/* • chown chmod • 设置Linux核心参数 • 编辑 /etc/sysctl.conf • 参数生效 #sysctl -p /etc/sysctl.conf net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.ip_local_port_range = 1024 65000 net.core.rmem_default = 262144 net.core.rmem_max = 262144 net.core.wmem_default = 262144 net.core.wmem_max = 262144 kernel.sysrq = 0 kernel.core_uses_pid = 1 kernel.shmmax = 1610612736 kernel.shmmni = 4096 kernel.shmall = 2097152 kernel.msgmax = 8192 kernel.msgmnb = 65535 kernel.msgmni = 2878 kernel.sem = 500 64000 100 512 #fs.aio-max-size =1048576 fs.file-max = 327680 fs.aio-max-nr = 1048576 Acegene IT Co. Ltd.