grub的简单应用与配置

        grub:(GRand Unified Bootloader)是一个来自GNU项目的多操作系统启动程序,主要用于选择操作系统分区上的不同内核,也用于向内核传递启动参数   

        启动流程:                      

              开电源POST加点自检,BIOS开始从第一个块硬盘的第一个扇区MBR(主引导记录)里读取Bootloader(GRUB)-->载入内核,加载内核中硬件驱动-->接着启动进程init,读取init配置文件-->进入终端程序

    我们可以看到GRUB为POST加电后BIOS找到MBR后开始执行的

目前GRUB分为两个版本:
    grub 0.x  grub legacy  #0版本grub传统版,即CentOS 6及以前版本
    grub 1.x  grub2        #1版本,为新版本, 即 CentOS 7及以上版本

传统grub大体分为三个阶段:
        
    stage1:mbr #第一个阶段
    stage1_5: mbr之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统;提供文件系统驱动,相当于stage1和stage2的桥梁,
    stage2:磁盘分区,挂载后一般放于/boot/grub/目录下

    stage2及内核等通常放置于一个基本磁盘分区;

grub的配置文件:
    
    配置文件位于 /boot/grub/grub.conf 和/etc/grub.conf(软链接)

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg0-root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda#引导盘
default=0  #设定默认启动的菜单项,落单项(title)编号从0开始
timeout=5  #等待时间
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz#是用作GRUB GUI背景的图片所在的位置
hiddenmenu#隐藏菜单
title CentOS 6 (2.6.32-504.el6.x86_64)#启动菜单项名称
        root (hd0,0)#GRUB的根目录设备及内核所在分区
        kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg0-root rd_NO_LUKS rd_NO_DM LANG=en_US.UTF-8 rd_LVM_LV=vg0/swap rd_NO_MD 
        #ro为载入内核为只读
        SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg0/root 
        KEYBOARDTYPE=pc KEYTABLE=us rhgb crashkernel=auto quiet rhgb quiet
        initrd /initramfs-2.6.32-504.el6.x86_64.img#加载的镜像文件

~                                                                  

启动单用户模式:
    
    单用户模式主要用于进行系统的修复工作,登陆系统以root用户身份所以不需要密码

    进入单用户模式的 GRUB操作界面的命令:

        e:编辑当前的启动菜单项
        c:进入GRUB的命令行方式
        b:启动当前菜单项
        d:删除当前行
        esc: 返回GRUB启动菜单界面,取消对当前菜单项所做的任何修改

启动系统后按任意键进入菜单项:
    
grub的简单应用与配置_ linux  grub  单用户
进入grub命令行方式,直接键入c键即可:

grub的简单应用与配置_ linux  grub  单用户_02
这样就进入grub的命令行模式,键入help可以查看帮助信息

grub的简单应用与配置_ linux  grub  单用户_03
键入 help root 我们可以查看到有关root的信息


root [DEVICE] [HDBIAS]
    #用来设置grub的根设备

grub的简单应用与配置_ linux  grub  单用户_04

find (PATH/TO/FILE)

find命令可以寻找分区设备上的文件,查看文件是否存在,如:

grub的简单应用与配置_ linux  grub  单用户_05

root (hd#,#):

我们还可以通过root (hd#,#)来设定根设备,而不需要每次都指定分区

grub的简单应用与配置_ linux  grub  单用户_06


kernel:

    kernel  /PATH/TO/KERNEL_FILE #设定本次启动时用到的内核文件,额外还可以添加许多内核支持使用的cmdline参数

    例如: init=/path/to/init,  selinux=0


initrd:

    initrd  /PATH/TO/INITRAMFS_FIlES#设定为选定内核提供额外文件的ramdisk(内核虚拟盘)

boot:引导启动选定的内核


重启系统后,我们直接键入e键即可看到三行信息,即为我们上边介绍的root(hd0,0),kernel,initrd

gurb会根据grub读取这三行信息

grub的简单应用与配置_ linux  grub  单用户_07


如果没有这三行信息,我们可以自己设定,键入c命令进入grub模式,设定其实与菜单栏一样,然后键入boot重新启动即可:


grub的简单应用与配置_ linux  grub  单用户_08

#ro为只读,root=/dev/DEVICE根据自己环境而定


grub-md5-crypt命令: 生成对应密码串

[root@www ~]# grub-md5-crypt #生成密码
Password: #键入密码
Retype password: #确认密码
$1$IbkOP$S00xFcjTSrPIpf.b6VY/1.#生成的密码串


接下来我们复制密码串到配置文件里: /etc/grub.conf里

[root@www ~]#vim /etc/grub.conf

#添加一行 password--md5  后面跟生成的密码串即可

并且添加一条title(标题)来看一下效果,然后加入password密码串:

grub的简单应用与配置_ linux  grub  单用户_09

重启,我们就可以看到效果,已经有我们自定义的一条标题(my grubG),多打了一个G,半夜了,有点困啊!

grub的简单应用与配置_ linux  grub  单用户_10


现在我们按e键是没有反应的。。这里提示敲回车选择一个系统或键入p键输入密码:

这里我们键入p键,就会出现password: 输入密码

grub的简单应用与配置_ linux  grub  单用户_11

     回车后。。就又回到正常状态,可以键入e,a,c, 键进行操作



Grub的安装:


由于开始对linux不是很熟悉,很多人都开始装双系统,如果先安装windows在装linux,linux是会保留windows的bootloader。,,但是后装windows时linux的引导grub就无法正常引导了,,遇到这样的情况我们可以自行安装grub解决问题,下边我们介绍一下具体的安装方法:


首先我们为虚拟机新加了一个20G(大小自定)的磁盘来装linux系统,然后分区,系统主要有三个分区,分别为root(grub), swap,/我们规划为:

grub引导:100M即可

swap:  2G

/(根分区)5G 

[root@www ~]# fdisk /dev/sdb 
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): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-2610, default 15): 
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): +2G
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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (277-2610, default 277): 
Using default value 277
Last cylinder, +cylinders or +size{K,M,G} (277-2610, default 2610): +5G
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: 0x0ec68c8e
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         276     2104515   82  Linux swap / Solaris
/dev/sdb3             277         930     5253255   83  Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@www ~]# 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

我们通过mkfs -t ext4 /dev/sdb1,sdb3来创建文件系统,swap我们用mkswap可直接创建

[root@www ~]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 2104508 KiB
no label, UUID=7a692be6-4c8e-4b30-b9f0-12ccc972330c
[root@www ~]#

由于我们安装grub是需要boot目录的。。当前我们系统在用,所以我们可以创建一个boot目录并挂载我们的启动分区/dev/sdb1

[root@www ~]# mkdir /mnt/boot
[root@www ~]# mount /dev/sdb1 /mnt/boot/
[root@www ~]# ls /mnt/boot/
lost+found

挂载完成我们就可以安装grub了,由于我们自己挂载的boot目录,安装时需要指定具体boot的安装目录,我们可以使用命令:grub-install --root-directory=/mnt /dev/sdb

[root@www ~]# grub-install --root-directory=/mnt /dev/sdb
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@www ~]# ls /mnt/boot/#查看一下boot下已经有grub了
grub  lost+found
[root@www ~]# ls /mnt/boot/grub/#stage文件已经加载
device.map     ffs_stage1_5      minix_stage1_5     stage2           xfs_stage1_5
e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5
fat_stage1_5   jfs_stage1_5      stage1             vstafs_stage1_5

注意到没,里边貌似没有配置文件,这个配置文件只能自己手动去编写,首先我们把kernel和initramfs拷贝到我们的boot目录,然后自己编辑一个配置文件vim /mnt/boot/grub/grub.conf

default=0
timeout=5
title centOS (fuck you)
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/sda3 selinux=0 init=/bin/bash
        initrd /initramfs.img

接下来创建根文件目录:

    

[root@www ~]# mkdir /mnt/sysroot#创建挂载点
[root@www ~]# mount /dev/sdb3 /mnt/sysroot/
[root@www ~]# cd /mnt/sysroot/
[root@www sysroot]# ls
lost+found
[root@www sysroot]# mkdir -pv etc bin sbin lib lib64 dev var usr home root mnt 
mkdir: created directory `etc'
mkdir: created directory `bin'
mkdir: created directory `sbin'
mkdir: created directory `lib'
mkdir: created directory `lib64'
mkdir: created directory `dev'
mkdir: created directory `var'
mkdir: created directory `usr'
mkdir: created directory `home'
mkdir: created directory `root'
mkdir: created directory `mnt'
[root@www sysroot]# ls
bin  dev  etc  home  lib  lib64  lost+found  mnt  root  sbin  usr  var#创建的文件系统

目录创建好以后。我们需要启动bash.所以同样我们要把/bin/bash目录拷贝到我们创建的/
mnt/sysroot/bin,这里注意我们不能只能把目录考过来。。因为bash依赖于一些库文件,所以我们用命令ldd /bin/bash 查看一下bash依赖的库文件有哪些,我们一起cp过去:

[root@www sysroot]# ldd /bin/bash    linux-vdso.so.1 =>  (0x00007fffadfff000)    libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003e4d600000)    libdl.so.2 => /lib64/libdl.so.2 (0x0000003e42a00000)    libc.so.6 => /lib64/libc.so.6 (0x0000003e42e00000)    /lib64/ld-linux-x86-64.so.2 (0x0000003e42600000)[root@www sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/[root@www sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/[root@www sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
[root@www sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/
[root@www sysroot]# cp /bin/bash /mnt/sysroot/bin/#接着把/bin/bash拷贝到/mnt/sysroot/bin/

配置文件cp以后我们要做根切换,用chroot命令:

[root@www sysroot]# chroot /mnt/sysroot/#把/mnt/sysroot当作根来用
bash-4.1# 
bash-4.1# 
bash-4.1# 
bash-4.1# 
bash-4.1#

这样bash就启动成功了,exit退出,sync同步一下即可


接下来就是测试我们系统的时候了,新建一个虚拟机把我们创建好的文件导入启动:


创建过程中注意这里选择使用现有虚拟磁盘:

grub的简单应用与配置_ linux  grub  单用户_12


记住这个磁盘文件名称。。下边就加入这个文件:

grub的简单应用与配置_ linux  grub  单用户_13

grub的简单应用与配置_ linux  grub  单用户_14

加入磁盘文件后打开虚拟机启动系统


grub的简单应用与配置_ linux  grub  单用户_15


这样就进入了我们自己创建的CentOS系统,大家是不是看到不和谐的东西,(fuck you)标题嘛

这个原因只因为这几天有些很不爽的东西,所以发泄发泄,,,运维嘛!有时候跪下给服务器磕个头还是有必要的。。。


这样回车我们的系统就跑起来了。。。啊。。。


grub的简单应用与配置_ linux  grub  单用户_16


是不是有些朦胧,,这很正常,朦胧过去就是黎明前的曙光。。。。