linux不重启挂载磁盘安装grub

挂载、分区、grub

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

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

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

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

 [root@cl Test]# ls /sys/class/scsi_host/
host0 host1 host2
[root@cl Test]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@cl Test]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@cl Test]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@cl Test]# fdisk -l

3.创建分区:

 [root@cl Test]# 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 0xa61749e4.
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 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 (-)
p
Partition number (-):
First cylinder (-, default ):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default ): +200M Command (m for help): n
Command action
e extended
p primary partition (-)
p
Partition number (-):
First cylinder (-, default ):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default ): +2G Command (m for help): t
Partition number (-):
Hex code (type L to list codes):
Changed system type of partition to (Linux swap / Solaris) Command (m for help): n
Command action
e extended
p primary partition (-)
p
Partition number (-):
First cylinder (-, default ):
Using default value
Last cylinder, +cylinders or +size{K,M,G} (-, default ): +5G Command (m for help): w
The partition table has been altered! Calling ioctl() to re-read partition table.
Syncing disks.
[root@cl Test]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition
BLKPG: Device or resource busy
error adding partition
BLKPG: Device or resource busy
error adding partition
[root@cl Test]# cat /proc/partitions
major minor #blocks name sda
sda1
sda2
sda3
sda4
sda5
sda6
sda7
sdb
sdb1
sdb2
sdb3

4.创建文件系统:

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

5.挂载分区:

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

6.安装grub:

 [root@cl ~]# grub-install --root-directory=/mnt /dev/sdb1 (指明/的位置在mnt下)
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
[root@cl ~]# ls /mnt/boot/
grub lost+found
[root@cl ~]# ls /mnt/boot/grub/
device.map ffs_stage1_5 minix_stage1_5 stage2 xfs_stage1_5
e2fs_stage1_5 iso9660_stage1_5 reiserfs_stage1_5 ufs2_stage1_5
fat_stage1_5 jfs_stage1_5 stage1 vstafs_stage1_5

7.配置grub:

 [root@cl ~]# cp /boot/vmlinuz-2.6.-.el6.x86_64 /mnt/boot/vmlinuz
[root@cl ~]# cp /boot/initramfs-2.6.-.el6.x86_64.img /mnt/boot/initramfs.img
[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.创建根文件目录:

 [root@cl ~]# mkdir /mnt/sysroot
[root@cl ~]# mount /dev/sdb3 /mnt/sysroot/
[root@cl ~]# cd /mnt/sysroot/
[root@cl sysroot]# mkdir -p etc bin sbin lib lib64 dev proc sys tmp var usr home mnt media
[root@cl sysroot]# ls
bin dev etc home lib lib64 lost+found media mnt proc sbin sys tmp usr var
[root@cl sysroot]# cp /bin/bash /mnt/sysroot/bin/ (复制程序bash)
[root@cl sysroot]# ldd /bin/bash (查看程序依赖库文件)
linux-vdso.so. => (0x00007fffd39f9000)
libtinfo.so. => /lib64/libtinfo.so. (0x0000003cc3800000)
libdl.so. => /lib64/libdl.so. (0x0000003cc1c00000)
libc.so. => /lib64/libc.so. (0x0000003cc2000000)
/lib64/ld-linux-x86-.so. (0x0000003cc1800000)
[root@cl sysroot]# cp /lib64/libtinfo.so. /mnt/sysroot/lib64/
[root@cl sysroot]# cp /lib64/libdl.so. /mnt/sysroot/lib64/
[root@cl sysroot]# cp /lib64/libc.so. /mnt/sysroot/lib64/
[root@cl sysroot]# cp /lib64/ld-linux-x86-.so. /mnt/sysroot/lib64/
[root@cl sysroot]# cd
[root@cl ~]# chroot /mnt/sysroot/ (切换根)
bash-4.1#
bash-4.1# exit
exit

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

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

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

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

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

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

 [root@cl ~]# dd if=/dev/sda of=/root/mbr.bak count= bs=
+ records in
+ records out
bytes ( B) copied, 0.000155983 s, 3.3 MB/
[root@cl ~]# dd if=/dev/zero of=/dev/sda bs= count=
+ records in
+ records out
bytes ( B) copied, 0.000307597 s, kB/s
[root@cl ~]# sync
[root@cl ~]# grub-install --root-directory=/ /dev/sda
Installation finished. No error reported.
This is the contents of the device map //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'. # this device map was generated by anaconda
(hd0) /dev/sda

    模拟破坏和修复方法二

 [root@cl ~]# dd if=/dev/zero of=/dev/sda bs= count=
+ records in
+ records out
bytes ( B) copied, 0.0170366 s, 11.7 kB/s
[root@cl ~]# sync
[root@cl ~]# grub
Probing devices to guess BIOS drives. This may take a long time. GNU GRUB version 0.97 (640K lower / 3072K upper memory) [ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename.]
grub> root (hd0,)
root (hd0,)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
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)"... sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
[root@cl ~]# sync

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

重启:

[root@cl ~]# dd if=/dev/zero of=/dev/sda bs= count=
+ records in
+ records out
bytes ( B) copied, 0.000283877 s, kB/s
[root@cl ~]# sync
[root@cl ~]# reboot Broadcast message from root@cl.y.dba
(/dev/pts/) at : ... The system is going down for reboot NOW!

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

linux不重启挂载磁盘安装grub

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

linux不重启挂载磁盘安装grub

  3.进入shell:

linux不重启挂载磁盘安装grub

4.修复并重启:

linux不重启挂载磁盘安装grub

上一篇:CentOS 7 环境下挂载新磁盘


下一篇:.vue文件里引用单独样式和js文件