浅谈linux中的grub

什么是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,然后修复

[[email protected] ~]# 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
[[email protected] ~]# 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=# 设定菜单等待时长

splashimage=(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系统,然后新建虚拟机挂载新硬盘启动并测试其功能。

[[email protected] ~]# 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 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
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +200M

Command (m for help): n
Command action
   e   extended
   p   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): +2G

Command (m for help): n
Command action
   e   extended
   p   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 2610

Command (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): p

Disk /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: 0x4312b6bb

   Device 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  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  125829120 sda
   8        1     204800 sda1
   8        2   62914560 sda2
   8       16   20971520 sdb
   8       17     208813 sdb1
   8       18    2104515 sdb2
   8       19   18651465 sdb3
 253        0   20971520 dm-0
 253        1    2097152 dm-1
 253        2   10485760 dm-2
 253        3   20971520 dm-3
[[email protected] ~]# 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, 204801

Writing inode tables: done                            
Creating journal (4096 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.
[[email protected] ~]# 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, 
	4096000

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# mkswap -L "swap" /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
LABEL=swap, UUID=91554fc3-8fbf-4927-944b-2a0c28cad1f5
[ro[email protected] ~]# mkdir -p /mnt/{boot,sysroot} #创建2个挂载点
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# ls
boot  sysroot
[[email protected] mnt]# mount /dev/sdb1 /mnt/boot/ #挂载新硬盘的分区到挂载点
[[email protected] mnt]# mount /dev/sdb3 /mnt/sysroot/
[[email protected] 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)
[[email protected] mnt]# cd sysroot/
[[email protected] sysroot]# mkdir bin  boot  dev  etc  home  lib  lib64  media  misc  mnt  net  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var
[[email protected] 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
[[email protected] sysroot]# cp /bin/bash /bin/ls bin/ #把bash和ls复制到bin/下
[[email protected] sysroot]# ls bin/
bash  ls
[[email protected] 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)
[[email protected] 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/
[[email protected] 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
###经测试可以在新硬盘的的根下可以使用
[[email protected] sysroot]# cd /mnt/boot/
[[email protected] boot]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 /boot/initramfs-2.6.32-504.el6.x86_64.img .  #复制内核
[[email protected] boot]# ls
initramfs-2.6.32-504.el6.x86_64.img  lost+found  vmlinuz-2.6.32-504.el6.x86_64
[[email protected] 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
[[email protected] boot]# ls
grub  initramfs-2.6.32-504.el6.x86_64.img  lost+found  vmlinuz-2.6.32-504.el6.x86_64
[[email protected] boot]# cd grub/
[[email protected] grub]# vi grub.conf ##制作grub配置文件
default=0
timeout=5
title Min Linux
    root (hd0,0)
    kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/sda3 selinux=0 init=/bin/bash
    initrd /initramfs-2.6.32-504.el6.x86_64.img
[[email protected] grub]# sync
[[email protected] grub]# sync

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

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

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

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

  

时间: 2024-08-01 16:14:08

浅谈linux中的grub的相关文章

浅谈Linux中的信号机制(二)

首先谢谢 @小尧弟 这位朋友对我昨天夜里写的一篇<浅谈Linux中的信号机制(一)>的指正,之前的题目我用的“浅析”一词,给人一种要剖析内核的感觉.本人自知功力不够,尚且不能对着Linux内核源码评头论足.以后的路还很长,我还是一步一个脚印的慢慢走着吧,Linux内核这座山,我才刚刚抵达山脚下. 好了,言归正传,我接着昨天写下去.如有错误还请各位看官指正,先此谢过. 上篇末尾,我们看到了这样的现象:send进程总共发送了500次SIGINT信号给rcv进程,但是实际过程中rcv只接受/处理了1

浅谈linux中shell变量$#,[email&#160;protected],$0,$1,$2,$?的含义解释

浅谈linux中shell变量$#,[email protected],$0,$1,$2,$?的含义解释 下面小编就为大家带来一篇浅谈linux中shell变量$#,[email protected],$0,$1,$2的含义解释.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 摘抄自:ABS_GUIDE 下载地址:http://www.tldp.org/LDP/abs/abs-guide.pdf linux中shell变量$#,[email protected],$

Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理

Java网络编程和NIO详解7:浅谈 Linux 中NIO Selector 的实现原理 转自:https://www.jianshu.com/p/2b71ea919d49 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Java网络编程和NIO https://blog.csdn.net/column/details/21963.html 部分代码会放在我的的Github:https://github.com/h2pl/ 浅谈 Linux

浅谈linux中umask值及其对应的文件权限

最近刚好系统学习到linux文件权限这一块,在"umask值及其对应的文件权限"这一个问题上,找过视频看(老男孩老师的视频,强烈推荐),也看过鸟哥的书,当然也在网上找了找,发现计算方法都是多种多样的,但是个人觉得掌握一种就好了,看了各种方法,觉得其实大多数都是在找规律,不是找规律的又实在是太复杂,因此个人感觉不太好记,时间一长可能就忘记了,于是就根据自己的理解,做各种测试,发现有了如下更好的方法.(从理解和记忆的层次上更好的方法)  1.问题引出:设置umask为004与005时,创建

辛星浅谈Linux中的环境变量

全局变量和本地环境变量: bash  shell使用一种称为环境变量的特性来存储关于shell会话和工作环境的信息,这也是环境变量的名字的由来.该特性还允许我们把数据存储在内存中,以便于在shell中运行的程序或者脚本中访问它们.bash  shell中共有两种类型的环境变量:全局变量和本地变量. 全局环境变量在shell会话和该shell产生的任何子进程中都可见.而本地变量则只在创建它们的shell中可见.在产生需要父进程信息的子进程的应用程序中,这便体现了全局变量的重要作用.系统环境变量使用

浅谈linux 中的目标文件 即.o后缀名的文件

实际上 目标文件从结构上讲,已经非常接近可执行文件,只是没有经过链接的过程,所以其中有些符号或者地址还没有被调整. 实际上上在linux下 都是可以称之为ELF文件. 看一下这个图,a.out 就是我们的hello.c的执行文件.hello.o 就是目标文件 所以实际上他们2 几乎是一样的. 一般目标文件有很多属性,比如符号表啊,字符串之类的,然后目标文件 把这些属性 按照 segment的形式存储,也叫段.就是大家经常遇到的段错误 里的那个段. 一般来说程序代码被编译以后 主要分为程序指令和程

浅谈Linux中男人的魅力—man

在Linux系统中提供帮助的命令.通俗点来说它就是命令的使用手册.它共分九个章节1.用户命令2.系统调用3.C库调用4.设备文件及特殊文件5.配置文件格式6.游戏7.杂项8.管理命令及守护进程9.Linux内核API(CentOS7以后出现)比如想知道某一个命令属于man的那一章节我们可以用whatis command(命令)例: whatis pwd[[email protected] ~]# whatis pwdpwd (1) - print name of current/working

浅谈Linux系统中的7种运行级别及其原理

浅谈Linux系统中的7种运行级别 Linux系统7个运行级别(runlevel): 运行级别0: 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动. 运行级别1: 单用户工作状态,root权限,用于系统维护,禁止远程登陆. 运行级别2: 多用户状态(没有NFS). 运行级别3: 完全的多用户状态(有NFS),登陆后进入控制台命令行模式. 运行级别4: 系统未使用,保留. 运行级别5: X11控制台,登陆后进入图形GUI模式. 运行级别6: 系统正常关闭并重启,默认运行级别不能设为6,

浅谈Linux下Makefile编写

浅谈Linux下Makefile的编写 前言:本文简要介绍Makefile文件的编写规范,结合具体项目中的应用进行讲解. 具体代码地址: https://github.com/AnSwErYWJ/DogFood/blob/master/Makefile 简介 Make工具最主要也是最基本的功能就是通过makefile文件来描述源程序之间的相互关系并自动维护编译工作.而makefile 文件需要按照某种语法进行编写,文件中需要说明如何编译各个源文件并连接生成可执行文件,并要求定义源文件之间的依赖关