linux不重启挂载磁盘安装grub

挂载、分区、grub

通过给一块新磁盘安装grub回顾磁盘挂载、分区文件系统创建等操作:

该实验基于(CtonOS6.8kernel:2.6.32-642.15.1.el6.x86_64

1.通过VMware Workstationg添加一块磁盘(SCSI);

2./sys下SCSI扫描,查看主机总线号,磁盘肯定是有总线连接着:

 

1 [root@cl Test]# ls /sys/class/scsi_host/
2 host0  host1  host2
3 [root@cl Test]# echo "- - -" >  /sys/class/scsi_host/host0/scan
4 [root@cl Test]# echo "- - -" >  /sys/class/scsi_host/host1/scan
5 [root@cl Test]# echo "- - -" >  /sys/class/scsi_host/host2/scan
6 [root@cl Test]# fdisk -l

 

3.创建分区:

 1 [root@cl Test]# fdisk /dev/sdb
 2 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 3 Building a new DOS disklabel with disk identifier 0xa61749e4.
 4 Changes will remain in memory only, until you decide to write them.
 5 After that, of course, the previous content won't be recoverable.
 6 
 7 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 8 
 9 WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
10          switch off the mode (command 'c') and change display units to
11          sectors (command 'u').
12 
13 Command (m for help): n
14 Command action
15    e   extended
16    p   primary partition (1-4)
17 p
18 Partition number (1-4): 1
19 First cylinder (1-2610, default 1): 
20 Using default value 1
21 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +200M
22 
23 Command (m for help): n
24 Command action
25    e   extended
26    p   primary partition (1-4)
27 p
28 Partition number (1-4): 2
29 First cylinder (27-2610, default 27): 
30 Using default value 27
31 Last cylinder, +cylinders or +size{K,M,G} (27-2610, default 2610): +2G
32 
33 Command (m for help): t
34 Partition number (1-4): 2
35 Hex code (type L to list codes): 82
36 Changed system type of partition 2 to 82 (Linux swap / Solaris)
37 
38 Command (m for help): n
39 Command action
40    e   extended
41    p   primary partition (1-4)
42 p
43 Partition number (1-4): 3
44 First cylinder (289-2610, default 289): 
45 Using default value 289
46 Last cylinder, +cylinders or +size{K,M,G} (289-2610, default 2610): +5G
47 
48 Command (m for help): w
49 The partition table has been altered!
50 
51 Calling ioctl() to re-read partition table.
52 Syncing disks.
53 [root@cl Test]# partx -a /dev/sdb
54 BLKPG: Device or resource busy
55 error adding partition 1
56 BLKPG: Device or resource busy
57 error adding partition 2
58 BLKPG: Device or resource busy
59 error adding partition 3
60 [root@cl Test]# cat /proc/partitions 
61 major minor  #blocks  name
62 
63    8        0  125829120 sda
64    8        1     204800 sda1
65    8        2    5242880 sda2
66    8        3    2097152 sda3
67    8        4          1 sda4
68    8        5   10487075 sda5
69    8        6    5253223 sda6
70    8        7    8393931 sda7
71    8       16   20971520 sdb
72    8       17     208813 sdb1
73    8       18    2104515 sdb2
74    8       19    5253255 sdb3

4.创建文件系统:

1 [root@cl Test]# mke2fs -t ext4 /dev/sdb1
2 [root@cl Test]# mke2fs -t ext4 /dev/sdb3
3 [root@cl Test]# mkswap /dev/sdb2

5.挂载分区:

1 [root@cl ~]# mkdir /mnt/boot
2 [root@cl ~]# mount /dev/sdb1 /mnt/boot/
3 [root@cl ~]# ls /mnt/boot/
4 lost+found

6.安装grub:

 1 [root@cl ~]# grub-install --root-directory=/mnt /dev/sdb1 (指明/的位置在mnt下)
 2 Probing devices to guess BIOS drives. This may take a long time.
 3 Installation finished. No error reported.
 4 This is the contents of the device map /mnt/boot/grub/device.map.
 5 Check if this is correct or not. If any of the lines is incorrect,
 6 fix it and re-run the script `grub-install'.
 7 
 8 (fd0)    /dev/fd0
 9 (hd0)    /dev/sda
10 (hd1)    /dev/sdb
11 [root@cl ~]# ls /mnt/boot/
12 grub  lost+found
13 [root@cl ~]# ls /mnt/boot/grub/
14 device.map     ffs_stage1_5      minix_stage1_5     stage2           xfs_stage1_5
15 e2fs_stage1_5  iso9660_stage1_5  reiserfs_stage1_5  ufs2_stage1_5
16 fat_stage1_5   jfs_stage1_5      stage1             vstafs_stage1_5

7.配置grub:

1 [root@cl ~]# cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot/vmlinuz
2 [root@cl ~]# cp /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot/initramfs.img
3 [root@cl ~]# vim /mnt/boot/grub/grub.conf

default=0
timeout=5
title CentOS (Express)
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda3
initrd /initramfs.img

8.创建根文件目录:

 1 [root@cl ~]# mkdir /mnt/sysroot
 2 [root@cl ~]# mount /dev/sdb3 /mnt/sysroot/
 3 [root@cl ~]# cd /mnt/sysroot/
 4 [root@cl sysroot]# mkdir -p etc bin sbin lib lib64 dev proc sys tmp var usr home mnt media
 5 [root@cl sysroot]# ls
 6 bin  dev  etc  home  lib  lib64  lost+found  media  mnt  proc  sbin  sys  tmp  usr  var
 7 [root@cl sysroot]# cp /bin/bash /mnt/sysroot/bin/ (复制程序bash)
 8 [root@cl sysroot]# ldd /bin/bash (查看程序依赖库文件)
 9     linux-vdso.so.1 =>  (0x00007fffd39f9000)
10     libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003cc3800000)
11     libdl.so.2 => /lib64/libdl.so.2 (0x0000003cc1c00000)
12     libc.so.6 => /lib64/libc.so.6 (0x0000003cc2000000)
13     /lib64/ld-linux-x86-64.so.2 (0x0000003cc1800000)
14 [root@cl sysroot]# cp /lib64/libtinfo.so.5 /mnt/sysroot/lib64/
15 [root@cl sysroot]# cp /lib64/libdl.so.2 /mnt/sysroot/lib64/
16 [root@cl sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib64/
17 [root@cl sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/
18 [root@cl sysroot]# cd
19 [root@cl ~]# chroot /mnt/sysroot/ (切换根)
20 bash-4.1# 
21 bash-4.1# exit
22 exit

9.编辑启动init为/bin/bash:

[root@cl ~]# vim /mnt/boot/grub/grub.conf 
   
default=0
timeout=5
title CentOS (Express)
         root (hd0,0)
         kernel /vmlinuz ro root=/dev/sda3 init=/bin/bash
         initrd /initramfs.img
[root@cl ~]# sync

note:该操作把bash当做第一个用户空间运行进程启动;

note:sync命令的作用是,将有关文件系统的存储器常驻信息送入物理介质内。在暂停系统之前,比如要重新启动机器,一定要去执行sync命令。

 补充:模拟破坏和修复方法一

 1 [root@cl ~]# dd if=/dev/sda of=/root/mbr.bak count=1 bs=512
 2 1+0 records in
 3 1+0 records out
 4 512 bytes (512 B) copied, 0.000155983 s, 3.3 MB/
 5 [root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1
 6 1+0 records in
 7 1+0 records out
 8 200 bytes (200 B) copied, 0.000307597 s, 650 kB/s
 9 [root@cl ~]# sync
10 [root@cl ~]# grub-install  --root-directory=/ /dev/sda
11 Installation finished. No error reported.
12 This is the contents of the device map //boot/grub/device.map.
13 Check if this is correct or not. If any of the lines is incorrect,
14 fix it and re-run the script `grub-install'.
15 
16 # this device map was generated by anaconda
17 (hd0)     /dev/sda

    模拟破坏和修复方法二

 1 [root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1
 2 1+0 records in
 3 1+0 records out
 4 200 bytes (200 B) copied, 0.0170366 s, 11.7 kB/s
 5 [root@cl ~]# sync
 6 [root@cl ~]# grub
 7 Probing devices to guess BIOS drives. This may take a long time.
 8 
 9 
10     GNU GRUB  version 0.97  (640K lower / 3072K upper memory)
11 
12  [ Minimal BASH-like line editing is supported.  For the first word, TAB
13    lists possible command completions.  Anywhere else TAB lists the possible
14    completions of a device/filename.]
15 grub> root (hd0,0)
16 root (hd0,0)
17  Filesystem type is ext2fs, partition type 0x83
18 grub> setup (hd0)
19 setup (hd0)
20  Checking if "/boot/grub/stage1" exists... no
21  Checking if "/grub/stage1" exists... yes
22  Checking if "/grub/stage2" exists... yes
23  Checking if "/grub/e2fs_stage1_5" exists... yes
24  Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
25 succeeded
26  Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
27 Done.
28 grub> quit
29 quit
30 [root@cl ~]# sync

    模拟破坏和修复方法三:(重启后救援模式修复)

重启:

[root@cl ~]# dd if=/dev/zero of=/dev/sda bs=200 count=1
1+0 records in
1+0 records out
200 bytes (200 B) copied, 0.000283877 s, 705 kB/s
[root@cl ~]# sync
[root@cl ~]# reboot

Broadcast message from root@cl.y.dba
    (/dev/pts/0) at 15:21 ...

The system is going down for reboot NOW!

修复:1.选择镜像光盘:

 

  2.重启系统选择救援模式:

  3.进入shell:

 4.修复并重启:

 

 

 

 

posted @ 2017-03-30 14:46  BeRichMan  阅读(2252)  评论(0编辑  收藏  举报