Linux中的软件RAID 1配置
从可用的RAID的不同级别来看,RAID 1以冗余而不剥离而闻名。
通过将一个磁盘上的整个数据块逐块完全复制到另一个磁盘,此RAID级别采用了镜像。
创建RAID 1阵列所需的最小磁盘数为2.
最大磁盘数最多可以达到32.
已使用的磁盘数必须始终为偶数。
以下文章介绍了不同类型的raid及其内部工作,以及Linux上raid 0上的配置文章。
根据raid 1的要求,我们至少需要两个分区。
因此我们创建两个分区/dev/sda8和/dev/sda9并将分区类型更改为raid类型。
[root@localhost ~]# fdisk /dev/sda The number of cylinders for this disk is set to 19457. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): n First cylinder (18895-19457, default 18895): Using default value 18895 Last cylinder or +size or +sizeM or +sizeK (18895-19457, default 19457): +100M Command (m for help): n First cylinder (18908-19457, default 18908): Using default value 18908 Last cylinder or +size or +sizeM or +sizeK (18908-19457, default 19457): +100M Command (m for help): t Partition number (1-9): 9 Hex code (type L to list codes): fd Changed system type of partition 9 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-9): 8 Hex code (type L to list codes): fd Changed system type of partition 8 to fd (Linux raid autodetect) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks. [root@localhost ~]#
解释:
fdisk:创建新分区的工具
n:表示创建一个新分区
8,9:是分区号
- 100M:分区大小
t:表示更改提到的分区的类型
fd:RAID类型分区的代码
w:是将更改保存到表中
使用partprobe命令无需重新引导即可更新分区表,然后查看该表。
[root@localhost ~]# partprobe [root@localhost ~]# fdisk -l Disk /dev/sda: 160.0 GB, 160041885696 bytes 255 heads, 63 sectors/track, 19457 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sda1 * 1 13 102400 7 HPFS/NTFS Partition 1 does not end on cylinder boundary. /dev/sda2 13 3825 30617600 7 HPFS/NTFS /dev/sda3 3825 11474 61440000 7 HPFS/NTFS /dev/sda4 11475 19457 64123447+ 5 Extended /dev/sda5 11475 18868 59392273+ 83 Linux /dev/sda6 18869 18881 104391 fd Linux raid autodetect /dev/sda7 18882 18894 104391 fd Linux raid autodetect /dev/sda8 18895 18907 104391 fd Linux raid autodetect /dev/sda9 18908 18920 104391 fd Linux raid autodetect Disk /dev/md0: 213 MB, 213647360 bytes 2 heads, 4 sectors/track, 52160 cylinders Units = cylinders of 8 * 512 = 4096 bytes Disk /dev/md0 doesn't contain a valid partition table
现在创建名为/dev/md1的RAID 1设备:
[root@localhost ~]# mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sda8 /dev/sda9
现在,要查看有关已创建的raid设备/dev/md1的详细信息,请使用以下命令:
[root@localhost ~]# mdadm --detail /dev/md1 /dev/md1: Version : 0.90 Creation Time : Tue Apr 9 14:50:00 2013 Raid Level : raid1 Array Size : 104320 (101.89 MiB 106.82 MB) Used Dev Size : 104320 (101.89 MiB 106.82 MB) Raid Devices : 2 Total Devices : 2 Preferred Minor : 1 Persistence : Superblock is persistent Update Time : Tue Apr 9 14:50:07 2013 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 UUID : 034c85f3:60ce1191:3b61e8dc:55851162 Events : 0.2 Number Major Minor RaidDevice State 0 8 8 0 active sync /dev/sda8 1 8 9 1 active sync /dev/sda9 [root@localhost ~]# cat /proc/mdstat Personalities : [raid0] [raid1] md1 : active raid1 sda9[1] sda8[0] 104320 blocks [2/2] [UU] md0 : active raid0 sda7[1] sda6[0] 208640 blocks 64k chunks unused devices: <none>
从上面的输出中,可以清楚地了解到raid1已经创建并使用分区/dev/sda8和/dev/sda9.
格式化RAID设备/dev/md1:
[root@localhost ~]# mke2fs -j /dev/md1 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 26104 inodes, 104320 blocks 5216 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 13 block groups 8192 blocks per group, 8192 fragments per group 2008 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 20 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
现在创建目录/raid1并在其上挂载/dev/md1.
[root@localhost ~]# mkdir /raid1 [root@localhost ~]# mount /dev/md1 /raid1 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 55G 18G 35G 34% / tmpfs 502M 0 502M 0% /dev/shm /dev/sda3 59G 31G 28G 53% /root/Desktop/win7 /dev/md0 198M 5.8M 182M 4% /raid0 /dev/md1 99M 5.6M 89M 6% /raid1 [root@localhost ~]
上面的输出清楚地表明raid1已成功创建并安装在/raid1上。
硬盘损坏和恢复测试
现在,如果任何磁盘出现故障或者崩溃,/raid1目录中的数据将如何处理?
raid1可以保护数据免受此类损失吗?
为了得到上述问题的答案,我们将在/raid1目录中创建一些文件。
之后,我们从raid1设备中手动删除或者删除任何一块硬盘,然后检查内容状态。
[root@localhost ~]# cd /raid1 [root@localhost raid1]# touch {a..z} [root@localhost raid1]# ls a b c d e f g h i j k l lost+found m n o p q r s t u v w x y z [root@localhost raid1]#
现在,我们将使raid1的/dev/sda9分区失败,然后检查数据状态。
[root@localhost ~]# mdadm /dev/md1 --fail /dev/sda9 mdadm: set /dev/sda9 faulty in /dev/md1 [root@localhost ~]# mdadm --detail /dev/md1 /dev/md1: Version : 0.90 Creation Time : Tue Apr 9 14:50:00 2013 Raid Level : raid1 Array Size : 104320 (101.89 MiB 106.82 MB) Used Dev Size : 104320 (101.89 MiB 106.82 MB) Raid Devices : 2 Total Devices : 2 Preferred Minor : 1 Persistence : Superblock is persistent Update Time : Tue Apr 9 15:47:22 2013 State : clean, degraded Active Devices : 1 Working Devices : 1 Failed Devices : 1 Spare Devices : 0 UUID : 034c85f3:60ce1191:3b61e8dc:55851162 Events : 0.4 Number Major Minor RaidDevice State 0 8 8 0 active sync /dev/sda8 1 0 0 1 removed 2 8 9 - faulty spare /dev/sda9 [root@localhost ~]# ls -l /raid1 total 12 -rw-r--r-- 1 root root 0 Apr 9 15:43 a -rw-r--r-- 1 root root 0 Apr 9 15:43 b -rw-r--r-- 1 root root 0 Apr 9 15:43 c -rw-r--r-- 1 root root 0 Apr 9 15:43 d -rw-r--r-- 1 root root 0 Apr 9 15:43 e -rw-r--r-- 1 root root 0 Apr 9 15:43 f -rw-r--r-- 1 root root 0 Apr 9 15:43 g -rw-r--r-- 1 root root 0 Apr 9 15:43 h -rw-r--r-- 1 root root 0 Apr 9 15:43 i -rw-r--r-- 1 root root 0 Apr 9 15:43 j -rw-r--r-- 1 root root 0 Apr 9 15:43 k -rw-r--r-- 1 root root 0 Apr 9 15:43 l drwx------ 2 root root 12288 Apr 9 15:21 lost+found -rw-r--r-- 1 root root 0 Apr 9 15:43 m -rw-r--r-- 1 root root 0 Apr 9 15:43 n -rw-r--r-- 1 root root 0 Apr 9 15:43 o -rw-r--r-- 1 root root 0 Apr 9 15:43 p -rw-r--r-- 1 root root 0 Apr 9 15:43 q -rw-r--r-- 1 root root 0 Apr 9 15:43 r -rw-r--r-- 1 root root 0 Apr 9 15:43 s -rw-r--r-- 1 root root 0 Apr 9 15:43 t -rw-r--r-- 1 root root 0 Apr 9 15:43 u -rw-r--r-- 1 root root 0 Apr 9 15:43 v -rw-r--r-- 1 root root 0 Apr 9 15:43 w -rw-r--r-- 1 root root 0 Apr 9 15:43 x -rw-r--r-- 1 root root 0 Apr 9 15:43 y -rw-r--r-- 1 root root 0 Apr 9 15:43 z
因此,我们可以清楚地看到,即使删除了一个分区(/dev/sda9),由于镜像,我们的数据仍然是安全的。
现在,如果我们要添加新的分区或者硬盘来代替有故障的备用附件,该怎么办?
插入新的硬盘:
- 首先创建一个新分区。
- 将分区类型更改为RAID类型
- 然后将创建的分区添加到现有团队。
按照本教程开始中显示的完全相同的步骤创建分区并将其类型修改为RAID。
假设新创建的分区是/dev/sda10
现在,使用mdadm命令将此新创建的分区添加到现有的raid中:
[root@localhost ~]# mdadm /dev/md1 --add /dev/sda10 mdadm: added /dev/sda10
因此,新创建的具有raid类型的分区已成功添加到raid1中。
[root@localhost ~]# cat /proc/mdstat Personalities : [raid0] [raid1] md1 : active raid1 sda10[1] sda9[2](F) sda8[0] 104320 blocks [2/2] [UU] md0 : active raid0 sda7[1] sda6[0] 208640 blocks 64k chunks unused devices: <none> [root@localhost ~]#
了解RAID 1的优势:
- 阅读本文时,我们会发现RAID 1技术非常简单。
- 使用RAID 1的最重要优点是,如果磁盘出现故障,则不必重建数据。它们只需复制到替换磁盘即可。
- 与单个磁盘相比,读写速度非常好。
了解RAID 1的缺点
- RAID1最坏的缺点是它只能使用50%的存储设备,因为每个数据必须写入两次。