实例:

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

(1) 为硬盘新建两个主分区;并为其安装grub

(2) 为硬盘的第一个主分区提供内核和ramdisk文件; 为第二个分区提供rootfs

(3) rootfs提供bashlscat程序及所依赖的库文件;

(4) grub提供配置文件;

(5) 将新的硬盘设置为第一启动项并能够正常启动目标主机;

 新增硬盘并分区

[root@localhost~]# fdisk -l /dev/sdb

 

Disk /dev/sdb: 10.7 GB, 10737418240 bytes

255 heads, 63 sectors/track, 1305 cylinders

Units = cylinders of 16065 * 512 = 8225280bytes

Sector size (logical/physical): 512 bytes /512 bytes

I/O size (minimum/optimal): 512 bytes / 512bytes

Disk identifier: 0x6a3c778b

 

  Device Boot      Start         End      Blocks  Id  System

/dev/sdb1               1         132    1060258+  83  Linux

/dev/sdb2             133         264    1060290   83  Linux

         创建文件系统

[root@localhost~]# mke2fs -t ext4 /dev/sdb1

[root@localhost~]# mke2fs -t ext4 /dev/sdb2

         挂载

[root@localhost~]# mkdir /mnt/boot

[root@localhost~]# mount /dev/sdb1 /mnt/boot

         安装grub至分区1

[root@localhost~]# grub-install --root-directory=/mnt /dev/sdb

Probing devices to guess BIOS drives. Thismay 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 ofthe lines is incorrect,

fix it and re-run the script`grub-install'.

         复制内核文件和initrdfs文件

[root@localhostboot]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz

[root@localhostboot]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img  /mnt/boot/initramfs

         创建grub配置文件

[root@localhostboot]# vim /mnt/boot/grub//grub.conf

default=0

timeout=5

title CentOS6(test)

root (hd0,0)

kernel /vmlinuz ro root=/dev/sdb2 selinux=0init=/bin/bash

initrd /initramfs

 

         卸载sdb1,挂载sdb2,并创建rootfs相关目录

[root@localhost /]# umount /dev/sdb1

[root@localhost /]# mount /dev/sdb2 /mnt

[root@localhost /]# mkdir -p/mnt/{bin,sbin,lib,lib64,etc,home,root,media,dev,mnt,tmp}

[root@localhost /]# mkdir -p/mnt/{usr/{bin,sbin,lib,lib64},var/{lib,lib64,log,local,cache

},proc,sys,selinux}

         rootfs提供bashlscat程序及所依赖的库文件;

cp /bin/{bash,ls,cat} /mnt/bin

ldd命令:                                   #需要ldd命令

- print shared library dependencies

ldd [OPTION]... FILE...

[root@localhost/]# cp `ldd /bin/{bash,ls,cat}|grep -Eo "/lib.*[[:space:]]"| sort -u`/mnt

/lib64

[root@localhost /]# sync                     #同步到磁盘

         重启后调整硬盘启动顺序测试

         调整虚拟机bios硬盘开机启动项

wKiom1kMY8uxJYfpAABcA3flcCI771.png-wh_50

启动后读取自定义的grub.conf

wKiom1kMY-_x4y9aAABGJWZVgUs974.png-wh_50

正常启动

wKioL1kMZALynmN2AABypJSZYlc740.png-wh_50