RAID磁盘分区的创建

RAID(Reduntant Arrays of Independent Disks),即独立冗余磁盘阵列。

RAID的级别:

       Raid 0: 条带

       Raid 1: 镜像

       Raid 2,3,4,5 等校验码类型

       Raid 01, Raid 10, Raid 50 等等。

 

以下是模拟创建RAID1 (2G)的过程:

1、创建raid auto分区(fd)

[root@ant-colonies ~]# fdisk /dev/sda
...

Command (m for help): t
Partition number (1-9): 8
Hex code (type L to list codes): fd
Changed system type of partition 8 to fd (Linux raid autodetect)
...

/dev/sda8            3424        3685     2104483+  fd  Linux raid autodetect
/dev/sda9            3686        3947     2104483+  fd  Linux raid autodetect
/dev/sda10           3948        4209     2104483+  fd  Linux raid autodetect

[root@ant-colonies ~]# partprobe                                #通知内核重新读取分区信息

[root@ant-colonies ~]# cat /proc/partitions
major minor      #blocks       name
   8        0    125829120        sda
...
   8        8       2104483       sda8
   8        9       2104483       sda9
   8       10      2104483       sda10

 

 2、将磁盘分区做成RAID盘

[root@ant-colonies ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 /dev/sda{8,9}
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.

[root@ant-colonies ~]# fdisk -l

...

Disk /dev/md0: 2152 MB, 2152857600 bytes
2 heads, 4 sectors/track, 525600 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@ant-colonies ~]# cat /proc/mdstat                     # 查看RAID信息配置文件
Personalities : [raid1]
md0 : active raid1 sda9[1] sda8[0]
      2102400 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>

[root@ant-colonies ~]# mdadm -D /dev/md0             # 查看RAID的详细信息
/dev/md0:
        Version : 1.2
  Creation Time : Fri Jan 20 05:29:57 2017
     Raid Level : raid1
     Array Size : 2102400 (2.01 GiB 2.15 GB)
  Used Dev Size : 2102400 (2.01 GiB 2.15 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Fri Jan 20 05:30:08 2017
          State : clean
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

           Name : ant-colonies:0  (local to host ant-colonies)
           UUID : xxxxxxxxxxxxxxxx
         Events : 17

    Number   Major   Minor   RaidDevice State
       0       8        8        0      active sync   /dev/sda8
       1       8        9        1      active sync   /dev/sda9

 

3、格式化RAID

[root@ant-colonies ~]# mke2fs -j -E stride=16  /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=16 blocks, Stripe width=0 blocks
131648 inodes, 525600 blocks
26280 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or 180 days, whichever comes first.  Use tune2fs -c or -i to override.

 

4、模拟磁盘损坏

[root@ant-colonies ~]# mdadm /dev/md0 -f /dev/sda8
mdadm: set /dev/sda8 faulty in /dev/md0
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda9[1] sda8[0](F)
      2102400 blocks super 1.2 [2/1] [_U]
      
unused devices: <none>
[root@ant-colonies ~]# mdadm --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Fri Jan 20 05:29:57 2017
     Raid Level : raid1
     Array Size : 2102400 (2.01 GiB 2.15 GB)
  Used Dev Size : 2102400 (2.01 GiB 2.15 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Fri Jan 20 05:55:52 2017
          State : clean, degraded
 Active Devices : 1
Working Devices : 1
 Failed Devices : 1
  Spare Devices : 0

           Name : ant-colonies:0  (local to host ant-colonies)
           UUID : xxxxxxxxxxxxxxxx
         Events : 19

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8        9        1      active sync   /dev/sda9

       0       8        8        -      faulty   /dev/sda8

5、移除损坏盘

[root@ant-colonies ~]# mdadm /dev/md0 -r /dev/sda8
mdadm: hot removed /dev/sda8 from /dev/md0
[root@ant-colonies ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Fri Jan 20 05:29:57 2017
     Raid Level : raid1
     Array Size : 2102400 (2.01 GiB 2.15 GB)
  Used Dev Size : 2102400 (2.01 GiB 2.15 GB)
   Raid Devices : 2
  Total Devices : 1
    Persistence : Superblock is persistent

    Update Time : Fri Jan 20 06:02:20 2017
          State : clean, degraded
 Active Devices : 1
Working Devices : 1
 Failed Devices : 0
  Spare Devices : 0

           Name : ant-colonies:0  (local to host ant-colonies)
           UUID : 7e24b602:766c7d33:85e03d1a:868c3d08
         Events : 20

    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8        9        1      active sync   /dev/sda9
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda9[1]
      2102400 blocks super 1.2 [2/1] [_U]
      
unused devices: <none>

6、添加新盘到阵列
[root@ant-colonies ~]# mdadm /dev/md0 -a /dev/sda10         # 不需要重新格式化
mdadm: added /dev/sda10
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda10[2] sda9[1]
      2102400 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>

7、增加热备盘

[root@ant-colonies ~]# mdadm /dev/md0 -a /dev/sda8
mdadm: added /dev/sda8
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda8[3](S) sda10[2] sda9[1]
      2102400 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>
[root@ant-colonies ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Fri Jan 20 05:29:57 2017
     Raid Level : raid1
     Array Size : 2102400 (2.01 GiB 2.15 GB)
  Used Dev Size : 2102400 (2.01 GiB 2.15 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Fri Jan 20 06:11:46 2017
          State : clean
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : ant-colonies:0  (local to host ant-colonies)
           UUID : xxxxxxxxxxxxxxxx
         Events : 40

    Number   Major   Minor   RaidDevice State
       2       8       10        0      active sync   /dev/sda10
       1       8        9        1      active sync   /dev/sda9

       3       8        8        -      spare   /dev/sda8

8、停止RAID阵列

[root@ant-colonies ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
unused devices: <none>
[root@ant-colonies ~]# mdadm -D /dev/md0
mdadm: cannot open /dev/md0: No such file or directory

9、重新启动RAID阵列

[root@ant-colonies ~]# mdadm -A /dev/md0 /dev/sda{9,10,8}
mdadm: /dev/md0 has been started with 2 drives and 1 spare.

[root@ant-colonies ~]# mdadm -D --scan                       # 查看磁盘阵列的磁盘数据信息
ARRAY /dev/md0 metadata=1.2 name=ant-colonies:0 UID=7e24b602:766c7d33:85e03d1a:868c3d08
[root@ant-colonies ~]# mdadm -D --scan > /etc/mdadm.conf   # 将磁盘阵列信息添加到系统配置文件,当阵列重新装配时系统会自动挂载对应的磁盘
[root@ant-colonies ~]# mdadm --stop /dev/md0
mdadm: stopped /dev/md0
[root@ant-colonies ~]# mdadm -A /dev/md0
mdadm: /dev/md0 has been started with 2 drives.

 

10、模拟RAID阵列一块盘损坏,热备盘自动启用作为镜像

[root@ant-colonies ~]# mdadm /dev/md0 --fail /dev/sda10
mdadm: set /dev/sda10 faulty in /dev/md0
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda10[2](F) sda8[3] sda9[1]
      2102400 blocks super 1.2 [2/1] [_U]
      [===============>.....]  recovery = 76.0% (1600000/2102400) finish=0.0min speed=228571K/sec                   # 显示系统在恢复数据至sda8
      
unused devices: <none>
[root@ant-colonies ~]# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda10[2](F) sda8[3] sda9[1]
      2102400 blocks super 1.2 [2/2] [UU]
      
unused devices: <none>
[root@ant-colonies ~]# watch -n 2 `cat /proc/mdstat`   # watch每2s执行一次cat,动态查看数据恢复情况

附:

mdadm: 将任何块设备做成RAID
模式化的命令:
    创建模式
        -C
            专用选项:
                -l: 级别
                -n #: 设备个数
                -a {yes|no}: 是否自动为其创建设备文件
                -c: CHUNK大小, 2^n,默认为64K
                -x #: 指定空闲盘个数
    管理模式
        --add, --remove, --fail
        mdadm /dev/md# --fail /dev/sda7
    监控模式
        -F
    增长模式
        -G
    装配模式
        -A

查看RAID阵列的详细信息
mdadm -D /dev/md#
    --detail
    
停止阵列:
    mdadm -S /dev/md#
        --stop

 

watch: 周期性地执行指定命令,并以全屏方式显示结果
    -n #:指定周期长度,单位为秒,默认为2
格式: watch -n # 'COMMAND'
    
将当前RAID信息保存至配置文件,以便以后进行装配:
mdamd -D --scan > /etc/mdadm.conf

mke2fs -j  -E stride=16   /dev/md2      // stride=chuncks/blocksizes    (指定stride即条带大小)

 

posted @ 2017-01-20 03:21  ant_colonies  阅读(1130)  评论(0编辑  收藏  举报