一、模拟磁盘设备 [root@localhost ~]# ls /dev/sd* # 查看添加的硬件----磁盘设备 /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdc [root@localhost ~]# fdisk /dev/sdb # 分区 模拟出4块硬盘 Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): # 默认 First sector (2048-20971519, default 2048): #默认 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G # 添加1G Partition 1 of type Linux and of size 1 GiB is set 操作同上共分了四个区 /dev/sdb{1,2,3,4} 当做四块硬盘使用 Command (m for help): p Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos Disk identifier: 0x0e220a08 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux /dev/sdb2 2099200 4196351 1048576 83 Linux /dev/sdb3 4196352 6293503 1048576 83 Linux /dev/sdb4 6293504 8390655 1048576 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# fdisk /dev/sdc # 分区模拟出4块硬盘 Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G Partition 1 of type Linux and of size 1 GiB is set 同上 分四个区 /dev/sdc{1,2,3,4} 当做4块硬盘使用 Device Boot Start End Blocks Id System /dev/sdc1 2048 2099199 1048576 83 Linux /dev/sdc2 2099200 4196351 1048576 83 Linux /dev/sdc3 4196352 6293503 1048576 83 Linux /dev/sdc4 6293504 8390655 1048576 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# ls /dev/sd* /dev/sda /dev/sda2 /dev/sdb1 /dev/sdb3 /dev/sdc /dev/sdc2 /dev/sdc4 /dev/sda1 /dev/sdb /dev/sdb2 /dev/sdb4 /dev/sdc1 /dev/sdc3 二、创建RAID 5 mdadm 命令 选项 -C 创建软RAID -l 指定RAID级别 -n 指定磁盘的个数 -x 指定备用设备的个数 -a 添加磁盘 -r 移除磁盘(热) -v 显示详细信息 -s 扫描配置文件 -S 停止阵列 释放 -A 激活,加载 Assemble -f 设为故障盘 [root@localhost ~]# mdadm -C /dev/md5 -l 5 -n 3 -x 1 /dev/sdb{1,2,3,4} # 创建raid 5 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started. [root@localhost ~]# mdadm -D /dev/md5 /dev/md5: Version : 1.2 Creation Time : Sun Jun 19 21:00:53 2016 Raid Level : raid5 Array Size : 2095104 (2046.34 MiB 2145.39 MB) Used Dev Size : 1047552 (1023.17 MiB 1072.69 MB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sun Jun 19 21:00:59 2016 State : clean Active Devices : 3 Working Devices : 4 Failed Devices : 0 Spare Devices : 1 Layout : left-symmetric Chunk Size : 512K Name : localhost.localdomain:5 (local to host localhost.localdomain) UUID : 467e61c9:1ddfd1c6:51c27a22:bb7d160c Events : 18 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 4 8 19 2 active sync /dev/sdb3 3 8 20 - spare /dev/sdb4 [root@localhost ~]# ls /dev/md* /dev/md5 [root@localhost ~]# fdisk /dev/md5 Device Boot Start End Blocks Id System /dev/md5p1 2048 2099199 1048576 83 Linux /dev/md5p2 2099200 4190207 1045504 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# mkfs.xfs /dev/md5p1 && mkfs.xfs /dev/md5p2 [root@localhost ~]# echo $? 0 [root@localhost ~]# mkdir /opt/raid{5_1,5_2} [root@localhost ~]# blkid /dev/md* /dev/md5: PTTYPE="dos" /dev/md5p1: UUID="f1354f72-bbe5-4364-9ade-576d95d4c812" TYPE="xfs" /dev/md5p2: UUID="d403dca4-6f77-411e-a519-b0536a8867e7" TYPE="xfs" [root@localhost ~]# echo "UUID="f1354f72-bbe5-4364-9ade-576d95d4c812" /opt/raid5_1 xfs defaults 0 0" >> /etc/fstab [root@localhost ~]# echo "UUID="d403dca4-6f77-411e-a519-b0536a8867e7" /opt/raid5_2 xfs defaults 0 0" >> /etc/fstab [root@localhost ~]# tail -3 !$ tail -3 /etc/fstab /dev/sr0 /mnt iso9660 defaults 0 0 UUID=f1354f72-bbe5-4364-9ade-576d95d4c812 /opt/raid5_1 xfs defaults 0 0 UUID=d403dca4-6f77-411e-a519-b0536a8867e7 /opt/raid5_2 xfs defaults 0 0 [root@localhost ~]# mount -a [root@localhost ~]# cp /etc/passwd /opt/raid5_1 [root@localhost ~]# cp /etc/passwd /opt/raid5_2 [root@localhost ~]# cp -r /etc /opt/raid5_1 [root@localhost ~]# cp -r /etc /opt/raid5_2 [root@localhost ~]# ls /opt/raid* /opt/raid5_1: etc passwd /opt/raid5_2: etc passwd [root@localhost ~]# echo "DEVICES /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4" >> /etc/mdadm.conf [root@localhost ~]# mdadm -Evs >> /etc/mdadm.conf [root@localhost ~]# mount -a [root@localhost ~]# tail -3 /etc/fstab /dev/sr0 /mnt iso9660 defaults 0 0 UUID=f1354f72-bbe5-4364-9ade-576d95d4c812 /opt/raid5_1 xfs defaults 0 0 UUID=d403dca4-6f77-411e-a519-b0536a8867e7 /opt/raid5_2 xfs defaults 0 0 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/md5p1 1018M 68M 951M 7% /opt/raid5_1 /dev/md5p2 1018M 68M 951M 7% /opt/raid5_2 [root@localhost ~]# 三、Raid 性能测试 (RAID 5 写入速度比普通磁盘块) [root@localhost ~]# time dd if=/dev/zero of=txt bs=1M count=20 real 0m0.197s user 0m0.001s sys 0m0.180s [root@localhost ~]# cd /opt/raid5_1 [root@localhost raid5_1]# time dd if=/dev/zero of=txt bs=1M count=20 real 0m0.102s user 0m0.000s sys 0m0.033s 四、RAID 故障模拟 [root@localhost ~]# mdadm /dev/md5 -f /dev/sdb1 mdadm: set /dev/sdb1 faulty in /dev/md5 此时可以动态查看一下 [root@localhost ~]# watch -n 1 cat /proc/mdstat [root@localhost ~]# mdadm --detail /dev/md5 /dev/md5: Version : 1.2 Creation Time : Sun Jun 19 21:00:53 2016 Raid Level : raid5 Array Size : 2095104 (2046.34 MiB 2145.39 MB) Used Dev Size : 1047552 (1023.17 MiB 1072.69 MB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sun Jun 19 22:43:16 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 1 Spare Devices : 0 Layout : left-symmetric Chunk Size : 512K Name : localhost.localdomain:5 (local to host localhost.localdomain) UUID : 467e61c9:1ddfd1c6:51c27a22:bb7d160c Events : 37 Number Major Minor RaidDevice State 3 8 20 0 active sync /dev/sdb4 1 8 18 1 active sync /dev/sdb2 4 8 19 2 active sync /dev/sdb3 0 8 17 - faulty /dev/sdb1 [root@localhost ~]# mdadm -r /dev/md5 /dev/sdb1 mdadm: hot removed /dev/sdb1 from /dev/md5 [root@localhost ~]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] md5 : active raid5 sdb2[1] sdb3[4] sdb4[3] 2095104 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU] unused devices: <none> [root@localhost ~]# mdadm -a /dev/md5 /dev/sdc1 mdadm: added /dev/sdc1 [root@localhost ~]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] md5 : active raid5 sdc1[5](S) sdb2[1] sdb3[4] sdb4[3] 2095104 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU] unused devices: <none> [root@localhost ~]# diff /etc/passwd /opt/raid5_1/passwd [root@localhost ~]# echo "DEVICE /dev/sdb2 /dev/sdb3 /dev/sdb4 /dev/sdc1" > /etc/mdadm.conf [root@localhost ~]# mdadm -Evs >> !$ mdadm -Evs >> /etc/mdadm.conf 五、删除RAID设备 [root@localhost ~]# umount /opt/raid5_1 [root@localhost ~]# umount /opt/raid5_2 [root@localhost ~]# mdadm -Ss [root@localhost ~]# rm -rf /etc/mdadm.conf [root@localhost ~]# rm -rf /opt/raid5_1 [root@localhost ~]# rm -rf /opt/raid5_2 [root@localhost ~]# vim /etc/fstab #删除开机RAID挂载项 [root@localhost ~]# mdadm --misc --zero-superblock /dev/sdb1 [root@localhost ~]# mdadm --misc --zero-superblock /dev/sdb2 [root@localhost ~]# mdadm --misc --zero-superblock /dev/sdb3 [root@localhost ~]# mdadm --misc --zero-superblock /dev/sdb4 [root@localhost ~]# mdadm --misc --zero-superblock /dev/sdc1 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 10G 3.2G 6.9G 32% / devtmpfs 898M 0 898M 0% /dev tmpfs 913M 84K 913M 1% /dev/shm tmpfs 913M 8.9M 904M 1% /run tmpfs 913M 0 913M 0% /sys/fs/cgroup /dev/sr0 3.8G 3.8G 0 100% /mnt /dev/sda1 497M 158M 340M 32% /boot tmpfs 183M 16K 183M 1% /run/user/42 tmpfs 183M 0 183M 0% /run/user/0 [root@localhost ~]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] unused devices: <none> [root@localhost ~]# mdadm --detail /dev/md5 mdadm: cannot open /dev/md5: No such file or directory
转载于:https://blog.51cto.com/mrpan/1790904