磁盘阵列(RAID
 
软件磁盘阵列 (Software RAID)
磁盘阵列全名是『 Redundant Arrays of Inexpensive Disks, RAID 』,英翻中的意思是:容错式廉价磁盘阵列。
整个 RAID 由选择的等级 (level)同,而使得整合后的磁盘具有同的功能, 基本常见的 level 有这几种(注1):
RAID ---0
利用率100%,不能最备份。
RAID-1 (映像模式, mirror):完整备
整体 RAID 的容量 50%,任何一颗硬盘损毁时,都可以上完整的保存资料。
RAID 0+1,RAID 1+0
 
RAID----5
磁盘容量是原来的75%,若采用5个硬盘时,4个是Active 1个作热备份(Spare Disk)
创建RAID的过程——RAID5
 
@ 创建磁盘分区
 
[root@wangfc~]# fdisk -clu /dev/sda
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 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: 0x0008a69b
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         652     5237158+   5  Extended
/dev/sdb5               1         200     1606437   83  Linux
/dev/sdb6             201         226      208813+  83  Linux
/dev/sdb7             227         252      208813+  83  Linux
/dev/sdb8             253         278      208813+  83  Linux
/dev/sdb9             279         304      208813+  83  Linux
/dev/sdb10            305         330      208813+  83  Linux
 
@ 更改磁盘的类型,(也可以不改)
fd  Linux raid auto
Command (m for help): t
Partition number (1-11): 7
Hex code (type L to list codes): fd
Changed system type of partition 7 to fd (Linux raid autodetect)
 
Command (m for help): t
Partition number (1-11): 8
Hex code (type L to list codes): fd
Changed system type of partition 8 to fd (Linux raid autodetect)
。。。。
/dev/sdb7             227         252      208813+  fd  Linux raid autodetect
/dev/sdb8             253         278      208813+  fd  Linux raid autodetect
/dev/sdb9             279         304      208813+  fd  Linux raid autodetect
/dev/sdb10            305         330      208813+  fd  Linux raid autodetect
/dev/sdb11            331         356      208813+  fd  Linux raid autodetect
 
[root@wangfc ~]# ll /dev/sdb*
brw-rw----. 1 root disk 8, 16 Aug  1 02:49 /dev/sdb
brw-rw----. 1 root disk 8, 17 Jul 31 19:37 /dev/sdb1
brw-rw----. 1 root disk 8, 26 Aug  1 02:47 /dev/sdb10
brw-rw----. 1 root disk 8, 27 Aug  1 02:49 /dev/sdb11
brw-rw----. 1 root disk 8, 21 Jul 31 19:51 /dev/sdb5
brw-rw----. 1 root disk 8, 22 Jul 31 19:51 /dev/sdb6
brw-rw----. 1 root disk 8, 23 Aug  1 02:44 /dev/sdb7
brw-rw----. 1 root disk 8, 24 Aug  1 02:47 /dev/sdb8
brw-rw----. 1 root disk 8, 25 Aug  1 02:47 /dev/sdb9
 
 
@mdadm创建RAID
 
[root@wangfc ~]# mdadm --create --auto=yes /dev/md0 --level=5 --raid-devices=4 --spare-devices=1 /dev/sdb{7,8,9,10,11}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
 
@ 查看
[root@wangfc ~]# mdadm  --detail /dev/md0
/dev/md0:                                                                                                                                   <==RAID 设备文件名
        Version : 1.2
  Creation Time : Wed Aug  1 03:30:54 2012
     Raid Level : raid5                                                                                                    <==RAID 等级为 RAID  5
     Array Size : 625152 (610.60 MiB 640.16 MB)                                                <==RAID的可用次磁盘容量
  Used Dev Size : 208384 (203.53 MiB 213.39 MB)                                                <==每个设备的可用容量
   Raid Devices : 4                                                                                                              <==用作RAID的设备数量
  Total Devices : 5                                                                                                                 <==全部设备数量
    Persistence : Superblock is persistent
 
    Update Time : Wed Aug  1 03:31:09 2012
          State : clean
 Active Devices : 4                                                                                                       <==启动的(active)设备数量
Working Devices : 5                                                                                                          <==可动作的设备数量
 Failed Devices : 0                                                                                                          <==预备错误的设备数量
  Spare Devices : 1                                                                                                            <==预备磁盘的设备数量
 
         Layout : left-symmetric
     Chunk Size : 512K
 
           Name : wangfc.example.com:0  (local to host wangfc.example.com)
           UUID : 6f8a672e:3af7ed12:de9d579a:06b582dd
         Events : 18
 
    Number   Major   Minor   RaidDevice State
       0       8       23        0      active sync   /dev/sdb7
       1       8       24        1      active sync   /dev/sdb8
       2       8       25        2      active sync   /dev/sdb9
       5       8       26        3      active sync   /dev/sdb10
 
       4       8       27        -      spare   /dev/sdb11
 
@ 查看系统磁盘阵列的情况
[root@wangfc ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb10[5] sdb11[4](S) sdb9[2] sdb8[1] sdb7[0]
      625152 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU]
 
第一行 :指出md0raid5,且使用了hdb7,8,9,10四块磁盘设备。每个设备后面的中括号的数字为此磁盘在RAID中的
顺序;至于sdb11后面的s达标十点半1spare之意。
第二行 :此磁盘阵列拥有625152block(每个block单位为1KB),总容量约为625MB,使用RAID5等级,写入磁盘的小区快(chunk)大小为512K,使用algorithm磁盘阵列算法。[m/n]代表此数组需要m个设备,且n个设备正常运行。因此本md0需要4个设备且zhe4个设备均正常工作。后面的[UUUU]代表的是四个需要的设备(就是[m/n]里面的m)的启动情况,U代表正常运行,若为_代表不正常
 
 
@格式化与挂载使用
 
[root@wangfc ~]# mkfs.ext4 /dev/md0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=384 blocks
39120 inodes, 156288 blocks
7814 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=163577856
5 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks:
        32768, 98304
 
Writing inode tables: done                           
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
 
[root@wangfc ~]# mkdir /mnt/raid
[root@wangfc ~]# mount /dev/md0 /mnt/raid/
[root@wangfc ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda2             47238516   4295328  40543600  10% /
tmpfs                  1025848        88   1025760   1% /dev/shm
/dev/sda1               297485     30367    251758  11% /boot
/dev/mapper/vg0-lv0     198337      5675    182422   4% /home
/dev/md0                615308     16864    567188   3% /mnt/raid
 
@仿真RAID错误的救援模式
 
[root@wangfc ~]# cp -a /etc/ /var/log  /mnt/raid/
[root@wangfc ~]# df /mnt/raid/ ; du -sm /mnt/raid/*
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/md0                615308     52700    531352  10% /mnt/raid
32      /mnt/raid/etc
4       /mnt/raid/log
1       /mnt/raid/lost+found
 
设置磁盘为错误
 
[root@wangfc ~]# mdadm --manage /dev/md0 --fail /dev/sdb7
mdadm: set /dev/sdb7 faulty in /dev/md0
[root@wangfc ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Wed Aug  1 03:30:54 2012
     Raid Level : raid5
     Array Size : 625152 (610.60 MiB 640.16 MB)
  Used Dev Size : 208384 (203.53 MiB 213.39 MB)
   Raid Devices : 4
  Total Devices : 5
    Persistence : Superblock is persistent
 
    Update Time : Wed Aug  1 04:03:21 2012
          State : clean, degraded, recovering
 Active Devices : 3
Working Devices : 4
 Failed Devices : 1
  Spare Devices : 1
 
         Layout : left-symmetric
     Chunk Size : 512K
 
 Rebuild Status : 98% complete
 
           Name : wangfc.example.com:0  (local to host wangfc.example.com)
           UUID : 6f8a672e:3af7ed12:de9d579a:06b582dd
         Events : 35
 
    Number   Major   Minor   RaidDevice State
       4       8       27        0      spare rebuilding   /dev/sdb11
       1       8       24        1      active sync   /dev/sdb8
       2       8       25        2      active sync   /dev/sdb9
       5       8       26        3      active sync   /dev/sdb10
 
       0       8       23        -      faulty spare   /dev/sdb7
一会儿通过spare disk重建完毕的RAID5情况
[root@wangfc ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb10[5] sdb11[4] sdb9[2] sdb8[1] sdb7[0](F)
      625152 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU]
     
unused devices: <none>
 
又恢复正常了
 
@将出错的磁盘删除并加入新磁盘
 
新建分区
[root@wangfc ~]# fdisk /dev/sdb
 
 
[root@wangfc ~]# mdadm --manage /dev/md0 --add /dev/sdb13 --remove /dev/sdb7
 
@开机自动启动RAID并自动挂载
 
[root@wangfc ~]# mdadm --detail  /dev/md0   | grep  -i  uuid
 
[root@wangfc ~]#vim /etc/mdadm.conf
ARRAY    /dev/md0  UUID=
 
@测试
[root@wangfc ~]#umount  /dev/md0; mount -a
 
[root@wangfc ~]#df    /mnt/raid
 
 
@关闭软件RAID
 
一、
[root@wangfc ~]#umount  /dev/md0
 
[root@wangfc ~]#vim /etc/fstab
 
二、
 
[root@wangfc ~]#mdadm  --stop /dev/md0
 
[root@wangfc ~]#cat  /proc/mdstat
 
[root@wangfc ~]#vim /etc/mdadm.conf