raid:独立的磁盘冗余阵列
创建raid0:
环境准备:准备三块大小相同的磁盘或分区,此处要特别注意:红色字体
[root@localhost6 home]#fdisk /dev/sdd ##对/dev/sdd磁盘进行分区 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x55958e14. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n #增加一块分区 Command action e extended p primary partition (1-4) p #增加主分区 Partition number (1-4): 1 First cylinder (1-6527, default 1): #分区编码为1 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +20G #分区大小为20G Command (m for help): t #改变分区系统的编码 Selected partition 1 Hex code (type L to list codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 Hex code (type L to list codes): fd #此处做raid测试,故选择fd Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): w #存盘退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost6 home]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom /media/CentOS_6.8_Final sda 8:0 0 200G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 48.8G 0 part / ├─sda3 8:3 0 39.1G 0 part /home ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 2G 0 part [SWAP] └─sda6 8:6 0 109.6G 0 part /apps sdb 8:16 0 100G 0 disk ├─sdb2 8:18 0 2G 0 part ├─sdb1 8:17 0 5G 0 part /mnt/sdb1 └─sdb3 8:19 0 20G 0 part sdc 8:32 0 50G 0 disk └─sdc1 8:33 0 20G 0 part sdd 8:48 0 50G 0 disk └─sdd1 8:49 0 20G 0 part
下面以/dev/sdc2和/dev/sdd2做raid0,以/dev/sdb3,/dev/sdc1,/dev/sdd1为例做raid5:
[root@localhost6 home]#lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 3.7G 0 rom /media/CentOS_6.8_Final sda 8:0 0 200G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 48.8G 0 part / ├─sda3 8:3 0 39.1G 0 part /home ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 2G 0 part [SWAP] └─sda6 8:6 0 109.6G 0 part /apps sdb 8:16 0 100G 0 disk ├─sdb2 8:18 0 2G 0 part ├─sdb1 8:17 0 5G 0 part /mnt/sdb1 └─sdb3 8:19 0 20G 0 part sdc 8:32 0 50G 0 disk ├─sdc1 8:33 0 20G 0 part └─sdc2 8:34 0 20G 0 part sdd 8:48 0 50G 0 disk ├─sdd1 8:49 0 20G 0 part └─sdd2 8:50 0 20G 0 part
#-C:创建raid0 后跟-a{yes|no}:自动创建raid设备的设备文件
-l level:指定要创建的raid的级别
-n number:指定用多少块设备来创建raid,还要详细的写出设备
[root@localhost6 home]#mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdc2 /dec/sdd2 #写错了设备 mdadm: cannot open /dec/sdd2: No such file or directory [root@localhost6 home]#mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdc2 /dev/sdd2 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
#-D:显示raid创建的详细信息
[root@localhost6 home]#mdadm -D /dev/md0 /dev/md0: #raid0的名称 Version : 1.2 Creation Time : Sun Apr 23 18:46:38 2017 Raid Level : raid0 #raid设备的级别 Array Size : 41928704 (39.99 GiB 42.93 GB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Sun Apr 23 18:46:38 2017 State : clean #设备目前的状态
Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 #备用设备数,目前没有备用设备,可用-x来指定备用设备的个数 Chunk Size : 512K #chunk的默认大小,即条带卷的默认大小;可用-c 修改默认大小 Name : localhost6.8.localdomain:0 (local to host localhost6.8.localdomain) UUID : 4975f63e:b94df707:df8c64b1:0f502244 #raid0的UUID Events : 0 Number Major Minor RaidDevice State #raid0包含的成员 0 8 34 0 active sync /dev/sdc2 1 8 50 1 active sync /dev/sdd2
[root@localhost6 home]#mkfs.ext4 /dev/md0 #raid0的格式化,必须是raid0整体进行格式化,而不是每个成员独立的进行格式化 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 2621440 inodes, 10482176 blocks 524108 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 320 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 23 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost6 home]#blkid #查看格式化之后的磁盘的UUID /dev/sda1: UUID="b3ef8664-92fc-4639-a78c-9db880895677" TYPE="ext4" /dev/sda2: UUID="6c72c208-95ae-4b37-9890-7b4327770a4d" TYPE="ext4" /dev/sda3: UUID="a2a735cb-20a0-4bfa-af8f-f33c4b3e3477" TYPE="ext4" /dev/sda5: UUID="e7081f48-f426-4050-86d9-dd9309523cb1" TYPE="swap" /dev/sda6: UUID="9da8eb51-8801-4a49-955b-6ed82a6d41dd" TYPE="ext4" /dev/sdb1: UUID="8251e11e-e770-4447-b6ba-e8ca57519544" TYPE="ext4" /dev/sdb2: UUID="615eb63b-18df-4004-b11b-6590436e186c" TYPE="ext4" #有UUID和UUID_SUB,UUID是相同的,都是raid0中的,type类型如下: /dev/sdc2: UUID="4975f63e-b94d-f707-df8c-64b10f502244" UUID_SUB="41ca2361-2c4d-ce5e-37ef-e6588755bfe7" LABEL="localhost6.8.localdomain:0" TYPE="linux_raid_member" /dev/sdd2: UUID="4975f63e-b94d-f707-df8c-64b10f502244" UUID_SUB="40d2db11-ac16-84a6-10a9-74370a01a26f" LABEL="localhost6.8.localdomain:0" TYPE="linux_raid_member" /dev/md0: UUID="8cefdce6-b198-47e2-b96f-6dbd43b11432" TYPE="ext4"
[root@localhost6 ~]#mkdir /mnt/raid0 #为raid0的挂载建目录 [root@localhost6 ~]#mount /dev/md0 /mnt/raid0 #挂载raid0
[root@localhost6 raid0]#dd if=/dev/zero of=file1 bs=1M count=2048 #设备名写错 dd: opening `/dev/z1ero': No such file or directory [root@localhost6 raid0]#dd if=/dev/zero of=file1 bs=1M count=2048 #对raid0进行写操作的测试
2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB) copied, 2.97907 s, 721 MB/s
[root@localhost6 raid0]#dd if=file1 of=/dev/null #对raid0进行读的操作测试
4194304+0 records in 4194304+0 records out 2147483648 bytes (2.1 GB) copied, 4.23014 s, 508 MB/s
#将创建的raid写入配置文件/etc/fstab
# /etc/fstab # Created by anaconda on Thu Mar 23 04:54:05 2017 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=6c72c208-95ae-4b37-9890-7b4327770a4d / ext4 defaults 1 1 UUID=9da8eb51-8801-4a49-955b-6ed82a6d41dd /apps ext4 defaults 1 2 UUID=b3ef8664-92fc-4639-a78c-9db880895677 /boot ext4 defaults 1 2 UUID=a2a735cb-20a0-4bfa-af8f-f33c4b3e3477 /home ext4 usrquota,grpquota 1 2 UUID=e7081f48-f426-4050-86d9-dd9309523cb1 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 UUID=8251e11e-e770-4447-b6ba-e8ca57519544 /mnt/sdb1 ext4 acl 0 0 UUID=8cefdce6-b198-47e2-b96f-6dbd43b11432 /mnt/raid0 ext4 defaults 0 0
创建raid5:
#-x:备用设备的个数,备用设备一般写到最后边,前边的-n 代表的是成员,故没有加上备用设备的数量
[root@localhost6 raid0]#mdadm -C /dev/md5 -a yes -l 5 -n 3 -x 1 /dev/sdb3 /dev/sdc1 /dev/sdd1 /dev/sdb4 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md5 started.
[root@localhost6 raid0]#mdadm -D /dev/md5 #查看raid的详细信息 /dev/md5: Version : 1.2 Creation Time : Sun Apr 23 19:34:50 2017 Raid Level : raid5 Array Size : 41928704 (39.99 GiB 42.93 GB) Used Dev Size : 20964352 (19.99 GiB 21.47 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sun Apr 23 19:35:44 2017 State : clean, degraded, recovering Active Devices : 2 Working Devices : 4 Failed Devices : 0 Spare Devices : 2 Layout : left-symmetric Chunk Size : 512K Rebuild Status : 53% complete Name : localhost6.8.localdomain:5 (local to host localhost6.8.localdomain) UUID : 946c8dfd:9bc77d44:381d0edc:985e0ba3 Events : 9 Number Major Minor RaidDevice State #raid的成员只有三个,sdb4只是备用。 0 8 19 0 active sync /dev/sdb3 1 8 33 1 active sync /dev/sdc1 4 8 49 2 spare rebuilding /dev/sdd1 3 8 20 - spare /dev/sdb4
[root@localhost6 raid0]#blkid #查看磁盘的UUID信息 /dev/sda1: UUID="b3ef8664-92fc-4639-a78c-9db880895677" TYPE="ext4" /dev/sda2: UUID="6c72c208-95ae-4b37-9890-7b4327770a4d" TYPE="ext4" /dev/sda3: UUID="a2a735cb-20a0-4bfa-af8f-f33c4b3e3477" TYPE="ext4" /dev/sda5: UUID="e7081f48-f426-4050-86d9-dd9309523cb1" TYPE="swap" /dev/sda6: UUID="9da8eb51-8801-4a49-955b-6ed82a6d41dd" TYPE="ext4" /dev/sdb1: UUID="8251e11e-e770-4447-b6ba-e8ca57519544" TYPE="ext4" /dev/sdb2: UUID="615eb63b-18df-4004-b11b-6590436e186c" TYPE="ext4" /dev/sdc2: UUID="4975f63e-b94d-f707-df8c-64b10f502244" UUID_SUB="41ca2361-2c4d-ce5e-37ef-e6588755bfe7" LABEL="localhost6.8.localdomain:0" TYPE="linux_raid_member" /dev/sdd2: UUID="4975f63e-b94d-f707-df8c-64b10f502244" UUID_SUB="40d2db11-ac16-84a6-10a9-74370a01a26f" LABEL="localhost6.8.localdomain:0" TYPE="linux_raid_member" /dev/md0: UUID="8cefdce6-b198-47e2-b96f-6dbd43b11432" TYPE="ext4" /dev/sdb3: UUID="946c8dfd-9bc7-7d44-381d-0edc985e0ba3" UUID_SUB="5ad41266-add6-4399-7666-0a29db8ad4d9" LABEL="localhost6.8.localdomain:5" TYPE="linux_raid_member" /dev/sdb4: UUID="946c8dfd-9bc7-7d44-381d-0edc985e0ba3" UUID_SUB="c5da8eae-6aaf-c3fa-4df7-df2ccf377976" LABEL="localhost6.8.localdomain:5" TYPE="linux_raid_member" /dev/sdc1: UUID="946c8dfd-9bc7-7d44-381d-0edc985e0ba3" UUID_SUB="e19a457c-327c-2076-0791-fadb8b65b7ca" LABEL="localhost6.8.localdomain:5" TYPE="linux_raid_member" /dev/sdd1: UUID="946c8dfd-9bc7-7d44-381d-0edc985e0ba3" UUID_SUB="bdc8eb2e-6f9f-9dd0-3a3d-7bb1ace8a394" LABEL="localhost6.8.localdomain:5" TYPE="linux_raid_member" [root@localhost6 raid0]#
[root@localhost6 raid0]#mkfs.ext4 /dev/md5 #格式化整个raid设备 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 2621440 inodes, 10482176 blocks 524108 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4294967296 320 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 24 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost6 raid0]#mkdir /mnt/md5 #为挂载raid设备创建目录 [root@localhost6 raid0]#mount /dev/md5 /mnt/md5 #挂载raid设备
[root@localhost6 md5]#dd if=/dev/zero of=file1 bs=1M count=2048 #raid设备的写测试,没有raid0高 2048+0 records in 2048+0 records out 2147483648 bytes (2.1 GB) copied, 4.94279 s, 434 MB/s
[root@localhost6 md5]#dd if=file1 of=/dev/null #raid设备的读测试 4194304+0 records in 4194304+0 records out 2147483648 bytes (2.1 GB) copied, 4.1317 s, 520 MB/s
#mdadm.conf 是raid默认的配置文件
#-s scan:扫描raid的配置文件
[root@localhost6 md5]#mdadm -Ds > /etc/mdadm.conf #此配置文件的用途是其中一块设备因故障停了再激活使用时,必须首先扫描配置文件。这条命令可认为是生成配置文件。
#-A:激活raid设备
#-S stop:停用raid设备
[root@localhost6 md5]#mdadm -S /dev/md0 #停用raid设备必须先取消挂载 mdadm: Cannot get exclusive access to /dev/md0:Perhaps a running process, mounted filesystem or active volume group? [root@localhost6 md5]#umount /mnt/raid0 #取消挂载 [root@localhost6 md5]#mdadm -S /dev/md0 #停用raid设备 mdadm: stopped /dev/md0 [root@localhost6 md5]#ll /dev/md0 #停用的raid设备查不到 ls: cannot access /dev/md0: No such file or directory [root@localhost6 md5]#mdadm -As /dev/md0 #重新激活raid设备。这时需要配置文件/etc/mdadm.conf的存在并扫描确认 mdadm: /dev/md0 has been started with 2 drives.
#将raid的创建写入配置文件/etc/fstab
# /etc/fstab # Created by anaconda on Thu Mar 23 04:54:05 2017 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=6c72c208-95ae-4b37-9890-7b4327770a4d / ext4 defaults 1 1 UUID=9da8eb51-8801-4a49-955b-6ed82a6d41dd /apps ext4 defaults 1 2 UUID=b3ef8664-92fc-4639-a78c-9db880895677 /boot ext4 defaults 1 2 UUID=a2a735cb-20a0-4bfa-af8f-f33c4b3e3477 /home ext4 usrquota,grpquota 1 2 UUID=e7081f48-f426-4050-86d9-dd9309523cb1 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 UUID=8251e11e-e770-4447-b6ba-e8ca57519544 /mnt/sdb1 ext4 acl 0 0 UUID=8cefdce6-b198-47e2-b96f-6dbd43b11432 /mnt/raid0 ext4 defaults 0 0 UUID=ae77fa79-8a9a-41f8-9aac-90bbcedca246 /mnt/md5 ext4 defaults 0 0