# 实测软Raid ## 一、环境介绍 | 操作系统 | 硬盘1 | 硬盘2 | | -------------------- | ------- | ------- | | ubuntu24.04 云服务器 | 70g ssd | 70g ssd | ## 二、测试创建Raid 1 1、创建Raid 1 ``` root@VM-2-2-ubuntu:/data# mdadm -C /dev/md0 -l 1 -n 2 /dev/vdb /dev/vdc 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. ``` 2、查看创建进度 ``` ###查看raid进度### root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[1] vdb[0] 73333760 blocks super 1.2 [2/2] [UU] [===>.................] resync = 15.5% (11405312/73333760) finish=5.0min speed=206165K/sec unused devices: ``` 3、创建完成 ``` ###Raid进度完成### root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[1] vdb[0] 73333760 blocks super 1.2 [2/2] [UU] unused devices: ``` 4、格式化磁盘 ``` ###格式化### root@VM-2-2-ubuntu:/data# mkfs.xfs /dev/md0 meta-data=/dev/md0 isize=512 agcount=4, agsize=4583360 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=1 = reflink=1 bigtime=1 inobtcount=1 nrext64=0 data = bsize=4096 blocks=18333440, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0, ftype=1 log =internal log bsize=4096 blocks=16384, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 ``` 5、挂载 ``` root@VM-2-2-ubuntu:/data# mount /dev/md0 /data root@VM-2-2-ubuntu:/data# df -h Filesystem Size Used Avail Use% Mounted on tmpfs 197M 1.1M 196M 1% /run /dev/vda2 50G 4.9G 43G 11% / tmpfs 984M 24K 984M 1% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 197M 12K 197M 1% /run/user/1000 /dev/md0 70G 1.4G 69G 2% /data root@VM-2-2-ubuntu:~# ls -l /dev/disk/by-id total 0 lrwxrwxrwx 1 root root 9 May 8 16:10 ata-QEMU_DVD-ROM_QM00002 -> ../../sr0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-name-VM-2-2-ubuntu:0 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-uuid-261fcd66:9689a33e:294c4f85:6e096e48 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:10 virtio-disk-f6vtxkjm -> ../../vdb lrwxrwxrwx 1 root root 9 May 8 16:10 virtio-disk-ld0km1yk -> ../../vdc ``` 6、写入数据测试性能,并计算文件的md5值 ``` root@VM-2-2-ubuntu:/data# dd if=/dev/zero of=sun.txt bs=1M count=2048 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 4.83928 s, 444 MB/s root@VM-2-2-ubuntu:/data# md5sum sun.txt a981130cf2b7e09f4686dc273cf7187e sun.txt ``` 三、模拟单盘故障,拔掉异常的盘,然后添加新盘 1、卸载vdb,尝试读取写入文件 ``` root@VM-2-2-ubuntu:~# ls -l /dev/disk/by-id total 0 lrwxrwxrwx 1 root root 9 May 8 16:10 ata-QEMU_DVD-ROM_QM00002 -> ../../sr0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-name-VM-2-2-ubuntu:0 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-uuid-261fcd66:9689a33e:294c4f85:6e096e48 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:10 virtio-disk-ld0km1yk -> ../../vdc ###可正常读取### root@VM-2-2-ubuntu:/data# md5sum sun.txt a981130cf2b7e09f4686dc273cf7187e sun.txt root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[1] 73333760 blocks super 1.2 [2/1] [_U] unused devices: root@VM-2-2-ubuntu:/data# rz -E Sent - Linux内核设计与实现(第三版).pdf 9.72 MB/s Spend: 4 seconds root@VM-2-2-ubuntu:/data# ls Linux内核设计与实现(第三版).pdf sun.txt 通用交流材料_95_1744028634000.pptx ``` 2、加回单盘,这里有点难受的是需要进行全盘数据校验 1. **全盘数据校验机制** RAID1 的同步并非仅针对已写入文件,而是对整块硬盘的 ​**所有存储单元(包括空白区域)​**​ 进行校验 即使你只拔插硬盘,RAID 控制器仍需逐块比对数据一致性,确保两个硬盘的镜像完全一致。 2. **触发同步的条件** 当硬盘被移除后重新插入时,RAID 控制器会将其标记为「潜在不一致状态」,即使文件系统未损坏,仍需通过全盘同步消除数据差异风险 ``` root@VM-2-2-ubuntu:/data# ls -al /dev/disk/by-id total 0 drwxr-xr-x 2 root root 140 May 9 10:51 . drwxr-xr-x 8 root root 160 May 8 16:10 .. lrwxrwxrwx 1 root root 9 May 8 16:10 ata-QEMU_DVD-ROM_QM00002 -> ../../sr0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-name-VM-2-2-ubuntu:0 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-uuid-261fcd66:9689a33e:294c4f85:6e096e48 -> ../../md0 lrwxrwxrwx 1 root root 9 May 9 10:51 virtio-disk-f6vtxkjm -> ../../vdb lrwxrwxrwx 1 root root 9 May 8 16:10 virtio-disk-ld0km1yk -> ../../vdc root@VM-2-2-ubuntu:/data# mdadm --add /dev/md0 /dev/vdb root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdb[2] vdc[1] 73333760 blocks super 1.2 [2/2] [UU] unused devices: ``` 3、干掉另外一块盘,测试数据完整性并同样加回 ``` root@VM-2-2-ubuntu:/data# ls -al /dev/disk/by-id total 0 drwxr-xr-x 2 root root 120 May 9 11:11 . drwxr-xr-x 8 root root 160 May 8 16:10 .. lrwxrwxrwx 1 root root 9 May 8 16:10 ata-QEMU_DVD-ROM_QM00002 -> ../../sr0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-name-VM-2-2-ubuntu:0 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-uuid-261fcd66:9689a33e:294c4f85:6e096e48 -> ../../md0 lrwxrwxrwx 1 root root 9 May 9 10:51 virtio-disk-f6vtxkjm -> ../../vdb root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdb[2] 73333760 blocks super 1.2 [2/1] [U_] unused devices: root@VM-2-2-ubuntu:/data# md5sum sun.txt a981130cf2b7e09f4686dc273cf7187e sun.txt root@VM-2-2-ubuntu:/data# ls -al /dev/disk/by-id total 0 drwxr-xr-x 2 root root 140 May 9 11:13 . drwxr-xr-x 8 root root 160 May 8 16:10 .. lrwxrwxrwx 1 root root 9 May 8 16:10 ata-QEMU_DVD-ROM_QM00002 -> ../../sr0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-name-VM-2-2-ubuntu:0 -> ../../md0 lrwxrwxrwx 1 root root 9 May 8 16:44 md-uuid-261fcd66:9689a33e:294c4f85:6e096e48 -> ../../md0 lrwxrwxrwx 1 root root 9 May 9 10:51 virtio-disk-f6vtxkjm -> ../../vdb lrwxrwxrwx 1 root root 9 May 9 11:13 virtio-disk-ld0km1yk -> ../../vdc root@VM-2-2-ubuntu:/data# mdadm --add /dev/md0 /dev/vdc mdadm: added /dev/vdc root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[3] vdb[2] 73333760 blocks super 1.2 [2/1] [U_] [>....................] recovery = 0.8% (601728/73333760) finish=6.0min speed=200576K/sec unused devices: ``` 四、模拟raid 1 扩容(拔盘之前优先关机,再拔盘,然后开机!!!) 1、扩容单硬盘至75G,然后挂载加回 ``` root@VM-2-2-ubuntu:/data# mdadm --add /dev/md0 /dev/vdc mdadm: added /dev/vdc root@VM-2-2-ubuntu:/data# cat /proc/mdstat Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[3] vdb[2] 73333760 blocks super 1.2 [2/1] [U_] [>....................] recovery = 1.3% (1001728/73333760) finish=4.8min speed=250432K/sec unused devices: ``` 2、等待重建完毕后,再扩容另外一块磁盘并加入 ``` Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdc[3] vdb[2] 73333760 blocks super 1.2 [2/2] [UU] unused devices: Every 2.0s: cat /proc/mdstat VM-2-2-ubuntu: Fri May 9 11:42:12 2025 Personalities : [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] md0 : active raid1 vdb[2] vdc[3] 73333760 blocks super 1.2 [2/2] [UU] unused devices: ``` 3、扩容Raid ``` mdadm --grow /dev/md0 --size=max ``` 4、扩容文件系统 ``` ```