./configure:

  检查编译环境:编译工具、库、头文件

  设定编译安装选项

  结果:结果Makefile.in生成makefile

make

make install

日志系统

Linux系统启动流程

PC: OS(Linux)

系统简单启动流程:

首先加电自检,CPU本身什么活也不会干,它要做的工作就是执行用户事先编排好的指令,指令到到内存中取,对于一个计算机最核心的部件就这两个,刚开机那一个内存中什么都没有,因为内存是易失性存储器,这个时候系统必要要完成自举的过程,必须要能有一个硬件,完成这点,硬件里面有一个固化指令,不然就没办法让系统能够运行起来,所以在硬件设计最初它就能够自动在开机的时候在CPU的指挥下将某个特殊的硬件设备的内容装载进内存,这个不需要任何额外指令,是硬件自己内置的,而这个过程就是将我们系统自检所需要用到的指令,将BIOS那些程序映射进CPU可以查找的线性地址空间,或者叫物理地址空间,设备有内存(RAM)、ROM,ROM这个芯片不需要加电,就能够实现数据保存的,CPU在刚开机的时候就能够实现将ROM芯片中的某些程序给它映射仅RAM中的地址空间里面,或者可以理解为将ROM和RAM拼接起来,使CPU同时能够找到那段内存空间,由此CPU就可以读取到ROM当中所配置好的这些指令信息,这些指令恰恰第一段主要就是完成系统硬件健康状况检查的,有没有显卡,显卡是否正常,包括有没有内存设备,内存是否正常,CPU风扇是否能够继续运转等等,都可以做检查,如果检测通过就没有问题,根据BIOS中的配置信息,BIOS当中定义了设备的启动次序(Boot Sequence),启动次序设定外部I/O设备或者存储设备,那一个作为默认找操作系统的,或系统所在的存储设备的,操作系统安装的位置有很多,比如;U盘、硬盘、光盘都可以,设置还可以通过网络启动操作系统,因此Boot Sequence决定了到那去找操作系统所在的位置,如果没有次序,或多个都有,势必会导致混乱的,因此必须得有次序,按照BIOS中所找到的次序自上而下,以此去找那个启动设备的MBR,MBR中需要有一段程序bootloader,它主要用于完成根据MBR当中的分区表查找活动分区,找到操作系统所在的分区,并加载分区上的操作系统的内核,因此当BIOS找到对应设备的MBR以后,或者找到bootloader以后,接下来所有的控制权都交给bootloader,尤其可见,如果第一个设备上没有MBR,或者MBR中没有bootloader,于是就按BIOS中的Boot Sequence次序找第二个设备,如果第一个设备有bootloader,但bootloader损坏了,它无法正常启动,不会找第二个启动设备,bootloader空间比较小,所以这些程序很可能是压缩存放的,BIOS将它载入进来以后,有可能还要解压缩,它支持不支持这种功能,要看BIOS本身有没有这种特性,bootloader获取控制权以后,BIOS就退出了,于是bootloader装载对应的,所选定的磁盘分区上操作系统的内核,内核一般也是压缩存放的,此时把内核装载入内存,在内存中完成解压缩,而后将控制权转交给内核,这时内核就取得了整体硬件的控制器,这时候bootloader也就退出了,Kernel此时获得了整个系统的控制权,Kernel获得控制权以后要识别每一个硬件设备,而且让每个硬件能够工作起来,驱动程序能使硬件更好的工作,此时内核在完成解压缩以后,它必要要尝试探测每一个硬件,看这个硬件的特性是什么,是那种型号的,有没有驱动,能不能让它正常工作起来,更重要的是Kernel必须要能够识别CPU、内存,当探测硬件结束之后,把硬件也加载完成了,接下来启动init进程,当init装载进来以后,接下来所有的控制权,init是用户空间的主导程序,所有在用户空间所启用的进程,都由init负责管理,因此对于用户空间的管理则交由init来完成,除非需要和硬件打交道,才从用户空间切换到内核空间。

POST-->BIOS(Boot Sequence)-->MBR(bootloader,446)-->Kernel-->initrd-->(ROOTFS)/sbin/init(/etc/inittab)

问题:

内核启动之间文件系统装载没有,没有装载,内核只是在探测各种硬件,那么内核被从那个地方装载进来的,从某一个磁盘分区上,而这个磁盘分区是由谁识别的分区,bootloader来识别这个分区,所以bootloader必须要能够识别内核所在的分区,这是最基本前提。

RedHat,SUSE

核心:动态加载 内核模块

内核:/lib/modules/"内核版本号命名的目录"/

vmlinuz-2.6.32:内核名称

/lib/modules/2.6.32/

内核设计风格:

  单内核:Linux 轻量级进程(LWP),把所有功能都做进内核,像文件系统、进程管理、内存管理,全部由一个内核管理,内核靠一个进程来完成所有的功能;

核心:ko(kernel object)

so(share object)

  微内核:Windows,Solaris(线程),内核很小,只是一个核心,它把各种外围功能,像文件系统、驱动程序等等都做成它的子系统,需要用到那个子系统,由内核去负责调度这个子系统,把这个子系统装载进来让它能够工作;

文件系统

进程管理

内存管理

网络管理

安全功能

驱动程序

chroot:chroot /PATH/TO/ [COMMAND...] 根切换

chroot /test/virroot /bin/bash

 

ldd /PATH/TO/BINARY_FILE:显示二进制文件所依赖的共享库

nash:红帽5完成根切换的shell

  switch_root:系统完成根切换命令,小根是由内核装载入内存中的文件,但这个文件可以在内存中展开,而且可以把内存当磁盘来用;

将小根上以下的三个目录搬运到真正的根文件系统

/proc

/sys

/dev

ramdisk-->initrd:RedHat5,把内存模拟成磁盘

ramfs-->initramfs:RedHat6

内核不能直接访问/sbin/init文件

内核访问根需要两个东西:

1、设备驱动

2、设备上文件系统

启动的服务不同:

  运行级别:0-6

    0:halt 关机

    1: single user mode, 单用户模式 直接以管理员身份切入,不需要密码 或使用s,S,single都表示1级别

    2:multi user mode, no NFS 多用户模式 有网络功能,不启用NFS(网络文件系统)

    3: multi user mode, text mode 正常多用户模式,文本模式,不启动图形界面

    4:reserved 保留级别

    5: multi user mode, graphic mode 正常多用户模式,图形终端模式

    6: reboot 重启

详解启动过程

  bootloader(MBR)

    LILO: LInux LOader (linux的bootloader,不能引导1024分区柱面以后的内核,不支持大硬盘)

    GRUB: GRand Unified Bootloader (统一的bootloader 红帽5和红帽6的GRUB不一样,GRUB两阶段程序)

      Stage1: MBR (第一阶段不是引导操作系统,而是引导第二阶段)

      Stage1_5: (用于识别常见不同类型文件系统)

      Stage2: /boot/grub/ (第二阶段才是引导操作系统)

bootloader是个程序,对于不同的操作系统是不一样的,这个程序是由操作系统提供,操作系统的安装程序提供,bootloader是位于MBR当中的,MBR不属于任何一个操作系统,它属于全硬盘级别的,如果硬盘安装多个操作系统,比如在C盘安装一个windows系统,让bootloader引导C盘的windows就可以了,但是如果C盘安装了windows,又找一个分区安装linux,以两个系统谁的引导程序为主,如果能引导windows,linux启动不了,如果引导linux,windows启动不了,如果有一种bootloader两种都可以引导,让用户进行选择就比较好,但是微软只允许引导自己的,不引导别人的,它只允许引导windows,所以先安装linux,又安装windows,它把MBR就覆盖掉了,更恶毒的是windows 8以后,如果系统安装windows 8,微软将会锁定MBR,也就是装了windows 8以后,MBR里面的程序替换不了,也就是装了windows 8以后想并存其他linux是不可能实现的,如果想并存只能将MBR清掉,这时windows就引导不了了,windows拒绝帮别人引导操作系统,也不允许别人引导它。

grub.conf

default=0 (全局属性定义) # 设定默认启动的title的编号,从0开始

timeout=5 # 等待用户选择的超时时长,单位是秒

splashimage=(hd0,0)/grub/splash.xpm.gz # grub的背景图片

hiddenmenu # 隐藏菜单

password redhat 给grub加密码,必须在title上面

password --md5 $1$HKXJ51$B9Z8A.X//XA.AtzU1.KuG. 使用md5加密对grub加密码,同样必须在title上面

title (指定操作系统或内核,要启动不同的内核,每一个内核给一个title,每一个title是用于定义当前系统的不同版本的内核,或者当前主机上不同操作系统) CentOS (2.6.32-431.11.2.el6.x86_64) # 内核标题,或操作系统名称,字符串,可自由修改

  root (hd0,0) # 内核文件所在的设备;对grub而言,所有类型硬盘一律hd,格式为(hd#,N);hd#, #表示第几个磁盘;最后的N表示对应磁盘的分区;

  kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet (# 内核文件路径,及传递给内核的参数 (为什么内核文件在/根下,但是内核文件在/boot下,因为/boot是一个独立的分区,在内核还没有装载,文件系统不会装载,意味着grub在访问这个磁盘分区的时候,不会通过/boot目录访问,/boot目录在根上,对应的分区如果不通过/根访问,那/boot就找不到,如果通过真正的文件系统访问就是/boot/vmlinux*文件,问题是现在文件系统还没装载,直接通过硬盘访问vmlinux*,它是直接在这个磁盘上,如果把磁盘本身当作/根,因为此时访问不了其他分区,对这个磁盘来讲/vmlinux*就是/根,因为访问入口不同,)

  initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img (# ramdisk/ramfs文件路径 (小linux,操作系统安装最后一步生成的,是一个完整类型的Linux,除了内核,各种文件都有,initrd也在/boot目录下,initrd的版本号一定要和内核版本号保持一致,后缀是img说明是ram磁盘文件,镜像文件,可以展开以后当磁盘使用,类似dd命令创建虚拟SWAP设备,本地回环设备,假如/boot没有独立分区,/boot就在/根上,内核就是/boot目录下,要找这个文件,内核在/boot/vmlinux*,因为它就是根文件系统系统,而且/根下的确有/boot目录)

title (指定操作系统或内核) CentOS (2.6.32-431.el6.x86_64)

  password --md5 $1$HKXJ51$B9Z8A.X//XA.AtzU1.KuG. 启动指定内核操作系统需要输入密码

title Install Red Hat Enterprise Linux 5

  root (hd0,0)

  kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD

crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet

  initrd /initramfs-2.6.32-431.el6.x86_64.img

  password --md5 $1$FSUEU/$uhUUc8USBK5QAXc.BfW4m.

[root@Smoke ~]# which init(查看init命令二进制程序路径)
/sbin/init
[root@Smoke ~]# cd /lib/modules/(切换到内核模块目录)
[root@Smoke modules]# ls(查看当前目录下文件及子目录)
2.6.32-431.11.2.el6.x86_64  2.6.32-431.el6.x86_64
[root@Smoke modules]# cd 2.6.32-431.11.2.el6.x86_64/(进入内核版本的目录)
[root@Smoke 2.6.32-431.11.2.el6.x86_64]# ls
build          modules.alias.bin  modules.dep.bin      modules.isapnpmap    modules.order     modules.symbols.bin  vdso
extra          modules.block      modules.drm          modules.modesetting  modules.pcimap    modules.usbmap       weak-updates
kernel         modules.ccwmap     modules.ieee1394map  modules.networking   modules.seriomap  source
modules.alias  modules.dep        modules.inputmap     modules.ofmap        modules.symbols   updates
[root@Smoke 2.6.32-431.11.2.el6.x86_64]# cd kernel/(且到kernel目录)
[root@Smoke kernel]# ls(查看当前目录文件及子目录)
arch(linux平台相关,驱动CPU)  crypto(加密相关)  drivers(驱动)  fs(文件系统)  kernel(内核自身额外其他功能)  lib(库文件)  mm(内存管理)  net(网络)  sound(声卡)
[root@Smoke kernel]# ls drivers/(查看drivers目录文件及子目录)
acpi        block      crypto    firmware  hwmon       input  memstick  mtd      platform   rtc      target  video
ata         bluetooth  dca       gpio      i2c         isdn   message   net(网络驱动)      power      scsi     uio     virtio
atm         cdrom      dma       gpu       idle        leds   mfd       parport  pps        serial   usb     watchdog
auxdisplay  char       edac      hid       ieee802154  md     misc      pci      ptp        ssb      uwb     xen
bcma        cpufreq    firewire  hv        infiniband  media  mmc       pcmcia   regulator  staging  vhost
[root@Smoke kernel]# ls drivers/net/(查看drivers/net目录文件及子目录)
3c59x.ko     bnx2.ko     e1000         ipg.ko      ne2k-pci.ko     ppp_mppe.ko     sfc            sunhme.ko        vmxnet3
8139cp.ko    bnx2x       e1000e        ixgb        netconsole.ko   pppoe.ko        sis190.ko      tehuti.ko        vxge
8139too.ko   bonding     e100.ko       ixgbe       netxen          pppol2tp.ko     sis900.ko      tg3.ko           vxlan.ko
8390.ko      can         enic          ixgbevf     niu.ko          pppox.ko        skge.ko        tlan.ko          wan
acenic.ko    cassini.ko  epic100.ko    jme.ko      ns83820.ko      ppp_synctty.ko  sky2.ko        tulip            wimax
amd8111e.ko  chelsio     ethoc.ko      macvlan.ko  pch_gbe         qla3xxx.ko      slhc.ko        tun.ko           wireless
atl1c        cnic.ko     fealnx.ko     macvtap.ko  pcmcia          qlcnic          slip.ko        typhoon.ko       xen-netfront.ko
atl1e        cxgb3       forcedeth.ko  mdio.ko     pcnet32.ko      qlge            smsc9420.ko    usb
atlx         cxgb4       hyperv        mii.ko      phy             r6040.ko        starfire.ko    veth.ko
b44.ko       dl2k.ko     ifb.ko        mlx4        ppp_async.ko    r8169.ko        sundance.ko    via-rhine.ko
benet        dnet.ko     igb           myri10ge    ppp_deflate.ko  s2io.ko         sungem.ko      via-velocity.ko
bna          dummy.ko    igbvf         natsemi.ko  ppp_generic.ko  sc92031.ko      sungem_phy.ko  virtio_net.ko
[root@Smoke kernel]# du -sh /lib/modules/2.6.32-431.11.2.el6.x86_64/(显示2.6.32-431.11.2.el6.x86_64/目录大小,-s,--summarize仅显示总计,-h,
--human-readable以K,M,G为单位)
104M        /lib/modules/2.6.32-431.11.2.el6.x86_64/
[root@Smoke kernel]# ls -lh /boot/vmlinuz-2.6.32-431.11.2.el6.x86_64(查看vmlinuz-2.6.32-431.11.2.el6.x86_64,-l目录的详细信息,-h并做单位换算)
-rwxr-xr-x. 1 root root 4.0M Mar 26  2014 /boot/vmlinuz-2.6.32-431.11.2.el6.x86_64
[root@Smoke kernel]# mkdir -pv /test/virroot(创建/test/virroot目录,-p递归创建,-v显示创建过程)
mkdir: created directory `/test'
mkdir: created directory `/test/virroot'
[root@Smoke kernel]# chroot /test/virroot/(切换根路径)
chroot: failed to run command `/bin/bash': No such file or directory
提示:无法切换,因为缺少bash二进制程序和bash依赖的库文件
[root@Smoke kernel]# mkdir /test/virroot/bin(创建/test/virroot/bin目录)
[root@Smoke kernel]# cp /bin/bash /test/virroot/bin/(复制/bin/bash到/test/virroot/bin目录)
[root@Smoke kernel]# chroot /test/virroot/(切换根路径)
chroot: failed to run command `/bin/bash': No such file or directory
提示:无法切换,因为缺少bash二进制程序和bash依赖的库文件
[root@Smoke kernel]# ls /test/virroot/bin/(查看/test/virroot/bin/目录文件及子目录)
bash
[root@Smoke kernel]# ldd /bin/bash(查看bash文件依赖的库)
       linux-vdso.so.1(库文件访问入口) =>  (0x00007fffc07ff000)
       libtinfo.so.5(依赖的库) => /lib64/libtinfo.so.5 (0x0000003703a00000)
       libdl.so.2 => /lib64/libdl.so.2 (0x00000035c8c00000)
       libc.so.6 => /lib64/libc.so.6 (0x00000035c9000000)
       /lib64/ld-linux-x86-64.so.2 (0x00000035c8800000)
[root@Smoke kernel]# mkdir /test/virroot/lib64(创建/test/virroot/lib目录)
[root@Smoke kernel]# cp /lib64/libtinfo.so.5 /test/virroot/lib/(复制libtinfo.so.5库文件到/test/virroot/lib目录)
[root@Smoke kernel]# cp /lib64/libdl.so.2 /test/virroot/lib64/(复制libdl.so.2库文件到/test/virroot/lib64目录)
[root@Smoke kernel]# cp /lib64/libc.so.6 /test/virroot/lib64/(复制libc.so.6库文件到/test/virroot/lib64/目录)
[root@Smoke kernel]# cp /lib64/ld-linux-x86-64.so.2 /test/virroot/lib64/(复制ld-linux-x86-64.so.2库文件到/test/virroot/lib64/目录)
[root@Smoke kernel]# tree /test/virroot/(查看/test/virroot目录树)
/test/virroot/
├── bin
│~~└── bash
└── lib64
   ├── ld-linux-x86-64.so.2
   ├── libc.so.6
   ├── libdl.so.2
   └── libtinfo.so.5
2 directories, 5 files
[root@Smoke kernel]# chroot /test/virroot/(切换根路径)
bash-4.1#
bash-4.1# ls
bash: ls: command not found
bash-4.1# cd /lib64/
bash-4.1# cd /bin/
bash-4.1# cd /root
bash: cd: /root: No such file or directory
提示:因为只有bash二进制命令文件,所以只能使用bash内部命令,ls属于外部命令所以不能使用,只能切换/lib64和/bin目录,因为小linux只有这两个目录;
bash-4.1# exit(退出)
exit
[root@Smoke ~]# vim /etc/inittab(编辑/etc/inittab文件,查看运行级别)
id:3:initdefault:
[root@Smoke ~]# ls /boot/grub/(查看/boot/groub目录)
device.map     fat_stage1_5  grub.conf         jfs_stage1_5  minix_stage1_5     splash.xpm.gz  stage2(GRUB第二阶段)         vstafs_stage1_5
e2fs_stage1_5  ffs_stage1_5  iso9660_stage1_5  menu.lst      reiserfs_stage1_5  stage1(GRUB第一阶段)         ufs2_stage1_5  xfs_stage1_5
[root@Smoke ~]# ls -lh /boot/grub/(查看/boot/grub目录文件及子目录详细信息,并做单位换算)
total 275K
-rw-r--r--. 1 root root   63 Apr 14 08:38 device.map
-rw-r--r--. 1 root root  14K Apr 14 08:38 e2fs_stage1_5
-rw-r--r--. 1 root root  13K Apr 14 08:38 fat_stage1_5
-rw-r--r--. 1 root root  12K Apr 14 08:38 ffs_stage1_5
-rw-------. 1 root root 1.1K Apr 14 23:05 grub.conf (grub配置文件,定义了加载那些图片,那些操作系统让我们选择,能够引导那些类型的操作系统)
-rw-r--r--. 1 root root  12K Apr 14 08:38 iso9660_stage1_5
-rw-r--r--. 1 root root  13K Apr 14 08:38 jfs_stage1_5
lrwxrwxrwx. 1 root root   11 Apr 14 08:38 menu.lst -> ./grub.conf
-rw-r--r--. 1 root root  12K Apr 14 08:38 minix_stage1_5
-rw-r--r--. 1 root root  15K Apr 14 08:38 reiserfs_stage1_5
-rw-r--r--. 1 root root 1.4K Nov 15  2010 splash.xpm.gz (linux启动红色背景图)
-rw-r--r--. 1 root root  512 Apr 14 08:38 stage1
-rw-r--r--. 1 root root 124K Apr 14 08:38 stage2 (第二阶段突破446字节限制)
-rw-r--r--. 1 root root  12K Apr 14 08:38 ufs2_stage1_5
-rw-r--r--. 1 root root  12K Apr 14 08:38 vstafs_stage1_5
-rw-r--r--. 1 root root  14K Apr 14 08:38 xfs_stage1_5
[root@Smoke ~]# rpm -q grub(查看是否安装grub软件)
grub-0.97-83.el6.x86_64[root@Smoke ~]# cat /etc/grub.conf(查看grub.conf文件内容)
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.11.2.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us
 rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us
 rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.el6.x86_64.img
提示:/etc/grub.conf是一个链接文件指向/boot/grub/grub.conf文件;
[root@Smoke ~]# ls -l /etc/grub.conf(查看grub.conf文件详细信息)
lrwxrwxrwx. 1 root root 22 Apr 14  2014 /etc/grub.conf -> ../boot/grub/grub.conf
[root@Smoke ~]# ls /proc/(查看/proc目录下文件及子目录)
1     149  171   181   188   1998  2090  2299  2675  29    4    acpi         fb           kpagecount  pagetypeinfo   timer_list
10    15   172   1819  189   2     2091  23    2677  2945  41   asound       filesystems  kpageflags  partitions     timer_stats
1021  155  173   182   19    20    2092  24    2679  2947  418  buddyinfo    fs           loadavg     sched_debug    tty
1052  156  174   183   190   2013  2093  2480  2681  3     42   bus          interrupts   locks       schedstat      uptime
1053  157  175   1834  1906  2033  2094  25    2683  30    454  cgroups      iomem        mdstat      scsi           version
11    16   1752  184   191   2075  21    2572  2685  31    5    cmdline(传递给内核参数)      ioports      meminfo     self           vmallocinfo
1101  165  176   1844  1919  2079  2115  2595  2690  32    6    cpuinfo      ipmi         misc        slabinfo       vmstat
12    166  177   1849  192   2084  2125  2596  2691  33    695  crypto       irq          modules     softirqs       zoneinfo
13    167  1777  185   193   2085  2165  26    27    3309  7    devices      kallsyms     mounts      stat
1360  168  178   186   194   2086  2173  2603  2757  366   72   diskstats    kcore        mpt         swaps
1395  169  179   1864  1944  2087  2185  2610  2767  367   8    dma          keys         mtd         sys
14    17   18    187   1953  2088  22    2629  28    38    868  driver       key-users    mtrr        sysrq-trigger
148   170  180   1870  1954  2089  2245  2664  2883  39    9    execdomains  kmsg         net         sysvipc
[root@Smoke ~]# cat /proc/cmdline(查看cmdline文件内容)
ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD  LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM
 rhgb quiet
提示:/proc/cmdline是启动内核所用的命令,这个cmdline中的参数和/etc/grub.conf文件中内核文件路径后面传递给内核的参数是一样的;
[root@Smoke ~]# cd /boot/(切换到/boot/目录)
[root@Smoke boot]# ls(查看目录下文件及子目录)
config-2.6.32-431.11.2.el6.x86_64         initramfs-2.6.32-431.el6.x86_64.img    System.map-2.6.32-431.el6.x86_64
config-2.6.32-431.el6.x86_64              lost+found                             vmlinuz-2.6.32-431.11.2.el6.x86_64
efi                                       symvers-2.6.32-431.11.2.el6.x86_64.gz  vmlinuz-2.6.32-431.el6.x86_64
grub                                      symvers-2.6.32-431.el6.x86_64.gz
initramfs-2.6.32-431.11.2.el6.x86_64.img  System.map-2.6.32-431.11.2.el6.x86_64
[root@Smoke boot]# pwd(显示当前所在目录)
/boot
[root@Smoke boot]# mount(查看系统所有挂载的文件系统)
/dev/sdb2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sdb1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
提示:/boot是一个独立的分区;
[root@Smoke boot]# vim /etc/grub.conf(查看grub.conf文件内容)
default=0
timeout=10(更改等待用户选择超时时长为10秒)
splashimage=(hd0,0)/grub/splash.xpm.gz
# hiddenmenu(不隐藏菜单)
title My Linux (2.6.32-431.11.2.el6.x86_64)(标题改为My Linux)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE
=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us
 rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@Smoke boot]# wget ftp:/172.16.0.1/pub/gls/server.repo (通过网络下载server.repo)
[root@Smoke boot]# yum install gimp(安装gimp软件)
提示:gimp类似windows下的PhotoShop软件,用于制作图片;
[root@Smoke yum.repos.d]# gimp &(后台运行gimp)
[root@Smoke yum.repos.d]# lftp 172.16.0.1(连接ftp服务器)
lftp 172.16.0.1:~> cd /pub/pictures/(切换到/pub/pictures目录)
lftp 172.16.0.1:/pub/pictures> get linux.jpg(下载linux.jpg图片)
提示:通过gimp软件对linux.jpg进行调整,调整为640*480分辨率,并且将像素设置为14,并保存为xpm格式的图片文件;
[root@Smoke ~]# gzip -9 linux.xpm(压缩问gz格式,并指定压缩比为9,默认6,压缩越小速度越快,但是压缩后文件越大)
[root@Smoke ~]# ls -lh | grep linux.xpm.gz(查看linux.xpm.gz文件详细信息,并单位换算)
-rw-r--r--   1 root    root    17K Oct 15 23:08 linux.xpm.gz
[root@Smoke ~]# mv linux.xpm.gz /boot/grub/(将linux.xpm.gz移动到/boot/grub目录)
[root@Smoke ~]# vim /etc/grub.conf(编辑grub.conf配置文件)
default=0
timeout=10
splashimage=(hd0,0)/grub/linux.xpm.gz(更改grub背景图片为linux.xpm.gz)
# hiddenmenu
title My Linux (2.6.32-431.11.2.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us
 rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us 
rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@Smoke ~]# shutdown -r now(立即重启计算机)

 grub界面应用:

使用e键可以编辑,使用a键可以修改内核参数,使用c键可以进入命令行界面

使用e键编辑title:将光标移动到需要边界的title,按下e键

想要编辑那一行,选择到相应的菜单,按e键,删除一行敲d,新增一行大O往前面一行,小o往后面一行;

进入编辑界面,按空格建,可以在后面添加字符

空格后面敲1,代表内核启动的级别,1: single user mode, 直接以管理员身份切入,或使用 s,S,single都表示1级别,b键盘进行启动

直接写完整单词single代表1级别,编辑完成敲回车;敲b键启动;

此时就会启动单用户模式,单用户模式,用户不需要密码就可以登录;如果有人能够接触到你电脑,不让它访问,简单也能够破解的方式获得密码,可以给grub加密码,加完密码以后想编辑功能

grub.conf

default=0 (全局属性定义) # 设定默认启动的title的编号,从0开始

timeout=5 # 等待用户选择的超时时长,单位是秒

splashimage=(hd0,0)/grub/splash.xpm.gz # grub的背景图片

hiddenmenu # 隐藏菜单

password redhat 给grub加密码,必须在title上面

password --md5 $1$HKXJ51$B9Z8A.X//XA.AtzU1.KuG. 使用md5加密对grub加密码,同样必须在title上面

title (指定操作系统或内核,要启动不同的内核,每一个内核给一个title,每一个title是用于定义当前系统的不同版本的内核,或者当前主机上不同操作系统) CentOS (2.6.32-431.11.2.el6.x86_64) # 内核标题,或操作系统名称,字符串,可自由修改

  root (hd0,0) # 内核文件所在的设备;对grub而言,所有类型硬盘一律hd,格式为(hd#,N);hd#, #表示第几个磁盘;最后的N表示对应磁盘的分区;

  kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet # 内核文件路径,及传递给内核的参数 (为什么内核文件在/根下,但是内核文件在/boot下,因为/boot是一个独立的分区,在内核还没有装载,文件系统不会装载,意味着grub在访问这个磁盘分区的时候,不会通过/boot目录访问,/boot目录在根上,对应的分区如果不通过/根访问,那/boot就找不到,如果通过真正的文件系统访问就是/boot/vmlinux*文件,问题是现在文件系统还没装载,直接通过硬盘访问vmlinux*,它是直接在这个磁盘上,如果把磁盘本身当作/根,因为此时访问不了其他分区,对这个磁盘来讲/vmlinux*就是/根,因为访问入口不同,)

  initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img # ramdisk/ramfs文件路径 (小linux,操作系统安装最后一步生成的,是一个完整类型的Linux,除了内核,各种文件都有,initrd也在/boot目录下,initrd的版本号一定要和内核版本号保持一致,后缀是img说明是ram磁盘文件,镜像文件,可以展开以后当磁盘使用,类似dd命令创建虚拟SWAP设备,本地回环设备,假如/boot没有独立分区,/boot就在/根上,内核就是/boot目录下,要找这个文件,内核在/boot/vmlinux*,因为它就是根文件系统系统,而且/根下的确有/boot目录)

title (指定操作系统或内核) CentOS (2.6.32-431.el6.x86_64)

  password --md5 $1$HKXJ51$B9Z8A.X//XA.AtzU1.KuG. 启动指定内核操作系统需要输入密码

title Install Red Hat Enterprise Linux 5

  root (hd0,0)

  kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet

  initrd /initramfs-2.6.32-431.el6.x86_64.img

  password --md5 $1$FSUEU/$uhUUc8USBK5QAXc.BfW4m.

[root@localhost ~]# grub-md5-crypt(创建md5密码)Password: (输入密码)
Retype password: (重复输入)
$1$18.Kx1$KASqTiJayN7x.kxY1MXVz1 (生成的md5密码串)
从单用户模式切换到3级别,3: multi user mode, text mode 正常多用户模式,文本模式,不启动图形界面

[root@Smoke ~]# vim /etc/grub.conf(编辑grub配置文件)
default=0
timeout=10
splashimage=(hd0,0)/grub/linux.xpm.gz
hiddenmenu
password --md5 $1$18.Kx1$KASqTiJayN7x.kxY1MXVz1(给grub设置密码,必须在title上面)
title My Linux (2.6.32-431.11.2.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us
 rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us 
rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@Smoke ~]# sync(强制被改变的内容立刻写入磁盘,更新超块信息)
[root@Smoke ~]# shutdown -r now(立即重启系统)

敲e键,没反映,敲p键,需要输入grub密码

输入grub密码

再敲e键,就可以了

查看运行级别:

runlevel: 运行级别

who -r

查看内核release号:

uname -r:查看当前使用内核版本号

安装grub stage1:

# grub 进入grub命令
grub> root (hd0,0) 指定内核所在的分区,hd0代表第一块磁盘,0代表磁盘的第一个主分区
grub> set (hd0) 安装grub,指定安装那块硬盘

安装grub第二种方式:

# grub-install --root-directory=/path/to/boot's_parent_dir (指定根目录,这里根不是根文件系统的根,而是打算把grub放在那个目录上,放在那个分区上对应的根,
这里的根指的是boot目录所在的分区,要确保把内核所在的分区挂载到boot目录下才可以)  /PATH/TO/DEVICE (指定磁盘设备)
如果有第二块硬盘,希望给其他硬盘的安装grub,给其他硬盘的MBR安装bootloaer,grub的安装可以指定任意设备,只要设备都MBR,都可以装进去,前提,安装之前这个设备需要做好
分区,有分区表,而且把boot目录已经挂载到某个位置;
GRUB界面启动系统(当grub.conf文件损坏时):
grub> find 查找内核文件所在分区
grub> root (hd#,N) 指定内核所在分区
grub> kernel /PATH/TO/KERNEL_FILE 指定内核路径
grub> initrd /PATH/TO/INITRD_FILE 指定initrd文件路径
grub> boot 尝试重启

  

[root@Smoke ~]# runlevel(查看系统运行级别)
N(上一个级别,N代表没切换过) 3(当前级别)
[root@Smoke ~]# who -r(查看当前系统运行级别)
        run-level 3  2014-10-17 00:37
[root@Smoke ~]# uname -r(查看内核版本号)
2.6.32-431.11.2.el6.x86_64
[root@Smoke ~]# fdisk -l(查看当前主机硬盘及分区使用情况)
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xaab2eeb2
  Device Boot      Start         End      Blocks   Id  System
Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00031216
  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              26        2576    20480000   83  Linux
/dev/sdb3            2576        2706     1048576   82  Linux swap / Solaris
/dev/sdb4            2706        5221    20203282+   5  Extended
/dev/sdb5            2706        3359     5246736   8e  Linux LVM
/dev/sdb6            3360        4013     5253223+  8e  Linux LVM
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xcf6c2b9f
  Device Boot      Start         End      Blocks   Id  System
Disk /dev/mapper/smokevg-lv1: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@Smoke ~]# dd if=/dev/zero of=/dev/sdb count=1 bs=400(从/dev/zeroe最开始处读数据,bs以400bytes为单位,count读1各bs,保存到/dev/sdb)
1+0 records in
1+0 records out
400 bytes (400 B) copied, 0.000160872 s, 2.5 MB/s
提示:破坏调/dev/sdb的MBR的bootloader
[root@Smoke ~]# sync(同步写入磁盘)
提示:重启后将无法重启,需要紧急救援模式修复,当前不重启情况下修复bootloader,重装Grub;
[root@Smoke ~]# grub(进入grub命令)
Probing devices to guess BIOS drives. This may take a long time.
   GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported.  For the first word, TAB
  lists possible command completions.  Anywhere else TAB lists the possible
  completions of a device/filename.]
grub> grub> root (hd0,0) (指定内核所在分区,hd0代表第一块磁盘,0代表第一个主分区)                    
root (hd0,0)
Error 22: No such partition (没有这样的分区)
grub> root (hd1,0) (指定内核所在分区,hd1代表第二块磁盘,0代表第一个主分区)
root (hd1,0)
Filesystem type is ext2fs, partition type 0x83 (ext2fs分区)
grub> root (hd2,0) (指定内核所在分区,hd2代表第三块磁盘,0代表第一个主分区)
root (hd2,0)
Error 22: No such partition (没有这样的分区)
grub> root (hd1,0) (指定内核所在分区,hd1代表第二块磁盘,0代表第一个主分区)
root (hd1,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd1)(安装grub,指定安装那块硬盘)
setup (hd1)
Checking if "/boot/grub/stage1" exists... no (检查grub第1阶段)
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes (检查grub第2阶段)
Checking if "/grub/e2fs_stage1_5" exists... yes (检查grub第1.5阶段)
Running "embed /grub/e2fs_stage1_5 (hd1)"...  27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd1) (hd1)1+27 p (hd1,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit (退出)
quit
quit
[root@Smoke ~]# vim /etc/grub.conf (编辑grub配置文件)
default=0
timeout=10
splashimage=(hd0,0)/grub/linux.xpm.gz
hiddenmenu
password --md5 $1$18.Kx1$KASqTiJayN7x.kxY1MXVz1
title Smoke Linux (2.6.32-431.11.2.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc 
KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.11.2.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
       root (hd0,0)
       kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=9f66aadb-2e9a-49d5-9748-ceceea2d41f0 rd_NO_LUKS  KEYBOARDTYPE=pc 
KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
       initrd /initramfs-2.6.32-431.el6.x86_64.img
[root@Smoke ~]# mount(查看当前系统所有挂载的文件系统)
/dev/sdb2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sdb1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
[root@Smoke ~]# fdisk -l(查看系统所有硬盘及分区使用情况)
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xaab2eeb2
  Device Boot      Start         End      Blocks   Id  System
Disk /dev/sdb: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00031216
  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              26        2576    20480000   83  Linux
/dev/sdb3            2576        2706     1048576   82  Linux swap / Solaris
/dev/sdb4            2706        5221    20203282+   5  Extended
/dev/sdb5            2706        3359     5246736   8e  Linux LVM
/dev/sdb6            3360        4013     5253223+  8e  Linux LVM
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xcf6c2b9f
  Device Boot      Start         End      Blocks   Id  System
Disk /dev/mapper/smokevg-lv1: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
提示:往/dev/sda硬盘安装grub,装grub有第二阶段,第二阶段一定要在/boot目录下,
[root@Smoke ~]# fdisk /dev/sda(管理/dev/sda设备,进入交互式)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
        switch off the mode (command 'c') and change display units to
        sectors (command 'u').
Command (m for help): n(新建分区)
Command action
  e   extended
  p   primary partition (1-4)
p(创建主分区)
Partition number (1-4): 1(第一个主分区,分区编号为1)
First cylinder (1-2610, default 1): (起始柱面)
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +20M (创建20M大小分区)
Command (m for help): n(新建分区)
Command action
  e   extended
  p   primary partition (1-4)
p(创建主分区)
Partition number (1-4): 2(第二个主分区,分区编号为2)
First cylinder (5-2610, default 5): (起始柱面)
Using default value 5
Last cylinder, +cylinders or +size{K,M,G} (5-2610, default 2610): +512M(创建512M大小分区)
Command (m for help): n(新建分区)
Command action
  e   extended
  p   primary partition (1-4)
p(创建主分区)
Partition number (1-4): 3(第三个主分区,分区编号为3)
First cylinder (71-2610, default 71):(起始柱面)
Using default value 71
Last cylinder, +cylinders or +size{K,M,G} (71-2610, default 2610): +128M(创建128M大小分区)
Command (m for help): t(调整分区类型)
Partition number (1-4): 3(调整第三块分区类型)
Hex code (type L to list codes): 82(调整分区类型为交换分区)
Changed system type of partition 3 to 82 (Linux swap / Solaris)
Command (m for help): p(显示分区情况)
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xaab2eeb2
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1           4       32098+  83  Linux
/dev/sda2               5          70      530145   83  Linux
/dev/sda3              71          87      136552+  82  Linux swap / Solaris
Command (m for help): w(保存退出)
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Smoke ~]# partprobe /dev/sda(让内核重新加载/dev/sda设备,RHEL 5X使用partprobe,RHEL 6X使用partx)
[root@Smoke ~]# mke2fs -j /dev/sda1(-j: 创建ext3类型文件系统)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
8032 inodes, 32096 blocks
1604 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33030144
4 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
       8193, 24577
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@Smoke ~]# mke2fs /dev/sda1(创建ext2类型文件系统)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
8032 inodes, 32096 blocks
1604 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33030144
4 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
       8193, 24577
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@Smoke ~]# mke2fs /dev/sda2(将/dev/sda2格式化为ext2类型文件系统)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
33200 inodes, 132536 blocks
6626 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=138412032
5 block groups
32768 blocks per group, 32768 fragments per group
6640 inodes per group
Superblock backups stored on blocks:
       32768, 98304
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@Smoke ~]# mkswap /dev/sda3(将/dev/sda3格式化为交换分区)
Setting up swapspace version 1, size = 136548 KiB
no label, UUID=0add4fa4-713f-4fb4-808f-226e0c0d4a09
提示:往上装grub的时候,一定会找boot目录
[root@Smoke ~]# mkdir /mnt/boot(创建目录/mnt/boot)
[root@Smoke ~]# mount /dev/sda1 /mnt/boot/(将/dev/sda1挂载到/mnt/boot目录)[root@Smoke ~]# grub-install --root-directory=/mnt /dev/sda
(安装grub,--root-directory指定根目录,boot所在目录,/dev/sda指定硬盘)
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.(磁盘映射文件)
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0)        /dev/fd0
(hd0)        /dev/sda
(hd1)        /dev/sdb
(hd2)        /dev/sdc
[root@Smoke ~]# sync(同步磁盘写入)
[root@Smoke ~]# cd /mnt/boot/(切换到/mnt/boot目录)
[root@Smoke boot]# ls grub/(查看grub目录文件及子目录)
device.map     fat_stage1_5  iso9660_stage1_5  minix_stage1_5     stage1  ufs2_stage1_5    xfs_stage1_5
e2fs_stage1_5  ffs_stage1_5  jfs_stage1_5      reiserfs_stage1_5  stage2  vstafs_stage1_5
提示:给grub创建配置文件
[root@Smoke boot]# vim /mnt/boot/grub/grub.conf(编辑grub.conf)
default=0 (设定默认启动的title的编号,从0开始)
timeout=5 (等待用户选择的超时时长,单位是秒)
title Fake Linux (指定操作系统或内核,要启动不同的内核,每一个内核给一个title,每一个title是用于定义当前系统的不同版本的内核,或者当前主机上不同操作系统)
       root (hd0,0) 内核文件所在的设备;对grub而言,所有类型硬盘一律hd,格式为(hd#,N);hd#, #表示第几个磁盘;最后的N表示对应磁盘的分区;)
       kernel /vmlinuz (内核文件路径,及传递给内核的参数 (为什么内核文件在/根下,但是内核文件在/boot下,因为/boot是一个独立的分区,在内核还没有装载,文件
系统不会装载,意味着grub在访问这个磁盘分区的时候,不会通过/boot目录访问,/boot目录在根上,对应的分区如果不通过/根访问,那/boot就找不到,如果通过真正的文件系
统访问就是/boot/vmlinux*文件,问题是现在文件系统还没装载,直接通过硬盘访问vmlinux*,它是直接在这个磁盘上,如果把磁盘本身当作/根,因为此时访问不了其他分区,
对这个磁盘来讲/vmlinux*就是/根,因为访问入口不同,)
       initrd /initrd.img (ramdisk/ramfs文件路径 (小linux,操作系统安装最后一步生成的,是一个完整类型的Linux,除了内核,各种文件都有,initrd也在/boot
目录下,initrd的版本号一定要和内核版本号保持一致,后缀是img说明是ram磁盘文件,镜像文件,可以展开以后当磁盘使用,类似dd命令创建虚拟SWAP设备,本地回环设备,假如
/boot没有独立分区,/boot就在/根上,内核就是/boot目录下,要找这个文件,内核在/boot/vmlinux*,因为它就是根文件系统系统,而且/根下的确有/boot目录)
[root@Smoke boot]# sync(同步写入磁盘)
[root@Smoke boot]# cd(切换到用户家目录)
[root@Smoke ~]# umount /mnt/boot/(卸载/mnt/boot目录下挂载的文件系统)
提示:挂起当前Linux虚拟机,重新创建虚拟机,使用刚刚创建的grub硬盘启动;

测试:通过新建的虚拟机加载创建grub硬盘

修复grub.conf配置文件:

[root@Smoke ~]# mv /boot/grub/grub.conf /root/(移动/boot/grub/grub.conf文件到/root目录)
提示:通过将grub.conf文件移动到/root目录模拟grub.conf文件损坏
[root@Smoke ~]# sync(同步写入磁盘)
[root@Smoke ~]# shutdown -r now(立即重启计算机)

失去grub.conf文件的系统,重启后进入grub提示符,在这个目录下想要启动系统,需要知道内核在那,根在那里,就能够启动起来;

通过root探测内核分区无法探测到;

通过find查找内核文件,硬盘分区后面需要加/斜线,然后按Tab键,会告诉这个分区有那些内容;

hd1磁盘的第一个分区什么都没有,但是有个grub目录是我们自己装进去的,因此hd1是不行的;

指定root根所在分区;

指定kernel为/vmlinuz-2.6.18-308.e15(按Tab键可以补全)

指定initrd文件为/initrd-2.6.18-308.e15.img需要和内核版本保持一致,使用boot尝试启动系统,它会尝试找根,如果能找到,就启动起来了,一般会在当前文件系统找根;

[root@Smoke ~]# mv grub.conf /boot/grub/(将grub.conf配置文件移动到/boot/grub目录)

Kernel初始化的过程:

1、设备探测

2、驱动初始化(可能会从initrd(红帽6.x initramfs)文件中装载驱动模块)

3、以只读挂载根文件系统;

4、装载第一个进程init(PID:1)

/sbin/init:(/etc/inittab)串行启动

  upstart(6.x版本的init): ubuntu, d-bus(系统内部各子系统之间IPC进程间通信机制), event-driven(基于事件驱动完成系统启动) 并行启动

  systemd: 比upstart还好的init 完全并行启动

id:runlevels:action:process

id: 标识符

runlevels: 在哪个级别运行此行;

action: 在什么情况下执行此行;

process: 要运行程序;

id:3:initdefault:

si(标识符):(没有指级别,代表任何级别):sysinit(系统初始化):/etc/rc.d/rc.sysinit(要运行的程序)

ACTION:

initdefault: 设定默认运行级别

sysinit: 系统初始化

wait: 等待级别切换至此级别时执行

respawn: 一旦程序终止,会重新启动

/etc/rc.d/rc.sysinit(系统初始化脚本)完成的任务:

1、激活udev和selinux;

2、根据/etc/sysctl.conf文件,来设定内核参数;

3、设定时钟时钟;

4、装载键盘映射;

5、启用交换分区;

6、设置主机名;

7、根文件系统检测,并以读写方式重新挂载;

8、激活RAID和LVM设备;

9、启用磁盘配额;

10、根据/etc/fstab,检查并挂载其它文件系统;

11、清理过期的锁和PID文件;

for I in /etc/rc3.d/K*; do
 $I stop
done
for I in /etc/rc3.d/S*; do
 $I start
done

##: 关闭或启动的优先次序,数据越小越优先被选定

先关闭以K开头的服务,后启动以S开头的服务;

[root@Smoke ~]# ls /boot/(查看/boot目录文件及子目录)
config-2.6.32-431.11.2.el6.x86_64         initramfs-2.6.32-431.el6.x86_64.img    System.map-2.6.32-431.el6.x86_64
config-2.6.32-431.el6.x86_64              lost+found                             vmlinuz-2.6.32-431.11.2.el6.x86_64
efi                                       symvers-2.6.32-431.11.2.el6.x86_64.gz  vmlinuz-2.6.32-431.el6.x86_64
grub                                      symvers-2.6.32-431.el6.x86_64.gz
initramfs-2.6.32-431.11.2.el6.x86_64.img  System.map-2.6.32-431.11.2.el6.x86_64
提示:initramfs-2.6.32-431.11.2.el6.x86_64.img为红帽6.x版本的initrd文件;
[root@Smoke ~]# cat /etc/inittab(查看/etc/inittab文件内容)
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
[root@Smoke ~]# ls /etc/init(查看/etc/init目录)
control-alt-delete.conf  prefdm.conf         rcS-emergency.conf        readahead-disable-services.conf  tty.conf
init-system-dbus.conf    quit-plymouth.conf  rcS-sulogin.conf          serial.conf
kexec-disable.conf       rc.conf             readahead-collector.conf  splash-manager.conf
plymouth-shutdown.conf   rcS.conf            readahead.conf            start-ttys.conf
提示:红帽6.x版本中/etc/init目录中存放/etc/inittab文件切割以后的各种片存放目录,这些文件都以.conf结尾,每一个文件都是基于事件驱动的方式来编写的;
[root@Smoke ~]# ls /etc/init.d/(查看/etc/init.d目录)
abrt-ccpp         certmonger  ip6tables     messagebus      ntpd         restorecond  single          vmware-tools
abrtd             cpuspeed    iptables      mysqld          ntpdate      rngd         smartd          vmware-tools-thinprint
abrt-oops         crond       irqbalance    netconsole      oddjobd      rpcbind      snmpd           wdaemon
acpid             cups        jexec         netfs           portreserve  rpcgssd      snmptrapd       winbind
atd               dnsmasq     kdump         network         postfix      rpcidmapd    spice-vdagentd  wpa_supplicant
auditd            firstboot   killall       NetworkManager  psacct       rpcsvcgssd   sshd            xinetd
autofs            functions   lvm2-lvmetad  nfs             puppet       rsyslog      sssd            ypbind
blk-availability  haldaemon   lvm2-monitor  nfslock         quota_nld    sandbox      sysstat
bluetooth         halt        mdmonitor     nrped           rdisc        saslauthd    udev-post
提示:红帽6.x版本脚本启动目录;
[root@Smoke ~]# cd /etc/init(切换到/etc/init目录)
[root@Smoke init]# vim rcS.conf(编辑rcS.conf)
# rcS - runlevel compatibility(这个文件存放的目录主要实现跟此前的方式兼容)
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.
start on startup(在系统启动时候会启动)
stop on runlevel(在指定某个级别时会停止)
task
# Note: there can be no previous runlevel here, if we have one it's bad
# information (we enter rc1 not rcS for maintenance).  Run /etc/rc.d/rc
# without information so that it defaults to previous=N runlevel=S.
console output
pre-start script
       for t in $(cat /proc/cmdline); do
               case $t in
                       emergency)
                               start rcS-emergency
# rcS - runlevel compatibility
#
# This task runs the old sysv-rc startup scripts.
#
# Do not edit this file directly. If you want to change the behaviour,
# please create a file rcS.override and put your changes there.
start on startup
stop on runlevel
task
# Note: there can be no previous runlevel here, if we have one it's bad
# information (we enter rc1 not rcS for maintenance).  Run /etc/rc.d/rc
# without information so that it defaults to previous=N runlevel=S.
console output
pre-start script
       for t in $(cat /proc/cmdline); do
               case $t in
                       emergency)
                               start rcS-emergency
                               break
                       ;;
               esac
       done
end script
exec /etc/rc.d/rc.sysinit(执行rc.sysinit脚本)
post-stop script
       if [ "$UPSTART_EVENTS" = "startup" ]; then
               [ -f /etc/inittab ] && runlevel=$(/bin/awk -F ':' '$3 == "initdefault" && $1 !~ "^#" { print $2 }' /etc/inittab)
               [ -z "$runlevel" ] && runlevel="3"
               for t in $(cat /proc/cmdline); do
                       case $t in
                               -s|single|S|s) runlevel="S" ;;
                               [1-9])       runlevel="$t" ;;
                       esac
               done
               exec telinit $runlevel
       fi
end script
[root@Smoke ~]# vim /etc/inittab(编辑/etc/inittab文件,此文件为centos 5.x版本/etc/inittab文件内容)
  #
  # inittab This file describes how the INIT process should set up
  # the system in a certain run-level.
  #
  # Author: Miquel van Smoorenburg, 〈miquels@drinkel.nl.mugnet.org〉
  # Modified for RHS Linux by Marc Ewing and Donnie Barnes
  #
  # Default runlevel. The runlevels used by RHS are:
  # 0 - halt (Do NOT set initdefault to this)
  # 1 - Single user mode
  # 2 - Multiuser, without NFS (The same as 3, if you do not havenetworking)
  # 3 - Full multiuser mode
  # 4 - unused
  # 5 - X11
  # 6 - reboot (Do NOT set initdefault to this)
  #
  ###表示当前缺省运行级别为5(initdefault);
  id:5:initdefault:
  ###启动时自动执行/etc/rc.d/rc.sysinit脚本(sysinit) 主要完成的工作有:激活交换分区,检查磁盘,加载硬件模块以及其它一些需要优先执行任务
  # System initialization.
  si::sysinit:/etc/rc.d/rc.sysinit
  l0:0:wait:/etc/rc.d/rc 0
  l1:1:wait:/etc/rc.d/rc 1
  l2:2:wait:/etc/rc.d/rc 2
  l3:3:wait:/etc/rc.d/rc 3
  l4:4:wait:/etc/rc.d/rc 4
  ###当运行级别为5时,以5为参数运行/etc/rc.d/rc脚本,init将等待其返回(wait)
  l5:5:wait:/etc/rc.d/rc 5
  l6:6:wait:/etc/rc.d/rc 6
  ###在启动过程中允许按CTRL-ALT-DELETE重启系统
  # Trap CTRL-ALT-DELETE
  ca::ctrlaltdel:/sbin/shutdown -t3 -r now
  # When our UPS tells us power has failed, assume we have a few minutes
  # of power left. Schedule a shutdown for 2 minutes from now.
  # This does, of course, assume you have powerd installed and your
  # UPS connected and working correctly.
  pf::powerfail(停电):/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down" (关机)
  # If power was restored before the shutdown kicked in, cancel it.
  pr:12345:powerokwait(来电):/sbin/shutdown -c "Power Restored; Shutdown Cancelled" (取消关机)
  ###在2、3、4、5级别上以ttyX为参数执行/sbin/mingetty程序,打开ttyX终端用于用户登录,
  ###如果进程退出则再次运行mingetty程序(respawn)
  # Run gettys in standard runlevels
  1:2345:respawn:/sbin/mingetty tty1 (主要用于控制终端登录,最多支持6个终端,可更改终端个数)
  2:2345:respawn:/sbin/mingetty tty2
  3:2345:respawn:/sbin/mingetty tty3
  4:2345:respawn:/sbin/mingetty tty4
  5:2345:respawn:/sbin/mingetty tty5
  6:2345:respawn:/sbin/mingetty tty6
  ###在5级别上运行xdm程序,提供xdm图形方式登录界面,并在退出时重新执行(respawn)
  # Run xdm in runlevel 5
  x:5:respawn:/etc/X11/prefdm -nodaemon
  以上面的inittab文件为例,来说明一下inittab的格式。其中以#开始的行是注释行,除了注释行之外,每一行都有以下格式:
  id:runlevel:action:process
  对上面各项的详细解释如下:
  1. id
  id是指入口标识符,它是一个字符串,对于getty或mingetty等其他login程序项,要求id与tty的编号相同,否则getty程序将不能正常工作。
  2. runlevel
  runlevel是init所处于的运行级别的标识,一般使用0-6以及S或s。0、1、6运行级别被系统保留:其中0作为shutdown动作,1作为重启至单用户模式,6为重启;S和s意义
相同,表示单用户模式,且无需inittab文件,因此也不在inittab中出现,实际上,进入单用户模式时,init直接在控制台(/dev/console)上运行/sbin/sulogin。在一般
的系统实现中,都使用了2、3、4、5几个级别,在Redhat系统中,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表示XDM图
形登录方式。7-9级别也是可以使用的,传统的Unix系统没有定义这几个级别。runlevel可以是并列的多个值,以匹配多个运行级别,对大多数action来说,仅当runlevel与当前
运行级别匹配成功才会执行。
  3. action
  action是描述其后的process的运行方式的。action可取的值包括:initdefault、sysinit、boot、bootwait等:
  initdefault是一个特殊的action值,用于标识缺省的启动级别;当init由核心激活以后,它将读取inittab中的initdefault项,取得其中的runlevel,并作为当前的运行
级别。如果没有inittab文件,或者其中没有initdefault项,init将在控制台上请求输入runlevel。
  sysinit、boot、bootwait等action将在系统启动时无条件运行,而忽略其中的runlevel。
  其余的action(不含initdefault)都与某个runlevel相关。各个action的定义在inittab的man手册中有详细的描述。
  4. process
  process为具体的执行程序。程序后面可以带参数。
[root@Smoke ~]# man inittab(查看inittab文件man帮助文档)
[root@Smoke init]# wc -l /etc/rc.d/rc.sysinit(统计rc.sysinit文件总共有多少行)
680 /etc/rc.d/rc.sysinit
[root@Smoke init]# file /etc/rc.d/rc.sysinit(查看/rc.sysinit文件类型)
/etc/rc.d/rc.sysinit: Bourne-Again shell script text executable(bash/shell脚本,需要学习读这个脚本)
[root@Smoke init]# less /etc/rc.d/rc.sysinit(分页显示rc.sysinit文件)
#!/bin/bash
#
# /etc/rc.d/rc.sysinit - run once at boot time
#
# Taken in part from Miquel van Smoorenburg's bcheckrc.
#
HOSTNAME=$(/bin/hostname)
set -m
if [ -f /etc/sysconfig/network ]; then
   . /etc/sysconfig/network
fi
if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
   HOSTNAME=localhost
fi
if [ ! -e /proc/mounts ]; then
       mount -n -t proc /proc /proc
       mount -n -t sysfs /sys /sys >/dev/null 2>&1
fi
if [ ! -d /proc/bus/usb ]; then
       modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
else
       mount -n -t usbfs /proc/bus/usb /proc/bus/usb
fi
[root@Smoke ~]# vim /etc/rc.d/rc(编辑/etc/rc.d/rc)
#! /bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes.
#
# Original Author:      
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#
set -m
# check a file to be a correct runlevel script
check_runlevel ()
{
       # Check if the file exists at all.
       [ -x "$1" ] || return 1
       is_ignored_file "$1" && return 1
       return 0
}
# Now find out what the current and what the previous runlevel are.
argv1="$1"
set $(/sbin/runlevel)
runlevel=$2
previous=$1
export runlevel previous
. /etc/init.d/functions
export CONSOLETYPE
do_confirm="no"
if [ -f /var/run/confirm ]; then
       do_confirm="yes"
fi
UPSTART=
[ -x /sbin/initctl ] && UPSTART=yes
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
       if [ "$do_confirm" = "yes" ]; then
               echo $"Entering interactive startup"
       else
               echo $"Entering non-interactive startup"
       fi
fi
# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"
# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0
# Set language, vc settings once to avoid doing it for every init script
# through functions
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
 . /etc/profile.d/lang.sh 2>/dev/null
 export LANGSH_SOURCED=1
fi
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
       # Check if the subsystem is already up.
       subsys=${i#/etc/rc$runlevel.d/K??}
       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
       check_runlevel "$i" || continue
       # Bring the subsystem down.
       [ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
       $i stop
#! /bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes.
#
# Original Author:      
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#
set -m
# check a file to be a correct runlevel script
check_runlevel ()
{
       # Check if the file exists at all.
       [ -x "$1" ] || return 1
       is_ignored_file "$1" && return 1
       return 0
}
# Now find out what the current and what the previous runlevel are.
argv1="$1"
set $(/sbin/runlevel)
runlevel=$2
previous=$1
export runlevel previous
. /etc/init.d/functions
export CONSOLETYPE
do_confirm="no"
if [ -f /var/run/confirm ]; then
       do_confirm="yes"
fi
UPSTART=
[ -x /sbin/initctl ] && UPSTART=yes
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
       if [ "$do_confirm" = "yes" ]; then
               echo $"Entering interactive startup"
       else
               echo $"Entering non-interactive startup"
       fi
fi
# Get first argument. Set new runlevel to this argument.
[ -n "$argv1" ] && runlevel="$argv1"
# Is there an rc directory for this new runlevel?
[ -d /etc/rc$runlevel.d ] || exit 0
# Set language, vc settings once to avoid doing it for every init script
# through functions
if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
 . /etc/profile.d/lang.sh 2>/dev/null
 export LANGSH_SOURCED=1
fi
# First, run the KILL scripts.
for i in /etc/rc$runlevel.d/K* ; do
       # Check if the subsystem is already up.
       subsys=${i#/etc/rc$runlevel.d/K??}
       [ -f /var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] || continue
       check_runlevel "$i" || continue
       # Bring the subsystem down.
       [ -n "$UPSTART" ] && initctl emit --quiet stopping JOB=$subsys
       $i stop
       [ -n "$UPSTART" ] && initctl emit --quiet stopped JOB=$subsys
done
# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do
       # Check if the subsystem is already up.
       subsys=${i#/etc/rc$runlevel.d/S??}
       [ -f /var/lock/subsys/$subsys ] && continue
       [ -f /var/lock/subsys/$subsys.init ] && continue
       check_runlevel "$i" || continue
       # If we're in confirmation mode, get user confirmation
       if [ "$do_confirm" = "yes" ]; then
               confirm $subsys
               rc=$?
               if [ "$rc" = "1" ]; then
                       continue
               elif [ "$rc" = "2" ]; then
                       do_confirm="no"
               fi
       fi
       update_boot_stage "$subsys"
       # Bring the subsystem up.
       [ -n "$UPSTART" ] && initctl emit --quiet starting JOB=$subsys
       if [ "$subsys" = "halt" -o "$subsys" = "reboot" ]; then
               export LC_ALL=C
               exec $i start
       fi
       $i start
       [ -n "$UPSTART" ] && initctl emit --quiet started JOB=$subsys
done
[ "$do_confirm" = "yes" ] && rm -f /var/run/confirm
exit 0
[root@Smoke ~]# ls /etc/rc3.d/(查看/etc/rc3.d目录文件及子目录)
K01smartd          K73winbind         K99rngd          S13irqbalance        S26haldaemon               S82abrtd
K02oddjobd         K74ntpd            S01sysstat       S13rpcbind           S26udev-post               S88nrped
K05wdaemon         K75ntpdate         S02lvm2-monitor  S15mdmonitor         S28autofs                  S90crond
K10psacct          K75quota_nld       S03vmware-tools  S22messagebus        S30nfs                     S95atd
K10saslauthd       K76ypbind          S08ip6tables     S23NetworkManager    S50bluetooth               S95jexec
K30spice-vdagentd  K80kdump           S08iptables      S24nfslock           S55sshd                    S99certmonger
K50dnsmasq         K84wpa_supplicant  S10network       S24rpcgssd           S56xinetd                  S99local
K50netconsole      K87restorecond     S11auditd        S25blk-availability  S57vmware-tools-thinprint
K50snmpd           K88sssd            S11portreserve   S25cups              S64mysqld
K50snmptrapd       K89rdisc           S12rsyslog       S25netfs             S80postfix
K69rpcsvcgssd      K95firstboot       S13cpuspeed      S26acpid             S82abrt-ccpp
[root@Smoke ~]# vim test4.sh(编辑test4.sh脚本)
#!/bin/bash
#
for I in /etc/rc3.d/K*;do
       echo $I
done
[root@Smoke ~]# bash test4.sh(执行test4.sh脚本)
/etc/rc3.d/K01smartd
/etc/rc3.d/K02oddjobd
/etc/rc3.d/K05wdaemon
/etc/rc3.d/K10psacct
/etc/rc3.d/K10saslauthd
/etc/rc3.d/K30spice-vdagentd
/etc/rc3.d/K50dnsmasq
/etc/rc3.d/K50netconsole
/etc/rc3.d/K50snmpd
/etc/rc3.d/K50snmptrapd
/etc/rc3.d/K69rpcsvcgssd
/etc/rc3.d/K73winbind
/etc/rc3.d/K74ntpd
/etc/rc3.d/K75ntpdate
/etc/rc3.d/K75quota_nld
/etc/rc3.d/K76ypbind
/etc/rc3.d/K80kdump
/etc/rc3.d/K84wpa_supplicant
/etc/rc3.d/K87restorecond
/etc/rc3.d/K88sssd
/etc/rc3.d/K89rdisc
/etc/rc3.d/K95firstboot
/etc/rc3.d/K99rngd
[root@Smoke ~]# ls -d /etc/rc*(查看/etc/下rc开头任意字符结尾的目录)
/etc/rc     /etc/rc1.d  /etc/rc3.d  /etc/rc5.d  /etc/rc.d      /etc/rc.sysinit
/etc/rc0.d  /etc/rc2.d  /etc/rc4.d  /etc/rc6.d  /etc/rc.local
[root@Smoke ~]# ls -ld /etc/rc*(查看/etc/下rc开头任意字符结尾的目录详细信息)
lrwxrwxrwx.  1 root root    7 Apr 14  2014 /etc/rc -> rc.d/rc
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root   10 Apr 14  2014 /etc/rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root 4096 Oct 26 04:27 /etc/rc.d
lrwxrwxrwx.  1 root root   13 Apr 14  2014 /etc/rc.local -> rc.d/rc.local
lrwxrwxrwx.  1 root root   15 Apr 14  2014 /etc/rc.sysinit -> rc.d/rc.sysinit
提示:这些文件都是链接文件;
[root@Smoke ~]# ls -ld /etc/rc.d/*(查看/etc/rc.d/目录下所有文件详细信息)
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/init.d
-rwxr-xr-x. 1 root root  2617 Feb 20  2014 /etc/rc.d/rc
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc0.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc1.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc2.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc3.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc4.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc5.d
drwxr-xr-x. 2 root root  4096 Sep 22 20:54 /etc/rc.d/rc6.d
-rwxr-xr-x. 1 root root   220 Feb 20  2014 /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 19688 Feb 20  2014 /etc/rc.d/rc.sysinit
[root@Smoke ~]# cd /etc/rc.d/rc3.d/(切换到/etc/rc.d/rc3.d目录)
[root@Smoke rc3.d]# ls -l(查看当前目录下文件及子目录详细信息)
total 0
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K01smartd -> ../init.d/smartd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K05wdaemon -> ../init.d/wdaemon
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K10psacct -> ../init.d/psacct
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx. 1 root root 24 Apr 14  2014 K30spice-vdagentd -> ../init.d/spice-vdagentd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K50dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K50snmpd -> ../init.d/snmpd
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K50snmptrapd -> ../init.d/snmptrapd
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K69rpcsvcgssd -> ../init.d/rpcsvcgssd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K73winbind -> ../init.d/winbind
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K74ntpd -> ../init.d/ntpd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K75ntpdate -> ../init.d/ntpdate
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K75quota_nld -> ../init.d/quota_nld
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K76ypbind -> ../init.d/ypbind
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K80kdump -> ../init.d/kdump
lrwxrwxrwx  1 root root 24 Sep 22 20:54 K84wpa_supplicant -> ../init.d/wpa_supplicant
lrwxrwxrwx. 1 root root 21 Apr 14  2014 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx  1 root root 14 Sep 22 20:54 K88sssd -> ../init.d/sssd
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K95firstboot -> ../init.d/firstboot
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K99rngd -> ../init.d/rngd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S01sysstat -> ../init.d/sysstat
lrwxrwxrwx. 1 root root 22 Apr 14  2014 S02lvm2-monitor -> ../init.d/lvm2-monitor
lrwxrwxrwx. 1 root root 22 Apr 14  2014 S03vmware-tools -> ../init.d/vmware-tools
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S08ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Apr 14  2014 S08iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S10network -> ../init.d/network
lrwxrwxrwx. 1 root root 16 Apr 14  2014 S11auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 21 Apr 14  2014 S11portreserve -> ../init.d/portreserve
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S12rsyslog -> ../init.d/rsyslog
lrwxrwxrwx. 1 root root 18 Apr 14  2014 S13cpuspeed -> ../init.d/cpuspeed
lrwxrwxrwx. 1 root root 20 Apr 14  2014 S13irqbalance -> ../init.d/irqbalance
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S13rpcbind -> ../init.d/rpcbind
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S15mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 20 Apr 14  2014 S22messagebus -> ../init.d/messagebus
lrwxrwxrwx. 1 root root 24 Apr 14  2014 S23NetworkManager -> ../init.d/NetworkManager
lrwxrwxrwx  1 root root 17 Sep 22 20:54 S24nfslock -> ../init.d/nfslock
lrwxrwxrwx  1 root root 17 Sep 22 20:54 S24rpcgssd -> ../init.d/rpcgssd
lrwxrwxrwx. 1 root root 26 Apr 14  2014 S25blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 14 Apr 14  2014 S25cups -> ../init.d/cups
lrwxrwxrwx. 1 root root 15 Apr 14  2014 S25netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 15 Apr 14  2014 S26acpid -> ../init.d/acpid
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S26haldaemon -> ../init.d/haldaemon
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S26udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 16 Apr 14  2014 S28autofs -> ../init.d/autofs
lrwxrwxrwx  1 root root 13 Apr 20  2014 S30nfs -> ../init.d/nfs
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S50bluetooth -> ../init.d/bluetooth
lrwxrwxrwx. 1 root root 14 Apr 14  2014 S55sshd -> ../init.d/sshd
lrwxrwxrwx  1 root root 16 Apr 20  2014 S56xinetd -> ../init.d/xinetd
lrwxrwxrwx. 1 root root 32 Apr 14  2014 S57vmware-tools-thinprint -> ../init.d/vmware-tools-thinprint
lrwxrwxrwx  1 root root 16 May 14 20:24 S64mysqld -> ../init.d/mysqld
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S80postfix -> ../init.d/postfix
lrwxrwxrwx. 1 root root 19 Apr 14  2014 S82abrt-ccpp -> ../init.d/abrt-ccpp
lrwxrwxrwx. 1 root root 15 Apr 14  2014 S82abrtd -> ../init.d/abrtd
lrwxrwxrwx  1 root root 15 May 22 10:46 S88nrped -> ../init.d/nrped
lrwxrwxrwx. 1 root root 15 Apr 14  2014 S90crond -> ../init.d/crond
lrwxrwxrwx. 1 root root 13 Apr 14  2014 S95atd -> ../init.d/atd
lrwxrwxrwx  1 root root 15 Sep 22 20:54 S95jexec -> ../init.d/jexec
lrwxrwxrwx. 1 root root 20 Apr 14  2014 S99certmonger -> ../init.d/certmonger
lrwxrwxrwx. 1 root root 11 Apr 14  2014 S99local -> ../rc.local
提示:这些文件都是连接文件;
[root@Smoke rc3.d]# cd ..(切换到上一级目录)
[root@Smoke rc.d]# ls(查看当前目录文件及子目录)
init.d  rc  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit
提示:链接到父目录的init.d目录下;
[root@Smoke rc.d]# cd init.d/(切换到init.d目录)
[root@Smoke init.d]# ls(查看当前目录文件及子目录)
abrt-ccpp         cups        lvm2-lvmetad    ntpd         rpcgssd         sssd
abrtd             dnsmasq     lvm2-monitor    ntpdate      rpcidmapd       sysstat
abrt-oops         firstboot   mdmonitor       oddjobd      rpcsvcgssd      udev-post
acpid             functions   messagebus      portreserve  rsyslog         vmware-tools
atd               haldaemon   mysqld          postfix      sandbox         vmware-tools-thinprint
auditd            halt        netconsole      psacct       saslauthd       wdaemon
autofs            ip6tables   netfs           puppet       single          winbind
blk-availability  iptables    network         quota_nld    smartd          wpa_supplicant
bluetooth         irqbalance  NetworkManager  rdisc        snmpd           xinetd
certmonger        jexec       nfs             restorecond  snmptrapd       ypbind
cpuspeed          kdump       nfslock         rngd         spice-vdagentd
crond             killall     nrped           rpcbind      sshd
提示:这个目录下才是真正的脚本,这些脚本在每个级别下都有一个连接,每个连接都决定这样一个脚本,每个脚本代表一个服务,在开机时候会被执行的服务,这个服务到底在那个
级别会被启动起来取决于在rc#.d下它是以K开头还是以S开头,K开头表示切换到这个级别就把它关闭,S开头把它启动起来;
[root@Smoke init.d]# cd ..(切换到上级目录)
[root@Smoke rc.d]# ls(查看当前目录文件及子目录)
init.d  rc  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local  rc.sysinit
[root@Smoke rc.d]# ls -l rc0.d/(查看/rc0.d目录文件及子目录详细信息)
total 0
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K01certmonger -> ../init.d/certmonger
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K01smartd -> ../init.d/smartd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K02oddjobd -> ../init.d/oddjobd
lrwxrwxrwx. 1 root root 13 Apr 14  2014 K05atd -> ../init.d/atd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K05wdaemon -> ../init.d/wdaemon
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K10cups -> ../init.d/cups
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K10psacct -> ../init.d/psacct
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx  1 root root 15 May 22 10:46 K12nrped -> ../init.d/nrped
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K16abrt-ccpp -> ../init.d/abrt-ccpp
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K16abrtd -> ../init.d/abrtd
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K25sshd -> ../init.d/sshd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K30postfix -> ../init.d/postfix
lrwxrwxrwx. 1 root root 24 Apr 14  2014 K30spice-vdagentd -> ../init.d/spice-vdagentd
lrwxrwxrwx  1 root root 16 May 14 20:24 K36mysqld -> ../init.d/mysqld
lrwxrwxrwx. 1 root root 32 Apr 14  2014 K43vmware-tools-thinprint -> ../init.d/vmware-tools-thinprint
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K50dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K50netconsole -> ../init.d/netconsole
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K50snmpd -> ../init.d/snmpd
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K50snmptrapd -> ../init.d/snmptrapd
lrwxrwxrwx  1 root root 16 Apr 20  2014 K50xinetd -> ../init.d/xinetd
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K60crond -> ../init.d/crond
lrwxrwxrwx  1 root root 13 Apr 20  2014 K60nfs -> ../init.d/nfs
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K69rpcsvcgssd -> ../init.d/rpcsvcgssd
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K72autofs -> ../init.d/autofs
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K73winbind -> ../init.d/winbind
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K74acpid -> ../init.d/acpid
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K74haldaemon -> ../init.d/haldaemon
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K74ntpd -> ../init.d/ntpd
lrwxrwxrwx. 1 root root 26 Apr 14  2014 K75blk-availability -> ../init.d/blk-availability
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K75netfs -> ../init.d/netfs
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K75ntpdate -> ../init.d/ntpdate
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K75quota_nld -> ../init.d/quota_nld
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K75udev-post -> ../init.d/udev-post
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K76ypbind -> ../init.d/ypbind
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K80kdump -> ../init.d/kdump
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K83bluetooth -> ../init.d/bluetooth
lrwxrwxrwx  1 root root 17 Sep 22 20:54 K83nfslock -> ../init.d/nfslock
lrwxrwxrwx  1 root root 17 Sep 22 20:54 K83rpcgssd -> ../init.d/rpcgssd
lrwxrwxrwx. 1 root root 24 Apr 14  2014 K84NetworkManager -> ../init.d/NetworkManager
lrwxrwxrwx  1 root root 24 Sep 22 20:54 K84wpa_supplicant -> ../init.d/wpa_supplicant
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K85mdmonitor -> ../init.d/mdmonitor
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K85messagebus -> ../init.d/messagebus
lrwxrwxrwx. 1 root root 20 Apr 14  2014 K87irqbalance -> ../init.d/irqbalance
lrwxrwxrwx. 1 root root 21 Apr 14  2014 K87restorecond -> ../init.d/restorecond
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K87rpcbind -> ../init.d/rpcbind
lrwxrwxrwx. 1 root root 16 Apr 14  2014 K88auditd -> ../init.d/auditd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K88rsyslog -> ../init.d/rsyslog
lrwxrwxrwx  1 root root 14 Sep 22 20:54 K88sssd -> ../init.d/sssd
lrwxrwxrwx. 1 root root 21 Apr 14  2014 K89portreserve -> ../init.d/portreserve
lrwxrwxrwx. 1 root root 15 Apr 14  2014 K89rdisc -> ../init.d/rdisc
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K90network -> ../init.d/network
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K92ip6tables -> ../init.d/ip6tables
lrwxrwxrwx. 1 root root 18 Apr 14  2014 K92iptables -> ../init.d/iptables
lrwxrwxrwx. 1 root root 19 Apr 14  2014 K95firstboot -> ../init.d/firstboot
lrwxrwxrwx. 1 root root 18 Apr 14  2014 K99cpuspeed -> ../init.d/cpuspeed
lrwxrwxrwx. 1 root root 22 Apr 14  2014 K99lvm2-monitor -> ../init.d/lvm2-monitor
lrwxrwxrwx. 1 root root 14 Apr 14  2014 K99rngd -> ../init.d/rngd
lrwxrwxrwx. 1 root root 17 Apr 14  2014 K99sysstat -> ../init.d/sysstat
lrwxrwxrwx. 1 root root 22 Apr 14  2014 K99vmware-tools -> ../init.d/vmware-tools
lrwxrwxrwx. 1 root root 17 Apr 14  2014 S00killall -> ../init.d/killall
lrwxrwxrwx. 1 root root 14 Apr 14  2014 S01halt -> ../init.d/halt
lrwxrwxrwx  1 root root 15 Sep 22 20:54 S95jexec -> ../init.d/jexec
提示:在0级别几乎都是K开头的;
[root@Smoke rc.d]# ls rc6.d/(查看rc6.d目录文件及子目录)
K01certmonger  K30spice-vdagentd          K74acpid             K84NetworkManager  K92ip6tables
K01smartd      K36mysqld                  K74haldaemon         K84wpa_supplicant  K92iptables
K02oddjobd     K43vmware-tools-thinprint  K74ntpd              K85mdmonitor       K95firstboot
K05atd         K50dnsmasq                 K75blk-availability  K85messagebus      K99cpuspeed
K05wdaemon     K50netconsole              K75netfs             K87irqbalance      K99lvm2-monitor
K10cups        K50snmpd                   K75ntpdate           K87restorecond     K99rngd
K10psacct      K50snmptrapd               K75quota_nld         K87rpcbind         K99sysstat
K10saslauthd   K50xinetd                  K75udev-post         K88auditd          K99vmware-tools
K12nrped       K60crond                   K76ypbind            K88rsyslog         S00killall
K16abrt-ccpp   K60nfs                     K80kdump             K88sssd            S01reboot
K16abrtd       K69rpcsvcgssd              K83bluetooth         K89portreserve     S95jexec
K25sshd        K72autofs                  K83nfslock           K89rdisc
K30postfix     K73winbind                 K83rpcgssd           K90network
提示:在6级别几乎也都是K开头的;

内核设计风格:

RedHat, SUSE

核心:动态加载 内核模块

内核:/lib/modules/"内核版本号命令的目录"/

vmlinuz-2.6.32

/lib/modules/2.6.32/

RedHat5: ramdisk-->initrd

RedHat6: ramfs-->initramfs

单内核:Linux (LWP)

  核心:ko(kernel object)
  so()

  微内核:Windows, Solaris (线程)

chroot: chroot /PATH/TO/TEMPROOT [COMMAND...]

  chroot /test/virrrot /bin/bash

 

ldd /PATH/TO/BINARY_FILE:显示二进制文件所依赖的共享库

MBR(bootloader)--> Kernel --> initrd(initramfs) --> (ROOTFS) --> /sbin/init(/etc/inittab)

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

  upstart

init /etc/inittab

id:runlevels:action:process

id:5:initdefault:

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

完成OS初始化

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

  rc0.d/

    K*

      stop

    S*

      start

/etc/rc.d/init.d, /etc/init.d

服务类脚本:

  start

  SysV: /etc/rc.d/init.d

    start|stop|restart|status

    reload|configtest

这类脚本靠这两行能够实现成为系统服务

chkconfig:定义它能够接受另外一个命令的控制并自动在对应rc#.d rc1.d rc2.d...给它创建链接,是K开头还是S开头,也能够通过命令实现;

 

# chkconfig: runlevels(默认启动级别) SS(启动优先次序) KK(关闭的优先次序)

当chkconfig命令来为此脚本在rc#.d目录创建链接时,runlevels表示默认创建为S*开头的链接,-表示没有级别默认为S*开头的链接;除此之外的级别默认创建为K*开头的链接;

S后面的启动优先级为SS所表示的数字;K后面关闭优先次序为KK所表示的数字;

# description: 用于说明此脚本的简单功能; \, 续行,不能超过80个字符一行,否则需要续行;

chkconfig --list: 查看所有独立守护服务的启动设定;独立守护进程!

chkconfig --list SERVICE_NAME:指定查询某个服务启动设定

 

chkconfig --add SERVICE_NAME:给脚本自动创建链接,把它加到服务列表中

chkconfig --del SERVICE_NAME:删除服务,删除连接服务

chkconfig [--level RUNLEVELS] SERVICE_NAME {on|off}

指定级别启动和关闭,如果省略级别指定,默认为2345级别;

样例脚本:

#!/bin/bash
#
# chkconfig: 2345 77 22
# description: Test Service
#
LOCKFILE=/var/lock/subsys/myservice
status() {
 if [ -e $LOCKFILE ]; then
   echo "Running..."
 else
   echo "Stopped."
 fi
}
usage() {
 echo "`basename $0` {start|stop|restart|status}"
}
case $1 in
start)
 echo "Starting..."
 touch $LOCKFILE ;;
stop)
 echo "Stopping..."
 rm -f $LOCKFILE &> /dev/null
 ;;
restart)
 echo "Restarting..." ;;
status)
 status ;;
*)
 usage ;;
esac

/etc/rc.d/rc.local:系统最后启动的一个服务,准确说,应该执行的一个脚本,不方便定义成服务的,或不会写脚本的,但是期望它开机能够自动执行的命令,可以都写到这个文件里面,到最后都会执行一次;

/etc/inittab的任务:

1、设定默认运行级别;

2、运行系统初始化脚本;

3、运行指定运行级别对应的目录下的脚本;

4、设定Ctrl+Alt+Del组合键的操作;

5、定义UPS电源在电源故障/恢复时执行的操作;

6、启动虚拟终端(2345级别);

7、启动图形终端(5级别);

守护进程的类型:

  独立守护进程

  xinetd:超级守护进程,代理人

    瞬时守护进程:不需要关联至运行级别

[root@Smoke ~]# cd /etc/rc.d/init.d/(切换到/etc/rc.d/init.d目录)
[root@Smoke init.d]# ls(查看当前目录文件及子目录)
abrt-ccpp         cups        lvm2-lvmetad    ntpd         rpcgssd         sssd
abrtd             dnsmasq     lvm2-monitor    ntpdate      rpcidmapd       sysstat
abrt-oops         firstboot   mdmonitor       oddjobd      rpcsvcgssd      udev-post
acpid             functions   messagebus      portreserve  rsyslog         vmware-tools
atd               haldaemon   mysqld          postfix      sandbox         vmware-tools-thinprint
auditd            halt        netconsole      psacct       saslauthd       wdaemon
autofs            ip6tables   netfs           puppet       single          winbind
blk-availability  iptables    network         quota_nld    smartd          wpa_supplicant
bluetooth         irqbalance  NetworkManager  rdisc        snmpd           xinetd
certmonger        jexec       nfs             restorecond  snmptrapd       ypbind
cpuspeed          kdump       nfslock         rngd         spice-vdagentd
crond             killall     nrped           rpcbind      sshd
[root@Smoke init.d]# head acpid anacron(查看acpid和anacron文件前10行)
==> acpid <==
#!/bin/bash
#
#        /etc/rc.d/init.d/acpid
#
# Starts the acpi daemon
#
# chkconfig: 345 26 74
# description: Listen and dispatch ACPI events from the kernel
# processname: acpid
==> acpid <==
#!/bin/bash
# Startup script for anacron
#
# chckonfig: 2345 95 05
# description Run cron jobs that were left out due to downtime
# pidfile: /var/run/anacron.pid
#
# Source function library.
. /etc/rc.d/init.d/functions
提示:两个文件都有# chckonfig: 2345 95 05和# description Run cron jobs that were left out due to downtime两行;
[root@Smoke ~]# vim myservice.sh(编辑myservice.sh文件)
#!/bin/bash
#
# chkconfig: 2345 77 22(SS和KK数值之和只要等于99,一般先开启的需要后关闭,后开启的先关闭,因为先开启的被其他服务所依赖)
# description: Test Service
#
LOCKFILE=/var/lock/subsys/myservice
status() {
       if [ -e $LOCKFILE ];then
       echo "Running..."
       else
       echo "Stopped."
       fi
}
usage() {
       echo "`basename $0` {start|stop|restart|status}"
}
case $1 in
start)
       echo "Starting..."  
       touch $LOCKFILE ;;
stop)
       echo "Stopping..."
       rm -f $LOCKFILE &> /dev/null ;;
restart)
       echo "Restarting..." ;;
status)
       status ;;
*)
       usges ;;
esac
[root@Smoke ~]# ls /var/lock/subsys/(查看/var/lock/subsys目录文件及子目录)
abrt-ccpp  auditd            crond      iptables      mysqld          nfs         rpc.mountd   sshd
abrtd      autofs            cups       local         netfs           postfix     rpc.rquotad  xinetd
acpid      blk-availability  haldaemon  lvm2-monitor  network         rpcbind     rpc.statd
atd        certmonger        ip6tables  messagebus    NetworkManager  rpc.idmapd  rsyslog
提示:一般启动一个服务都会在/var/lock/subsys/下创建一个锁文件;
[root@Smoke ~]# chmod +x myservice.sh(给myservice.sh执行权限)
[root@Smoke ~]# ./myservice.sh start(执行myservice.sh文件)
Starting...
[root@Smoke ~]# ./myservice.sh status
Running...
[root@Smoke ~]# ./myservice.sh stop
Stopping...
[root@Smoke ~]# ./myservice.sh status
Stopped.
[root@Smoke ~]# cp myservice.sh /etc/rc.d/init.d/myservice(复制myservice.sh文件到/etc/rc.dinit.d/目录改名为myservice)
[root@Smoke ~]# chkconfig --list(列出系统当中所有服务)
NetworkManager        0:off        1:off        2:on        3:on        4:on        5:on        6:off
abrt-ccpp              0:off        1:off        2:off        3:on        4:off        5:on        6:off
abrtd                  0:off        1:off        2:off        3:on        4:off        5:on        6:off
acpid                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
atd                    0:off        1:off        2:off        3:on        4:on        5:on        6:off
auditd                0:off        1:off        2:on        3:on        4:on        5:on        6:off
autofs                0:off        1:off        2:off        3:on        4:on        5:on        6:off
blk-availability        0:off        1:on        2:on        3:on        4:on        5:on        6:off
bluetooth              0:off        1:off        2:off        3:on        4:on        5:on        6:off
certmonger            0:off        1:off        2:off        3:on        4:on        5:on        6:off
cpuspeed              0:off        1:on        2:on        3:on        4:on        5:on        6:off
crond                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
cups                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
dnsmasq                0:off        1:off        2:off        3:off        4:off        5:off        6:off
firstboot              0:off        1:off        2:off        3:off        4:off        5:off        6:off
haldaemon              0:off        1:off        2:off        3:on        4:on        5:on        6:off
ip6tables              0:off        1:off        2:on        3:on        4:on        5:on        6:off
iptables              0:off        1:off        2:on        3:on        4:on        5:on        6:off
提示:每一个服务都有一个脚本名,而且会显示在对应级别下是start还是stop的;
[root@Smoke ~]# chkconfig --list network(查看network服务启动设定)
network                0:off        1:off        2:on        3:on        4:on        5:on        6:off
[root@Smoke ~]# cd /etc/rc.d/init.d/(切换到/etc/rc.d/init.d目录)
[root@Smoke init.d]# ls myservice(查看myservice文件)
myservice
[root@Smoke init.d]# cd ../(切换到上级目录)
[root@Smoke rc.d]# ls rc3.d/(查看rc3.d目录中有没有K开头或S开头的myservice)
K01smartd          K75ntpdate         S08ip6tables       S24rpcgssd                 S64mysqld
K02oddjobd         K75quota_nld       S08iptables        S25blk-availability        S80postfix
K05wdaemon         K76ypbind          S10network         S25cups                    S82abrt-ccpp
K10psacct          K80kdump           S11auditd          S25netfs                   S82abrtd
K10saslauthd       K84wpa_supplicant  S11portreserve     S26acpid                   S88nrped
K30spice-vdagentd  K87restorecond     S12rsyslog         S26haldaemon               S90crond
K50dnsmasq         K88sssd            S13cpuspeed        S26udev-post               S95atd
K50netconsole      K89rdisc           S13irqbalance      S28autofs                  S95jexec
K50snmpd           K95firstboot       S13rpcbind         S30nfs                     S99certmonger
K50snmptrapd       K99rngd            S15mdmonitor       S50bluetooth               S99local
K69rpcsvcgssd      S01sysstat         S22messagebus      S55sshd
K73winbind         S02lvm2-monitor    S23NetworkManager  S56xinetd
K74ntpd            S03vmware-tools    S24nfslock         S57vmware-tools-thinprint
提示:没有将myservice放到列表当中没有创建链接文件;
[root@Smoke rc.d]# find ./ -name "*myservice*"(查看当前目录下有没有myservice文件)
./init.d/myservice
[root@Smoke rc.d]# chkconfig --add myservice(给myservice脚本创建链接文件)
[root@Smoke rc.d]# find ./ -name "*myservice*"(查看当前目录下有没有myservice文件)
./rc0.d/K22myservice
./rc4.d/S77myservice
./rc2.d/S77myservice
./rc5.d/S77myservice
./rc6.d/K22myservice
./rc1.d/K22myservice
./rc3.d/S77myservice
./init.d/myservice
[root@Smoke rc.d]# chkconfig --del myservice(删除服务的链接文件)
[root@Smoke rc.d]# find ./ -name "*myservice*"(查看当前目录下有没有myservice文件)
./init.d/myservice
[root@Smoke rc.d]# chkconfig --add myservice(给myservice脚本创建链接文件)
[root@Smoke rc.d]# find ./ -name "*myservice*"(查看当前目录下有没有myservice文件)
./rc0.d/K22myservice
./rc4.d/S77myservice
./rc2.d/S77myservice
./rc5.d/S77myservice
./rc6.d/K22myservice
./rc1.d/K22myservice
./rc3.d/S77myservice
./init.d/myservice
[root@Smoke rc.d]# chkconfig --list myservice(查看myservice服务启动设定)
myservice              0:off        1:off        2:on        3:on        4:on        5:on        6:off
[root@Smoke rc.d]# chkconfig --level 24 myservice off(指定在24级别myservice服务为off)
[root@Smoke rc.d]# chkconfig --list myservice(查看myservice服务启动设定)
myservice              0:off        1:off        2:off        3:on        4:off        5:on        6:off
[root@Smoke init.d]# cd ../rc3.d/(切换到rc3.d目录)
[root@Smoke rc3.d]# ls(查看目录下的文件及子目录)
K01smartd          K75ntpdate         S08ip6tables       S24rpcgssd                 S64mysqld
K02oddjobd         K75quota_nld       S08iptables        S25blk-availability        S77myservice
K05wdaemon         K76ypbind          S10network         S25cups                    S80postfix
K10psacct          K80kdump           S11auditd          S25netfs                   S82abrt-ccpp
K10saslauthd       K84wpa_supplicant  S11portreserve     S26acpid                   S82abrtd
K30spice-vdagentd  K87restorecond     S12rsyslog         S26haldaemon               S88nrped
K50dnsmasq         K88sssd            S13cpuspeed        S26udev-post               S90crond
K50netconsole      K89rdisc           S13irqbalance      S28autofs                  S95atd
K50snmpd           K95firstboot       S13rpcbind         S30nfs                     S95jexec
K50snmptrapd       K99rngd            S15mdmonitor       S50bluetooth               S99certmonger
K69rpcsvcgssd      S01sysstat         S22messagebus      S55sshd                    S99local
K73winbind         S02lvm2-monitor    S23NetworkManager  S56xinetd
K74ntpd            S03vmware-tools    S24nfslock         S57vmware-tools-thinprint
[root@Smoke rc3.d]# ll S99local(查看S99local文件相信信息)
lrwxrwxrwx. 1 root root 11 Apr 14  2014 S99local -> ../rc.local
[root@Smoke rc3.d]# cd ..(切换到上级目录)
[root@Smoke rc.d]# ls -l /etc/rc.local(查看/etc/rc.local文件详细信息)
lrwxrwxrwx. 1 root root 13 Apr 14  2014 /etc/rc.local -> rc.d/rc.local
[root@Smoke rc.d]# vim rc.local(编辑rc.local文件)
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
touch /tmp/file-`date +%s.txt
提示:rc.local文件是系统启动最后要执行的脚本,这是系统启动完成以后最后要执行的脚本,不方便定义成服务的,或不会写脚本的,但是期望它开机能够自动执行的命令,
可以都写到这个文件里面,到最后都会执行一次;
[root@Smoke rc.d]# vim /etc/inittab(编辑/etc/inittab文件,此文件为centos 5.x版本/etc/inittab文件内容)
  #
  # inittab This file describes how the INIT process should set up
  # the system in a certain run-level.
  #
  # Author: Miquel van Smoorenburg, 〈miquels@drinkel.nl.mugnet.org〉
  # Modified for RHS Linux by Marc Ewing and Donnie Barnes
  #
  # Default runlevel. The runlevels used by RHS are:
  # 0 - halt (Do NOT set initdefault to this)
  # 1 - Single user mode
  # 2 - Multiuser, without NFS (The same as 3, if you do not havenetworking)
  # 3 - Full multiuser mode
  # 4 - unused
  # 5 - X11
  # 6 - reboot (Do NOT set initdefault to this)
  #
  ###表示当前缺省运行级别为5(initdefault);
  id:5:initdefault:
  ###启动时自动执行/etc/rc.d/rc.sysinit脚本(sysinit) 主要完成的工作有:激活交换分区,检查磁盘,加载硬件模块以及其它一些需要优先执行任务
  # System initialization.
  si::sysinit:/etc/rc.d/rc.sysinit
  l0:0:wait:/etc/rc.d/rc 0
  l1:1:wait:/etc/rc.d/rc 1
  l2:2:wait:/etc/rc.d/rc 2
  l3:3:wait:/etc/rc.d/rc 3
  l4:4:wait:/etc/rc.d/rc 4
  ###当运行级别为5时,以5为参数运行/etc/rc.d/rc脚本,init将等待其返回(wait)
  l5:5:wait:/etc/rc.d/rc 5
  l6:6:wait:/etc/rc.d/rc 6
  ###在启动过程中允许按CTRL-ALT-DELETE重启系统
  # Trap CTRL-ALT-DELETE
  ca::ctrlaltdel:/sbin/shutdown -t3 -r now
  # When our UPS tells us power has failed, assume we have a few minutes
  # of power left. Schedule a shutdown for 2 minutes from now.
  # This does, of course, assume you have powerd installed and your
  # UPS connected and working correctly.
  pf::powerfail(停电):/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down" (关机)
  # If power was restored before the shutdown kicked in, cancel it.
  pr:12345:powerokwait(来电):/sbin/shutdown -c "Power Restored; Shutdown Cancelled" (取消关机)
  ###在2、3、4、5级别上以ttyX为参数执行/sbin/mingetty程序,打开ttyX终端用于用户登录,
  ###如果进程退出则再次运行mingetty程序(respawn)
  # Run gettys in standard runlevels
  1:2345:respawn:/sbin/mingetty tty1 (主要用于控制终端登录,最多支持6个终端,可更改终端个数)
  2:2345:respawn:/sbin/mingetty tty2
  3:2345:respawn:/sbin/mingetty tty3
  4:2345:respawn:/sbin/mingetty tty4
  5:2345:respawn:/sbin/mingetty tty5
  6:2345:respawn:/sbin/mingetty tty6
  ###在5级别上运行xdm程序,提供xdm图形方式登录界面,并在退出时重新执行(respawn)
  # Run xdm in runlevel 5
  x:5:respawn:/etc/X11/prefdm -nodaemon
  以上面的inittab文件为例,来说明一下inittab的格式。其中以#开始的行是注释行,除了注释行之外,每一行都有以下格式:
  id:runlevel:action:process
  对上面各项的详细解释如下:
  1. id
  id是指入口标识符,它是一个字符串,对于getty或mingetty等其他login程序项,要求id与tty的编号相同,否则getty程序将不能正常工作。
  2. runlevel
  runlevel是init所处于的运行级别的标识,一般使用0-6以及S或s。0、1、6运行级别被系统保留:其中0作为shutdown动作,1作为重启至单用户模式,6为重启;S和s
意义相同,表示单用户模式,且无需inittab文件,因此也不在inittab中出现,实际上,进入单用户模式时,init直接在控制台(/dev/console)上运行/sbin/sulogin。
在一般的系统实现中,都使用了2、3、4、5几个级别,在Redhat系统中,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表
示XDM图形登录方式。7-9级别也是可以使用的,传统的Unix系统没有定义这几个级别。runlevel可以是并列的多个值,以匹配多个运行级别,对大多数action来说,仅当
runlevel与当前运行级别匹配成功才会执行。
  3. action
  action是描述其后的process的运行方式的。action可取的值包括:initdefault、sysinit、boot、bootwait等:
  initdefault是一个特殊的action值,用于标识缺省的启动级别;当init由核心激活以后,它将读取inittab中的initdefault项,取得其中的runlevel,并作为当前
的运行级别。如果没有inittab文件,或者其中没有initdefault项,init将在控制台上请求输入runlevel。
  sysinit、boot、bootwait等action将在系统启动时无条件运行,而忽略其中的runlevel。
  其余的action(不含initdefault)都与某个runlevel相关。各个action的定义在inittab的man手册中有详细的描述。
  4. process
  process为具体的执行程序。程序后面可以带参数。
[root@Smoke rc.d]# mingetty(启用终端)
[root@Smoke rc.d]# mingetty tty7(启动终端tty7)
[root@Smoke rc.d]# yum -y install xinetd(安装xinetd超级守护进程,-y所有询问都回答yes)
[root@Smoke rc.d]# chkconfig --list xinetd(查看xinetd服务启动设定)
xinetd                0:off        1:off        2:off        3:on        4:on        5:on        6:off
[root@Smoke rc.d]# service xinetd start(启动xinetd服务)
Starting xinetd:
[root@Smoke rc.d]# chkconfig --list(查看系统启动服务)
NetworkManager        0:off        1:off        2:on        3:on        4:on        5:on        6:off
abrt-ccpp              0:off        1:off        2:off        3:on        4:off        5:on        6:off
abrtd                  0:off        1:off        2:off        3:on        4:off        5:on        6:off
acpid                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
atd                    0:off        1:off        2:off        3:on        4:on        5:on        6:off
auditd                0:off        1:off        2:on        3:on        4:on        5:on        6:off
autofs                0:off        1:off        2:off        3:on        4:on        5:on        6:off
blk-availability        0:off        1:on        2:on        3:on        4:on        5:on        6:off
bluetooth              0:off        1:off        2:off        3:on        4:on        5:on        6:off
certmonger            0:off        1:off        2:off        3:on        4:on        5:on        6:off
cpuspeed              0:off        1:on        2:on        3:on        4:on        5:on        6:off
crond                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
cups                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
dnsmasq                0:off        1:off        2:off        3:off        4:off        5:off        6:off
firstboot              0:off        1:off        2:off        3:off        4:off        5:off        6:off
haldaemon              0:off        1:off        2:off        3:on        4:on        5:on        6:off
ip6tables              0:off        1:off        2:on        3:on        4:on        5:on        6:off
iptables              0:off        1:off        2:on        3:on        4:on        5:on        6:off
irqbalance            0:off        1:off        2:off        3:on        4:on        5:on        6:off
jexec                  0:on        1:on        2:on        3:on        4:on        5:on        6:on
kdump                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
lvm2-monitor          0:off        1:on        2:on        3:on        4:on        5:on        6:off
mdmonitor              0:off        1:off        2:on        3:on        4:on        5:on        6:off
messagebus            0:off        1:off        2:on        3:on        4:on        5:on        6:off
myservice              0:off        1:off        2:off        3:on        4:off        5:on        6:off
mysqld                0:off        1:off        2:on        3:on        4:on        5:on        6:off
netconsole            0:off        1:off        2:off        3:off        4:off        5:off        6:off
netfs                  0:off        1:off        2:off        3:on        4:on        5:on        6:off
network                0:off        1:off        2:on        3:on        4:on        5:on        6:off
nfs                    0:off        1:off        2:on        3:on        4:on        5:on        6:off
nfslock                0:off        1:off        2:off        3:on        4:on        5:on        6:off
nrped                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
ntpd                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
ntpdate                0:off        1:off        2:off        3:off        4:off        5:off        6:off
oddjobd                0:off        1:off        2:off        3:off        4:off        5:off        6:off
portreserve            0:off        1:off        2:on        3:on        4:on        5:on        6:off
postfix                0:off        1:off        2:on        3:on        4:on        5:on        6:off
psacct                0:off        1:off        2:off        3:off        4:off        5:off        6:off
quota_nld              0:off        1:off        2:off        3:off        4:off        5:off        6:off
rdisc                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
restorecond            0:off        1:off        2:off        3:off        4:off        5:off        6:off
rngd                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
rpcbind                0:off        1:off        2:on        3:on        4:on        5:on        6:off
rpcgssd                0:off        1:off        2:off        3:on        4:on        5:on        6:off
rpcsvcgssd            0:off        1:off        2:off        3:off        4:off        5:off        6:off
rsyslog                0:off        1:off        2:on        3:on        4:on        5:on        6:off
saslauthd              0:off        1:off        2:off        3:off        4:off        5:off        6:off
smartd                0:off        1:off        2:off        3:off        4:off        5:off        6:off
snmpd                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
snmptrapd              0:off        1:off        2:off        3:off        4:off        5:off        6:off
spice-vdagentd        0:off        1:off        2:off        3:off        4:off        5:on        6:off
sshd                  0:off        1:off        2:on        3:on        4:on        5:on        6:off
sssd                  0:off        1:off        2:off        3:off        4:off        5:off        6:off
sysstat                0:off        1:on        2:on        3:on        4:on        5:on        6:off
udev-post              0:off        1:on        2:on        3:on        4:on        5:on        6:off
vmware-tools          0:off        1:off        2:on        3:on        4:on        5:on        6:off
vmware-tools-thinprint        0:off        1:off        2:on        3:on        4:on        5:on        6:off
wdaemon                0:off        1:off        2:off        3:off        4:off        5:off        6:off
winbind                0:off        1:off        2:off        3:off        4:off        5:off        6:off
wpa_supplicant        0:off        1:off        2:off        3:off        4:off        5:off        6:off
xinetd                0:off        1:off        2:off        3:on        4:on        5:on        6:off
ypbind                0:off        1:off        2:off        3:off        4:off        5:off        6:off
xinetd based services:(瞬时守护进程)
       chargen-dgram:        off
       chargen-stream:        off
       daytime-dgram:        off
       daytime-stream:        off
       discard-dgram:        off
       discard-stream:        off
       echo-dgram:            off
       echo-stream:          off
       rsync:                off
       tcpmux-server:        off
       time-dgram:            off
       time-stream:          off
[root@Smoke rc.d]# chkconfig rsync on(启动瞬时守护进程sync服务)
[root@Smoke rc.d]# chkconfig --list(查看系统启动进程启动设定)
xinetd based services:
       chargen-dgram:        off
       chargen-stream:        off
       daytime-dgram:        off
       daytime-stream:        off
       discard-dgram:        off
       discard-stream:        off
       echo-dgram:            off
       echo-stream:          off
       rsync:                on
       tcpmux-server:        off
       time-dgram:            off
       time-stream:          off

核心:/boot/vmlinuz-version

内核模块(ko): /lib/modules/version/

内核设计:

单内核

模块化设计

微内核

装载模块:

insmod

modprobe

www.kernel.org :内核网站

内核版本:偶数为稳定版,奇数为开发版

2.4:稳定版

2.5:开发版

2.6以后分类:会同时维护两个分支

2.6.17:稳定版

2.6.18-rc1:修订号升级称为18,rc表示发行候选,正处于修订阶段,可能往里面补充新特性,修订一些BUG等等;

2.6.17.1:稳定版,1表示次修订号,每一次修订代表修订一些BUG;

uname -r: 查看内核版本

用户空间访问、监控内核的方式:

/proc, /sys:跟内核打交道的机制,通过用户空间来完成的;

/sys:伪文件系统

/proc/sys: 大多数目录文件都是只读的,此目录中的文件很多是可读写的

/sys/: 大多数目录文件都是只读的,某些文件可写

设定内核参数值的方法:修改后重启系统都会丢失,因为内核在内存当中,修改以后保存在内存中;

echo VALUE > /proc/sys/TO/SOMEFILE

sysctl -w kernel.hostname=

能立即生效,但无法永久有效;

永久有效:/etc/sysctl.conf

修改文件完成之后,执行如下命令可立即生效:

sysctl -p:让内核重读sysctl.conf配置文件

sysctl -a: 显示所有内核参数及其值

内核模块管理:

lsmod: 查看内核装载那些模块

modprobe MOD_NAME:装载某模块,不需要指定模块路径

modprobe -r MOD_NAME: 卸载某模块,不需要指定模块路径

modinfo MOD_NAME: 查看模块的具体信息

insmod /PATH/TO/MODULE_FILE: 装载模块,需要指定模块路径

rmmod MOD_NAME:移除模块,不需要指定模块路径

depmod /PATH/TO/MODILES_DIR:生成模块间的依赖关系,生成文件之后会保存到这个目录当中;

在linux上众多的驱动程序其实都是内核模块,如果要给某个网卡装驱动,下载驱动,下载的是源代码,需要编译成ko的模式,内核模块必须要跟内核完全匹配,在那个版本的内核使用需要在那个版本内核上编译;

内核中的功能除了核心功能之外,在编译时,大多功能都有三种选择:

1、不使用此功能(不编译不选择不启用);

2、编译成内核模块;

3、编译进内核(成为内核的一部分);

如何手动编译内核:

图形界面编译:

make gconfig: Gnome桌面环境使用,需要安装图形开发库组:GNOME Software Development

make kconfig: KDE桌面环境使用,需要安装图形开发库:GNOME Software Development

make menuconfig: 内核编译文本图形菜单

make

make modules_install:安装内核模块

make install:安装内核

screen命令:能够在当前远程连接窗口模拟好几个窗口

screen -ls: 显示已经建立的屏幕

screen: 直接打开一个新的屏幕

  Ctrl+a, d: 拆除屏幕

screen -r ID: 还原回某屏幕

exit: 退出

二次编译时清理,清理前,如果有需要,请备份配置文件.config:

make clean:清理此前编译好的二进制模块

make mrproper:清理此前编译好的所残留的任何操作,还会将.config文件清理;

grub-->kernel-->initrd-->ROOTFS(/sbin/init, /bin/bash)

创建initrd(父进程)文件:redhat 5.x和redhat 6.x都支持;但是redhat 6.x

mkinitrd initrd文件路径 内核版本号

mkinitrd /boot/initrd-`uname -r`.img `uname -r`:是在当前系统内核创建;对uname -r显示内核版本,创建initrd文件版本号和内核版本号必须一致

bash截取变量的字符串:

${parameter#*word} (parameter变量名,word关键字,分隔符,*星号在word左面,截取一个字符串以word为分隔符,如果使用一个#井号表示从左往右取,取消从左往右第一个word前面的内容)

${parameter##*word}(两个#井号从左往右匹配找到最后一次出现word分隔符,取消从左往右最后一个word关键字前面的内容)

  The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of

  parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ?. € .?.

  case) or the longest matching pattern (the ?.deleted. If parameter is @ or *, the pattern removal operation is

  applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable sub-

  scripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the

  resultant list.

FILE=/usr/local/src:/分隔符,word就是分隔符,

${FILE#*/}: usr/local/src 从左往右匹配第一个/斜线,取消第一个/斜线前面的内容

${FILE##*/}: src 从左往右匹配最后一个/斜线,取消最后一个/斜线前面的内容

${FILE%/*}: /usr/local 从右往左匹配第一个/斜线,取消第一个/斜线后面的内容

${FILE%%/*}: 从右往左匹配最后一个/斜线,取消最后一个/斜线后面的内容

${parameter%word*}(parameter变量名,word关键字,分隔符,*星号在word右面,从右向左匹配找到第一个word关键字,取消第一个word关键字后面的内容)

${parameter%%word*}(两个#井号从右向左匹配找到最后一次出现word分割器,取消从右往左最后一个word关键字后面的内容)

  The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the

  expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching

  pattern (the ?.or the longest matching pattern (the ?.deleted. If parameter is @ or *, the pattern

  removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is

  an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and

  the expansion is the resultant list.

复制二进制程序及其依赖的库文件的脚本:

#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
 LIBPATH=${1%/*}
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."
}
bincp() {
 CMDPATH=${1%/*}
 [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
 [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH
 for LIB in  `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do
   libcp $LIB
 done
}
read -p "Your command: " CMD
until [ $CMD == 'q' ]; do
  ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again:" CMD && continue
 COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`
 bincp $COMMAND
 echo "copy $COMMAND finished."
 read -p "Continue: " CMD
done
[root@Smoke ~]# ls -l /proc/(查看/proc目录文件及子目录详细信息)
-rw-r--r--  1 root      root         0 Nov  9 22:46 slabinfo
-r--r--r--  1 root      root         0 Nov  9 22:46 softirqs
-r--r--r--  1 root      root         0 Oct 25 22:40 stat
-r--r--r--  1 root      root         0 Nov  9 22:46 swaps
dr-xr-xr-x  1 root      root         0 Oct 24 00:13 sys
--w-------  1 root      root         0 Nov  9 22:46 sysrq-trigger
dr-xr-xr-x  2 root      root         0 Nov  9 22:46 sysvipc
-r--r--r--  1 root      root         0 Nov  9 22:46 timer_list
-rw-r--r--  1 root      root         0 Nov  9 22:46 timer_stats
dr-xr-xr-x  4 root      root         0 Nov  9 22:46 tty
-r--r--r--  1 root      root         0 Nov  9 22:46 uptime
-r--r--r--  1 root      root         0 Nov  9 22:46 version
-r--------  1 root      root         0 Nov  9 22:46 vmallocinfo
-r--r--r--  1 root      root         0 Oct 25 22:40 vmstat
-r--r--r--  1 root      root         0 Nov  9 22:46 zoneinfo
提示:/proc目录大多数文件都是只读的;[root@Smoke ~]# cd /proc/sys(切换到/proc/sys目录)
[root@Smoke sys]# ls -l(查看当前目录文件及子目录)
total 0
dr-xr-xr-x 0 root root 0 Oct 26 00:19 abi
dr-xr-xr-x 0 root root 0 Oct 24 00:13 crypto
dr-xr-xr-x 0 root root 0 Oct 26 00:19 debug
dr-xr-xr-x 0 root root 0 Oct 26 00:19 dev
dr-xr-xr-x 0 root root 0 Oct 24 00:13 fs
dr-xr-xr-x 0 root root 0 Oct 24 00:13 kernel
dr-xr-xr-x 0 root root 0 Oct 24 00:13 net
dr-xr-xr-x 0 root root 0 Oct 26 00:19 sunrpc
dr-xr-xr-x 0 root root 0 Oct 26 00:19 vm
[root@Smoke sys]# ls -l vm/(查看vm目录文件及子目录)
total 0
-rw-r--r-- 1 root root 0 Oct 26 00:19 block_dump
--w------- 1 root root 0 Oct 26 00:19 compact_memory
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_background_bytes
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_background_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_bytes
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_expire_centisecs
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 dirty_writeback_centisecs
-rw-r--r-- 1 root root 0 Oct 26 00:19 drop_caches(清理缓存)
-rw-r--r-- 1 root root 0 Oct 26 00:19 extfrag_threshold
-rw-r--r-- 1 root root 0 Oct 26 00:19 extra_free_kbytes
-rw-r--r-- 1 root root 0 Oct 26 00:19 hugepages_treat_as_movable
-rw-r--r-- 1 root root 0 Oct 26 00:19 hugetlb_shm_group
-rw-r--r-- 1 root root 0 Oct 26 00:19 laptop_mode
-rw-r--r-- 1 root root 0 Oct 26 00:19 legacy_va_layout
-rw-r--r-- 1 root root 0 Oct 26 00:19 lowmem_reserve_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 max_map_count
-rw-r--r-- 1 root root 0 Oct 26 00:19 memory_failure_early_kill
-rw-r--r-- 1 root root 0 Oct 26 00:19 memory_failure_recovery
-rw-r--r-- 1 root root 0 Oct 26 00:19 min_free_kbytes
-rw-r--r-- 1 root root 0 Oct 26 00:19 min_slab_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 min_unmapped_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 mmap_min_addr
-rw-r--r-- 1 root root 0 Oct 26 00:19 nr_hugepages
-rw-r--r-- 1 root root 0 Oct 26 00:19 nr_hugepages_mempolicy
-rw-r--r-- 1 root root 0 Oct 26 00:19 nr_overcommit_hugepages
-r--r--r-- 1 root root 0 Oct 26 00:19 nr_pdflush_threads
-rw-r--r-- 1 root root 0 Oct 26 00:19 numa_zonelist_order
-rw-r--r-- 1 root root 0 Oct 26 00:19 oom_dump_tasks
-rw-r--r-- 1 root root 0 Oct 26 00:19 oom_kill_allocating_task
-rw-r--r-- 1 root root 0 Oct 26 00:19 overcommit_memory
-rw-r--r-- 1 root root 0 Oct 26 00:19 overcommit_ratio
-rw-r--r-- 1 root root 0 Oct 26 00:19 page-cluster
-rw-r--r-- 1 root root 0 Oct 26 00:19 panic_on_oom
-rw-r--r-- 1 root root 0 Oct 26 00:19 percpu_pagelist_fraction
-rw-r--r-- 1 root root 0 Oct 26 00:19 scan_unevictable_pages
-rw-r--r-- 1 root root 0 Oct 26 00:19 stat_interval
-rw-r--r-- 1 root root 0 Oct 26 00:19 swappiness(在多大程度上使用交换内存)
-rw-r--r-- 1 root root 0 Oct 26 00:19 unmap_area_factor
-rw-r--r-- 1 root root 0 Oct 26 00:19 vfs_cache_pressure
-rw-r--r-- 1 root root 0 Oct 26 00:19 would_have_oomkilled
-rw-r--r-- 1 root root 0 Oct 26 00:19 zone_reclaim_mode
提示:对于管理员这里面文件是可写的,管理员可以修改这些文件,这些数据是内核参数,改完以后就可以修改内核的工作特性,要想往这些文件写数据,不能使用vm打开,要使用echo;
[root@Smoke sys]# free -m(查看内存使用情况,-m以MB为单位显示内存使用情况)
            total       used       free     shared    buffers     cached
Mem:           988        727        260          0        147        381
-/+ buffers/cache:        198        789
Swap:         1023          0       1023
[root@Smoke sys]# echo 1 > /proc/sys/vm/drop_caches(将1输入仅drop_caches文件)
[root@Smoke sys]# free -m(查看内存使用情况,-m以MB为单位显示内存使用情况)
            total       used       free     shared    buffers     cached
Mem:           988        211        776          0          1         20
-/+ buffers/cache:        189        798
Swap:         1023          0       1023
提示:缓存中的buffers和cached被清理;
[root@Smoke sys]# ls kernel/(查看kernel目录文件及子目录)
acct                    msgmax                        sched_domain
acpi_video_flags        msgmnb                        sched_features
auto_msgmni             msgmni                        sched_latency_ns
blk_iopoll              ngroups_max                   sched_migration_cost
bootloader_type         nmi_watchdog                  sched_min_granularity_ns
bootloader_version      osrelease                     sched_nr_migrate
cad_pid                 ostype                        sched_rr_timeslice_ms
compat-log              overflowgid                   sched_rt_period_us
core_pattern            overflowuid                   sched_rt_runtime_us
core_pipe_limit         panic                         sched_shares_window
core_uses_pid           panic_on_io_nmi               sched_time_avg
ctrl-alt-del            panic_on_oops                 sched_tunable_scaling
dmesg_restrict          panic_on_unrecovered_nmi      sched_wakeup_granularity_ns
domainname              perf_event_max_sample_rate    sem
exec-shield             perf_event_mlock_kb           shmall
ftrace_dump_on_oops     perf_event_paranoid           shmmax
ftrace_enabled          pid_max                       shmmni
hostname                poweroff_cmd                  shm_rmid_forced
hotplug                 print-fatal-signals           slow-work
hung_task_check_count   printk                        softlockup_panic
hung_task_panic         printk_delay                  stack_tracer_enabled
hung_task_timeout_secs  printk_ratelimit              sysrq
hung_task_warnings      printk_ratelimit_burst        tainted
io_delay_type           pty                           threads-max
kexec_load_disabled     random                        timer_migration
keys                    randomize_va_space            unknown_nmi_panic
kptr_restrict           real-root-dev                 usermodehelper
kstack_depth_to_print   sched_autogroup_enabled       version
max_lock_depth          sched_cfs_bandwidth_slice_us  vsyscall64
modprobe                sched_child_runs_first        watchdog
modules_disabled        sched_compat_yield            watchdog_thresh
[root@Smoke sys]# pwd(查看当前所处目录路径)
/proc/sys
[root@Smoke sys]# cat /proc/sys/kernel/hostname(查看/proc/sys/kernel/hostname文件内容)
Smoke.com
[root@Smoke sys]# hostname(查看主机名)
Smoke.com
[root@Smoke sys]# echo www.Smoke.com > /proc/sys/kernel/hostname(显示 www.Smoke.com 输入仅/proc/sys/hostname文件)
[root@Smoke sys]# hostname(查看主机名)
www.Smoke.com
[root@Smoke sys]# sysctl -w kernel.hostname=Smoke.com(-w更改/proc/sys/kernel目录文件hostname文件)
kernel.hostname = Smoke.com
[root@Smoke sys]# hostname(查看主机名)
Smoke.com
[root@Smoke sys]# sysctl -w vm.drop_caches=1(-w更改/pro/sys/vm目录文件drop_caches文件)
vm.drop_caches = 1
[root@Smoke sys]# cat /proc/sys/net/ipv4/ip_forward(查看/proc/sys/net/ipv4/ip_forward文件内容)
0
提示:ip_forward允许多块网卡之间转发数据包,常用于路由器和防火墙,0表示不启用,1表示启用;
[root@Smoke sys]# vim /etc/sysctl.conf(编辑sysctl.conf文件)
net.ipv4.ip_forward = 1
[root@Smoke sys]# cat /proc/sys/net/ipv4/ip_forward (查看ip_forward文件内容)
0
提示:更改sysctl.conf文件不会立即生效,但会永久有效
[root@Smoke sys]# sysctl -p(-p让内核重读sysctl.conf配置文件)
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
[root@Smoke sys]# cat /proc/sys/net/ipv4/ip_forward(查看ip_forward文件内容)
1
[root@Smoke sys]# sysctl -a | wc -l(-a显示所有内核参数及值,将输出结果送给wc -l统计列数)
901
[root@Smoke sys]# lsmod(列出当前系统装载那些模块)
Module(模块名称)                  Size(模块大小)  Used by(使用次数)
lp                      9242  0
rfcomm                 74119  4
sco                    18005  2
bridge                 83273  0
bnep                   16690  2
l2cap                  55714  16 rfcomm,bnep
nfsd                  309196  13
nfs_acl                 2647  1 nfsd
auth_rpcgss            44949  1 nfsd
exportfs                4236  1 nfsd
autofs4                26513  3
8021q                  25349  0
garp                    7152  1 8021q
stp                     2218  2 bridge,garp
llc                     5546  3 bridge,garp,stp
lockd                  73662  1 nfsd
sunrpc                262864  17 nfsd,nfs_acl,auth_rpcgss,lockd
iptable_filter          2793  0
iptable_nat             6158  0
nf_nat                 22759  1 iptable_nat
nf_conntrack_ipv4       9506  3 iptable_nat,nf_nat
nf_defrag_ipv4          1483  1 nf_conntrack_ipv4
ip_tables              17831  2 iptable_filter,iptable_nat
ip6t_REJECT             4628  2
nf_conntrack_ipv6       8337  2
nf_defrag_ipv6         11156  1 nf_conntrack_ipv6
xt_state                1492  2
nf_conntrack           79758  5 iptable_nat,nf_nat,nf_conntrack_ipv4,nf_conntrack_ipv6,xt_state
ip6table_filter         2889  1
ip6_tables             18732  1 ip6table_filter
ipv6                  317829  177 ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
fuse                   73530  2
uinput                  7992  0
ppdev                   8537  0
microcode             112685  0
vmware_balloon          7199  0
btusb                  16851  2
bluetooth              99271  9 rfcomm,sco,bnep,l2cap,btusb
rfkill                 19255  2 bluetooth
parport_pc             22690  1
parport                36209  3 lp,ppdev,parport_pc
snd_ens1371            21587  0
snd_rawmidi            23017  1 snd_ens1371
snd_ac97_codec(声卡驱动)        124967  1 snd_ens1371
ac97_bus                1452  1 snd_ac97_codec
snd_seq                55727  0
snd_seq_device          6500  2 snd_rawmidi,snd_seq
snd_pcm                87409  2 snd_ens1371,snd_ac97_codec
snd_timer              22443  2 snd_seq,snd_pcm
snd                    70569  7 snd_ens1371,snd_rawmidi,snd_ac97_codec,snd_seq,snd_seq_device,snd_pcm,snd_timer
soundcore               7958  1 snd
snd_page_alloc          8856  1 snd_pcm
e1000                 170678  0
sg                     29350  0
i2c_piix4              12608  0
i2c_core               31084  1 i2c_piix4
shpchp                 32778  0
ext4(ext4文件系统)                  374902  2
jbd2                   93427  1 ext4
mbcache                 8193  1 ext4
sr_mod                 15177  0
cdrom(光盘驱动)                  39085  1 sr_mod
sd_mod                 39069  5
crc_t10dif              1541  1 sd_mod
ahci                   42247  0
mptspi                 16603  4
mptscsih               36700  1 mptspi
mptbase                93647  2 mptspi,mptscsih
scsi_transport_spi     25863  1 mptspi
pata_acpi               3701  0
ata_generic             3837  0
ata_piix               24601  0
dm_mirror              14384  0
dm_region_hash         12085  1 dm_mirror
dm_log                  9930  2 dm_mirror,dm_region_hash
dm_mod                 84209  4 dm_mirror,dm_log  
[root@Smoke sys]# lsmod | grep floppy(查看当前系统装载那些模块将输出结果送给grep只显示floppy模块)
floppy                 61447  0
[root@Smoke sys]# modprobe -r floppy(-r表示remove卸载floppy模块)
[root@Smoke sys]# lsmod | grep floppy(查看当前系统装载那些模块将输出结果送给grep只显示floppy模块)
[root@Smoke sys]# modprobe floppy(装载floppy模块)
[root@Smoke sys]# lsmod | grep floppy(查看当前系统装载那些模块将输出结果送给grep只显示floppy模块)
floppy                 61447  0
[root@Smoke sys]# modinfo floppy(查看floppy模块具体信息)
filename:(模块文件路径)       /lib/modules/2.6.32-431.11.2.el6.x86_64/kernel/drivers/block/floppy.ko
alias:          block-major-2-*
license:        GPL
author:         Alain L. Knaff
srcversion:     4FE4A1303A32321170C1A4F
depends:        
vermagic:       2.6.32-431.11.2.el6.x86_64 SMP mod_unload modversions
parm:(额外参数)           floppy:charp
parm:           FLOPPY_IRQ:int
parm:           FLOPPY_DMA:int
[root@Smoke sys]# modinfo pcnet32(查看pcnet32模块具体信息)
filename:       /lib/modules/2.6.32-431.11.2.el6.x86_64/kernel/drivers/net/pcnet32.ko
license:        GPL
description:    Driver for PCnet32 and PCnetPCI based ethercards
author:         Thomas Bogendoerfer
srcversion:     E10734EFA65F9BAD2B8082A
alias:          pci:v00001023d00002000sv*sd*bc02sc00i*
alias:          pci:v00001022d00002000sv*sd*bc*sc*i*
alias:          pci:v00001022d00002001sv*sd*bc*sc*i*
depends:(依赖的模块)        mii
vermagic:       2.6.32-431.11.2.el6.x86_64 SMP mod_unload modversions
parm:           debug:pcnet32 debug level (int)
parm:           max_interrupt_work:pcnet32 maximum events handled per interrupt (int)
parm:           rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int)
parm:           tx_start_pt:pcnet32 transmit start point (0-3) (int)
parm:           pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int)
parm:           options:pcnet32 initial option setting(s) (0-15) (array of int)
parm:           full_duplex:pcnet32 full duplex setting(s) (1) (array of int)
parm:           homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int)
[root@Smoke sys]# modinfo mii(查看mii模块具体信息)
filename:       /lib/modules/2.6.32-431.11.2.el6.x86_64/kernel/drivers/net/mii.ko
license:        GPL
description:    MII hardware support library(支持硬件的库)
author:         Jeff Garzik <jgarzik@pobox.com>
srcversion:     94D0B170BD4AA5F0553F61D
depends:        
vermagic:       2.6.32-431.11.2.el6.x86_64 SMP mod_unload modversions
[root@Smoke sys]# modprobe floppy(装载floppy模块)
[root@Smoke sys]# modprobe -r floppy(卸载floppy模块)
[root@Smoke sys]# insmod floppy(装载floopy模块)
insmod: can't read 'floppy': No such file or directory
提示:使用insmod装载模块需要指定模块路径;
[root@Smoke sys]# modinfo floppy(查看floppy模块具体信息)
filename:       /lib/modules/2.6.32-431.11.2.el6.x86_64/kernel/drivers/block/floppy.ko
alias:          block-major-2-*
license:        GPL
author:         Alain L. Knaff
srcversion:     4FE4A1303A32321170C1A4F
depends:        
vermagic:       2.6.32-431.11.2.el6.x86_64 SMP mod_unload modversions
parm:           floppy:charp
parm:           FLOPPY_IRQ:int
parm:           FLOPPY_DMA:int
[root@Smoke sys]# insmod /lib/modules/2.6.32-431.11.2.el6.x86_64/kernel/drivers/block/floppy.ko(装载floopy模块,需要指定具体模块路径)
[root@Smoke sys]# lsmod | grep floppy(列出系统所有装载的模块将结果送给grep只显示floppy模块)
floppy                 61447  0
[root@Smoke sys]# rmmod floppy(卸载floopy模块)
[root@Smoke sys]# lsmod | grep floppy(列出系统所有装载的模块将结果送给grep只显示floppy模块)
[root@Smoke ~]# uname -r(查看内核版本)
2.6.32-431.11.2.el6.x86_64
[root@Smoke ~]# yum grouplist(查看软件包组)
提示:查看是否安装Gnome桌面环境使用,需要安装图形开发库组:GNOME Software Development和KDE桌面环境使用,需要安装图形开发库
[root@Smoke ~]# cd /etc/yum.repos.d/(切换到/etc/yum.repos.d目录)
[root@Smoke yum.repos.d]# vim local.repo(编辑本地yum源)
[base]
name=Server
baseurl=file:///media/cdrom/Server
enable=1
gpgcheck=0
[root@Smoke ~]# mkdir /media/cdrom(创建cdrom目录)
[root@Smoke ~]# mount /dev/cdrom /media/cdrom(将光驱挂载到/media/cdrom)[root@Smoke ~]# yum groupinstall -y "Development Tools" "Development
 Libraries"
[root@Smoke ~]# lftp 172.16.0.1(连接ftp服务器)
lftp 172.16.0.1:~>  cd pub/Sources/kernel
lftp 172.16.0.1:/pub/Sources/kernel> get linux-2.6.28.10.tar.gz
66796955 bytes transferred in 4 seconds (15.66M/s)
lftp 172.16.0.1:/pub/Sources/kernel> bye(退出)
[root@Smoke ~]# ls(查看当前目录文件及子目录)
linux-2.6.39.tar.gz
[root@Smoke ~]# tar xf linux-2.6.39.tar.gz -C /usr/src(解压文件linux-2.6.39.tar.gz,-C更改解压目录)
提示:内核的源码一般放在/usr/src目录;
[root@Smoke ~]# cd /usr/src/(切换到/usr/src目录)
[root@Smoke src]# ls(查看当前目录文件及子目录)
debug  kernels  linux-2.6.39
[root@Smoke src]# ln -sv linux-2.6.39 linux(给linux-2.6.39创建软连接为linux,并显示创建过程)
`linux' -> `linux-2.6.39'
[root@Smoke src]# ls(查看当前目录的文件及子目录)
debug  kernels  linux  linux-2.6.39
[root@Smoke src]# cd linux(切换到linux目录)
[root@Smoke linux]# ls(查看当前目录下文件及子目录)
arch(平台相关的)     CREDITS        drivers(驱动)   include  Kbuild   lib          mm(内存管理)      REPORTING-BUGS  security  usr
block    crypto(加密解密算法模块)         firmware(部件)  init     Kconfig  MAINTAINERS  net(网络功能相关)     samples         sound     virt(虚拟化)
COPYING  Documentation(文档)  fs(文件系统)        ipc(进程间通信的)      kernel(内核自己的)   Makefile     README  scripts         tools
[root@Smoke linux]# ls arch/(查看arch目录文件及子目录)
alpha  avr32     cris  h8300  Kconfig  m68k        mips     parisc   s390   sh     tile  unicore32  xtensa
arm    blackfin  frv   ia64   m32r     microblaze  mn10300  powerpc  score  sparc  um    x86
提示:arch是CPU平台架构目录;
[root@Smoke linux]# ls fs/(查看fs/目录文件及子目录)
9p                  binfmt_som.c         debugfs        fifo.c          internal.h      namei.c      pnode.c       stack.c
adfs                bio.c                devpts         file.c          ioctl.c         namespace.c  pnode.h       stat.c
affs                bio-integrity.c      direct-io.c    filesystems.c   ioprio.c        ncpfs        posix_acl.c   statfs.c
afs                 block_dev.c          dlm            file_table.c    isofs           nfs          proc          super.c
aio.c               btrfs                drop_caches.c  freevxfs        jbd             nfs_common   pstore        sync.c
anon_inodes.c       buffer.c             ecryptfs       fscache         jbd2            nfsctl.c     qnx4          sysfs
attr.c              cachefiles           efs            fs_struct.c     jffs2           nfsd         quota         sysv
autofs4             ceph                 eventfd.c      fs-writeback.c  jfs             nilfs2       ramfs         timerfd.c
bad_inode.c         char_dev.c           eventpoll.c    fuse            Kconfig         nls          readdir.c     ubifs
befs                cifs                 exec.c         generic_acl.c   Kconfig.binfmt  no-block.c   read_write.c  udf
bfs                 coda                 exofs          gfs2            libfs.c         notify       read_write.h  ufs
binfmt_aout.c       compat_binfmt_elf.c  exportfs       hfs             lockd           ntfs         reiserfs      utimes.c
binfmt_elf.c        compat.c             ext2           hfsplus         locks.c         ocfs2        romfs         xattr_acl.c
binfmt_elf_fdpic.c  compat_ioctl.c       ext3           hostfs          logfs           omfs         select.c      xattr.c
binfmt_em86.c       configfs             ext4           hpfs            Makefile        open.c       seq_file.c    xfs
binfmt_flat.c       cramfs               fat            hppfs           mbcache.c       openpromfs   signalfd.c
binfmt_misc.c       dcache.c             fcntl.c        hugetlbfs       minix           partitions   splice.c
binfmt_script.c     dcookies.c           fhandle.c      inode.c         mpage.c         pipe.c       squashfs
提示:fs目录为各种文件系统;
[root@Smoke linux]# yum grouplist(查看软件包组)
KDE Software Development(KDE开发库)
GNOME Software Development(GNOME开发库)
[root@Smoke linux]# make menuconfig(内核配置菜单)
提示:一定要在内核目录下;打开内核编译字符图形界面,需要将终端窗口放大不然会报错,提示窗口至少要能容纳19行,容纳80列;

提示:箭头--->表示底下有很多子条目菜单;在General setup --->选项点击回车确认;

提示:中括号内[*]表示做进内核;[M]表示做进模块,如果[]什么都不显示表示不启用此功能,()Local version - append to kernel release本地版本号;

[root@localhost linux]# uname -r(查看内核版本号)
2.6.18-308.el5
提示:本地版本号就是-308.e15;在Local version - append to kernel release本地版本号敲回车;

提示:修改完成敲回车,第一次编译-1.el5;

提示:当在本界面选择完成之后,可以敲两下ESC键返回上一页菜单;

提示:选择File systems --->文件系统选项敲回车;

提示:<*>星号表示做进内核,<M>表示做进模块,通过光标移动键盘找到DOS/FAT/NT Filesystems --->选项敲回车;

提示:NTFS file system support默认没有开启,敲空格键可以选择,在多种选择可以变化,M表示模块,*型号表示做进内核;

提示:再敲也可以光比此内核功能,双击敲ESC返回上一级菜单;

提示:最后一级主菜单敲ESC会提示是否保存修改的配置,Yes表示保存,No表示不保存,单机Yes回车保存,它保存到那里了;

[root@localhost linux]# pwd(查看当前工作目录)
/usr/src/linux
[root@localhost linux]# ls -a(查看当前目录所有文件及子目录)
.           COPYING        Makefile        crypto    init    net       usr
..          CREDITS        README          drivers   ipc     samples   virt
.config     Documentation  REPORTING-BUGS  firmware  kernel  scripts
.gitignore  Kbuild         arch            fs        lib     security
.mailmap    MAINTAINERS    block           include   mm      sound
提示:在内核源码目录下会生成.config文件,这是一个隐藏文件;
[root@localhost linux]# make(对内核进行编译)
提示:根据所选择内核功能特性不同,编译一般需要半个小时到五个小时不等,有可能编译到最后系统一启动不能启动,有可能需要某个特性没有编译进来,需要重新编译;
[root@localhost linux]# ls /boot/(查看/boot目录文件及子目录)
System.map-2.6.18-308.el5  initrd-2.6.18-308.el5.img  symvers-2.6.18-308.el5.gz
config-2.6.18-308.el5      lost+found                 vmlinuz-2.6.18-308.el5
grub                       message
提示:在红帽和centos系统/boot目录会有config-2.6.18-308.el5,这表示红帽和centos编译它的内核的时候所使用的配置,所以可以使用它的配置为模版,再进行修改,
这样一般出错的地方会小很多,因此我们一般来讲不是上来就执行make menuconfig,而是先将红帽或centos的/boot目录下的config-2.6.18-308.el5复制成.config文
件放到源码文件里面,然后在它基础上修改;
[root@localhost linux]# cp /boot/config-2.6.18-308.el5 /usr/src/linux/.config(拷贝红帽或centos系统的内核配置文件到内核源码文件为.config)
提示:复制完成以后,替换掉源码包中原来的.config文件,然后再执行make munuconfig
[root@localhost linux]# make menuconfig(打开内核编译功能文本界面菜单)

提示:选择General setup --->选项点击回车;

提示:选择Local version - append to kernel releease修改修订版本号,点击回车;

提示:点击OK确认修改;

提示:双击ESC返回上一级菜单;

提示:选择Processor type and features --->你的处理器类型和特性选项,红帽为了适应更多硬件选择的特定太多了,尤其是硬件驱动,可能很多我们用不着,它会导致你的系统编译比较慢,因此我们可以改一改,敲回车;

提示:选择Processor family (Pentium-Pro) --->奔腾-pro,这个CPU很老,选择这个平台可以支持绝大数的X86平台,红帽默认就是Pentium-pro(奔腾-pro),敲回车;

提示:如果CPU是Core的可以选择Core 2/newer Xeon;

提示:这样它就能最大化的发挥你的硬件性能,如果是AMD可以选择K6/K6-II/K6-III或Athlon/Duron/K7的;

提示:一般选择Athlon/Duron/K7,这样要比选择Pentium-Pro要好的多的多,我这里是Core CPU所以选择Core 2/newer Xeon的CPU,选择并敲空格;

提示:双击ESC返回上级菜单,选择Device Drivers --->设备驱动;

提示:有很多驱动都可以去掉,用不着的,以网卡为例,选择Networking device support --->选择,点击回车;

提示:比如说Token Ring driver support --->令牌环网,用不到可以去掉此功能;

提示:还有PCMCIA network device support --->和ATM drives (NEW) --->还有FDDI driver support都用不上的功能,可以去掉;

提示:Ethernet (10000 Mbit) (NEW) --->万兆以太网卡也永不着可以去掉,选择Ethernet (10 or 100Mbit) --->10兆和100兆以往网卡,点击回车;

提示:我们vmware用到的网卡的AMD PCnet32 PCI support;

提示:改成星号,内核就可以直接驱动这个网卡,

点击Exit进入上一级菜单,去掉Ethernet (1000 Mbit) --->

再点Exit或双击ESC返回到主菜单

选择Yes保存退出;

提示:在编译内核时候最好不要使用远程连接,万一远程连接断开了,内核编译过程也就中断了,你要再重新执行又要重头开始,可以使用screen工具,能够在当前远程窗口中模拟好几个窗口;

[root@localhost linux]# yum -y install screen(安装screen工具,-y所有提问全部回答yes)
[root@localhost linux]# screen(打开一个窗口)
[root@localhost linux]# ls(显示当前目录文件及子目录)
arch     crypto         fs       Kbuild       Makefile  REPORTING-BUGS  sound
block    Documentation  include  kernel       mm        samples         usr
COPYING  drivers        init     lib          net       scripts         virt
CREDITS  firmware       ipc      MAINTAINERS  README    security
[root@localhost linux]# screen(打开一个窗口)
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  linux-2.6.28.10.tar.gz
提示:使用Ctrl+a或d拆除屏幕
[root@localhost linux]# screen(打开一个窗口)
[root@localhost ~]# ls /etc/(显示/etc目录文件及子目录)
a2ps.cfg               gnome-vfs-2.0         minicom.users   request-key.conf
a2ps-site.cfg          gnome-vfs-mime-magic  mke2fs.conf     resolv.conf
acpi                   gpm-root.conf         modprobe.conf   resolv.conf.predhclient
adjtime                gre.d                 modprobe.conf~  rhgb
aliases                group                 modprobe.d      rmt
aliases.db             group-                motd            rpc
alsa                   grub.conf             mtab            rpm
alternatives           gshadow               mtools.conf     rsyslog.conf
anacrontab             gshadow-              multipath.conf  rwtab
asound.state           gssapi_mech.conf      Muttrc          rwtab.d
at.deny                gtk-2.0               Muttrc.local    samba
audisp                 hal                   netplug         sane.d
audit                  host.conf             netplug.d       sasl2
autofs_ldap_auth.conf  hosts                 NetworkManager  scim
auto.master            hosts.allow           nscd.conf       screenrc
auto.misc              hosts.deny            nsswitch.conf   scrollkeeper.conf
auto.net               hp                    ntp             scsi_id.config
auto.smb               htdig                 ntp.conf        securetty
avahi                  httpd                 oddjob          security
[root@localhost linux]# screen(打开一个窗口)
[root@localhost ~]# ls /var
account  cvs  empty  gdm  local  log   nis  preserve  run    tmp  yp
cache    db   games  lib  lock   mail  opt  racoon    spool  www
提示:计算这些窗口断掉了,也能还原回来
[root@localhost ~]# screen -ls(查看打开的屏幕)
There are screens on:
       2229(号码).pts-3.localhost        (Detached)
       2167.pts-1.localhost        (Attached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# screen -r 2229(还原2229号码的窗口)
制作小Linux系统:
在现有linux虚拟机增加一块20G的IDE硬盘,并对磁盘进行分区和创建文件系统,将创建的文件系统挂载到/mnt/boot和/mnt/sysroot目录;
[root@localhost ~]# fdisk -l(查看系统上磁盘及分区情况)
Disk /dev/hda: 21.4 GB, 21474836480 bytes
15 heads, 63 sectors/track, 44384 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
Disk /dev/hda doesn't contain a valid partition table
Disk /dev/sda: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2624    20972857+  83  Linux
/dev/sda3            2625        2755     1052257+  82  Linux swap / Solaris
[root@localhost ~]# fdisk /dev/hda(管理磁盘分区,进入交互式模式)
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 44384.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
  (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n(创建分区)
Command action
  e   extended
  p   primary partition (1-4)
p(主分区)
Partition number (1-4): 1(分区编号)
First cylinder (1-44384, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-44384, default 44384): +20M(创建20M分区)
Command (m for help): n(创建分区)
Command action
  e   extended
  p   primary partition (1-4)
p(主分区)
Partition number (1-4): 2(分区编号)
First cylinder (43-44384, default 43):
Using default value 43
Last cylinder or +size or +sizeM or +sizeK (43-44384, default 44384): +512M(创建512M分区)
Command (m for help): w(保存退出)
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/hda(让内核重新扫描分区表)
[root@localhost ~]# fdisk -l /dev/hda(查看/dev/hda分区情况)
Disk /dev/hda: 21.4 GB, 21474836480 bytes
15 heads, 63 sectors/track, 44384 cylinders
Units = cylinders of 945 * 512 = 483840 bytes
  Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1          42       19813+  83  Linux
/dev/hda2              43        1101      500377+  83  Linux
[root@localhost ~]# mke2fs -j /dev/hda1(将/dev/hda1创建为带日志的文件系统,即ext3文件系统)
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
4968 inodes, 19812 blocks
990 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20447232
3 block groups
8192 blocks per group, 8192 fragments per group
1656 inodes per group
Superblock backups stored on blocks:
       8193
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -j /dev/hda2(将/dev/hda2创建为带日志的文件系统,即ext3文件系统)
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
125488 inodes, 500376 blocks
25018 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
62 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks:
       8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /mnt/{boot,sysroot}(创建/mnt/boot目录和/mnt/sysroot目录,花括号{}展开)
[root@localhost ~]# mount /dev/hda1 /mnt/boot/(将/dev/hda1挂载到/mnt/boot/目录)
[root@localhost ~]# mount /dev/hda2 /mnt/sysroot/(将/dev/hda2挂载到/mnt/boot/目录)
[root@localhost ~]# mount(查看系统所有挂载的文件系统)
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hda1 on /mnt/boot type ext3 (rw)
/dev/hda2 on /mnt/sysroot type ext3 (rw)
安装grub:
[root@localhost ~]# grub-install --root-directory=/mnt /dev/hda(安装grub,--root-directory指定根目录,boot所在目录,/dev/hda指定硬盘)
Probing devices to guess BIOS drives. This may take a long time.
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0)        /dev/fd0
(hd0)        /dev/hda
(hd1)        /dev/sda
[root@localhost ~]# ls /mnt/boot/(查看/mnt/boot目录文件及子目录)
grub  lost+found
提示:grub目录存在了,但这个时候grub还没有配置文件;
[root@localhost ~]# cp /boot/vmlinuz-2.6.28.10-1.el5 /mnt/boot/vmlinuz(复制系统内核到/mnt/boot/目录叫vmlinuz)
制作initrd文件:这里不能使用mkinitrd  /boot/initrd-`uname -r`.img  `uname -r`创建initrd文件,这个命令是对原linux系统创建;
第一种展开initrd文件方法:
[root@localhost ~]# file /boot/initrd-2.6.28.10-1.el5.img(查看/boot/initrd-2.6.18-308.el5.img文件类型)
/boot/initrd-2.6.18-308.el5.img: gzip compressed data, from Unix, last modified: Tue Nov 11 07:50:01 2014, max compression
提示:该文件为gzip压缩的文件;
[root@localhost ~]# cp /boot/initrd-2.6.28.10-1.el5.img /root(将initrd-2.6.28.10-1.el5.img复制到/root目录)
[root@localhost ~]# ls(查看当前目录文件及子目录)
anaconda-ks.cfg             install.log         linux-2.6.28.10.tar.gz
initrd-2.6.28.10-1.el5.img  install.log.syslog
[root@localhost ~]# mv initrd-2.6.28.10-1.el5.img initrd-2.6.28.10-1.el5.img.gz(更改initrd-2.6.28.10-1.el5.img文件后缀为initrd-2
.6.28.10-1.el5.img.gz)
[root@localhost ~]# gzip -d initrd-2.6.28.10-1.el5.img.gz(解压缩GZ格式文件)
[root@localhost ~]# file initrd-2.6.28.10-1.el5.img(查看解压后的initrd-2.6.28.10-1.el5.img文件类型)
initrd-2.6.28.10-1.el5.img: ASCII cpio archive (SVR4 with no CRC)
提示:文件类型为cpio文件,展开cpio文件会创建很多文件,需要创建目录;
[root@localhost ~]# mkdir test(创建test目录)
[root@localhost ~]# cd test/(切换到test目录)
[root@localhost test]# cpio -id < ../initrd-2.6.28.10-1.el5.img(-i执行copy-in模式,还原备份档,-d如有需要cpio会自行创建目录)
11923 blocks
[root@localhost test]# ls(查看当前目录文件及子目录)
bin  dev  etc  init  lib  proc  sbin  sys  sysroot
第二种种展开initrd方法:
[root@localhost test]# cd ..(返回上一级目录)
[root@localhost ~]# mkdir iso(创建iso目录)
[root@localhost ~]# cd iso/(切换到iso目录)
[root@localhost iso]# zcat /boot/initrd-2.6.28.10-1.el5.img | cpio -id(不解压的情况,查看initrd-2.6.28.10-1.el5.img文件的内容,通过管道
送给cpid -id解压到当前目录)
11923 blocks
[root@localhost iso]# ls(查看当前目录文件及子目录)
bin  dev  etc  init  lib  proc  sbin  sys  sysroot提示:cpio只能展开到当前目录下,展开的目录需要放到一个空目录下,需要创建空目录;
编辑init文件,这个文件是个脚本文件;
[root@localhost iso]# file init(查看init文件类型)
init: a /bin/nash script text executable
提示:init文件是一个nash脚本;
[root@localhost iso]# vim init(编辑init文件)
mkrootdev -t ext3 -o defaults,ro /dev/hda2
#resume LABEL=SWAP-sda3(注释掉此项,因为没有创建swap分区)
提示:在末行模式使用$跳到init文件末行,找到mkrootdev -t ext3 -o defaults,ro /dev/sda2(创建根设备,-t指定文件系统类型,defaults默认,只读,挂
载的是/dev/sda2),我们应该挂载/dev/hda2
[root@localhost iso]# find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz(查看当前目录所有文件通过管道送给cpio归档,-H类
型newc,--quiet指定为静默模式,-o归档出去,通过管道再送给gzip压缩,-9使用最大压缩比,到/mnt/boot目录下较initrd.gz文件)
[root@localhost boot]# ls(查看当前目录文件及子目录)
grub  initrd.gz  lost+found  vmlinuz
[root@localhost boot]# ls -lh(查看当前目录文件及子目录详细信息,并做单位换算)
总计 4.6M
drwxr-xr-x 2 root root 1.0K 11-11 13:04 grub
-rw-r--r-- 1 root root 2.6M 11-11 14:10 initrd.gz
drwx------ 2 root root  12K 11-11 12:57 lost+found
-rw-r--r-- 1 root root 2.0M 11-11 13:27 vmlinuz
提供grub.conf配置文件;
[root@localhost boot]vim /mnt/boot/grub/grub.conf(编辑grub.conf文件)
default=0(全局属性定义)   设定默认启动的title的编号,从0开始
timeout=5 等待用户选择的超时时长,单位是秒
title (指定操作系统或内核,要启动不同的内核,每一个内核给一个title,每一个title是用于定义当前系统的不同版本的内核,或者当前主机上不同操作系统)  
Smoke Linux (Smoke Team) 内核标题,或操作系统名称,字符串,可自由修改
       root (hd0,0) 内核文件所在的设备;对grub而言,所有类型硬盘一律hd,格式为(hd#,N);hd#, #表示第几个磁盘;最后的N表示对应磁盘的分区;
       kernel /vmlinuz 内核文件路径,及传递给内核的参数 (为什么内核文件在/根下,但是内核文件在/boot下,因为/boot是一个独立的分区,在内核还没有装载,
文件系统不会装载,意味着grub在访问这个磁盘分区的时候,不会通过/boot目录访问,/boot目录在根上,对应的分区如果不通过/根访问,那/boot就找不到,如果通过真
正的文件系统访问就是/boot/vmlinux*文件,问题是现在文件系统还没装载,直接通过硬盘访问vmlinux*,它是直接在这个磁盘上,如果把磁盘本身当作/根,因为此时访问
不了其他分区,对这个磁盘来讲/vmlinux*就是/根,因为访问入口不同,)
       initrd /initrd.gz (ramdisk/ramfs文件路径 (小linux,操作系统安装最后一步生成的,是一个完整类型的Linux,除了内核,各种文件都有,initrd也在
/boot目录下,initrd的版本号一定要和内核版本号保持一致,后缀是img说明是ram磁盘文件,镜像文件,可以展开以后当磁盘使用,类似dd命令创建虚拟SWAP设备,本地回环设备,
假如/boot没有独立分区,/boot就在/根上,内核就是/boot目录下,要找这个文件,内核在/boot/vmlinux*,因为它就是根文件系统系统,而且/根下的确有/boot目录)
创建真正的根文件系统:
[root@localhost boot]# cd /mnt/sysroot/(切换到/mnt/sysroot目录)
[root@localhost sysroot]# mkdir -pv proc sys dev etc/rc.d lib bin sbin boot home var/log usr/{bin,sbin} root tmp(创建目录,-v显示创建过程,
-p递归创建)
mkdir: 已创建目录 "proc"
mkdir: 已创建目录 "sys"
mkdir: 已创建目录 "dev"
mkdir: 已创建目录 "etc"
mkdir: 已创建目录 "etc/rc.d"
mkdir: 已创建目录 "lib"
mkdir: 已创建目录 "bin"
mkdir: 已创建目录 "sbin"
mkdir: 已创建目录 "boot"
mkdir: 已创建目录 "home"
mkdir: 已创建目录 "var"
mkdir: 已创建目录 "var/log"
mkdir: 已创建目录 "usr"
mkdir: 已创建目录 "usr/bin"
mkdir: 已创建目录 "usr/sbin"
mkdir: 已创建目录 "root"
mkdir: 已创建目录 "tmp"
[root@localhost sysroot]# tree .(显示当前目录的目录树)
.
|-- bin
|-- boot
|-- dev
|-- etc
|   `-- rc.d
|-- home
|-- lib
|-- lost+found
|-- proc
|-- root
|-- sbin
|-- sys
|-- tmp
|-- usr
|   |-- bin
|   `-- sbin
`-- var
   `-- log
18 directories, 0 files
[root@localhost sysroot]# cp /sbin/init /mnt/sysroot/sbin/(复制init文件到/mnt/sysroot/sbin目录)
[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/(复制bash文件到/mnt/sysroot/bin目录)
[root@localhost sysroot]# ldd /sbin/init(查看init依赖的库文件)
       linux-gate.so.1 =>  (0x00f34000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x00d69000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00d4f000)
       libc.so.6 => /lib/libc.so.6 (0x0019f000)
       libdl.so.2 => /lib/libdl.so.2 (0x00326000)
       /lib/ld-linux.so.2 (0x00180000)
[root@localhost sysroot]# cp /lib/libsepol.so.1 /mnt/sysroot/lib/(复制libsepol.so.1库文件到/mnt/sysroot/lib/目录)
[root@localhost sysroot]# cp /lib/libselinux.so.1 /mnt/sysroot/lib/(复制libselinux.so.1库文件到/mnt/sysroot/lib目录)
[root@localhost sysroot]# cp /lib/libc.so.6 /mnt/sysroot/lib/(复制libc.so.6库文件到/mnt/sysroot/lib/目录)
[root@localhost sysroot]# cp /lib/libdl.so.2 /mnt/sysroot/lib/(复制libdl.so.2库文件到/mnt/sysroot/lib/目录)
[root@localhost sysroot]# cp /lib/ld-linux.so.2 /mnt/sysroot/lib/(复制ld-linux.so.2库文件到/mnt/sysroot/lib目录)
[root@localhost ~]# ldd /bin/bash(查看init依赖的库文件)
       linux-gate.so.1 =>  (0x00489000)
       libtermcap.so.2 => /lib/libtermcap.so.2 (0x07a99000)
       libdl.so.2 => /lib/libdl.so.2 (0x00326000)
       libc.so.6 => /lib/libc.so.6 (0x0019f000)
       /lib/ld-linux.so.2 (0x00180000)
[root@localhost ~]# cp /lib/libtermcap.so.2 /mnt/sysroot/lib/(复制libtermcap.so.2库文件到/mnt/sysroot/lib目录)
[root@localhost sysroot]# tree(查看当前目录树)
.
|-- bin
|   `-- bash
|-- boot
|-- dev
|-- etc
|   `-- rc.d
|-- home
|-- lib
|   |-- ld-linux.so.2
|   |-- libc.so.6
|   |-- libdl.so.2
|   |-- libselinux.so.1
|   |-- libsepol.so.1
|   `-- libtermcap.so.2
|-- lost+found
|-- proc
|-- root
|-- sbin
|   `-- init
|-- sys
|-- tmp
|-- usr
|   |-- bin
|   `-- sbin
`-- var
   `-- log
18 directories, 8 files
[root@localhost sysroot]# cd (切换到根目录)
[root@localhost ~]# chroot /mnt/sysroot/(切换根文件系统)
bash-3.2# exit(退出)
exit
提示:通过chroot根文件系统切换测试
[root@localhost ~]# sync(同步磁盘写入)
[root@localhost ~]# cd /mnt/sysroot/(切换到/mnt/sysroot目录)
[root@localhost sysroot]# tree etc/(查看etc目录树)
etc/
`-- rc.d
1 directory, 0 files
[root@localhost sysroot]# vim etc/inittab(编辑inittab文件)
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit(完成系统初始化脚本文件)
[root@localhost sysroot]# vim etc/rc.d/rc.sysinit(编辑rc.sysinit文件)
#!/bin/bash
#
echo -e "\tWelcome to \033[31mSmoke Tean\033[0m Linux."
/bin/bash
[root@localhost sysroot]# chmod +x etc/rc.d/rc.sysinit(给rc.sysinit文件执行权限)
[root@localhost sysroot]# sync(同步磁盘写入)
[root@localhost sysroot]# sync(同步磁盘写入)
[root@localhost sysroot]# sync(同步磁盘写入)
提示:需要多同步sync几次,将虚拟机挂起,创建新的虚拟机,将制作的linux系统的硬盘装到新的虚拟机进行测试;

经过测试成功进入/bin/bash:

复制ls命令:

[root@localhost ~]# ldd /bin/ls(查看ls命令所依赖的库文件)
       linux-gate.so.1 =>  (0x00edd000)
       librt.so.1 => /lib/librt.so.1 (0x0011d000)
       libacl.so.1 => /lib/libacl.so.1 (0x0017f000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00ca7000)
       libc.so.6 => /lib/libc.so.6 (0x005ef000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x00126000)
       /lib/ld-linux.so.2 (0x00d2d000)
       libattr.so.1 => /lib/libattr.so.1 (0x00818000)
       libdl.so.2 => /lib/libdl.so.2 (0x008a2000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x00a6e000)
[root@localhost ~]# which vim(查看vim命令的路径)
/usr/bin/vim
[root@localhost ~]# ldd /usr/bin/vim(查看vim命令依赖的库文件)
       linux-gate.so.1 =>  (0x00d96000)
       libncurses.so.5 => /usr/lib/libncurses.so.5 (0x00dd5000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00e4e000)
       libacl.so.1 => /lib/libacl.so.1 (0x00aaf000)
       libgpm.so.1 => /usr/lib/libgpm.so.1 (0x00fb8000)
       libperl.so => /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so (0x00830000)(需要创建目录)
       libresolv.so.2 => /lib/libresolv.so.2 (0x00110000)
       libutil.so.1 => /lib/libutil.so.1 (0x0075b000)
       libc.so.6 => /lib/libc.so.6 (0x00125000)
       libm.so.6 => /lib/libm.so.6 (0x007aa000)
       libdl.so.2 => /lib/libdl.so.2 (0x00e95000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x00c48000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x0040d000)
       /lib/ld-linux.so.2 (0x003dc000)
       libattr.so.1 => /lib/libattr.so.1 (0x0027f000)
       libnsl.so.1 => /lib/libnsl.so.1 (0x00284000)
       libcrypt.so.1 => /lib/libcrypt.so.1 (0x00f70000)
[root@localhost ~]# which ls(查看ls命令的路径)
alias ls='ls --color=tty'(ls命令还有别名)
       /bin/ls
You have mail in /var/spool/mail/root
[root@localhost ~]# man bash(查看bash的man帮助文档)
      ${parameter#word}(截取变量的字符串)
      ${parameter##word}
             The word is expanded to produce a pattern just as in pathname expansion.  If the
             pattern matches the beginning of the value of parameter, then the result of  the
             expansion  is the expanded value of parameter with the shortest matching pattern
             (the ''#'' case) or the longest matching pattern (the ''##'' case) deleted.   If
             parameter is @ or *, the pattern removal operation is applied to each positional
             parameter in turn, and the expansion is the resultant list.  If parameter is  an
             array variable subscripted with @ or *, the pattern removal operation is applied
             to each member of the array in turn, and the expansion is the resultant list.
      ${parameter%word}
      ${parameter%%word}
             The word is expanded to produce a pattern just as in pathname expansion.  If the
             pattern  matches a trailing portion of the expanded value of parameter, then the
             result of the expansion is the expanded value of  parameter  with  the  shortest
             matching  pattern  (the  ''%'' case) or the longest matching pattern (the ''%%''
             case) deleted.  If parameter is @ or *, the pattern removal operation is applied
             to  each  positional parameter in turn, and the expansion is the resultant list.
             If parameter is an array variable subscripted with @ or *, the  pattern  removal
             operation  is  applied to each member of the array in turn, and the expansion is
             the resultant list.
[root@localhost ~]# FILE=/usr/local/src(将/usr/local/src赋值给FILE变量)
[root@localhost ~]# echo ${FILE#/}(显示FILE变量)
usr/local/src
[root@localhost ~]# echo ${FILE#/}(显示变量FILE,去掉/usr/local/src从左向左第一个/斜线前面的内容,)
usr/local/src
[root@localhost ~]# echo ${FILE##/}(显示变量FILE,去掉/usr/local/src从左向左第一个/斜线前面的内容,两个#井号好一个效果一样)
usr/local/src
[root@localhost ~]# echo ${FILE##*/}(显示变量FILE,取消/usr/local/src从左往右最后一个/斜线前面的内容)
src
[root@localhost ~]# echo ${FILE#*/}(显示变量FILE,取消/usr/local/src从左向左第一个/斜线前面的内容,和一个或多个#井号效果相同)
usr/local/src
[root@localhost ~]# echo ${FILE%/*}(显示变量FILE,取消/usr/local/src从右向左第一个/斜线后面的内容)
/usr/local
[root@localhost ~]# echo ${FILE%%/*}(显示变量FILE,取消/usr/local/src从右向左最后一个/斜线后面的内容)
[root@localhost ~]# FILE=hello/usr/local/src(将/hello/usr/local/src赋值给FILE变量)
[root@localhost ~]# echo ${FILE%%/*}(显示变量FILE,取消hello/usr/local/src从右向左最后一个/斜线后面的内容)
hello
[root@localhost ~]# FILE=/usr/local/src(将/usr/local/src赋值给FILE变量)
[root@localhost ~]# echo ${FILE%/*}(显示变量FILE,取消/usr/local/src从右向左第一个/斜线后面的内容,可以取到一个指定文件目录)
/usr/local
[root@localhost ~]# ldd /bin/ls(查看ls命令所依赖的库文件)
       linux-gate.so.1 =>  (0x00784000)
       librt.so.1 => /lib/librt.so.1 (0x00bb0000)
       libacl.so.1 => /lib/libacl.so.1 (0x00b85000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00c03000)
       libc.so.6 => /lib/libc.so.6 (0x009f1000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x00b94000)
       /lib/ld-linux.so.2 (0x009d2000)
       libattr.so.1 => /lib/libattr.so.1 (0x0025b000)
       libdl.so.2 => /lib/libdl.so.2 (0x00b78000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x00bbb000)
提示:使用${parameter%word*}截取目录,找到/lib/目录以后,截取/lib/目录,并判断这个目录在/mnt/sysroot目录下有没有这样对应的目录,如果有就无所谓,如
果没有就创建这个目录;
[root@localhost ~]# cp /usr/lib/a.so /mnt/sysroot/usr/lib/a.so(复制/usr/lib/a.so到/mnt/sysroot/usr/lib/a.so)
提示:复制/usr/lib/a.so到/mnt/sysroot/usr/lib目录下,如果/usr/lib/目录不存在,无法将/usr/lib/a.so文件复制过来,因此/usr/lib/目录必须事先存在,
这个/usr/lib/目录事实刚好和/usr/lib/a.so目录对应的,因此需要判断在/mnt/sysroot目录下有没有/usr/lib目录,如果没有把它创建出来,然后创建之后再把它复制过来;
如何复制二进制程序及其依赖的库文件的脚本:
[root@localhost ~]# vim bincopy.sh(边界bincopy.sh脚本)
#!/bin/bash
#
DEST=/mnt/sysroot (定义变量DEST赋值为/mnt/sysroot)
libcp() { (定义函数)
 LIBPATH=${1%/*} (定义变量LIBPATH,赋值以/斜线为分隔符从右往左取消第一个/斜线后面的内容,用于截取文件的目录)
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH (判断$DEST$LIBPATH路径是否为目录,如果不是目录代表目录不存在,就创建该目录)
 cp $1 $DEST$LIBPATH (复制$1变量赋值的路径下的文件到$DEST$LIBPATH目录)
}
libcp /lib/librt.so.1 (调用函数libcp)
[root@localhost ~]# bash -x bincopy.sh(执行bincopy.sh脚本,并显示执行过程)
+ DEST=/mnt/sysroot
+ libcp /lib/librt.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ cp /lib/librt.so.1 /mnt/sysroot/lib
[root@localhost ~]# ldd /usr/bin/vim(查看vim命令所依赖的库文件)
       linux-gate.so.1 =>  (0x00beb000)
       libncurses.so.5 => /usr/lib/libncurses.so.5 (0x07b00000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00c03000)
       libacl.so.1 => /lib/libacl.so.1 (0x00b85000)
       libgpm.so.1 => /usr/lib/libgpm.so.1 (0x00bb0000)
       libperl.so => /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so (0x00c1d000)
       libresolv.so.2 => /lib/libresolv.so.2 (0x00766000)
       libutil.so.1 => /lib/libutil.so.1 (0x009c8000)
       libc.so.6 => /lib/libc.so.6 (0x009f1000)
       libm.so.6 => /lib/libm.so.6 (0x00b4d000)
       libdl.so.2 => /lib/libdl.so.2 (0x00b78000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x00b94000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x00110000)
       /lib/ld-linux.so.2 (0x009d2000)
       libattr.so.1 => /lib/libattr.so.1 (0x0025b000)
       libnsl.so.1 => /lib/libnsl.so.1 (0x001c4000)
       libcrypt.so.1 => /lib/libcrypt.so.1 (0x07a71000)
[root@localhost ~]# vim bincopy.sh(编辑bincopy.sh脚本)
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
 LIBPATH=${1%/*}
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
 cp $1 $DEST$LIBPATH
}
libcp /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so
[root@localhost ~]# bash -x bincopy.sh(执行bincopy.sh脚本,并显示执行过程)
+ DEST=/mnt/sysroot
+ libcp /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so
+ LIBPATH=/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE
+ '[' '!' -d /mnt/sysroot/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE ']'
+ mkdir -p /mnt/sysroot/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE
+ cp /usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so /mnt/sysroot/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE
[root@localhost ~]# ldd /bin/ls(查看ls命令所依赖的库文件)
       linux-gate.so.1 =>  (0x0055f000)
       librt.so.1 => /lib/librt.so.1 (0x00bb0000)
       libacl.so.1 => /lib/libacl.so.1 (0x00b85000)
       libselinux.so.1 => /lib/libselinux.so.1 (0x00c03000)
       libc.so.6 => /lib/libc.so.6 (0x009f1000)
       libpthread.so.0 => /lib/libpthread.so.0 (0x00b94000)
       /lib/ld-linux.so.2 (0x009d2000)
       libattr.so.1 => /lib/libattr.so.1 (0x0025b000)
       libdl.so.2 => /lib/libdl.so.2 (0x00b78000)
       libsepol.so.1 => /lib/libsepol.so.1 (0x00bbb000)
[root@localhost ~]# ldd /usr/bin/vim | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"(查找vim命令所依赖的库将结果送给管道只显示/斜线开
头中间任意长度任意字符后面lib,64出现0次或1次后面跟/斜线,后面加非空白任意字符至少一次)
/usr/lib/libncurses.so.5
/lib/libselinux.so.1
/lib/libacl.so.1
/usr/lib/libgpm.so.1
/usr/lib/perl5/5.8.8/i386-linux-thread-multi/CORE/libperl.so
/lib/libresolv.so.2
/lib/libutil.so.1
/lib/libc.so.6
/lib/libm.so.6
/lib/libdl.so.2
/lib/libpthread.so.0
/lib/libsepol.so.1
/lib/ld-linux.so.2
/lib/libattr.so.1
/lib/libnsl.so.1
/lib/libcrypt.so.1
[root@localhost ~]# vim bincopy.sh(编辑bincopy.sh文件)
#!/bin/bash
#
DEST=/mnt/sysroot (定义变量DEST赋值为/mnt/sysroot)
libcp() { (定义函数libcp)
 LIBPATH=${1%/*} (定义变量LIBPATH,赋值以/斜线为分隔符从右往左取消第一个/斜线后面的内容,用于截取文件的目录)
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH (判断$DEST$LIBPATH路径是否为目录,如果不是目录代表目录不存在,就创建该目录)
 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH(判断$DEST${1}是否存在,如果不存在就复制变量$1赋值的路径下的的文件到$DEST$LIBPATH目录)
}
bincp() { (定义函数bincp)
 CMDPATH=${1%/*} (定义变量CMDPATH,赋值以/斜线为风格副从右往左取消第一个/斜线后面的内容,用于截取文件的目录)
 [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CODPATH (判断$DEST$CMDPATH是否为目录,如果不是目录代表不存在,就创建该目录)
 [ ! -e $DEST${1} ] &&  cp $1 $DEST$CMDPATH (判断$DEST${1}是否存在,如果不存在就复制变量$1赋值的路径下的文件到$DEST$CMDPATH目录)
 for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;do (命令引用显示$1参数所依赖的库文件,将执行结果送给管道只显示匹配
到/斜线开头中间任意长度任意字符后面lib出现0或1此64字符串后面/斜线跟非空白字符出现至少1次的字符串,将结果赋值给LIB变量执行for循环)
   libcp $LIB (执行函数libcp)
 done
}
bincp /bin/ls (执行函数bincp)
[root@localhost ~]# bash -x bincopy.sh(执行bincopy脚本,并显示执行过程)
+ DEST=/mnt/sysroot
+ bincp /bin/ls
+ CMDPATH=/bin
+ '[' '!' -d /mnt/sysroot/bin ']'
+ '[' '!' -e /mnt/sysroot/bin/ls ']'
++ ldd /bin/ls
++ grep -o '/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/librt.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/librt.so.1 ']'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libacl.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libacl.so.1 ']'
+ cp /lib/libacl.so.1 /mnt/sysroot/lib
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libselinux.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libselinux.so.1 ']'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libc.so.6
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libc.so.6 ']'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libpthread.so.0
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libpthread.so.0 ']'
+ cp /lib/libpthread.so.0 /mnt/sysroot/lib
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/ld-linux.so.2
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/ld-linux.so.2 ']'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libattr.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libattr.so.1 ']'
+ cp /lib/libattr.so.1 /mnt/sysroot/lib
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libdl.so.2
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libdl.so.2 ']'
+ for LIB in '`ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`'
+ libcp /lib/libsepol.so.1
+ LIBPATH=/lib
+ '[' '!' -d /mnt/sysroot/lib ']'
+ '[' '!' -e /mnt/sysroot/lib/libsepol.so.1 ']'
[root@localhost ~]# chroot /mnt/sysroot/(切换根文件系统)
bash-3.2# ls(显示当前目录文件及子目录)
bin  boot  dev        etc  home  lib        lost+found  ls        proc  root  sbin  sys  tmp  usr  var
提示:通过测试,可以正常执行ls命令;
bash-3.2# exit(退出当前shell)
exit
[root@localhost ~]# which ls(显示ls命令的目录)
alias ls='ls --color=tty'
       /bin/ls
[root@localhost ~]# which ls | grep -v "^alias"(显示ls命令的目录,将结果送给管道,使用grep命令-v选项去掉以alias开头的行)
       /bin/ls
[root@localhost ~]# which ls | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"(显示ls命令的目录,将结果送给管道,使用grep命令-v选项去
掉以alias开头的行,将结果送给|管道通过grep命令-o选项只显示非空白字符匹配的内容)
/bin/ls
[root@localhost ~]# which rm | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"(显示rm命令的目录,将结果送给管道,使用grep命令-v选项去
掉以alias开头的行,将结果送给|管道通过grep命令-o选项只显示非空白字符匹配的内容)
/bin/rm
[root@localhost ~]# vim bincopy.sh(编辑bincopy.sh文件)
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
 LIBPATH=${1%/*}
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH
}
bincp() {
 CMDPATH=${1%/*}
 [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
 [ ! -e $DEST${1} ] &&  cp $1 $DEST$CMDPATH
 for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;do
   libcp $LIB
 done
}
read -p "Your command:" CMD (使用read -p可以直接键入提示的内容,将内容赋值给CMD变量)
until [ $CMD == 'q' ];do (until循环,如果$CMD等于q就执行循环)
  ! which $CMD && echo "Wrong command" && read -p "Input againe" CMD && continue (判断$CMD命令是否存在,如果不存在就显示错误命令,并让重
新输入命令,continue提前结束本轮循环,而进入下一轮循环)
 COMMAND=`which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"` (显示$CMD命令的路径将执行结果通过|管道送给grep命令-v去掉以alias字
符串开头的行,将结果送给|管道只显示非空白字符匹配到的字符串)
 bincp $COMMAND (执行bincp函数)
 echo "copy $COMMAND finishd." (显示复制$COMMAND完成)
 read -p "Continue:" CMD (使用read -p让用户根据提示输入对应的内容,将输入内容赋值给CMD变量)
done
[root@localhost ~]# chmod +x bincopy.sh(给bincopy.sh执行权限)
[root@localhost ~]# ./bincopy.sh(当前目录执行bincopy.sh脚本)
Your command:mkdir
/bin/mkdir
copy /bin/mkdir finishd.
Continue:rm
/bin/rm
copy /bin/rm finishd.
Continue:mount
/bin/mount
copy /bin/mount finishd.
Continue:q
[root@localhost ~]# vim bincopy.sh(边界bincopy.sh脚本)
#!/bin/bash
#
DEST=/mnt/sysroot
libcp() {
 LIBPATH=${1%/*}
 [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH
 [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."
}
bincp() {
 CMDPATH=${1%/*}
 [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
 [ ! -e $DEST${1} ] &&  cp $1 $DEST$CMDPATH
 
 for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`;do
   libcp $LIB
 done
}
read -p "Your command:" CMD
until [ $CMD == 'q' ];do
 ! which $CMD && echo "Wrong command" && read -p "Input again:" CMD && continue
 COMMAND=`which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`
 bincp $COMMAND
 echo "copy $COMMAND finishd."
 read -p "Continue:" CMD
done
[root@localhost ~]# ./bincopy.sh(当前目录执行bincopy.sh脚本)
Your command:runlevel
/sbin/runlevel
copy /sbin/runlevel finishd.
Continue:vim
/usr/bin/vim
copy lib /usr/lib/libncurses.so.5 finished.
copy lib /usr/lib/libgpm.so.1 finished.
copy lib /lib/libresolv.so.2 finished.
copy lib /lib/libutil.so.1 finished.
copy lib /lib/libm.so.6 finished.
copy lib /lib/libnsl.so.1 finished.
copy lib /lib/libcrypt.so.1 finished.
copy /usr/bin/vim finishd.
Continue:q
[root@localhost ~]# ./bincopy.sh(当前目录执行bincopy.sh脚本)
Your command:ping
/bin/ping
copy /bin/ping finishd.
Continue:ifconfig
/sbin/ifconfig
copy /sbin/ifconfig finishd.
Continue:insmod
/sbin/insmod
copy /sbin/insmod finishd.
Continue:rmmod    
/sbin/rmmod
copy /sbin/rmmod finishd.
Continue:modprobe
/sbin/modprobe
copy /sbin/modprobe finishd.
Continue:halt
/sbin/halt
copy /sbin/halt finishd.
Continue:reboot
/sbin/reboot
copy /sbin/reboot finishd.
Continue:rm
/bin/rm
copy /bin/rm finishd.
Continue:mv
/bin/mv
copy /bin/mv finishd.
Continue:chmod
/bin/chmod
copy /bin/chmod finishd.
Continue:chown
/bin/chown
copy /bin/chown finishd.
Continue:sync
/bin/sync
copy /bin/sync finishd.
Continue:q
[root@localhost ~]# sync(同步磁盘写入)
[root@localhost ~]# sync(同步磁盘写入)
[root@localhost ~]# chroot /mnt/sysroot/(切换环根文件系统到/mnt/sysroot)
bash-3.2# ls(查看当前目录文件及子目录)
bin  boot  dev        etc  home  lib        lost+found  ls        proc  root  sbin  sys  tmp  usr  var
bash-3.2# cd /etc/(切换到/etc目录)
bash-3.2# pwd(查看所处的路径)
/etc
bash-3.2# ls(查看当前目录文件及子目录)
inittab  rc.d
bash-3.2# cd rc.d/(切换到rc.d目录)
bash-3.2# chmod +x rc.sysinit
bash-3.2# ls -l(查看当前目录文件及子目录的详细信息)
total 2
-rwxr-xr-x 1 0 0 81 Nov 21 18:47 rc.sysinit
bash-3.2# exit(退出)
exit
提示:经过测试所复制的命令均能正常使用执行;
[root@localhost ~]# mkdir /mnt/sysroot/lib/modules(创建/mnt/sysroot/lib/modules目录)
[root@localhost ~]# modinfo mii(查看mii模块的信息)
filename:       /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko
license:        GPL
description:    MII hardware support library
author:         Jeff Garzik <jgarzik@pobox.com>
srcversion:     16DCEDEE4B5629C222C352D
depends:        
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
module_sig:        883f3504f23273dc995cc0b59af12111211c809f6f79961e4a84bc133b97d3d32bb217be64af3d1c09d11dbf04e53225fd698ce3def944
8666b2a5a31
[root@localhost ~]# modinfo pcnet32(查看pcnet32模块的信息)
filename:       /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko
license:        GPL
description:    Driver for PCnet32 and PCnetPCI based ethercards
author:         Thomas Bogendoerfer
srcversion:     F81443556AAE169CBF80F55
alias:          pci:v00001023d00002000sv*sd*bc02sc00i*
alias:          pci:v00001022d00002000sv*sd*bc*sc*i*
alias:          pci:v00001022d00002001sv*sd*bc*sc*i*
depends:        mii
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
parm:           debug:pcnet32 debug level (int)
parm:           max_interrupt_work:pcnet32 maximum events handled per interrupt (int)
parm:           rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int)
parm:           tx_start_pt:pcnet32 transmit start point (0-3) (int)
parm:           pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int)
parm:           options:pcnet32 initial option setting(s) (0-15) (array of int)
parm:           full_duplex:pcnet32 full duplex setting(s) (1) (array of int)
parm:           homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int)
module_sig:        883f3504f2326fedc995cc0b59af121112c9240a0888050bfd72bf9d5ba34fa2b2d35ad9c599dc2b509f6fc0ce174c811a747cc9
292d8a4d837983e5b14
[root@localhost ~]# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules/(复制mii.ko模块到/mnt/sys
root/lib/modules目录)
[root@localhost ~]# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules/(复制pcnet32.ko模块到
/mnt/sysroot/lib/modules目录)
[root@localhost ~]# sync(同步磁盘写入)
提示:期望系统在开机的时候能够自动装载这两个模块mii.ko和pcnet32.ko,而且装载完以后能够给系统配置IP地址;
[root@localhost ~]# cd /mnt/sysroot/(切换到/mnt/sysroot命令)
[root@localhost sysroot]# vim etc/rc.d/rc.sysinit(编辑/etc/rc.d/rc.sysinit文件)
#!/bin/bash
#
echo -e "\tWelcome to \033[31mSmoke Team\033[0m Linux."
insmod /lib/modules/mii.ko (装载mii.ko模块)
insmod /lib/modules/pcnet32.ko (装载pcnet32.ko模块)
ifconfig eth0 172.16.100.13/16 (eth0地址)
ifconfig lo 127.0.0.1/8 (loopback地址)
/bin/bash
[root@localhost sysroot]# sync(同步磁盘写入)
[root@localhost sysroot]# sync(同步磁盘写入)

测试:将虚拟机挂起,打开制作的小Linux系统;

提示:pcnet32模块初始化,初始化完成,eth0接口激活,使用ifconfig命令查看接口地址;

提示:进行ping测试;