mdadm-raid1管理

raid1原理:

RAID-1 :mirroring(镜像卷)需要磁盘两块以上
原理:是把一个磁盘的数据镜像到另一个磁盘上,也就是说数据在写入一块磁盘的同时,会在另一块闲置的磁盘上生成镜像文件,(同步)
特性:当一块硬盘失效时,系统会忽略该硬盘,转而使用剩余的镜像盘读写数据,具备很好的磁盘冗余能力。

mdadm介绍

mdadm是linux下用于创建和管理软件RAID的命令,是一个模式化命令
mdadm 命令的参数

管理软raid工具:mdadm

-C 建立一个新阵列
-A 激活磁盘阵列
-a 添加设备到阵列
-D 打印阵列设备详细信息
-s 扫描配置文件或者/proc/mdstat 得到阵列缺失信息
-v 显示详细信息
-r 移除设备
-f 将设备状态定为故障
-l 设定磁盘阵列的级别
-n 指定磁盘阵列的成员(分区/磁盘的数量)
-x 指定阵列中备用磁盘的数量

实战raid 1

新增两块硬盘

[root@192 ~]#  ll /dev/sd*
brw-rw----. 1 root disk 8,  0 2月  28 02:57 /dev/sda
brw-rw----. 1 root disk 8,  1 2月  28 02:57 /dev/sda1
brw-rw----. 1 root disk 8,  2 2月  28 02:57 /dev/sda2
brw-rw----. 1 root disk 8, 16 2月  28 03:02 /dev/sdb
brw-rw----. 1 root disk 8, 17 2月  28 03:02 /dev/sdb1
brw-rw----. 1 root disk 8, 32 2月  28 02:57 /dev/sdc

1)创建分区

[root@192 ~]# fdisk /dev/sdb

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): +3G

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@192 ~]# fdisk /dev/sdb

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
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (394-2610, default 394): 
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-2610, default 2610): +5G

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@192 ~]#  ll /dev/sd*
brw-rw----. 1 root disk 8,  0 2月  28 02:57 /dev/sda
brw-rw----. 1 root disk 8,  1 2月  28 02:57 /dev/sda1
brw-rw----. 1 root disk 8,  2 2月  28 02:57 /dev/sda2
brw-rw----. 1 root disk 8, 16 2月  28 03:09 /dev/sdb
brw-rw----. 1 root disk 8, 17 2月  28 03:09 /dev/sdb1
brw-rw----. 1 root disk 8, 18 2月  28 03:09 /dev/sdb2
brw-rw----. 1 root disk 8, 32 2月  28 02:57 /dev/sdc
[root@192 ~]# 

2)创建raid1

[root@192 ~]# mdadm -C -v /dev/md1  -l 1 -n 2 -x 1 /dev/sdb1 /dev/sdb[2,3]
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
mdadm: size set to 3154624K
mdadm: largest drive (/dev/sdb2) exceeds size (3154624K) by more than 1%
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@192 ~]# 

3) 将RAID1信息保存到配置文件中

[root@192 ~]# mdadm -Dsv > /etc/mdadm.conf
[root@192 ~]# 

4)检查我们的磁盘阵列

[root@192 ~]# mdadm -D /dev/md1 
/dev/md1:
        Version : 1.2
  Creation Time : Fri Feb 28 03:15:59 2020  //创建时间
     Raid Level : raid1
     Array Size : 3154624 (3.01 GiB 3.23 GB)
  Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Fri Feb 28 03:17:48 2020
          State : clean 
 Active Devices : 2    //当前活动设备
Working Devices : 3    //工作设备
 Failed Devices : 0    // 失效设备
  Spare Devices : 1    //热备盘的数量

           Name : 192.168.148.130:1  (local to host 192.168.148.130)
           UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
         Events : 17

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

       2       8       19        -      spare   /dev/sdb3
[root@192 ~]# 

5) 在raid设备上创建文件系统并挂载

[root@192 ~]# mkfs.ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
197200 inodes, 788656 blocks
39432 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=809500672
25 block groups
32768 blocks per group, 32768 fragments per group
7888 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

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

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@192 ~]# mkdir /raid1
[root@192 ~]# mou
mount        mount.fuse   mount.nfs4   mountstats   mousetweaks
mount.cifs   mount.nfs    mountpoint   mount.tmpfs  
[root@192 ~]# mount /dev/md1 /raid1/
[root@192 ~]# df -h | tail -l
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  3.6G   13G  22% /
tmpfs                         491M   80K  491M   1% /dev/shm
/dev/sda1                     485M   35M  426M   8% /boot
/dev/sr0                      4.2G  4.2G     0 100% /media/CentOS_6.5_Final
/dev/md1                      3.0G   69M  2.8G   3% /raid1
[root@192 ~]# 

6) 创建测试文件,看如果一块磁盘坏掉,数据是否丢失

[root@192 ~]# touch aaa.txt
[root@192 ~]# echo "磁盘坏了,我也在" >aaa.txt 
[root@192 ~]# 

7) 模拟损坏(sdb1盘坏掉了)

[root@192 ~]# mdadm /dev/md1 -f /dev/sdb1 
mdadm: set /dev/sdb1 faulty in /dev/md1
[root@192 ~]# 
[root@192 ~]# mdadm -D /dev/md1 
/dev/md1:
        Version : 1.2
  Creation Time : Fri Feb 28 03:15:59 2020
     Raid Level : raid1
     Array Size : 3154624 (3.01 GiB 3.23 GB)
  Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Fri Feb 28 03:29:29 2020
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 1
  Spare Devices : 0

           Name : 192.168.148.130:1  (local to host 192.168.148.130)
           UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
         Events : 36

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

       0       8       17        -      faulty   /dev/sdb1
[root@192 ~]# 
测试移除之后文件是否还在
[root@192 ~]# ls
aaa.txt          install.log         公共的  视频  文档  音乐
anaconda-ks.cfg  install.log.syslog  模板    图片  下载  桌面
[root@192 ~]# cat aaa.txt 
磁盘坏了,我也在
[root@192 ~]# 

8) 移除坏掉的设备,同时另外加一个备份盘

[root@192 ~]# mdadm -r /dev/md1 /dev/sdb1
mdadm: hot removed /dev/sdb1 from /dev/md1
[root@192 ~]# 

查看是否移除
[root@192 ~]# mdadm -D /dev/md1 
/dev/md1:
        Version : 1.2
  Creation Time : Fri Feb 28 03:15:59 2020
     Raid Level : raid1
     Array Size : 3154624 (3.01 GiB 3.23 GB)
  Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Fri Feb 28 03:37:08 2020
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

           Name : 192.168.148.130:1  (local to host 192.168.148.130)
           UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
         Events : 37

    Number   Major   Minor   RaidDevice State
       2       8       19        0      active sync   /dev/sdb3
       1       8       18        1      active sync   /dev/sdb2
[root@192 ~]# 

9) 增加一块热备盘

[root@192 ~]# mdadm -a /dev/md1 /dev/sdc1

总结:

raid1中一块硬盘坏了不影响raid正常运行
使用率是50%

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章