什么是GRUB

GRUB 是一个多重操作系统启动管理器。既可以用来引导window也可以引导linux,是一个非常强大的bootloader,linux运维必用神器。

GRUB版本

grub 0.x  grub legacy Centos 5,6中广泛使用

stage1 位于mbr的前446字节,其主要作用是定位stage2,由于不能识别文件系统,则需要位于紧跟mbr之后的stage1_5帮助其识别文件系统。

stage2 位于磁盘分区上,为内核提供菜单,交互式接口,以及菜单保护机制

grub 1.x  grub2 Centos 7中使用

GRUB修复

当系统的grub损坏时,可使用grub-install 或者grub命令

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

或者 grub

grub> root (hd#,#)

grub> setup (hd#)

实验:通过dd破坏grub,然后修复

[root@test-1 ~]# dd if=/dev/zero of=/dev/sda count=1 bs=200
1+0 records in
1+0 records out
200 bytes (200 B) copied, 0.00882837 s, 22.7 kB/s
[root@test-1 ~]# reboot

重启后,由于没有grub引导,自动转由pxe启动

挂载镜像,进入救援模式修复grub

一路默认,到救援模式下的shell

通过grub命令修复

然后,quit,reboot系统。重启后,系统OK

或者在救援模式shell命令行使用grub-install --root-directory=ROOT /dev/DISK也可以修复

在GRUB命令行接口手动启动系统

grub> root (hd#,#)

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

grub> initrd /initramfs-VERSION-RELEASE.img

grub> boot

GRUB配置文件

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

   参数配置项:

default=# 设定默认启动的菜单项;标题项(title)编号从0开始;  
    timeout=# 设定菜单等待时长

splashp_w_picpath=(hd#,#)/PATH/TO/FILE:指明菜单背景图片文件路径

hiddenmenu 隐藏菜单

[password [--md5]] STRING: 设置菜单编辑密码 (可有grub-md5-crypt生成md5密码串)

title String 定义菜单项标题,有多个内核时会有多个

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

kernel /PATH/TO/VMLINUZ_FILE ro root=/dev/xxx  开机启动的内核

initrd /PATH/TO/INITRAMFS_FILE  内核匹配的ramfs文件;

[password [--md5] STRING] 启动选定的内核或操作系统时进行认证;

实验题:在先有虚拟机上新增一块硬盘,为其提供单独运行的bash系统,然后新建虚拟机挂载新硬盘启动并测试其功能。

[root@test-1 ~]# fdisk /dev/sdb #新硬盘,创建3个分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4312b6bb.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. It's strongly recommended toswitch off the mode (command 'c') and change display units tosectors (command 'u').Command (m for help): n
Command actione   extendedp   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +200MCommand (m for help): n
Command actione   extendedp   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (27-2610, default 27):
Using default value 27
Last cylinder, +cylinders or +size{K,M,G} (27-2610, default 2610): +2GCommand (m for help): n
Command actione   extendedp   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (289-2610, default 289):
Using default value 289
Last cylinder, +cylinders or +size{K,M,G} (289-2610, default 2610):
Using default value 2610Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)Command (m for help): pDisk /dev/sdb: 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: 0x4312b6bbDevice Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          26      208813+  83  Linux
/dev/sdb2              27         288     2104515   82  Linux swap / Solaris
/dev/sdb3             289        2610    18651465   83  LinuxCommand (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
[root@test-1 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@test-1 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@test-1 ~]# cat /proc/partitions
major minor  #blocks  name8        0  125829120 sda8        1     204800 sda18        2   62914560 sda28       16   20971520 sdb8       17     208813 sdb18       18    2104515 sdb28       19   18651465 sdb3253        0   20971520 dm-0253        1    2097152 dm-1253        2   10485760 dm-2253        3   20971520 dm-3
[root@test-1 ~]# mkfs -t ext4 -L "boot" /dev/sdb1 #创建文件系统
mke2fs 1.41.12 (17-May-2010)
Filesystem label=boot
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
52208 inodes, 208812 blocks
10440 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
26 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@test-1 ~]# mkfs -t ext4 -L "root" /dev/sdb3
mke2fs 1.41.12 (17-May-2010)
Filesystem label=root
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1166880 inodes, 4662866 blocks
233143 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
143 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@test-1 ~]# mkswap -L "swap" /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
LABEL=swap, UUID=91554fc3-8fbf-4927-944b-2a0c28cad1f5
[root@test-1 ~]# mkdir -p /mnt/{boot,sysroot} #创建2个挂载点
[root@test-1 ~]# cd /mnt/
[root@test-1 mnt]# ls
boot  sysroot
[root@test-1 mnt]# mount /dev/sdb1 /mnt/boot/ #挂载新硬盘的分区到挂载点
[root@test-1 mnt]# mount /dev/sdb3 /mnt/sysroot/
[root@test-1 mnt]# mount
/dev/mapper/vg0-root 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/sda1 on /boot type ext4 (rw)
/dev/mapper/vg0-usr on /usr type ext4 (rw)
/dev/mapper/vg0-var on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /mnt/boot type ext4 (rw)
/dev/sdb3 on /mnt/sysroot type ext4 (rw)
[root@test-1 mnt]# cd sysroot/
[root@test-1 sysroot]# mkdir bin  boot  dev  etc  home  lib  lib64  media  misc  mnt  net  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@test-1 sysroot]# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  misc  mnt  net  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[root@test-1 sysroot]# cp /bin/bash /bin/ls bin/ #把bash和ls复制到bin/下
[root@test-1 sysroot]# ls bin/
bash  ls
[root@test-1 sysroot]# ldd /bin/bash /bin/ls
/bin/bash:linux-vdso.so.1 =>  (0x00007fffd6fff000)libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003c51c00000)libdl.so.2 => /lib64/libdl.so.2 (0x0000003c47000000)libc.so.6 => /lib64/libc.so.6 (0x0000003c47400000)/lib64/ld-linux-x86-64.so.2 (0x0000003c46c00000)
/bin/ls:linux-vdso.so.1 =>  (0x00007fff78bff000)libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003c48c00000)librt.so.1 => /lib64/librt.so.1 (0x0000003c47c00000)libcap.so.2 => /lib64/libcap.so.2 (0x0000003c4a800000)libacl.so.1 => /lib64/libacl.so.1 (0x0000003c52000000)libc.so.6 => /lib64/libc.so.6 (0x0000003c47400000)libdl.so.2 => /lib64/libdl.so.2 (0x0000003c47000000)/lib64/ld-linux-x86-64.so.2 (0x0000003c46c00000)libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c47800000)libattr.so.1 => /lib64/libattr.so.1 (0x0000003c52800000)
[root@test-1 sysroot]# cp /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 /lib64/libselinux.so.1 /lib64/librt.so.1 /lib64/libcap.so.2 /lib64/libacl.so.1 /lib64/libpthread.so.0 /lib64/libattr.so.1 lib64/
[root@test-1 sysroot]# chroot /mnt/sysroot/  #chroot测试bash是否可用
bash-4.1# ls
bin  boot  dev  etc  home  lib  lib64  lost+found  media  misc mnt  net  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
bash-4.1# exit
exit
###经测试可以在新硬盘的的根下可以使用
[root@test-1 sysroot]# cd /mnt/boot/
[root@test-1 boot]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /boot/initramfs-2.6.32-504.el6.x86_64.img .  #复制内核
[root@test-1 boot]# ls
initramfs-2.6.32-504.el6.x86_64.img  lost+found  vmlinuz-2.6.32-504.el6.x86_64
[root@test-1 boot]# grub-install --root-directory=/mnt /dev/sdb    #制作grub
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
[root@test-1 boot]# ls
grub  initramfs-2.6.32-504.el6.x86_64.img  lost+found  vmlinuz-2.6.32-504.el6.x86_64
[root@test-1 boot]# cd grub/
[root@test-1 grub]# vi grub.conf ##制作grub配置文件
default=0
timeout=5
title Min Linuxroot (hd0,0)kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/sda3 selinux=0 init=/bin/bashinitrd /initramfs-2.6.32-504.el6.x86_64.img
[root@test-1 grub]# sync
[root@test-1 grub]# sync

然后创建新的虚拟机,挂载新的硬盘,测试。

开机显示grub菜单,说明grub是OK的。

最后进入bash命令,测试一下ls是否可用。

这样完成了一个有简单bash命令的linux。

  

转载于:https://blog.51cto.com/iznowow/1691563

浅谈linux中的grub相关推荐

  1. 浅谈Linux中ldconfig和ldd的用法

    ldd 查看程序依赖库 ldd 作用:用来查看程式运行所需的共享库,常用来解决程式因缺少某个库文件而不能运行的一些问题. 示例:查看test程序运行所依赖的库: /opt/app/todeav1/te ...

  2. linux中sh+$0,浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释

    摘抄自:ABS_GUIDE 下载地址:http://www.tldp.org/LDP/abs/abs-guide.pdf linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: ...

  3. 浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释

    浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释 下面小编就为大家带来一篇浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释.小编觉得挺不错的,现在就分享给 ...

  4. linux中whoami命令的作用是,浅谈linux中的whoami与 who指令

    浅谈linux中的whoami与 who指令 whoami 功能说明: 显示用户名称 语法: whoami 补充说明: 显示自身的用户名称,本指令相当于执行  id -un 指令 whoami 与 w ...

  5. ldconfig mysql_浅谈Linux中ldconfig和ldd的用法

    ldd 查看程序依赖库 ldd 作用:用来查看程式运行所需的共享库,常用来解决程式因缺少某个库文件而不能运行的一些问题. 示例:查看test程序运行所依赖的库: /opt/app/todeav1/te ...

  6. 实现Linux的whoami命令,浅谈linux中的whoami与 who指令

    whoami 功能说明: 显示用户名称 语法: whoami 补充说明: 显示自身的用户名称,本指令相当于执行  id -un 指令 whoami 与 who am i的区别 who这个命令重点在用来 ...

  7. 浅谈linux中的根文件系统(rootfs的原理和介绍)

    转自:点击打开 linux中有一个让很多初学者都不是特别清楚的概念,叫做"根文件系统".我接触linux前前后后也好几年了,但是对这个问题,至今也不是特别的清楚,至少没法给出一个很 ...

  8. 浅谈Linux中的信号处理机制(三)

    一晃眼,已经到9月底了,都来不及去感慨时间匆匆.最近常常会想明年的今天我将会在那里干着什么样的工作?对未来又是憧憬又是担忧,压力山大.无论如何现在还是踏踏实实的学习吧,能这样安安静静学习的日子也不多了 ...

  9. ln -s命令 linux_浅谈linux中ln命令,附带案例

    链接类型: 1.硬链接 通过索引节点来进行连接.在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index).多个文件名指向同一索引节点是 ...

最新文章

  1. 小米8 SE和小米9 SE 开源 Android 9 Pie 内核代码
  2. 两轴机械臂+机械爪整体控制板设计与机械爪控制调试
  3. Qt应用程序主窗口之一:主窗口框架
  4. 顺序表的结构和9个基本运算算法
  5. 【STM32】FreeRTOS 任务切换
  6. 基于函数计算的 BFF 架构
  7. [原创]分布式系统之缓存的微观应用经验谈(三)【数据分片和集群篇】
  8. 多项式(polynomial)和 单项式(monomial)
  9. Spring Cloud构建微服务架构:分布式服务跟踪(跟踪原理)【Dalston版】
  10. 开奖啦!CSDN 程序员节 1024 中奖名单揭晓!
  11. 3年后准确率仍达97%:利用谷歌语音转文本 API 绕过reCAPTCHA
  12. 用机器学习来预测情侣间何时吵架,这是一个很实用的算法
  13. iOS安装CocoaPods详细过程
  14. 取消锚(a/)点击后页面跳转的几种方法
  15. 转置矩阵使用T,Hermite矩阵、正交矩阵、酉矩阵、奇异矩阵、正规矩阵、幂等矩阵
  16. web前端笔试题-完善版
  17. ZYNQ ucos-ii中使用lwip及usb
  18. Jmeter 接口造数
  19. 附录A 计算机的0和1
  20. SAP 软件价格体系及项目费用构成

热门文章

  1. linux搭建yum源
  2. CentOS 6.0 + Zabbix 1.8.5 服务器端安装
  3. 工控2月补丁星期二:西门子、施耐德电气修复近50个漏洞
  4. 中秋逢国庆 | 盛世华诞 阖家团圆
  5. exchange2013卸载
  6. BZOJ 1041 圆上的整点 数学
  7. 呼叫中心职场EQ与情绪压力管控(时刻提醒自己!)
  8. iOS之socket编程
  9. 如果你不懂备案,那我简单点跟你说
  10. 教你一种U盘病毒的快速检测方法