Linux使用mdadm模拟RAID5磁盘阵列

Linux使用mdadm模拟RAID5磁盘阵列

RAID 磁盘阵列

RAID可以通过一个技术(软件或硬件),将多个较小的磁盘整合成为一个较大的磁盘,除了数据存储之外,还有数据保护的功能,这里简介一下RAID 0, 1, 5,并模拟RAID 5

RAID-0 (等量模式, stripe):性能最佳
数据平均分布到多个磁盘中, 没有冗余,任何一个磁盘坏掉都会导致数据永久丢失。但性能最佳


RAID-1 (映像模式, mirror):完整备份
如果使用两块磁盘,每个数据在两个磁盘上各存一份,任何一个磁盘损坏,不会导致数据丢失。要“浪费”一半的存储空间

RAID-5
至少三块磁盘,如果一个磁盘上的数据丢失,可以通过其他两块磁盘上的数据(并非真正的数据)来还原。

下面的测试中,有5个active device 和一个 Spare Devices 来组成RAID5, active device可以理解为正在使用的磁盘, Spare Devices则是备用的磁盘,如果任何一个active device出现问题,则Spare Device会自动接替,丢失的数据会自动在Spare Device上重建,测试中,使用了一个分区来模拟一个磁盘:

1. 安装RAID管理工具 mdadm
root@db2a:~# apt-get install mdadm

2. 有下面10个可用“磁盘”
root@db2a:~# fdisk -l /dev/sdc

Disk /dev/sdc: 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: 0x55628409

Device Boot Start End Blocks Id System
/dev/sdc1 2048 4196351 2097152 83 Linux
/dev/sdc2 4196352 8390655 2097152 83 Linux
/dev/sdc3 8390656 12584959 2097152 83 Linux
/dev/sdc4 12584960 41943039 14679040 5 Extended
/dev/sdc5 12587008 16781311 2097152 83 Linux
/dev/sdc6 16783360 20977663 2097152 83 Linux
/dev/sdc7 20979712 25174015 2097152 83 Linux
/dev/sdc8 25176064 29370367 2097152 83 Linux
/dev/sdc9 29372416 33566719 2097152 83 Linux
/dev/sdc10 33568768 37763071 2097152 83 Linux
/dev/sdc11 37765120 41943039 2088960 83 Linux

规划如下: 将sdc6-sdc11这6个“磁盘”作为RAID盘,每个大小为2G,其中5个为active,1个为备胎,最后RAID盘大小应该为(5-1)*2GB=8G

3. 创建RAID盘
创建RAID盘,指定盘的名子为/dev/md/raid05disk, level 5表示RAID5, raid-devices表示5个active device,
root@db2a:~# mdadm --create --auto=yes /dev/md/raid05disk --level=5 --raid-devices=5 --spare-devices=1 /dev/sdc{6,7,8,9,10,11}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md/raid05disk started.

查看详细状况,可以看备用盘为/dev/sdc11
root@db2a:~# mdadm --detail /dev/md/raid05disk
/dev/md/raid05disk:
Version : 1.2
Creation Time : Wed Aug 23 19:52:49 2017
Raid Level : raid5
Array Size : 8349696 (7.96 GiB 8.55 GB)
Used Dev Size : 2087424 (2038.84 MiB 2137.52 MB)
Raid Devices : 5
Total Devices : 6
Persistence : Superblock is persistent

Update Time : Wed Aug 23 19:53:03 2017
State : clean
Active Devices : 5
Working Devices : 6
Failed Devices : 0
Spare Devices : 1

Layout : left-symmetric
Chunk Size : 512K

Name : db2a:raid05disk (local to host db2a)
UUID : f0b2137b:ee393900:50b6cd77:b72453e9
Events : 18

Number Major Minor RaidDevice State
0 8 38 0 active sync /dev/sdc6
1 8 39 1 active sync /dev/sdc7
2 8 40 2 active sync /dev/sdc8
3 8 41 3 active sync /dev/sdc9
6 8 42 4 active sync /dev/sdc10

5 8 43 - spare /dev/sdc11
You have mail in /var/mail/root

4. 在RAID盘上建立文件系统,并挂载:
root@db2a:~# mkfs.ext4 /dev/md/raid05disk
root@db2a:~# mkdir /mnt/raid05
root@db2a:~# mount /dev/md/raid05disk /mnt/raid05
....
root@db2a:~# df -h
Filesystem Size Used Avail Use% Mounted on
...
/dev/md127 7.8G 588M 6.8G 8% /mnt/raid05

5. 使用-fail选项,模拟磁盘损坏
这里损坏磁盘sdc9
root@db2a:~# mdadm --manage /dev/md/raid05disk --fail /dev/sdc9
mdadm: set /dev/sdc9 faulty in /dev/md/raid05disk

立刻查看RAID,发现正在原来的spare device /dev/sdc11上重建:
root@db2a:~# mdadm --detail /dev/md/raid05disk
...

Rebuild Status : 5% complete <---重建进度

Name : db2a:raid05disk (local to host db2a)
UUID : f0b2137b:ee393900:50b6cd77:b72453e9
Events : 20

Number Major Minor RaidDevice State
0 8 38 0 active sync /dev/sdc6
1 8 39 1 active sync /dev/sdc7
2 8 40 2 active sync /dev/sdc8
5 8 43 3 spare rebuilding /dev/sdc11
6 8 42 4 active sync /dev/sdc10

3 8 41 - faulty spare /dev/sdc9

root@db2a:~# mdadm --detail /dev/md/raid05disk | grep complete
Rebuild Status : 53% complete
root@db2a:~# mdadm --detail /dev/md/raid05disk | grep complete
Rebuild Status : 79% complete
root@db2a:~# mdadm --detail /dev/md/raid05disk | grep complete
Rebuild Status : 92% complete

重建完成:
root@db2a:~# mdadm --detail /dev/md/raid05disk
/dev/md/raid05disk:
Version : 1.2
Creation Time : Wed Aug 23 19:52:49 2017
Raid Level : raid5
Array Size : 8349696 (7.96 GiB 8.55 GB)
Used Dev Size : 2087424 (2038.84 MiB 2137.52 MB)
Raid Devices : 5
Total Devices : 6
Persistence : Superblock is persistent

Update Time : Wed Aug 23 20:03:36 2017
State : clean
Active Devices : 5
Working Devices : 5
Failed Devices : 1
Spare Devices : 0

Layout : left-symmetric
Chunk Size : 512K

Name : db2a:raid05disk (local to host db2a)
UUID : f0b2137b:ee393900:50b6cd77:b72453e9
Events : 37

Number Major Minor RaidDevice State
0 8 38 0 active sync /dev/sdc6
1 8 39 1 active sync /dev/sdc7
2 8 40 2 active sync /dev/sdc8
5 8 43 3 active sync /dev/sdc11
6 8 42 4 active sync /dev/sdc10

3 8 41 - faulty spare /dev/sdc9

6. 替换损坏盘
拔掉损坏的磁盘/dev/sdc9,并添加一块新的磁盘/dev/sdc5作为备用盘
root@db2a:~# mdadm --manage /dev/md/raid05disk --add /dev/sdc5 --remove /dev/sdc9
mdadm: added /dev/sdc5
mdadm: hot removed /dev/sdc9 from /dev/md/raid05disk

root@db2a:~# mdadm --detail /dev/md/raid05disk
/dev/md/raid05disk:
Version : 1.2
Creation Time : Wed Aug 23 19:52:49 2017
Raid Level : raid5
Array Size : 8349696 (7.96 GiB 8.55 GB)
Used Dev Size : 2087424 (2038.84 MiB 2137.52 MB)
Raid Devices : 5
Total Devices : 6
Persistence : Superblock is persistent

Update Time : Wed Aug 23 20:04:08 2017
State : clean
Active Devices : 5
Working Devices : 6
Failed Devices : 0
Spare Devices : 1

Layout : left-symmetric
Chunk Size : 512K

Name : db2a:raid05disk (local to host db2a)
UUID : f0b2137b:ee393900:50b6cd77:b72453e9
Events : 39

Number Major Minor RaidDevice State
0 8 38 0 active sync /dev/sdc6
1 8 39 1 active sync /dev/sdc7
2 8 40 2 active sync /dev/sdc8
5 8 43 3 active sync /dev/sdc11
6 8 42 4 active sync /dev/sdc10

7 8 37 - spare /dev/sdc5
You have new mail in /var/mail/root

7. 关闭RAID
root@db2a:~# umount /dev/md/raid05disk
root@db2a:~# mdadm --stop /dev/md/raid05disk
mdadm: stopped /dev/md/raid05disk


参考资料:鸟哥的linux私房菜

你可能感兴趣的:(LINUX,linux,raid)