Linux RAID mdadm 建立和删除

ENV:

[root@centos7 ~]# uname -r 
3.10.0-514.el7.x86_64
[root@centos7 ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@centos7 ~]# mdadm -V
mdadm - v3.4 - 28th January 2016
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0    5G  0 disk 
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sdf           8:80   0    5G  0 disk 
sdg           8:96   0    5G  0 disk 
sdh           8:112  0    8G  0 disk 
├─sdh1        8:113  0    1G  0 part /boot
└─sdh2        8:114  0    7G  0 part 
  ├─cl-root 253:0    0  6.2G  0 lvm  /
  └─cl-swap 253:1    0  820M  0 lvm  [SWAP]
sr0          11:0    1 1024M  0 rom  

一、RAID0

1.建立并挂载:

[root@centos7 ~]# mdadm -Cv /dev/md0 -a yes -l 0 -n 2 /dev/sda /dev/sdb
                //建立RAID0
mdadm: chunk size defaults to 512K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@centos7 ~]# mdadm -D /dev/md0  //查看建立的RAID0
/dev/md0:
        Version : 1.2
  Creation Time : Thu May  9 16:42:42 2019
     Raid Level : raid0
     Array Size : 10477568 (9.99 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Thu May  9 16:42:42 2019
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

     Chunk Size : 512K

           Name : centos7:0  (local to host centos7)
           UUID : 1cbcd5a9:0be5dcf0:45c52307:2dd5b5a6
         Events : 0

    Number   Major   Minor   RaidDevice State
       0       8        0        0      active sync   /dev/sda
       1       8       16        1      active sync   /dev/sdb
[root@centos7 ~]# mkfs.xfs /dev/md0  //格式化md0
mkfs.xfs: /dev/md0 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos7 ~]# mkfs.xfs -f /dev/md0 
meta-data=/dev/md0               isize=512    agcount=16, agsize=163712 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2619392, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos7 ~]# mkdir /mnt/md0
[root@centos7 ~]# mount /dev/md0 /mnt/md0/
[root@centos7 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       6.2G  3.6G  2.7G  58% /
devtmpfs            devtmpfs  905M     0  905M   0% /dev
tmpfs               tmpfs     920M     0  920M   0% /dev/shm
tmpfs               tmpfs     920M  8.8M  912M   1% /run
tmpfs               tmpfs     920M     0  920M   0% /sys/fs/cgroup
/dev/sdh1           xfs      1014M  173M  842M  18% /boot
tmpfs               tmpfs     184M     0  184M   0% /run/user/0
/dev/md0            xfs        10G   33M   10G   1% /mnt/md0
[root@centos7 ~]# echo "/dev/md0 /mnt/md0 xfs defaults 0 0" >> /etc/fstab 
                //将分区加入到/etc/fstab,
[root@centos7 ~]# mount -a
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda           8:0    0    5G  0 disk  
└─md0         9:0    0   10G  0 raid0 /mnt/md0
sdb           8:16   0    5G  0 disk  
└─md0         9:0    0   10G  0 raid0 /mnt/md0
sdc           8:32   0    5G  0 disk  
sdd           8:48   0    5G  0 disk  
sde           8:64   0    5G  0 disk  
sdf           8:80   0    5G  0 disk  
sdg           8:96   0    5G  0 disk  
sdh           8:112  0    8G  0 disk  
├─sdh1        8:113  0    1G  0 part  /boot
└─sdh2        8:114  0    7G  0 part  
  ├─cl-root 253:0    0  6.2G  0 lvm   /
  └─cl-swap 253:1    0  820M  0 lvm   [SWAP]
sr0          11:0    1 1024M  0 rom   

2.移除挂载并删除:

[root@centos7 ~]# umount /dev/md0  //卸载挂载
[root@centos7 ~]# sed -i '$d' /etc/fstab //删除fstab中的挂载项
[root@centos7 ~]# mdadm -S /dev/md0 //停止md0
mdadm: stopped /dev/md0
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0    5G  0 disk 
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 
sdd           8:48   0    5G  0 disk 
sde           8:64   0    5G  0 disk 
sdf           8:80   0    5G  0 disk 
sdg           8:96   0    5G  0 disk 
sdh           8:112  0    8G  0 disk 
├─sdh1        8:113  0    1G  0 part /boot
└─sdh2        8:114  0    7G  0 part 
  ├─cl-root 253:0    0  6.2G  0 lvm  /
  └─cl-swap 253:1    0  820M  0 lvm  [SWAP]
sr0          11:0    1 1024M  0 rom  
[root@centos7 ~]# mdadm -r /dev/md0 //移除md0块
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sda //删除sda中的md0信息
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sdb //删除sdb中的md0信息
[root@centos7 ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 
     Raid Level : raid0
  Total Devices : 0

          State : inactive

    Number   Major   Minor   RaidDevice

二、RAID1

1.建立并挂载:

[root@centos7 ~]# mdadm -Cv /dev/md1 -l 1 -n 2 -x 1 /dev/sda /dev/sdb /dev/sdc 
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 5238784K
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@centos7 ~]# mdadm -D /dev/md1 
/dev/md1:
        Version : 1.2
  Creation Time : Thu May  9 17:20:52 2019
     Raid Level : raid1
     Array Size : 5238784 (5.00 GiB 5.36 GB)
  Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Thu May  9 17:21:03 2019
          State : clean, resyncing 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

  Resync Status : 45% complete

           Name : centos7:1  (local to host centos7)
           UUID : d5d5c463:e814cdb7:ea45914d:9da6c653
         Events : 7

    Number   Major   Minor   RaidDevice State
       0       8        0        0      active sync   /dev/sda
       1       8       16        1      active sync   /dev/sdb

       2       8       32        -      spare   /dev/sdc
[root@centos7 ~]# mkfs.xfs /dev/md1 
mkfs.xfs: /dev/md1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
[root@centos7 ~]# mkfs.xfs -f /dev/md1 
meta-data=/dev/md1               isize=512    agcount=4, agsize=327424 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos7 ~]# mkdir /mnt/md1
[root@centos7 ~]# mount /dev/md1 /mnt/md1/
[root@centos7 ~]# echo "/dev/md1 /mnt/md1 xfs defaults 0 0" >> /etc/fstab

2.移除并卸载:

[root@centos7 ~]# sed -i '$d' /etc/fstab   
[root@centos7 ~]# umount /dev/md1 
[root@centos7 ~]# mdadm -S /dev/md1 
mdadm: stopped /dev/md1
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sda
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sdb
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sdc

三、RAID5

1.建立并挂载:

[root@centos7 ~]# mdadm -Cv /dev/md5 -l 5 -n 3 -x 1 /dev/sda /dev/sdb /dev/sdc /dev/sdd
mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 512K
mdadm: size set to 5238784K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
[root@centos7 ~]# mdadm -D /dev/md5 
/dev/md5:
        Version : 1.2
  Creation Time : Thu May  9 17:37:10 2019
     Raid Level : raid5
     Array Size : 10477568 (9.99 GiB 10.73 GB)
  Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
   Raid Devices : 3
  Total Devices : 4
    Persistence : Superblock is persistent

    Update Time : Thu May  9 17:37:28 2019
          State : clean, degraded, recovering 
 Active Devices : 2
Working Devices : 4
 Failed Devices : 0
  Spare Devices : 2

         Layout : left-symmetric
     Chunk Size : 512K

 Rebuild Status : 63% complete

           Name : centos7:5  (local to host centos7)
           UUID : f1b2cb3e:39c3dfa7:0f498d6a:db4f3bbe
         Events : 11

    Number   Major   Minor   RaidDevice State
       0       8        0        0      active sync   /dev/sda
       1       8       16        1      active sync   /dev/sdb
       4       8       32        2      spare rebuilding   /dev/sdc

       3       8       48        -      spare   /dev/sdd
[root@centos7 ~]# mkfs.xfs -f /dev/md5 
meta-data=/dev/md5               isize=512    agcount=16, agsize=163712 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2619392, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos7 ~]# mkdir /mnt/md5
[root@centos7 ~]# mount /dev/md5 /mnt/md5
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda           8:0    0    5G  0 disk  
└─md5         9:5    0   10G  0 raid5 /mnt/md5
sdb           8:16   0    5G  0 disk  
└─md5         9:5    0   10G  0 raid5 /mnt/md5
sdc           8:32   0    5G  0 disk  
└─md5         9:5    0   10G  0 raid5 /mnt/md5
sdd           8:48   0    5G  0 disk  
└─md5         9:5    0   10G  0 raid5 /mnt/md5
sde           8:64   0    5G  0 disk  
sdf           8:80   0    5G  0 disk  
sdg           8:96   0    5G  0 disk  
sdh           8:112  0    8G  0 disk  
├─sdh1        8:113  0    1G  0 part  /boot
└─sdh2        8:114  0    7G  0 part  
  ├─cl-root 253:0    0  6.2G  0 lvm   /
  └─cl-swap 253:1    0  820M  0 lvm   [SWAP]
sr0          11:0    1 1024M  0 rom   

2.移除并卸载:

[root@centos7 ~]# umount /dev/md5 
[root@centos7 ~]# sed -i '$d' /etc/fstab 
[root@centos7 ~]# mdadm -S /dev/md5 
mdadm: stopped /dev/md5
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sd[a-d]

四、RAID 10

1.建立并挂载:

[root@centos7 ~]# mdadm -Cv /dev/md10 -l 10 -n 4 -x 1 /dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde
mdadm: layout defaults to n2
mdadm: layout defaults to n2
mdadm: chunk size defaults to 512K
mdadm: size set to 5238784K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md10 started.
[root@centos7 ~]# mdadm -D /dev/md10 
/dev/md10:
        Version : 1.2
  Creation Time : Thu May  9 17:48:55 2019
     Raid Level : raid10
     Array Size : 10477568 (9.99 GiB 10.73 GB)
  Used Dev Size : 5238784 (5.00 GiB 5.36 GB)
   Raid Devices : 4
  Total Devices : 5
    Persistence : Superblock is persistent

    Update Time : Thu May  9 17:49:08 2019
          State : clean, resyncing 
 Active Devices : 4
Working Devices : 5
 Failed Devices : 0
  Spare Devices : 1

         Layout : near=2
     Chunk Size : 512K

  Resync Status : 30% complete

           Name : centos7:10  (local to host centos7)
           UUID : 1cb9b392:c8b41904:c091f0b5:2cbc07b4
         Events : 4

    Number   Major   Minor   RaidDevice State
       0       8        0        0      active sync set-A   /dev/sda
       1       8       16        1      active sync set-B   /dev/sdb
       2       8       32        2      active sync set-A   /dev/sdc
       3       8       48        3      active sync set-B   /dev/sdd

       4       8       64        -      spare   /dev/sde
[root@centos7 ~]# mkfs.xfs -f /dev/md10 
meta-data=/dev/md10              isize=512    agcount=16, agsize=163712 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=2619392, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos7 ~]# mkdir /mnt/md10
[root@centos7 ~]# mount /dev/md10 /mnt/md10
[root@centos7 ~]# echo "/dev/md10 /mnt/md10 xfs defaults 0 0" >> /etc/fstab 
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE   MOUNTPOINT
sda           8:0    0    5G  0 disk   
└─md10        9:10   0   10G  0 raid10 /mnt/md10
sdb           8:16   0    5G  0 disk   
└─md10        9:10   0   10G  0 raid10 /mnt/md10
sdc           8:32   0    5G  0 disk   
└─md10        9:10   0   10G  0 raid10 /mnt/md10
sdd           8:48   0    5G  0 disk   
└─md10        9:10   0   10G  0 raid10 /mnt/md10
sde           8:64   0    5G  0 disk   
└─md10        9:10   0   10G  0 raid10 /mnt/md10
sdf           8:80   0    5G  0 disk   
sdg           8:96   0    5G  0 disk   
sdh           8:112  0    8G  0 disk   
├─sdh1        8:113  0    1G  0 part   /boot
└─sdh2        8:114  0    7G  0 part   
  ├─cl-root 253:0    0  6.2G  0 lvm    /
  └─cl-swap 253:1    0  820M  0 lvm    [SWAP]
sr0          11:0    1 1024M  0 rom  

2.移除并卸载:

[root@centos7 ~]# sed -i '$d' /etc/fstab 
[root@centos7 ~]# umount /dev/md10 
[root@centos7 ~]# mdadm -S /dev/md10 
mdadm: stopped /dev/md10
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sd[a-e]

五、RAID50 (感觉用到的时候很少)

这个RAID模式是没有的,如果直接操作会出错,只能按原理先RAID5再RAID0:

[root@centos7 ~]# mdadm -Cv /dev/md50 -l 50 -n 6 -x 1 /dev/sd[a-f]
mdadm: invalid raid level: 50

1.建立并挂载:

[root@centos7 ~]# mdadm -Cv /dev/md501 -l 5 -n 3 /dev/sd[a-c]  
mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 512K
mdadm: size set to 5238784K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md501 started.
[root@centos7 ~]# mdadm -Cv /dev/md502 -l 5 -n 3 /dev/sd[d-f] 
mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 512K
mdadm: size set to 5238784K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md502 started.
[root@centos7 ~]# mdadm -Cv /dev/md50 -l 0 -n 2 /dev/md501 /dev/md502
mdadm: chunk size defaults to 512K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md50 started.
[root@centos7 ~]# mdadm -D /dev/md50
/dev/md50:
        Version : 1.2
  Creation Time : Thu May  9 18:00:52 2019
     Raid Level : raid0
     Array Size : 20938752 (19.97 GiB 21.44 GB)
   Raid Devices : 2
  Total Devices : 2
    Persistence : Superblock is persistent

    Update Time : Thu May  9 18:00:52 2019
          State : clean 
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0

     Chunk Size : 512K

           Name : centos7:50  (local to host centos7)
           UUID : e046c197:0c595902:62be10ef:93114d23
         Events : 0

    Number   Major   Minor   RaidDevice State
       0       9      501        0      active sync   /dev/md501
       1       9      502        1      active sync   /dev/md502
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda           8:0    0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sdb           8:16   0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sdc           8:32   0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sdd           8:48   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sde           8:64   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sdf           8:80   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 
sdg           8:96   0    5G  0 disk  
sdh           8:112  0    8G  0 disk  
├─sdh1        8:113  0    1G  0 part  /boot
└─sdh2        8:114  0    7G  0 part  
  ├─cl-root 253:0    0  6.2G  0 lvm   /
  └─cl-swap 253:1    0  820M  0 lvm   [SWAP]
sr0          11:0    1 1024M  0 rom   
[root@centos7 ~]# mkfs.xfs -f /dev/md50
meta-data=/dev/md50              isize=512    agcount=16, agsize=327040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5232640, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@centos7 ~]# mkdir /mnt/md50
[root@centos7 ~]# mount /dev/md50 /mnt/md50
[root@centos7 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda           8:0    0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sdb           8:16   0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sdc           8:32   0    5G  0 disk  
└─md501       9:501  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sdd           8:48   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sde           8:64   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sdf           8:80   0    5G  0 disk  
└─md502       9:502  0   10G  0 raid5 
  └─md50      9:50   0   20G  0 raid0 /mnt/md50
sdg           8:96   0    5G  0 disk  
sdh           8:112  0    8G  0 disk  
├─sdh1        8:113  0    1G  0 part  /boot
└─sdh2        8:114  0    7G  0 part  
  ├─cl-root 253:0    0  6.2G  0 lvm   /
  └─cl-swap 253:1    0  820M  0 lvm   [SWAP]
sr0          11:0    1 1024M  0 rom   
[root@centos7 ~]# df -hT
Filesystem          Type      Size  Used Avail Use% Mounted on
/dev/mapper/cl-root xfs       6.2G  3.6G  2.7G  58% /
devtmpfs            devtmpfs  905M     0  905M   0% /dev
tmpfs               tmpfs     920M     0  920M   0% /dev/shm
tmpfs               tmpfs     920M  8.8M  912M   1% /run
tmpfs               tmpfs     920M     0  920M   0% /sys/fs/cgroup
/dev/sdh1           xfs      1014M  173M  842M  18% /boot
tmpfs               tmpfs     184M     0  184M   0% /run/user/0
/dev/md50           xfs        20G   33M   20G   1% /mnt/md50
[root@centos7 ~]#echo "/dev/md50 /mnt/md50 xfs defaults 0 0" >> /etc/fstab

2.移除并卸载:

[root@centos7 ~]# sed -i '$d' /etc/fstab 
[root@centos7 ~]# umount /dev/md50
[root@centos7 ~]# mdadm -S /dev/md50
mdadm: stopped /dev/md50
[root@centos7 ~]# mdadm -S /dev/md501
mdadm: stopped /dev/md501
[root@centos7 ~]# mdadm -S /dev/md502
mdadm: stopped /dev/md502
[root@centos7 ~]# mdadm --misc --zero-superblock /dev/sd[a-f] 

其他的RAID就不再操作了。

你可能感兴趣的:(Linux)