一.第一种方式安装grub
1.首先要确定你的系统所在的分区
[root@RedHat-5 boot]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 89 514080 82 Linux swap / Solaris /dev/sda3 90 2610 20249932+ 83 Linux
2.手动破坏bootloader(只要破坏MBR中的前446字节就行)
[root@RedHat-5 boot]# dd if=/dev/zero of=/dev/sda bs=400 count=1
然后同步数据到磁盘上
[root@RedHat-5 boot]# sync [root@RedHat-5 boot]# sync
记住这时一定不要重启,如果重启了只有在紧急救援模式才能修复
3.用grub命令进入grub命令行接口
[root@RedHat-5 boot]# grub
4.指定内核所在分区及要安装到那个分区上
grub> root (hd0,0) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd0) Checking if "/boot/grub/stage1" exists... no Checking if "/grub/stage1" exists... yes Checking if "/grub/stage2" exists... yes Checking if "/grub/e2fs_stage1_5" exists... yes Running "embed /grub/e2fs_stage1_5 (hd0)"... 15 sectors are embedded. succeeded Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.co nf"... succeeded Done. grub> quit
5.然后重启就行了
二.第二中方式安装grub
1.再添加一块硬盘用于做测试使用, 下面为对新增的硬盘进行分区和格式化
[root@RedHat-5 ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. 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) Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System 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-130, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): +100M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (14-130, default 14): Using default value 14 Last cylinder or +size or +sizeM or +sizeK (14-130, default 130): +500M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (76-130, default 76): Using default value 76 Last cylinder or +size or +sizeM or +sizeK (76-130, default 130): +100M Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 13 104391 83 Linux /dev/sdb2 14 75 498015 83 Linux /dev/sdb3 76 88 104422+ 83 Linux Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): L 0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris 1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 82 Linux swap / So c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 83 Linux c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 84 OS/2 hidden C: c7 Syrinx 5 Extended 41 PPC PReP Boot 85 Linux extended da Non-FS data 6 FAT16 42 SFS 86 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set de Dell Utility 8 AIX 4e QNX4.x 2nd part 88 Linux plaintext df BootIt 9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 93 Amoeba e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi ee EFI GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a6 OpenBSD f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT Hex code (type L to list codes): 82 Changed system type of partition 3 to 82 (Linux swap / Solaris) Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 13 104391 83 Linux /dev/sdb2 14 75 498015 83 Linux /dev/sdb3 76 88 104422+ 82 Linux swap / Solaris Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@RedHat-5 ~]# partprobe /dev/sdb [root@RedHat-5 ~]# sync [root@RedHat-5 ~]# sync [root@RedHat-5 ~]# sync [root@RedHat-5 ~]# sync [root@RedHat-5 ~]# sync [root@RedHat-5 ~]# fdisk -l
2.在/mnt/下创建boot目录用于挂载boot
[root@RedHat-5 boot]# cd /mnt/ [root@RedHat-5 mnt]# ls -a . .. [root@RedHat-5 mnt]# mkdir -pv boot mkdir: created directory `boot' [root@RedHat-5 mnt]# ls -a . .. boot [root@RedHat-5 mnt]# mount /dev/sda3 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/sda1 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) [root@RedHat-5 mnt]# fdisk -l /dev/sdb Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 13 104391 83 Linux /dev/sdb2 14 75 498015 83 Linux /dev/sdb3 76 88 104422+ 82 Linux swap / Solaris [root@RedHat-5 mnt]# mount /dev/sdb1 /mnt/boot/ [root@RedHat-5 mnt]# ls -a . .. boot
3.手动安装grub
[root@RedHat-5 mnt]# 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
4.在/mnt/boot/目录下自动生成了grub目录
[root@RedHat-5 boot]# ls -a . .. grub lost+found
5.手动提供/mnt/boot/grub/grub.conf文件
default=0 timeout=5 title This is my linux root (hd0,0) kernel /vmlinuz initrd /initrd.img
6.将设备文件卸载
[root@RedHat-5 boot]# umount /mnt/boot/
7.挂起当前系统 重新新建一个虚拟机(使用一个已存在的虚拟硬盘,上面制作好的)
启动虚拟机