linux系统启动、grub、内核

centos5和6的启动流程:

         linux:kernel+rootfs

         kernel:进程管理、内存管理、网络管理、驱动程序、文件系统、安全功能

         rootfs:

glibc

         库:函数集合,function,调用接口

                  过程调用:procedure

                   函数调用:function

         程序:

         内核设计流派:

                   单内核设计:linux

                            把所有功能集成于同一个程序

                   微内核设计:windows,solaris

                            每种功能使用一个单独子系统实现

         linux内核特点:

                   支持模块化:.ko

                   支持模块的动态装载和卸载

                   组成部分:

                            核心文件:/boot/vmlinuz-version-release

                                     ramdisk:

                                              centos5:/boot/initrd-verison-release.img

                                               centos6:/boot/initramfs-version-release.img

                            模块文件:/lib/modules/version-release

centos系统启动流程:

         POST:加电自检:

                   ROM:CMOS

                            BIOS:Basic Inputand output system

                            Rom+Ram

         BOOTsequence:

                   按次序查找各引导设备,第一个有引导程序的设备即为本次启动用到的设备

                   bootloader:引导加载器,程序

                            windows:ntloader

                            linux:

                                     lilo:linux loader

                                     grub:granduniform bootloader

                                               grub0.x:grub legacy

                                               grub1.x:grub2

                            功能:提供一个菜单,允许用户选择要启动系统的不同内核版本,把用户选定的内核装载到内存中的特定空间中、展开、并把系统控制权移交给内核。

                   MBR:

                            446:bootloader

                            64:fat

                            2:55AA

                   GRUB:

                            bootloader:1st stage

                            disk:2nd stage

                   kernel:

                            自身初始化:

                                     探测可识别到的所有硬件设备

                                     加载硬件驱动程序(有可能会借助ramdisk加载驱动)

                                     以只读方式挂载跟文件系统

                                     运行用户空间的第一个应用程序:/sbin/init

                                     init程序的类型:

                                     syssv:init,centos 5

                                               配置文件:/etc/inittab

                                     upstart:init centos6

                                               配置文件:/etc/inittab,  /etc/init/*.conf

                                     systemd:systemd,centos7

                                               配置文件:/usr/lib/system/system,/etc/system/system

                            ramdisk:

                                     内核中的特性之一:使用缓冲和缓存来回事对磁盘上的文件访问

                                              ramdisk-->ramfs

                                               centos5:initrd,工具程序:mkinitrd

                                               centos6:initramfs 工具程序:mkinitrd,dracut

         系统初始化:

                   POST-->bootsquence(bios)-->bootloader(MBR)-->kernel(ramdisk)-->rootfs(只读)-->init或systemd

 

 

 

/sbin/init

         centos5:

                   运行级别:

                            为了系统的正常运行或维护等应用目的而设定

                            0-6:7个级别

                                     0:关机

                                     1:单用户模式(root,无需登录),single,维护模式

                                     2:多用户模式,回启动网络功能,但不会启动NFS,维护模式

                                     3:多用户模式:正常模式,文本界面

                                     4:预留级别:可同3级别

                                     5:多用户模式,正常模式,图形界面

                                     6:重启

                            默认级别:

                                     3,5

                            切换级别:

                                     init#

                            查看级别:

                                     runlevel

                                     who–r

         配置文件:/etc/inittab

                   每一行定义一种action异机与之对应的process

                            id:runlevel:action:process

                                     action:

                                               wait:切换至此级别运行一次

                                               resawn:此process终止,就重新启动

                                               initdefault:设定默认运行级别,process省略

                                               sysinit:设定系统初始化方式,此处一般指定/etc/rc.d/rc.sysinit

                            id:3 :initdefault:

                            si::sysinit:/etc/rc.d/rc.sysinit

                            l0:0:wait:/etc/rc.d/rc0

                            l1:1:wait:/etc/rc.d/rc1

                            …

                            l6:6:wait:/etc/rc.d/rc6

                            说明:rc 0-->意味着读取/etc/rc.d/rc0.d/

                                     K*:K##*##运行次序:数字越小,越先运行,数字越小的服务,通常为依赖到别的服务

                                     S*:S##*:##运行次序,数字越小,越先运行,数字越小的服务,通常为被依赖到的服务

                                     forsrv in /etc/rc.d/rc0.d/K*; do

                                               $srvstop

                                     done

                                     forsrv in /etc/rc.d/rc0.d/S*; do

                                               $srvstart

                                     done

                            chkconfig命令:

                                     查看服务在所有几倍的启动或关闭设定情形

                                     chkconfig[--list] [--type type] [name]

                                     添加:

                                               sysv的服务脚本放置于/etc/rc.d/init.d(/etc/init.d)

                                               chkconfig–add name

                                     删除:

                                               chkconfig–del name

                                     修改指定的链接类型:

                                            chkconfig[--level levels] name

                                            --level LLLL:指定要设置的级别,省略时表示2345

                        注意:正产级别下,最后启动一个服务s99local没有链接至/etx/rc.d/init.d一个服务脚本,而是指向                                                   了/etc/rc.d/rc.local脚本,因此,不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时                                     自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中

                                   tty1:2345:respawn:/usr/sbin/mingettytty1

                                   tty2:2345:respawn:/usr/sbin/mingetty tty2

                                    …

 

                                   tty6:2345:respawn:/usr/sbin/mingetty tty6

                            mingetty会调用login程序

             /etc/rc.d/rc.sysinit:系统初始化脚本

            (1)      设置主机名

            (2)      设置欢迎信息

            (3)      激活udev和selinux

            (4)      挂载/etc/fstab文件中定义的文件系统

            (5)      检测跟文件系统,并以读写方式重新挂载跟文件系统

            (6)      设置系统时钟

            (7)      激活swap设备

            (8)      根据/etc/sysctl.conf文件设置内核参数

            (9)      激活lvm及software raid设备

            (10)  加载额外设备的驱动程序

            (11)  清理操作

        总结:

            /sbin/init-->(/etc/inittab)-->设置默认运行级别-->运行系统初始化脚本、完成系统初始化-->关闭对应下需要关闭的               服务,启动需要启动的服务-->设置终端登录

        centos 6:

         init程序为:upstart,其配置文件:

                   /etc/inittab,/etc/init/*.conf

                   注意:/etc/init/*.conf文件语法 遵循 upstart配置文件语法格式

GRUB(bootloader):

         grub:grandunified bootloader

                   grub0.x:grub legacy

                   grub1.x:grub2

         grublegacy:

                   stage1:mbr

                    stage1_5:mbr之后的扇区,让stage1中的bootloader能识别stages所在的分区上的文件系统

                   stage2:磁盘分区(/boot/brub/)

                   配置文件:/boot/grub/brub.conf<--/et/grub.conf

                   stage2及内核等通常放置于一个基本磁盘分区

                            功用:

                            (1)      提供菜单、并提供交互式接口

                                    e:编辑模式,用户编辑菜单

                                    c:命令模式,交互式接口

                            (2)      加载用户选择的内核或操作系统

                                    允许传递参数给内核

                                    可隐藏此菜单

                            (3)      为菜单提供保护机制

                                    为编辑菜单进行认证

                                    为启用内核或操作系统进行认证

                    如何识别设备:

                         (hd#,#)第几块磁盘的第几块分区

                           hd#:磁盘编号,用数字表示,从0开始编号

                           #:分区编号,用数字表示,从0开始编号

                           (hd0,0)

    grub的命令行接口

         help:获取帮助列表

         helpkeyword:详细帮助信息

         find(hd#,#)/path/to/somefile:

         root(hd#,#):把哪个磁盘设为根目录

         kernel/path/to/kernel_file:设定本次启动时用到的内核文件

                   例如:init=/apth/to/init,selinux=0

         initrd/path/to/initramds_file:设定为选定的内核提供额外文件的ramdisk

         boot:引导启动选定的内核

         手动在grub命令行接口启动系统:

                   grub>root(hd#,#)

                   grub>kernel/vmlinuz-VERSION-RELEASE ro root=/dev/DEVICE

                   grub>initrd/initramfs-VERSION-RELEASE.img

    grub>boot

    配置文件:/boot/grub/grub.conf

         配置项:

                   default=#:设定默认启动的菜单项,落单项(title)编号从0开始

                   timeout=#:设定菜单项等待选项选择的时长

                   splashimage=(hd#,#)/path/to/xpm_pic_file:指明菜单北京图片文件路径

                   hiddenmenu:隐藏菜单项

                   password[--md5] string:菜单编辑认证

                   titletitle:定义菜单项”标题”,可出现多次

                            root(hd#,#):grub查找stage2及kernel文件所在的设备分区,为grub的“跟”

                            kernel/path/to/vmlinuz_file [parameters]:启动的内核

                            initrd/path/to/initrmfs_file:内核匹配的ramfs文件

         grub-md5-crypt命令:

            进入单用户模式:

            (1)      编辑grub菜单(选定要编辑的title,而后使用e命令)

            (2)      在选定的kernel后附加

                    1,s,S或single都可以

            (3)      在kernel所在行,键入“b”命令

        安装grub:

        (1)      grub-install

            grub-install --root-directory=ROOT /dev/disk

        (2)      grub

            grub>root (hd#,#)

            grub>setup (hd#)

linux kernel:

         但内核体系设计、但充分借鉴了微内核设计体系的优点,为内核引入模块化机制

                   内核组成部分:

                            kernel:内核核心,一般为bzImage,通常放置在/boot目录下,名称为vmlinuz

                            kernelobject:内核对象,一般放置在/lib/modules/version=release/

                                     [ ]:N

                                     [M]:M

                                     [*]:Y

                            辅助文件:ramdisk

                                     initrd

                                     initramfs

                   运行中的内核:

                            uname命令:

                                     uname- print system information

                                     uname[OPTION]...

                                     -n:显示节点名称

                                     -r:显示verison-release

                            模块:

                                lsmod命令:

                                     显示由核心已经装载的内核模块

                                     显示的内容来自于:/proc/modules文件

                                              lsmod - Show the status of modules inthe Linux Kernel

                                      modinfo:

                                               显示模块的详细描述信息

                                              modinfo  [-k kernel] [modulename|filename...]

                                               -n:只显示模块文件路径

                                               -p:显示模块参数

                                               -a:author

                                               -d:description

                                               -l:license

                                     modprobe命令:

                                               装载或卸载内核模块

                                               modprobe[-C config-file] [modulename] [module parameters...]

                                                        配置文件:/etc/modprobe.conf,/etc/podprobe.d/*.conf

                                               modprobe[-r] [modulename...]

                                     dpmod命令:

                                               内核模块依赖关系及系统信息映射文件的生成工具

                                     装载或卸载内核模块

                                               insmod命令:

                                                        insmod[filename] [module options...]

                                               rmmod命令:

                                                        rmmod[-f] [-s] [-v] [modulename]

                   /proc目录:

                             内核把自己的内部状态信息及统计信息,异机可配置参数通过proc伪文件系统加以输出

                   参数:

                            只读:输出信息

                            可写:可接受用户指定“新值”来实现对内核某功能或特性的配置

                                     /proc/sys

                            (1)sysctl命令用户查看或省此目录中诸多参数

                                     sysctl–w path.to.parameter=value

                            (2)echo命令通过重定向的方式也可以修改大多数参数的值

                                     echo“value” >path/to/parameter

                                     ~]#echo www.magedu.com >/proc/sys/kernel/hostname

         sysctl命令:

                   配置文件:/etc/sysctl.conf

                            (1)sysctl –wparameter=value

                            (2)通过读取配置文件设置参数

                                     sysctl–p [/path/to/conf_file]

                           

         内核中的路由转发功能:

                   /proc/sys/net/ipv4/ip_forward

                   常用的几个参数:

                            net.ipv4.ip_forward

                            vm.drop_caches

                            kernel.hostname

    /sys目录:

                sysfs:输出内核识别出的各硬件设备的相关属性信息,也有内核对硬件特性的设定信息,有些参数是可以修改单,用                  于调整硬件工作特性

                    udev通过此路径下输出的信息动态为各设备创建所需设备文件:dev时运行用户空间程序:专用工具:                                     udevadmin,hotplug

                    udev为设备创建设备文件时,会读取其事先定义好的规则文件,一般在/etc/udev/rules.d及/usr/lib/rules.d目录                        下

        ramdisk文件的制作:

        (1)      mkinitrd命令

                为当前正在使用的内核重新制作rmdisk文件

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

            (2)      dracut命令

                    为当前正在使用的内核重新制作rmdisk文件

                        ~]# dracut  /boot/initramfs-$(uname -r).img $(uname -r)

        编译内核:

         前提:

        (1)      贮备好开发环境

        (2)      获取目标主机上硬件设备的相关信息

        (3)      获取到目标主机系统功能的相关信息,例如要启用的文件系统

        (4)      获取内核源代码包

准备好开发环境:

         包组(centos 6):

                   server patform development

                   development tools

         目标主机硬件设备相关信息:

                   cpu:

                            cat /proc/cpuinfo

                            lscpu

                            x86info–a

                   PCI设备:

                            lspci

                                     -v

                                     -vv

                            lsusb

                                     -v

                                     -vv

                            lsblk

                   了解全部硬件设备信息

                            ~]#hal-device

         简单依据模板文件的制作过程:

                   ~]#tar xf linux-3.10.67.tar.xz –C /usr/src

                   ~]#cd /usr/src

                   ~]#l –sv ;inux-3.10.67 linux

                   ~]#cd linux

                   ~]#cp /boot/config-$(uname -r)  ./config

                   ~]#make menuconfig

                   ~]#screen

                   ~]#make –j #

                   ~]#make modiles_install

                   ~]#make install

                   重启系统,并测试使用新内核

                    ~]# init 6

                            


 

 


你可能感兴趣的:(linux)