引导加载程序grub的使用详解

引导加载程序grub的使用详解

  • 为运行于虚拟机上的CentOS 6添加一个新硬件,提供两个主分区

    • 为硬盘建两个主分区;并为其安装grub
    • 为硬盘的第一个主分区提供内核和ramdisk文件;为第二个分区提供rootfs
    • 为rootfs提供bash、ls、cat程序以及所依赖的库文件
    • 为grub提供配置文件
    • 将新的硬盘设置为第一启动项并能够正常启动目标主机

      • 1.新建分区

          [root@slave1 ~]# fdisk /dev/sdb
           Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
           Building a new DOS disklabel with disk identifier 0xf614f379.
           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)
           1
           Invalid partition number for type `1'
           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): +5G
        
           Command (m for help): n
           Command action
              e   extended
              p   primary partition (1-4)
           p
           Partition number (1-4): 2
           First cylinder (655-2610, default 655):
           Using default value 655
           Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G
        
           Command (m for help): w
           The partition table has been altered!
        
           Calling ioctl() to re-read partition table.
           Syncing disks.
           [root@slave1 ~]# fdisk -l
        
           Disk /dev/sda: 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: 0x00026a25
        
              Device Boot      Start         End      Blocks   Id  System
           /dev/sda1   *           1          64      512000   83  Linux
           Partition 1 does not end on cylinder boundary.
           /dev/sda2              64        2611    20458496   8e  Linux LVM
        
           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: 0xf614f379
        
              Device Boot      Start         End      Blocks   Id  System
           /dev/sdb1               1         654     5253223+  83  Linux
           /dev/sdb2             655        1308     5253255   83  Linux
        
           Disk /dev/mapper/vg_slave1-lv_root: 18.9 GB, 18865979392 bytes
           255 heads, 63 sectors/track, 2293 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: 0x00000000
        
           Disk /dev/mapper/vg_slave1-lv_swap: 2080 MB, 2080374784 bytes
           255 heads, 63 sectors/track, 252 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: 0x00000000
      • 2.挂载boot和根文件系统

           [root@slave1 ~]# mke2fs -t ext4 /dev/sdb1
           [root@slave1 ~]# mke2fs -t ext4 /dev/sdb2
           [root@slave1 ~]# mkdir /myroot
           [root@slave1 ~]# mkdir /myroot/boot
           [root@slave1 ~]# mount /dev/sdb2 /myroot/boot
           [root@slave1 ~]# mkdir /myroot/sysroot
           [root@slave1 ~]# mount /dev/sdb1 /myroot/sysroot
           [root@slave1 myroot]# vi /etc/fstab
           /dev/sdb1               /myroot/sysroot                ext4     defaults        1 1
           /dev/sdb2               /myroot/boot           ext4     defaults        1 2
      • 3.安装配置grub

           [root@slave1 /]# grub-install --root-directory=/myroot /dev/sdb
           [root@slave1 boot]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /myroot/boot/
           [root@slave1 boot]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /myroot/boot/
           [root@slave1 grub]# vim /myroot/boot/grub/grub.conf
            default=0
            timeout=5
            title centos6.5 simple
               root (hd1,1)
               kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sdb1 selinux=0 init=/bin/bash
               initrd /initramfs-2.6.32-431.el6.x86_64.img
      • 4.创建根文件系统

           [root@slave1 myroot]# mkdir etc bin sbin lib lib64 usr home root sys proc var mnt media
           [root@slave1 myroot]# cp /bin/{bash,ls,cat} /myroot/bin/
           [root@slave1 bin]# ldd bash
                   linux-vdso.so.1 =>  (0x00007fffc5f45000)
                   libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f0fbe3f8000)
                   libdl.so.2 => /lib64/libdl.so.2 (0x00007f0fbe1f4000)
                   libc.so.6 => /lib64/libc.so.6 (0x00007f0fbde5f000)
                   /lib64/ld-linux-x86-64.so.2 (0x00007f0fbe622000)
           [root@slave1 bin]# cp  /lib64/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux-x86-64.so.2}  /myroot/lib64/
           [root@slave1 bin]# ldd ls
                   linux-vdso.so.1 =>  (0x00007fff6adfe000)
                   libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd0f7832000)
                   librt.so.1 => /lib64/librt.so.1 (0x00007fd0f762a000)
                   libcap.so.2 => /lib64/libcap.so.2 (0x00007fd0f7425000)
                   libacl.so.1 => /lib64/libacl.so.1 (0x00007fd0f721d000)
                   libc.so.6 => /lib64/libc.so.6 (0x00007fd0f6e89000)
                   libdl.so.2 => /lib64/libdl.so.2 (0x00007fd0f6c84000)
                   /lib64/ld-linux-x86-64.so.2 (0x00007fd0f7a5a000)
                   libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd0f6a67000)
                   libattr.so.1 => /lib64/libattr.so.1 (0x00007fd0f6862000)
           [root@slave1 bin]# cp /lib64/ {libselinux.so.1,librt.so.1,libcap.so.2,libacl.so.1,libc.so.6,libdl.so.2,ld-linux-x86-64.so.2,libpthread.so.0,libattr.so.1} /myroot/lib64/   
             cp: overwrite `/myroot/lib64/libc.so.6'? no
             cp: overwrite `/myroot/lib64/libdl.so.2'? no
             cp: overwrite `/myroot/lib64/ld-linux-x86-64.so.2'? no
           [root@slave1 bin]# ldd cat
                   linux-vdso.so.1 =>  (0x00007fff749e7000)
                   libc.so.6 => /lib64/libc.so.6 (0x00007f6bdc516000)
                   /lib64/ld-linux-x86-64.so.2 (0x00007f6bdc8b3000)
           [root@slave1 bin]# cp  /lib64/{libc.so.6,ld-linux-x86-64.so.2} /myroot/lib64/
             cp: overwrite `/myroot/lib64/libc.so.6'? no
             cp: overwrite `/myroot/lib64/ld-linux-x86-64.so.2'? no
      • 5.更改启动项

        • 点击菜单栏的虚拟机选项,下拉菜单里点击电源,电源选项里选择打开电源时进入固件,进入BIOS设置界面,选择boot菜单里的VMware Virtual SCSI Hard Drive(0:1)将新的硬盘设置为第一启动项,保存

原创文章,作者:N27_xiaoni,如若转载,请注明出处:http://www.178linux.com/85827

(2)
上一篇 2017-09-04 11:12
下一篇 2017-09-04 11:16

相关推荐

  • Linux文本处理工具

    Linux文本处理工具 文件内容查看的工具:cat tac rev cat [options] [filename]     -E //显示行结束符     -n //对显示出的行编号     -A //显示所有控制符     -b //非空行编号     -S //压缩连续的空行为一行 tac //用于将文件已行为单位的反序输出,即第一行最后显示,最后一行…

    Linux干货 2017-09-09
  • shell编程——让你又爱又恨的东西

    变量类型:      不同的数据类型在系统中所占资源不同,并且表示的范围也不同      数值型:          短整型short:      &…

    Linux干货 2016-08-12
  • CentOS 7 忘记root密码的解决之道

    1、启动时任意键暂停启动,会出现如下界面: 2、按e键进入编辑模式,将光标移动linux16开始的行,添加内核参数 rd.break 3、按 ctrl+x 组合键启动 4、查看各分区和文件系统的挂载情况,需要将 / 的模式改为 rw 5、当前系统上的 / 是在光盘上的,切换至挂载目录下的 / ,才可以修改密码 6、在 / 目录下创建autorela…

    Linux干货 2016-09-26
  • 第六周小练习

    详细总结vim编辑器的使用并完成一下练习 vim编辑器的基本使用 vim模式: 1、编辑模式(命令模式) 2、输入模式 3、末行模式  一、打开文件 vim /path/to/somefile 打开单个文件 如果文件存在为打开,否则保存退出时为新建 vim /path/to/somefile vim +# : 打开文件…

    Linux干货 2016-12-19
  • cp复制软链接失效的原因

    在学习cp命令时我们会知道复制软链接时,如果要保留链接文件使用-d,但当我们实际操作时却常常出现如下情况 [root@localhost ~]# ls -l /etc/redhat-release     #此文件为链接文件 lrwxrwxrwx. …

    Linux干货 2016-10-25
  • 程序包管理:rpm , yum ,与编译

    1.RPM,Redhat Package Manager。是Redhat和CentOS等Linux系统上常见的软件安装工具。 软件安装: 常见选项 -i                #install…

    Linux干货 2016-03-26

评论列表(1条)

  • 马哥教育
    马哥教育 2017-10-10 13:01

    看完的眼都花了,完成的很不错,讲解的也比较清楚。注意给代码着色,增加可读性。