首页 > 代码库 > linux不重启挂载磁盘安装grub

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 wont 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. Its 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命令。

 

 

 

 

 

 

 

 

linux不重启挂载磁盘安装grub