一、模拟磁盘设备 [[email protected] ~]# ls /dev/sd* # 查看添加的硬件----磁盘设备 /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdc [[email protected] ~]# 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. [[email protected]calhost ~]# 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. [[email protected] ~]# 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 设为故障盘 [[email protected] ~]# 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. [[email protected] ~]# 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 [[email protected] ~]# ls /dev/md* /dev/md5 [[email protected] ~]# 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. [[email protected] ~]# mkfs.xfs /dev/md5p1 && mkfs.xfs /dev/md5p2 [[email protected] ~]# echo $? 0 [[email protected] ~]# mkdir /opt/raid{5_1,5_2} [[email protected] ~]# 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" [[email protected] ~]# echo "UUID="f1354f72-bbe5-4364-9ade-576d95d4c812" /opt/raid5_1 xfs defaults 0 0" >> /etc/fstab [[email protected] ~]# echo "UUID="d403dca4-6f77-411e-a519-b0536a8867e7" /opt/raid5_2 xfs defaults 0 0" >> /etc/fstab [[email protected] ~]# 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 [[email protected] ~]# mount -a [[email protected] ~]# cp /etc/passwd /opt/raid5_1 [[email protected] ~]# cp /etc/passwd /opt/raid5_2 [[email protected] ~]# cp -r /etc /opt/raid5_1 [[email protected] ~]# cp -r /etc /opt/raid5_2 [[email protected] ~]# ls /opt/raid* /opt/raid5_1: etc passwd /opt/raid5_2: etc passwd [[email protected] ~]# echo "DEVICES /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb4" >> /etc/mdadm.conf [[email protected] ~]# mdadm -Evs >> /etc/mdadm.conf [[email protected] ~]# mount -a [[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# 三、Raid 性能测试 (RAID 5 写入速度比普通磁盘块) [[email protected] ~]# time dd if=/dev/zero of=txt bs=1M count=20 real 0m0.197s user 0m0.001s sys 0m0.180s [[email protected] ~]# cd /opt/raid5_1 [[email protected] raid5_1]# time dd if=/dev/zero of=txt bs=1M count=20 real 0m0.102s user 0m0.000s sys 0m0.033s 四、RAID 故障模拟 [[email protected] ~]# mdadm /dev/md5 -f /dev/sdb1 mdadm: set /dev/sdb1 faulty in /dev/md5 此时可以动态查看一下 [[email protected] ~]# watch -n 1 cat /proc/mdstat [[email protected] ~]# 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 [[email protected] ~]# mdadm -r /dev/md5 /dev/sdb1 mdadm: hot removed /dev/sdb1 from /dev/md5 [[email protected] ~]# 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> [[email protected] ~]# mdadm -a /dev/md5 /dev/sdc1 mdadm: added /dev/sdc1 [[email protected] ~]# 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> [[email protected] ~]# diff /etc/passwd /opt/raid5_1/passwd [[email protected] ~]# echo "DEVICE /dev/sdb2 /dev/sdb3 /dev/sdb4 /dev/sdc1" > /etc/mdadm.conf [[email protected] ~]# mdadm -Evs >> !$ mdadm -Evs >> /etc/mdadm.conf 五、删除RAID设备 [[email protected] ~]# umount /opt/raid5_1 [[email protected] ~]# umount /opt/raid5_2 [[email protected] ~]# mdadm -Ss [[email protected] ~]# rm -rf /etc/mdadm.conf [[email protected] ~]# rm -rf /opt/raid5_1 [[email protected] ~]# rm -rf /opt/raid5_2 [[email protected] ~]# vim /etc/fstab #删除开机RAID挂载项 [[email protected] ~]# mdadm --misc --zero-superblock /dev/sdb1 [[email protected] ~]# mdadm --misc --zero-superblock /dev/sdb2 [[email protected] ~]# mdadm --misc --zero-superblock /dev/sdb3 [[email protected] ~]# mdadm --misc --zero-superblock /dev/sdb4 [[email protected] ~]# mdadm --misc --zero-superblock /dev/sdc1 [[email protected] ~]# 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 [[email protected] ~]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] unused devices: <none> [[email protected] ~]# mdadm --detail /dev/md5 mdadm: cannot open /dev/md5: No such file or directory
时间: 2024-10-27 09:39:40