个人工具

“Quick HOWTO : Ch26 : Linux Software RAID/zh”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
检查RAID的状态
检查RAID的状态
 
第499行: 第499行:
 
  [root@bigboy tmp]#
 
  [root@bigboy tmp]#
 
[[Category:服务器]]
 
[[Category:服务器]]
 +
[[Category:Quick_HOWTO]]

2010年7月18日 (日) 16:01的最新版本



简介

使用独立磁盘冗余阵列(RAID)的主要目的是提高磁盘数据处理能力和提供数据冗余。

RAID既能通过操作系统来设置(软件式RAID),也可以在不设置操作系统的情况下通过专用RAID控制卡来实现(硬件式RAID)。这章将向您解释如何在Redhat/Fedora linux下配置软件式RAID结构。

为简洁起见,本章的重点是:在没有/boot或/文件系统的分区上进行RAID设置。

RAID 类型

不管是硬件式或软件式,冗余磁盘阵列RAID能用很多不同的标准来配置,下面我们看看最流行的几种配置方式

线性RAID

在线性RAID中,RAID控制器将RAID集(set)视为一个磁盘链。当前一个磁盘空间被填满后,数据顺序写入磁盘链中的下一个磁盘。 线性RAID的目的是容纳跨磁盘的超大文件系统,而且没有数据冗余性。驱动器故障将损坏您的数据。

Fedora Linux不支持此方式的RAID。

RAID 0

在RAID 0下,RAID控制器试图将数据平均的写入RAID集(set)中的所有磁盘。

将磁盘视为盘子,将数据视为蛋糕。假设你有四个蛋糕-巧克力味、香草味、樱桃味、草莓味-和四个盘子。RAID 0初始操作是将蛋糕切成片,然后将这些片放到每个盘子里。RAID 0 驱动器让操作系统觉得蛋糕是完整的,而且是放到了一个大盘子里。例如,4个9GB的硬盘,配置成RAID 0集的话,操作系统将它们视为一个36GB的硬盘

和线性RAID相似,RAID 0也是为了容纳跨磁盘的超大容量文件系统,并没有数据冗余性。RAID 0的优点是数据访问速度。一个文件分放在四个磁盘上能以四倍于一个磁盘的读取速度读取出来。记住RAID 0常被称为条带集

RAID 0允许磁盘的空间大小不同。当RAID使用完最小磁盘上的条带空间时,它继续使用剩余磁盘的可用空间作为条带集。当这种情况发生时,这部分数据的访问速度将变慢,因为RAID驱动器的数量减少了。所以,RAID 0 最好使用同容量的磁盘。

Fedora Linux支持RAID 0 ,图26.1说明了RAID 0的数据分配过程

RAID 1

在RAID 1方式下,数据被复制放到另一磁盘上。这种RAID方式因此经常被称之为磁盘镜像。想像一下,您将一个同样的故事说给两个人挺,这样的话,如果其中一个人忘记了故事的情节,您可以让另一个来告诉您。

当RAID 1集中的一个磁盘坏了的话,剩下的那个磁盘仍然能够工作。当坏的磁盘被更换后,数据能够自动的从剩下的那个好的磁盘上复制到新磁盘上。在任一主RAID磁盘发生故障时,RAID 1还可以将数据自动的复制到一个热备的空磁盘上。

RAID 1提供了数据冗余性,但没有RAID 0的速度优点。软件式的RAID 1的一个缺点是服务器不得不将数据写入两次以便写入每个镜像磁盘。这回占用数据总线和cpu资源。在硬件式RAID 1下,服务器cpu只需将数据传给RAID磁盘控制器一次,剩下的工作就由磁盘控制器来完成了。这使得在实现RAID 1时,优先选用能提供RAID的磁盘控制器

RAID 1 的一个限制是总的RAID集大小等于磁盘集中容量最小的那块磁盘。这跟RAID 0不同,较大磁盘中剩余部分的磁盘空间会浪费掉。

Fedora Linux支持RAID 1。 图26.1说明了RAID 1方式下的数据分发过程

图26.1 RAID 0和RAID 1 的工作方式

Raid0.gif

RAID 4

RAID 4和RAID 0工作方式一样,但增加了错误纠正或奇偶校验。为达到这个目的,需要再增加一块磁盘

RAID 4需要最少3块磁盘才能组成RAID集,而且它能在其中一个磁盘坏掉的情况下恢复数据。当其中一个磁盘坏掉时,在奇偶校验磁盘的帮助下,数据能够被重建。当坏掉的磁盘被更换后,在奇偶校验磁盘的帮助下丢失的数据在新磁盘中重建

RAID 4既有RAID 0的高访问速度也有RAID 1的数据冗余性。他的最大缺点是数据被条带化(striped)了,但奇偶校验信息没有。换句话说,任何数据在写入RAID集中数据的任何位置时,必须更新奇偶校验磁盘中的数据。奇偶校验磁盘因此成为了瓶颈。因此,RAID 4并不常用。

Fedora Linux不支持RAID 4

RAID 5

RAID 5 改进了RAID 4的工作方式,它将奇偶校验数据条带到RAID集中的所有磁盘上。这就避免了奇偶校验磁盘的瓶颈效应,而保留了RAID 0的高速和RAID 1 的数据冗余性。和RAID 4 一样,RAID 5只能在其中一块磁盘故障时恢复数据。

Fedora Linux支持RAID 5。图26.2说明了RAID 5的数据分放方式

Linux RAID 5需要最少三个磁盘或分区。

图26.2 RAID 5的工作方式

Raid5.gif

前言

专用硬件式RAID磁盘控制器对IDE和SCSI驱动器都是可用的。通常情况下它们有自己的bios,因此在系统开机自检(POST)后你可以手动配置它们。系统可以完全控制硬件式RAID;RAID磁盘控制器来完成所有工作

如果您不能使用硬件式RAID,那么您该了解以下基础知识,按照它们来设置软件式RAID。

IDE驱动器

为了节省开支,许多小公司会使用IDE磁盘,可是IDE磁盘有一些缺点。

  • IDE数据线的总长度只有几英尺长,这样IDE磁盘只能用在小公司
  • IDE磁盘没有交换分区(swap),你不能在系统运行时更换它们
  • 一个IDE控制器上只能接两个IDE设备
  • 当接上第二块IDE设备时,IDE总线的性能会下降
  • IDE总线上一个设备的故障经常导致第二个设备也不能工作。如果你将同一数据线的两个IDE磁盘设置了RAID集的话,这将是致命的。

由于这些原因,当您设置RAID时,我建议您一个控制器上只使用一个IDE磁盘,特别是在公司环境下。在家里或soho办公时,IDE软件式RAID是够用的。

SATA 驱动器

串行ATA类型的驱动器正在迅速的取代IDE或ultra ATA驱动器成为首选的入门级磁盘存储,它有以下优势:

  • 数据线长度能达1m,相比之下,IDE只有18英寸
  • 有更好的错误检测功能
  • 1根数据线只有一个驱动器,并且支持热交换分区(hot swap),即在系统运行时都能更换它,而不用担心它会影响在数据总线上的其它设备。
  • 串行ATA驱动器不用设置跳线来区别主从关系,这使得它更易配置
  • IDE驱动器的数据传输率为133M/s,而串行ATA最初速率是150M/s,最终能够达到600M/s

如果你能买得起更贵但更快的SCSI驱动器,串行ATA设备将是你设置软件式和硬件式RAID的首选。

SCSI驱动器

SCSI磁盘的一些特性使得它们比IDE或串行ATA驱动器更适于设置RAID

  • SCSI控制器具有更强的磁盘容错性能。单个磁盘故障不会引起总线上剩余磁盘故障
  • SCSI数据线可以达到25m长,这使它们非常适合数据中心使用
  • 一条SCSI数据线上可以接上两个以上的设备。它能容纳7个单端SCSI设备或15个其它SCSI类型的设备
  • 一些型号的SCSI设备支持热交换分区(hot swap),这使得您可以在系统运行时更换它们
  • SCSI目前支持640M/s的传输率,这使的它们非常适用于数据传输率要求高的场合

但是SCSI驱动器比IDE驱动器贵很多,这使得它们对家庭用户没有太多吸引力

我应该用软件式RAID分区还是整个磁盘?

磁盘上同时有RAID分区和非RAID分区显然不是个好主意。理由狠明显:磁盘故障将使系统不可用。

如果你决定使用RAID,每个RAID磁盘上的所有分区应该都是RAID集的一部分。为了简化这一问题,许多人将RAID集中的每个磁盘只分一个区。

第一是备份你的系统

软件式RAID产生了一个RAID虚拟磁盘,它由所有形成它的普通分区组成。您必须先格式化这个RAID虚拟磁盘,您的linux才能在上面存放文件。但是格式化操作将会使您失去原磁盘上的旧数据。所以最好先将要设置RAID的分区上的数据备份下来。一个小的错误即有可能意外地损坏您的有效数据。

将RAID配置为单用户模式

由于您会修改系统的磁盘结构,您应该在单用户模式下进行RAID配置。这样系统中的大部分应用程序和网络服务会被关闭,而且其它用户不能访问系统。这会降低配置过程中产生数据损失的风险。

[root@bigboy tmp]# init 1

一旦完成配置,输入exit命令,您的系统会按/etc/inittab文件中的默认启动级别启动。

配置软件RAID

使用Fedora Linux配置RAID需要小心地跟随一些步骤。在这个教程的例子中,你将会使用这样一个系统来配置RAID5,它的硬盘被预先分成三个区。被使用到的分区如下:

/dev/hde1
/dev/hdf2
/dev/hdg1

请确保以下被列出的各步骤能适应你特殊的环境。

RAID 分区

You first need to identify two or more partitions, each on a separate disk. If you are doing RAID 0 or RAID 5, the partitions should be of approximately the same size, as in this scenario. RAID limits the extent of data access on each partition to an area no larger than that of the smallest partition in the RAID set.

首先,你得划定两个或者更多的分区,每个分区在一个独立的磁盘上。如果你组建RAID 0或者RAID 5,每个分区应该大致相等,就像本方案一样。RAID限制数据在每个分区的存取空间不大于RAID集中最小的那个分区

确定可用分区

First use the fdisk -l command to view all the mounted and unmounted filesystems available on your system. You may then also want to use the df -k command, which shows only mounted filesystems but has the big advantage of giving you the mount points too.

首先,使用 fdisk -l 命令查看你系统上所有可用的、已挂载或还没挂载的文件系统。也许你还要使用 df -k 命令来显示已安装的文件系统的挂载点。

These two commands should help you to easily identify the partitions you want to use. Here is some sample output of these commands.

这两条命令可以帮助你轻松的划定你想用的分区。下面是这两条命令的一些输出信息的例子。

[root@bigboy tmp]# fdisk -l

Disk /dev/hda: 12.0 GB, 12072517632 bytes
255 heads, 63 sectors/track, 1467 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1        13    104391   83  Linux
/dev/hda2            14       144   1052257+  83  Linux
/dev/hda3           145       209    522112+  82  Linux swap
/dev/hda4           210      1467  10104885    5  Extended
/dev/hda5           210       655   3582463+  83  Linux
...
...
/dev/hda15         1455      1467    104391   83  Linux
[root@bigboy tmp]#

[root@bigboy tmp]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda2              1035692    163916    819164  17% /
/dev/hda1               101086      8357     87510   9% /boot
/dev/hda15              101086      4127     91740   5% /data1
...
...
...
/dev/hda7              5336664    464228   4601344  10% /var
[root@bigboy tmp]#

卸载分区

You don't want anyone else accessing these partitions while you are creating the RAID set, so you need to make sure they are unmounted.

你不希望其他人在你创建RAID集时使用这些分区,所以你要确定它们已经被卸载。

[root@bigboy tmp]# umount /dev/hde1
[root@bigboy tmp]# umount /dev/hdf2
[root@bigboy tmp]# umount /dev/hdg1


使用FDISK准备分区

You have to change each partition in the RAID set to be of type FD (Linux raid autodetect), and you can do this with fdisk. Here is an example using /dev/hde1.

你需要改变RAID集中每个分区的类型为FD (Linux raid autodetect),你可以使用fdisk完成这些。下面以 /dev/hde1 为例

[root@bigboy tmp]# fdisk /dev/hde
The number of cylinders for this disk is set to 8355. 
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):

使用FDISK的帮助

Now use the fdisk m command to get some help:

现在使用 fdisk m 命令来获取一些帮助信息

Command (m for help): m
  ...
  ...
  p   print the partition table
  q   quit without saving changes
  s   create a new empty Sun disklabel
  t   change a partition's system id
  ...
  ...
Command (m for help):

设置 ID 类型为 FD

Partition /dev/hde1 is the first partition on disk /dev/hde. Modify its type using the t command, and specify the partition number and type code. You also should use the L command to get a full listing of ID types in case you forget.

分区 /dev/hde1 是磁盘 /dev/hde 的第一个分区。使用 t 命令改变它的(文件系统)类型,指定分区号和(文件系统)类型代码。如果你忘记了,你也许需要使用 L 命令获取完整的 ID 类型列表。

Command (m for help): t
Partition number (1-5): 1
Hex code (type L to list codes): L


...
...
...
16  Hidden FAT16    61   SpeedStor       f2  DOS secondary
17  Hidden HPFS/NTF 63  GNU HURD or Sys fd  Linux raid auto
18  AST SmartSleep  64  Novell Netware  fe  LANstep
1b  Hidden Win95 FA 65  Novell Netware  ff  BBT
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help):


确认修改

Use the p command to get the new proposed partition table:

使用 p 命令来获得(打印)新的分区表信息:

Command (m for help): p

Disk /dev/hde: 4311 MB, 4311982080 bytes
16 heads, 63 sectors/track, 8355 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hde1             1      4088   2060320+  fd  Linux raid autodetect
/dev/hde2          4089      5713    819000   83  Linux
/dev/hde4          6608      8355    880992    5  Extended
/dev/hde5          6608      7500    450040+  83  Linux
/dev/hde6          7501      8355    430888+  83  Linux

Command (m for help):


保存修改

Use the w command to permanently save the changes to disk /dev/hde:

使用 w 命令保存对磁盘 /dev/hde 的修改

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@bigboy tmp]#

The error above will occur if any of the other partitions on the disk is mounted.

如果磁盘上的其他分区仍然被挂载着,就会出现上面的错误

为其他分区重复一样的操作

For the sake of brevity, I won't show the process for the other partitions. It's enough to know that the steps for changing the IDs for /dev/hdf2 and /dev/hdg1 are very similar.

为简洁起见,我就不演示其他分区的分区过程。可以清楚的了解到,改变 /dev/hdf2 和 /dev/hdg1 的 IDs 的步骤是十分相似的操作。


组建RAID

Now that the partitions have been prepared, we have to merge them into a new RAID partition that we'll then have to format and mount. Here's how it's done.

既然已经准备好分区了,我们可以把它们合并到一个RAID的分区中,我们将要在其上面格式化并安装(文件系统)。下面将介绍怎么实现的。

创建 RAID 集

You use the mdadm command with the --create option to create the RAID set. In this example we use the --level option to specify RAID 5, and the --raid-devices option to define the number of partitions to use.

使用带 --create 选项参数的 mdadm 命令创建一个RAID 集。在这个例子中,我们使用了 --level 选项来指定RAID 5,--raid-devices 选项定义使用分区的数量。

[root@bigboy tmp]# mdadm --create --verbose /dev/md0 --level=5 \
   --raid-devices=3 /dev/hde1 /dev/hdf2 /dev/hdg1

mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 64K
mdadm: /dev/hde1 appears to contain an ext2fs file system
    size=48160K  mtime=Sat Jan 27 23:11:39 2007
mdadm: /dev/hdf2 appears to contain an ext2fs file system
    size=48160K  mtime=Sat Jan 27 23:11:39 2007
mdadm: /dev/hdg1 appears to contain an ext2fs file system
    size=48160K  mtime=Sat Jan 27 23:11:39 2007
mdadm: size set to 48064K
Continue creating array? y
mdadm: array /dev/md0 started.
[root@bigboy tmp]#

确认 RAID 已经正确初始化

The /proc/mdstat file provides the current status of all RAID devices. Confirm that the initialization is finished by inspecting the file and making sure that there are no initialization related messages. If there are, then wait until there are none.

/proc/mdstat 文件显示所有RAID设备的当前状态。检查这个文件以确认初始化已经完成,确定没有与初始化相关的信息。如果还有,请等待直至没有(与初始化相关的信息)。

[root@bigboy tmp]# cat /proc/mdstat
Personalities : [raid5]
read_ahead 1024 sectors
md0 : active raid5 hdg1[2] hde1[1] hdf2[0]
      4120448 blocks level 5, 32k chunk, algorithm 3 [3/3] [UUU]

unused devices: <none>
[root@bigboy tmp]#

Notice that the new RAID device is called /dev/md0. This information will be required for the next step.

请记住(当前这个)新的RAID设备叫作 /dev/md0。这个信息在下面的步骤中要用到。

格式化 RAID 集

Your new RAID partition now has to be formatted. The mkfs.ext3 command is used to do this.

新的RAID分区现在需要格式化。使用 mkfs.ext3 命令完成这些。

[root@bigboy tmp]# mkfs.ext3 /dev/md0
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
36144 inodes, 144192 blocks
7209 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
18 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 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@bigboy tmp]#

创建 mdadm.conf 配置文件

Your system doesn't automatically remember all the component partitions of your RAID set. This information has to be kept in the mdadm.conf file. The formatting can be tricky, but fortunately the output of the mdadm --detail --scan --verbose command provides you with it. Here we see the output sent to the screen.

系统不会自动记住RAID集中的所有组件和分区。这些信息需要记录在 mdadm.conf 文件中。(文件的)书写格式是需要技巧的,幸运的是可以使用 mdadm --detail --scan --verbose 命令的输出信息来提供。下面我们看到的是(命令)输出到屏幕的信息。

[root@bigboy tmp]# mdadm --detail --scan --verbose
ARRAY /dev/md0 level=raid5 num-devices=4 
UUID=77b695c4:32e5dd46:63dd7d16:17696e09
   devices=/dev/hde1,/dev/hdf2,/dev/hdg1
[root@bigboy tmp]#


Here we export the screen output to create the configuration file.

现在我们导出屏幕的输出信息来创建配置文件。

[root@bigboy tmp]# mdadm --detail --scan --verbose > /etc/mdadm.conf


给RAID集创建一个挂载点

The next step is to create a mount point for /dev/md0. In this case we'll create one called /mnt/raid

下一步为 /dev/md0 创建一个挂载点。在这里我们将创建一个叫作 /mnt/raid 挂载点。

[root@bigboy mnt]# mkdir /mnt/raid

编辑 /etc/fstab 文件

The /etc/fstab file lists all the partitions that need to mount when the system boots. Add an Entry for the RAID set, the /dev/md0 device.

/etc/fstab 文件列举了系统启动时所有需要挂载的分区。为RAID集——/dev/md0 设备添加一项记录。

/dev/md0      /mnt/raid     ext3    defaults    1 2

Do not use labels in the /etc/fstab file for RAID devices; just use the real device name, such as /dev/md0. In older Linux versions, the /etc/rc.d/rc.sysinit script would check the /etc/fstab file for device entries that matched RAID set names listed in the now unused /etc/raidtab configuration file. The script would not automatically start the RAID set driver for the RAID set if it didn't find a match. Device mounting would then occur later on in the boot process. Mounting a RAID device that doesn't have a loaded driver can corrupt your data and produce this error.

在/etc/fstab文件中不要使用标签来记录RAID设备,要使用真实的设备名称,例如/dev/md0。在更老版本的linux中,/etc/rc.d/rc.sysinit 脚本将检查/etc/fstab文件来匹配列在/etc/raidtab(现在已经不使用了)配置文件中的RAID集名称的设备入口。如果找不到匹配项,这个脚本不会自动启动RAID驱动器。随后的启动过程中将挂载设备。挂载一个没有装载驱动的RAID设备将会破坏你的数据并产生一个错误。

Starting up RAID devices: md0(skipped)
Checking filesystems
/raiddata: Superblock has a bad ext3 journal(inode8)
CLEARED.
***journal has been deleted - file system is now ext 2 only***

/raiddata: The filesystem size (according to the superblock) is 2688072 blocks.
The physical size of the device is 8960245 blocks.
Either the superblock or the partition table is likely to be corrupt!
/boot: clean, 41/26104 files, 12755/104391 blocks

/raiddata: UNEXPECTED INCONSISTENCY; Run fsck manually (ie without -a or -p options).

If you are not familiar with the /etc/fstab file use the man fstab command to get a comprehensive explanation of each data column it contains.

如果你不熟悉/etc/fstab文件,使用man fstab命令来获取一份关于它(fstab)包含的每一列数据域的详尽解释。

The /dev/hde1, /dev/hdf2, and /dev/hdg1 partitions were replaced by the combined /dev/md0 partition. You therefore don't want the old partitions to be mounted again. Make sure that all references to them in this file are commented with a # at the beginning of the line or deleted entirely.

/dev/hde1、/dev/hdf2、/dev/hdg1分区被联合分区/dev/md0所取代。你因此不想旧的分区被再次挂载。确保文件中所有与之相关的行的开头以 # 号注释或者全部删掉。

#/dev/hde1       /data1        ext3    defaults        1 2
#/dev/hdf2       /data2        ext3    defaults        1 2
#/dev/hdg1       /data3        ext3    defaults        1 2

安装RAID集

Use the mount command to mount the RAID set. You have your choice of methods:

使用挂载命令来挂载RAID集,你可以使用自己喜欢的方式:

  • The mount command's -a flag causes Linux to mount all the devices in the /etc/fstab file that have automounting enabled (default) and that are also not already mounted.
  • 挂载命令的 -a 标志使linux挂载列举在 /etc/fstab文件里面开启automounting(默认开启)选项并且还没有挂载的所有设备。
[root@bigboy tmp]# mount -a

  • You can also mount the device manually.
  • 你可以手动挂载设备
[root@bigboy tmp]# mount /dev/md0 /mnt/raid


检查RAID的状态

The /proc/mdstat file provides the current status of all the devices.

/proc/mdstat文件提供所有设备的当前状态信息。

[root@bigboy tmp]# raidstart /dev/md0
[root@bigboy tmp]# cat /proc/mdstat
Personalities : [raid5]
read_ahead 1024 sectors
md0 : active raid5 hdg1[2] hde1[1] hdf2[0]
      4120448 blocks level 5, 32k chunk, algorithm 3 [3/3] [UUU]

unused devices: <none>
[root@bigboy tmp]#