当前位置:首页 > 操作系统 > Linux

谢烟客---------Linux之grub应用

Grub基础、工作流程、grub配置文件、grub功能、grub内置命令行接口

grub安装、grub修复

双系统安装(先Windows后Linux,以免windows NTloader会覆盖Linux loader)


GRUB Grand Uniform Bootloader

    CentOS5,6 grub1 0系,设备或分区编号从0开始

    CentOS7 grub2 1系,设备或分区编号从1开始


CentOS5,6 grub应用:


一、grub的工作流程

stage1 mbr中   

stage1.5 mbr之后的扇区,存储stage2所在分区的驱动

stage2 提供菜单,加载内核和ramdisk至内存,移交控制权


二、grub配置文件

通常boot(stage2)在一个普通的分区上:

boot为一个独立的分区, /可以为普通分区或lvm。访问boot通过挂载在根中实现

内核: kernel /vmlinuz, initrd /initramfs


boot和根在同一个分区,/必须为普通分区

内核: kernel /boot/vmlinuz, initrd /boot/initramfs

    

配置路径

/etc/grub.conf -> ../boot/grub/grub.conf

/boot/grub/menu.lst -> ./grub.conf


配置格式

default=0            ## 默认启动菜单 
timeout=5            ## 等待用户选择菜单的超时时长
splashimage=(hd0,0)/meiling.xpm.gz     ## 菜单的背景图片 640X480 14位索引色
hiddenmenu                             ## 是否隐藏菜单
password --md5 STRING                  ## 菜单加密功能,STRING由grub-md5-crypt 或 openssl passwd -1 -salt STRING 生成
title CentOS 6 (2.6.32-696.el6.i686)   ## 定义菜单项的标题
root (hd0,0)                           ## 引导设备位置 左0: 第一个磁盘设备,右0:第一个分区
    kernel /vmlinuz-2.6.32-696.el6.i686 ro root=/dev/mapper/myvg-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_LVM_LV=myvg/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us crashkernel=auto rhgb quiet rd_NO_DM rd_LVM_LV=myvg/lv_root rhgb quiet
    initrd /initramfs-2.6.32-696.el6.i686.img
    password --md5 STRING

注意:  root,kernel,initd格式的缩进    


三、grub的功能

提供菜单 : tile TITLE

加载用户选择的kernel及ramdisk : kernel path parameters , initrd path

为菜单提供保护机制(kernel和菜单编辑认证) : password --md5 STRING


四、grub的内置命令行接口

root (hd#,#)  指定引导设备在哪个设备的哪个分区上。#从0开始,表示1. 


kernel /vmlinuz PARAMETERS

  当stage1.5找到stage2时,内核只识别stage2所在的分区,此分区为/,所以分区的vmlinuz路径为,/vmlinuz

PARAMETERS:

  ro              以只读方式挂载root=PATH指明的路径的根

  root=<路径>        指明root的位置

  quiet            静默模式

  selinux=0        不启动selinux

  init=/path/to/somefile 开机后,init程序为哪个。默认为/sbin/init  

  1,s,S,single       单用户模式

  <更多参数: google: kernel command line parameters>

  

initrd /initramfs 指定initramfs的路径


五、grub的安装

安装grub: grub-install命令 重新安装grub stage1,1.5,2


grub-install命令

grub-install --root-directory=DIR DEIVCE

    --root-directory=DIR 指向boot目录的上一级目录

    DEVICE 给哪个设备安装grub


1)关机

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055815397.jpg" title="QQ截图20170829174624.png" />


2)添加新的磁盘

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055815480.jpg" title="QQ截图20170829174926.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055815847.jpg" title="QQ截图20170829174950.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055815974.jpg" title="QQ截图20170829175036.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816135.jpg" title="QQ截图20170829175056.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816332.jpg" title="QQ截图20170829175123.png" />


3)启动虚拟机

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816497.jpg" title="QQ截图20170829175457.png" />


4)进入命令行

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816585.jpg" title="QQ截图20170829175838.png" />


5)查看磁盘

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816668.jpg" title="QQ截图20170829180009.png" />


6)分区

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816775.jpg" title="QQ截图20170829180101.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055816918.jpg" title="QQ截图20170829180208.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817101.jpg" title="QQ截图20170829180223.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817237.jpg" title="QQ截图20170829180244.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817341.jpg" title="QQ截图20170829180341.png" />


7)格式化

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817522.jpg" title="QQ截图20170829180404.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817663.jpg" title="QQ截图20170829180434.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817782.jpg" title="QQ截图20170829180448.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055817937.jpg" title="QQ截图20170829180513.png" />


8)挂载

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818038.jpg" title="QQ截图20170829180700.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818113.jpg" title="QQ截图20170829180820.png" />


9)安装grub

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818260.jpg" title="QQ截图20170829181020.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818362.jpg" title="QQ截图20170829181040.png" />


10)复制bash程序及其相关的库

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818465.jpg" title="QQ截图20170829181555.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818629.jpg" title="QQ截图20170829181611.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055818939.jpg" title="QQ截图20170829182208.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055819095.jpg" title="QQ截图20170829182238.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055819321.jpg" title="QQ截图20170829184432.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055819404.jpg" title="QQ截图20170829185616.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055820733.jpg" title="QQ截图20170829185914.png" />

11) 给出配置文件

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055820910.jpg" title="QQ截图20170829184621.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055821024.jpg" title="QQ截图20170829184712.png" />


12)同步

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055821204.jpg" title="QQ截图20170829182428.png" />


13)关机

# shutdown -h now


13)查看第二个磁盘的名字

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055821375.jpg" title="QQ截图20170829182622.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055821863.jpg" title="QQ截图20170829182653.png" />

13)、新建虚拟机

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822029.jpg" title="QQ截图20170829182821.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822212.jpg" title="QQ截图20170829182908.png" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822317.jpg" title="QQ截图20170829182953.png" />


14)启动新建的虚拟机

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822550.jpg" title="QQ截图20170829184855.png" />


15)启动OK

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822873.jpg" title="QQ截图20170829190304.png" />



六、grub的修复

1、在命令行中: grub只是stage1(MBR 512)被破坏时,才能修复. 

2、开机失效: rescue模式


1、在命令行中: grub

1)备份mbr

[root@localhost ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1


2)破坏mbr的stage1

[root@localhost ~]# dd of=/dev/sda if=/dev/zero bs=446 count=1


3)用grub命令行修复 

1)、进入grub命令行

[root@localhost ~]# 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>


2)、设置引导设备位置

grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83


3)、给设备安装grub

grub> setup (hd0)
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.


4)重启验证

grub> quit  
quit
[root@localhost ~]# reboot
[root@localhost ~]#

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822974.jpg" title="11.PNG" />


2、开机失效: rescue模式

1)备份mbr

[root@localhost ~]# dd if=/dev/sda of=/root/mbr.bak bs=512 count=1


2)破坏mbr的stage1

[root@localhost ~]# dd of=/dev/sda if=/dev/zero bs=446 count=1


3)重启

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055823377.jpg" title="12.PNG" />


4)rescue模式救援

1)、挂载光盘 关机 --> 编辑虚拟主机 --> CD/DVD(IDE) --> 使用ISO镜像文件 --> 浏览选择DVD光盘


2)、开机进入界面 开启虚拟机 

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055823613.jpg" title="1.PNG" />

保证光盘连接


3)、选择黄色标记的行或ESC,输入 linux rescue 后回车

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055823796.jpg" title="1.PNG" />

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055823874.jpg" title="2.PNG" />


4)、选择语言

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824078.jpg" title="y.PNG" />

OK


5)、键盘类型

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824198.jpg" title="z.PNG" />

OK

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824308.jpg" title="x.PNG" />


6)、是否需要启动网络

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824417.jpg" title="w.PNG" />

NO 


7)、临时将/挂载在/mnt/sysimage中

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824622.jpg" title="v.PNG" />

Continue

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055824823.jpg" title="u.PNG" />


8)、告知已经找到磁盘,需要使用root,运行chroot /mnt/sysimage即可

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825057.jpg" title="t.PNG" />

OK


9)、

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825225.jpg" title="s.PNG" />


10)、选择第一个

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825318.jpg" title="r.PNG" />


11)、打印一个shell提示符

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825410.jpg" title="q.PNG" />


12)、切换至原root根目录

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825555.jpg" title="p.PNG" />


13)、修复

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825778.jpg" title="whtj.PNG" />

14)、重启测试

技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055825879.jpg" title="123.PNG" />


技术分享650) this.width=650;" src="/upload/getfiles/default/2022/11/5/20221105055822974.jpg" title="11.PNG" />




双系统安装

1、新建一个虚拟机

2、添加U盘安装windows

3、安装Linux



本文出自 “Reading” 博客,请务必保留此出处http://sonlich.blog.51cto.com/12825953/1960959

原文:http://sonlich.blog.51cto.com/12825953/1960959


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐