概述

RAID(Redundant Array of Independent Disk),即独立冗余磁盘阵列。

其思想是通过一些技术(软件或硬件)将多块独立的磁盘按照不同的方式组合为一个逻辑磁盘,从而提高存储容量,提升存储性能并提高数据的安全性与可靠性。

级别(level)

RAID 0(stripe,不含校验与冗余的条带存储):性能最佳

多块磁盘组合为RAID 0后,每块磁盘都将会被分割为小区快(chunk),而数据会被分割成块的大小,然后依序交错的存放到不同的磁盘中。

Linux笔记之RAID技术

特点:读写性能有提升,冗余能力无,空间利用率100%,至少需要2块磁盘。

RAID 1(mirror,不含校验的镜像存储):完整备份

多块磁盘组合为RAID 1后,数据将被同时复制到每块磁盘在。

Linux笔记之RAID技术

特点:读性能有提升,写性能下降,冗余能力有,空间利用率50%,至少需要2块磁盘。

RAID 5(数据块级别的分布式校验条带存储):性能与数据备份的均衡考虑

多块磁盘组合为RAID 5后,数据将以块为单位同步式分别存储在不同的磁盘上,在数据写入过程中,在每块磁盘还循环加入一个同位检验数据(parity),这个数据会记录其他磁盘的备份数据,用于有磁盘损坏时的救援,不过默认仅能支持一块磁盘的损毁情况。


Linux笔记之RAID技术

特点:读写性能有提升,冗余能力有,空间利用率(n-1)/n,至少需要3块磁盘。

RAID 0+1(条带+镜像存储)

Disk0与Disk1组成第一组RAID 0,Disk2与Disk3组成第二组RAID 0,然后这两组再整合成为一组RAID 1。


Linux笔记之RAID技术

特点:读写性能有提升,冗余能力有,空间利用率50%,至少需要4块盘。

RAID 1+0(镜像+条带存储)

Disk0与Disk1组成第一组RAID 1,Disk2与Disk3组成第二组RAID 1,然后这两组再整合成为一组RAID 0。

Linux笔记之RAID技术

特点:读写性能有提升,冗余能力有,空间利用率50%,至少需要4块盘。

RAID 5+0

硬盘1、硬盘2与硬盘3组成第一组RAID 5,硬盘4、硬盘5与硬盘6组成第二组RAID 5,然后这两组再整合成为一组RAID 0。

Linux笔记之RAID技术

特点:读写性能有提升,冗余能力有,空间利用率(n-2)/n,至少需要6块盘。

JBOD(Just a Bunch Of Disks,磁盘簇)

JBOD是“线性模式”,不是一个真正的RAID级别,而每种RAID控制器似乎都实现了它。其把多块磁盘上的地址前后衔接起来,构成一个更大的虚拟磁盘。现如今,最好通过一个逻辑卷而不是一个RAID控制器来实现JBOD功能。

Linux笔记之RAID技术

特点:读写性能无提升,冗余能力无,空间利用率100%,至少需要2块盘。

Spare Disk:预留磁盘的功能

Spare Disk就是一块或多块没有包含在原本磁盘阵列等级中的磁盘,这块磁盘平时并不会被磁盘阵列所使用,当磁盘阵列有任何磁盘损坏时,则这块Spare Disk会被主动拉进磁盘阵列中,并将坏掉的那块磁盘移出磁盘阵列,然后立即重建(rebuild)数据系统。

分类

硬件磁盘阵列(hardware RAID):RAID卡

软件磁盘阵列(software RAID):mdadm工具(以分区或磁盘为单位)

创建与管理软件RAID实例

环境:CentOS6.4

任务:创建RAID 1

1、查看磁盘信息

[[email protected] ~]# fdisk -lu

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007a953

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2          411648    20891647    10240000   83  Linux
/dev/sda3        20891648    31131647     5120000   83  Linux
/dev/sda4        31131648    41943039     5405696    5  Extended
/dev/sda5        31131711    33238484     1053387   82  Linux swap / Solaris

2、创建磁盘分区

[[email protected] ~]# fdisk /dev/sda

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
First cylinder (2070-2611, default 2070): 
Using default value 2070
Last cylinder, +cylinders or +size{K,M,G} (2070-2611, default 2611): +500M
Command (m for help): t
Partition number (1-10): 6
Hex code (type L to list codes): fd
Changed system type of partition 6 to fd (Linux raid autodetect)

#上述操作共执行5次

Command (m for help): p

Disk /dev/sda: 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: 0x0007a953

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1938     5120000   83  Linux
/dev/sda4            1938        2611     5405696    5  Extended
/dev/sda5            1938        2069     1053387   82  Linux swap / Solaris
/dev/sda6            2070        2134      522081   fd  Linux raid autodetect
/dev/sda7            2135        2199      522081   fd  Linux raid autodetect
/dev/sda8            2200        2264      522081   fd  Linux raid autodetect
/dev/sda9            2265        2329      522081   fd  Linux raid autodetect
/dev/sda10           2330        2394      522081   fd  Linux raid autodetect

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[[email protected] ~]# partprobe

3、创建RAID 1

mdadm - manage MD devices aka Linux Software RAID

Currently,  Linux  supports LINEAR md devices, RAID0 (striping), RAID1 (mirroring), RAID4, RAID5, RAID6, RAID10,MULTIPATH, FAULTY, and CONTAINER.


[[email protected] ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/sda{6,7,8}
mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

注:mdadm选项

-C    创建软件RAID

-a    自动为其创建设备文件

-l    指定RAID级别

-n    指定磁盘个数

-x    指定备用设备个数

[[email protected] ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Thu Dec  3 21:52:10 2015
     Raid Level : raid1
     Array Size : 521792 (509.65 MiB 534.32 MB)
  Used Dev Size : 521792 (509.65 MiB 534.32 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Thu Dec  3 21:52:17 2015
          State : clean 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : bogon:0  (local to host bogon)
           UUID : f21bae7a:c755e086:5d74cfbd:d1112aac
         Events : 17

    Number   Major   Minor   RaidDevice State
       0       8        6        0      active sync   /dev/sda6
       1       8        7        1      active sync   /dev/sda7

       2       8        8        -      spare   /dev/sda8


[[email protected] ~]# cat /proc/mdstat
Personalities : [raid1] 
md0 : active raid1 sda8[2](S) sda7[1] sda6[0]
      521792 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>


4、格式化与挂载

[[email protected] ~]# mkfs -t ext4 /dev/md0
[[email protected] ~]# mkdir /mnt/raid0
[[email protected] ~]# mount /dev/md0 /mnt/raid0

[[email protected] ~]# ls /mnt/raid0/
lost+found
[[email protected] ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             10079084   1800048   7767036  19% /
tmpfs                   510268         0    510268   0% /dev/shm
/dev/sda1               198337     28563    159534  16% /boot
/dev/sda3              5039616    141080   4642536   3% /home
/dev/md0                505316     10510    468717   3% /mnt/raid0

5、故障模拟

[[email protected] ~]# mdadm /dev/md0 -f /dev/sda6
mdadm: set /dev/sda6 faulty in /dev/md0

[[email protected] ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Thu Dec  3 21:52:10 2015
     Raid Level : raid1
     Array Size : 521792 (509.65 MiB 534.32 MB)
  Used Dev Size : 521792 (509.65 MiB 534.32 MB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Thu Dec  3 22:16:09 2015
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 1
  Spare Devices : 0

           Name : bogon:0  (local to host bogon)
           UUID : f21bae7a:c755e086:5d74cfbd:d1112aac
         Events : 36

    Number   Major   Minor   RaidDevice State
       2       8        8        0      active sync   /dev/sda8
       1       8        7        1      active sync   /dev/sda7

       0       8        6        -      faulty spare   /dev/sda6

6、开机启动并自动挂载

修改配置文件/etc/mdadm.conf

设置开机自动挂载/etc/fstab

[[email protected] ~]# mdadm -D --scan > /etc/mdadm.conf

[[email protected] ~]# cat /etc/mdadm.conf 
ARRAY /dev/md0 metadata=1.2 name=bogon:0 UUID=f21bae7a:c755e086:5d74cfbd:d1112aac
[[email protected] ~]# vim /etc/fstab 
/dev/md0    /mnt/raid0    ext4    default    0    0

7、关闭软件RAID

[[email protected] ~]# umount /dev/md0
[[email protected] ~]# vim /etc/fstab 
#/dev/md0    /mnt/raid0    ext4    default    0    0

[[email protected] ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
[[email protected] ~]# cat /proc/mdstat 
Personalities : [raid1] 
unused devices: <none>

[[email protected] ~]# vim /etc/mdadm.conf 
#ARRAY /dev/md0 metadata=1.2 name=bogon:0 UUID=f21bae7a:c755e086:5d74cfbd:d1112aac

结束