您的位置:首页 > 其它

浅谈grub

2015-09-04 21:03 399 查看
GRUB:GRand Unified Bootloader 开机管理程序扮演的角色,就是要在计算机开机时负责加载和传输操作系统的核心程序。

现在大部分的操作系统默认提供的开机管理程序都可以多重开机,GRUB(GRand Unified Bootloader)是由GNU(GNU’s Not Unix)所提供,GRUB 在使用、设置、操作或接口上,都比 Windows 或之前使用的另一套 LILO 方便,而grub是存在MBR里面的。为什么需要这个MBR,主要是因为BIOS太小,功能有限。当系统加电,bios自检后,就会将MBR的Load进内存。也就意味着引导程序被激活,分区信息被加载到内存,同时也意味着对系统的控制权从bios过渡到grubGRUB的版本: GRand uniform Bootloader 0.9 ceontos6 Grub2 centos7GRUB是一个系统引导程序,提供一个菜单,允许用户选择要启动系统或不同的内核版本,把用选定的内核装载到内存中的特定空间中,解压.展开,并把系统控制权移交给内核.(而Bootloader里面的程序是grub,它的作用是启动内核,grub分两部分组成,stage1和stage2,stage1是在MBR里面的,为了引导stage2的。stage2是存放在分区文件系统上的,分区在系统没有启动之前是无法识别的。则需要stage1.5这个中间层来协助stage1找到stage2,当stage2引导后,将内核和文件系统映射到内存,从而把系统控制权交给内核。

通过制作一个Linux系统启动流程,来了解grub的作用:
给虚拟机新添加一块磁盘,分三个区(100M 2G 5G),格式化



[root@centos6 ~]# mke2fs -t ext4 /dev/sdb1
[root@centos6 ~]# mkswap /dev/sdb2
[root@centos6 ~]# mke2fs -t ext4 /dev/sdb3
[root@centos6 ~]# mkdir /mnt/boot
[root@centos6 ~]# mount /dev/sdb1 /mnt/boot
[root@centos6 ~]# cd /mnt/boot/
[root@centos6 boot]# grub-install --root-directory=/mnt /dev/sdb  #安装grub
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@centos6 boot]# ls
grub  lost+found
[root@centos6 boot]# cp /boot/vmlinuz-2.6.32-504.el6.x86_64 vmlinuz
[root@centos6 boot]# cp /boot/initramfs-2.6.32-504.el6.x86_64.img initramfs.img
[root@centos6 boot]# ls
grub  initramfs.img  lost+found  vmlinuz
[root@centos6 grub]# vim grub.conf
default=0
timeout=5
title minilinux
root (hd0,0)
kernel /vmlinuz   ro root=/dev/sda3  selinux=0 init=/bin/bash
initrd /initramfs.img
[root@centos6 ~]# umount /dev/sdb1
[root@centos6 ~]# mkdir /mnt/sysroot
[root@centos6 ~]# mount /dev/sdb1 /mnt/sysroot/
[root@centos6 ~]# cd /mnt/sysroot/
[root@centos6 sysroot]# mkdir -pv etc lib lib64 root home sbin bin proc sys dev opt
[root@centos6 bin]# cp /bin/bash .
[root@centos6 sysroot]# ldd /bin/bash
linux-vdso.so.1 =>  (0x00007fffa6bff000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00000035c7e00000)
libdl.so.2 => /lib64/libdl.so.2 (0x00000035b6600000)
libc.so.6 => /lib64/libc.so.6 (0x00000035b6a00000)
/lib64/ld-linux-x86-64.so.2 (0x00000035b6200000)
[root@centos6 sysroot]# cp /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
[root@centos6 sysroot]# ls /mnt/sysroot/lib64/
ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libtinfo.so.5
[root@centos6 bin]# chroot /mnt/sysroot/
bash-4.1#





grub的修复--光盘修复
[root@centos6 ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1 #用dd来破坏grub
然后用光盘来修复
chroot /mnt/sysimage  #切换根
grub-install --root-directory=/ /dev/sda 安装grub
如此重要的MBR应该要做备份: dd if=/dev/sda of=sda.mbr.back bs=512 count=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: