创建软RAID

通过mdadm工具创建软RAID5

在虚拟机上新加一块磁盘,
1.创建分区(sdb1.2.3.4.5,sdb4为扩展分区)

[root@wangteng ~]# 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 0x20dc796f.
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): p

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: 0x20dc796f

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
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): +100M

Command (m for help): p

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: 0x20dc796f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (15-2610, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-2610, default 2610): +100M

Command (m for help): P

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: 0x20dc796f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (29-2610, default 29):
Using default value 29
Last cylinder, +cylinders or +size{K,M,G} (29-2610, default 2610): +100M

Command (m for help): N^Hn
Command action
   e   extended
   p   primary partition (1-4)
e
Selected partition 4
First cylinder (43-2610, default 43):
Using default value 43
Last cylinder, +cylinders or +size{K,M,G} (43-2610, default 2610):
Using default value 2610

Command (m for help): n
First cylinder (43-2610, default 43):
Using default value 43
Last cylinder, +cylinders or +size{K,M,G} (43-2610, default 2610): +100M

Command (m for help): p

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: 0x20dc796f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15          28      112455   83  Linux
/dev/sdb3              29          42      112455   83  Linux
/dev/sdb4              43        2610    20627460    5  Extended
/dev/sdb5              43          56      112423+  83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

更新一下新创建的分区

[root@wangteng ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@wangteng ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5

2、创建RAID5
设定/dev/sdb5作为备用磁盘,其余作为活动磁盘

[root@wangteng ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sdb{1,2,3,5}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@wangteng ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr  1 06:17:16 2020
     Raid Level : raid5
     Array Size : 222208 (217.00 MiB 227.54 MB)
  Used Dev Size : 111104 (108.50 MiB 113.77 MB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Wed Apr  1 06:17:19 2020
          State : clean
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1

         Layout : left-symmetric
     Chunk Size : 512K

           Name : wangteng:0  (local to host wangteng)
           UUID : 76629f88:1ccdfbbd:e8d7f2dd:5be8996f
         Events : 18

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
       4       8       19        2      active sync   /dev/sdb3

       3       8       21        -      spare   /dev/sdb5

3、创建RAID的配置文件
RAID的配置文件名为"mdadm.conf",默认是不存在的,所以需要手工创建。生成的“mdadm.conf”文件的内容并不符合所规定的格式,即他不生效,手动修改格式如下。

[root@wangteng ~]# mdadm --detail --scan > /etc/mdadm.conf
[root@wangteng ~]# vi /etc/mdadm.conf
DEVICE /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb5
ARRAY /dev/md0 level=raid5 num-devices=3 UUID=76629f88:1ccdfbbd:e8d7f2dd:5be8996f

~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/mdadm.conf" 3L, 130C
若没有创建RAID的配置文件,那麽在系统每次启动后,需要手工手动加载RAID才能使用,命令如下(因为我已启动,所以显示的是在工作中)
[root@wangteng ~]# mdadm --assemble /dev/md0 /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb5
mdadm: /dev/sdb1 is busy - skipping
mdadm: /dev/sdb2 is busy - skipping
mdadm: /dev/sdb3 is busy - skipping
mdadm: /dev/sdb5 is busy - skipping

4、创建文件系统
规定一个/dev/md0目录下创建文件系统,创完文件系统后,挂载就可使用了

[root@wangteng ~]# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=512 blocks, Stripe width=1024 blocks
55552 inodes, 222208 blocks
11110 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67371008
28 block groups
8192 blocks per group, 8192 fragments per group
1984 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185

正在写入inode表: 完成
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@wangteng ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb3[4] sdb5[3](S) sdb2[1] sdb1[0]
      222208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>

5、模拟故障磁盘
若软RAID检测到某个磁盘有故障时,会自动标记该磁盘为故障磁盘,并停止对故障磁盘的读写操作,标记命令如下(这里指定sdb3为故障磁盘)当一个设备出现故障或标记故障,相应的设备方括号后将被标记为(F)

[root@wangteng ~]# mdadm  /dev/md0 --fail /dev/sdb3
mdadm: set /dev/sdb3 faulty in /dev/md0
[root@wangteng ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb3[4](F) sdb5[3] sdb2[1] sdb1[0]
      222208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@wangteng ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb3[4](F) sdb5[3] sdb2[1] sdb1[0]
      222208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>

6、移除故障磁盘

[root@wangteng ~]# mdadm /dev/md0 --remove /dev/sdb3
mdadm: hot removed /dev/sdb3 from /dev/md0

7、添加新磁盘

[root@wangteng ~]# mdadm  /dev/md0 --add /dev/sdb3
mdadm: added /dev/sdb3
[root@wangteng ~]# mdadm  --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Apr  1 06:17:16 2020
     Raid Level : raid5
     Array Size : 222208 (217.00 MiB 227.54 MB)
  Used Dev Size : 111104 (108.50 MiB 113.77 MB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Wed Apr  1 06:54:18 2020
          State : clean
 Active Devices : 3
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 1

         Layout : left-symmetric
     Chunk Size : 512K

           Name : wangteng:0  (local to host wangteng)
           UUID : 76629f88:1ccdfbbd:e8d7f2dd:5be8996f
         Events : 39

    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
       3       8       21        2      active sync   /dev/sdb5

       4       8       19        -      spare   /dev/sdb3

猜你喜欢

转载自blog.csdn.net/weixin_45961525/article/details/105234219