【系统启动】grub & initrd


===系统启动====
1. BIOS硬件检测,加载MBR中boot loader
2. Boot loader 根据设置(/boot/grub/grub.conf) 加载kernel和initrd
3. Kernel检查并驱动硬件(/lib/modules),执行init
   (如果kernel无法驱动/所在硬盘,会通过initrd去加载/所在的硬盘驱动,能够以只读方式挂载/分区,然后再到/lib/modules调用模块进行驱动)
4. Init获得runlevel (/etc/inittab)
   系统初始化(加载网络,LVM,RAID...)
   根据runlevel去执行/etc/rc.d/rcN.d目录下的脚本,从而启动或停止服务
   手动开关一些服务,开机会生效哦:chkconfig sshd on|off  chkconfig sshd --list
   执行/etc/rc.d/rc.local(/etc/rc.local)    //在这里可以放些想要开机执行的脚本
   启动多个终端


=== Boot Loader: GRUB ====
Stage1: 主程序,安装在MBR
# dd if=/dev/zero of=/dev/sda bs=1 count=440  小心
覆盖硬盘第一个扇区的前440字节,破坏boot loader
使用光盘进入救援模式:
> linux rescue
选择跳过加载网络
sh-3.2# chroot /mnt/sysimage        切换到真实根目录
确定引导分区:df (/dev/sda1)
sh-3.2# grub
> root (hd0,0)           指定引导分区
> setup (hd0)           安装grub主程序到MBR
> quit
sh-3.2# reboot


Stage2: 配置文件 文件系统中
# rm -rf /boot/grub/
删除GRUB的第二部分
# vim /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
#hiddenmenu
**********熟悉并记住这几行***********************
title Red Hat Enterprise Linux Server (2.6.18-308.el5)  直接加载
#       root (hd0,0)      指定引导分区
        kernel (hd0,0)/vmlinuz-2.6.18-308.el5 ro root=LABEL=/  rhgb quiet   *********//rhgb 表示彩色方式,quiet 静默方式
        initrd (hd0,0)/initrd-2.6.18-308.el5.img     *************记住这两行

title Windows 2008                         转交,链式加载
        rootnoverify (hd0,5)     指定windows内核所在的分区
        chainloader +1          将启动转交给内核所在分区的第一个扇区(boot sector)
=================================================================================

grub手动加载内核
grub> kernel (hd0,0)/vm... ro root=/dev/sda2
grub> init.. (hd0,0)/init..
grub> boot


grub加密:支持明文和密文
1. 生成密文口令
[root@station230 ~]# grub-md5-crypt >> /etc/grub.conf
2. vim /etc/grub.conf
password --md5 $1$1Kzpu0$eT9ZXEoP1BvPT6bY6ouej0   //这一行一定要放在title上面,成为全局的

==== initrd= ===
[root@station230 ~]# ll /boot/
-rw------- 1 root root 2616453 10-23 13:47 initrd-2.6.18-308.el5.img
-rw-r--r-- 1 root root 1901940 2012-01-28  vmlinuz-2.6.18-308.el5   //initrd 要和内核绝对匹配

1. 释放出来看看,initrd就是一个伪根目录
[root@station230 boot]# mkdir newinitrd         //创建一个目录
[root@station230 boot]# cp initrd-2.6.18-308.el5.img newinitrd/        拷贝原有的
[root@station230 boot]# cd newinitrd/
[root@station230 newinitrd]# ls
initrd-2.6.18-308.el5.img
[root@station230 newinitrd]#  gunzip -c  initrd-2.6.18-308.el5.img | cpio -id    //以这种解压方式看看里面的东西
11709 blocks
[root@station230 newinitrd]# ll
总计 2577
drwx------ 2 root root    1024 11-06 11:58 bin
drwx------ 3 root root    1024 11-06 11:58 dev
drwx------ 2 root root    1024 11-06 11:58 etc
-rwx------ 1 root root    2291 11-06 11:58 init
-rw------- 1 root root 2616453 11-06 11:57 initrd-2.6.18-308.el5.img
drwx------ 3 root root    1024 11-06 11:58 lib
drwx------ 2 root root    1024 11-06 11:58 proc
lrwxrwxrwx 1 root root       3 11-06 11:58 sbin -> bin
drwx------ 2 root root    1024 11-06 11:58 sys
drwx------ 2 root root    1024 11-06 11:58 sysroot

2. 重新创建initrd(增加模块)
# ls /lib/modules/2.6.18-308.el5/kernel/drivers/net/8139too.ko
/lib/modules/2.6.18-308.el5/kernel/drivers/net/8139too.ko

语法:
mkinitrd -v --with=模块名 新initrd文件名 内核版本
mkinitrd -v --with =8139too new-$(uname -r).img $(uname -r)
注:新增加的8139too模块必须存在于/lib/modules

使用新的initrd文件,目的增加一个模块
# cp new-2.6.18-308.el5.img /boot/  
# vim /etc/grub.conf
title Red Hat Enterprise Linux Server (2.6.18-308.el5)
#       root (hd0,0)
        kernel (hd0,0)/vmlinuz-2.6.18-308.el5 ro root=LABEL=/1 rhgb quiet
        initrd (hd0,0)/new-2.6.18-308.el5.img       使用新的initrd


修改默认的启动级别
将某个服务设置为在相应的级别开机启动或不启动
一些特殊启动,例如启动源码安装的服务/etc/rc.local

光盘进入救援模式
[root@station07 ~]# rm -rf /bin/bash      删除bash了,需要重新安装bash
[root@station07 ~]# rm -rf /etc/inittab   删除了inittab,需要重新安装initscripts
[root@station230 ~]# rpm -qf /etc/inittab
initscripts-8.45.42-1.el5
[root@station230 ~]# rpm -qf /bin/bash
bash-3.2-32.el5

救援模式安装bash
sh-3.2#rpm -ivh bash-3.2-32.el5.i386.rpm --root=/mnt/sysimage/ --force    // --root 指定真实/目录    强制安装

删除inittab后救援。
方法一:
sh-3.2#mkdir yang
sh-3.2#mount /dev/sr0 yang
sh-3.2#cd /yang/Server
sh-3.2#rpm -ivh initscripts-8.45.42-l.el5 --root=/mnt/sysimage/ --force
方法二:
救援模式,内核参数添加    init=/bin/bash


救援模式备份文件:开启网络
sh-3.2#rsync -va /mnt/sysimage/etc 192.168.2.3:/tmp 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值