引導加載程序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.www58058.com/85827
看完的眼都花了,完成的很不錯,講解的也比較清楚。注意給代碼著色,增加可讀性。