您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Linux 上配置软件 RAIDConfigure Software RAID on Linux

在 Azure 中的 Linux 虚拟机上使用软件 RAID 将多个附加的数据磁盘呈现为一个单一的 RAID 设备,是一种常见的情形。It's a common scenario to use software RAID on Linux virtual machines in Azure to present multiple attached data disks as a single RAID device. 通常,使用这种方法可以改进性能,而且与只使用单独一块磁盘相比,吞吐量也会有所改进。Typically this can be used to improve performance and allow for improved throughput compared to using just a single disk.

附加数据磁盘Attaching data disks

配置 RAID 设备需要两个或更多的空数据磁盘。Two or more empty data disks are needed to configure a RAID device. 创建 RAID 设备的主要原因是提高磁盘 IO 的性能。The primary reason for creating a RAID device is to improve performance of your disk IO. 根据 IO 需求,可以选择附加存储在标准存储且一个磁盘最多具有 500 IO/ps 的磁盘,或高级存储且一个磁盘最多具有 5000 IO/ps 的磁盘。Based on your IO needs, you can choose to attach disks that are stored in our Standard Storage, with up to 500 IO/ps per disk or our Premium storage with up to 5000 IO/ps per disk. 本文不详细介绍如何为 Linux 虚拟机预配和附加数据磁盘。This article does not go into detail on how to provision and attach data disks to a Linux virtual machine. 请参阅 Microsoft Azure 文章附加磁盘,详细了解如何在 Azure 上为 Linux 虚拟机附加空数据磁盘。See the Microsoft Azure article attach a disk for detailed instructions on how to attach an empty data disk to a Linux virtual machine on Azure.

安装 mdadm 实用程序Install the mdadm utility

  • UbuntuUbuntu

    sudo apt-get update
    sudo apt-get install mdadm
  • CentOS 和 Oracle LinuxCentOS & Oracle Linux

    sudo yum install mdadm
  • SLES 和 openSUSESLES and openSUSE

    zypper install mdadm

创建磁盘分区Create the disk partitions

本示例会在 /dev/sdc 上创建一个单一的磁盘分区。In this example, we create a single disk partition on /dev/sdc. 该新磁盘分区将命名为 /dev/sdc1。The new disk partition will be called /dev/sdc1.

  1. 启动 fdisk,以开始创建分区Start fdisk to begin creating partitions

    sudo fdisk /dev/sdc
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
    Building a new DOS disklabel with disk identifier 0xa34cb70c.
    Changes will remain in memory only, until you decide to write them.
    After that, of course, the previous content won't be recoverable.
    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').
  2. 在提示符下按 n 键,创建分区:Press 'n' at the prompt to create a new partition:

    Command (m for help): n
  3. 接下来,按 p 键,创建分区:Next, press 'p' to create a primary partition:

    Command action
            e   extended
            p   primary partition (1-4)
  4. 按 1 键,以选择分区号 1:Press '1' to select partition number 1:

    Partition number (1-4): 1
  5. 选择新分区的起始点,或者按 <enter> 键接受默认值,将该分区置于驱动器可用空间的开始处:Select the starting point of the new partition, or press <enter> to accept the default to place the partition at the beginning of the free space on the drive:

    First cylinder (1-1305, default 1):
    Using default value 1
  6. 选择分区大小,例如,键入“+10G”以创建一个 10 GB 的分区。Select the size of the partition, for example type '+10G' to create a 10 gigabyte partition. 或者,按 <enter> 键创建一个跨整个驱动器的单一分区:Or, press <enter> create a single partition that spans the entire drive:

    Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 
    Using default value 1305
  7. 接下来,将该分区的 ID 和类型从默认的 ID“83”(Linux) 更改为 ID“fd”(Linux raid auto):Next, change the ID and type of the partition from the default ID '83' (Linux) to ID 'fd' (Linux raid auto):

    Command (m for help): t
    Selected partition 1
    Hex code (type L to list codes): fd
  8. 最后,将分区表写入驱动器并退出 fdisk:Finally, write the partition table to the drive and exit fdisk:

    Command (m for help): w
    The partition table has been altered!

创建 RAID 阵列Create the RAID array

  1. 以下示例将给位于三个单独的数据磁盘(sdc1、sdd1、sde1)上的三个分区设置带区(RAID 级别 0)。The following example will "stripe" (RAID level 0) three partitions located on three separate data disks (sdc1, sdd1, sde1). 运行此命令之后,会创建一个名为 /dev/md127 的新 RAID 设备。After running this command a new RAID device called /dev/md127 is created. 另请注意,如果这些数据磁盘以前属于另一失效的 RAID 阵列,则可能有必要将 --force 参数添加到 mdadm 命令:Also note that if these data disks we previously part of another defunct RAID array it may be necessary to add the --force parameter to the mdadm command:

    sudo mdadm --create /dev/md127 --level 0 --raid-devices 3 \
        /dev/sdc1 /dev/sdd1 /dev/sde1
  2. 在新 RAID 设备上创建文件系统Create the file system on the new RAID device

    a.a. CentOS、Oracle Linux、SLES 12、openSUSE 和 UbuntuCentOS, Oracle Linux, SLES 12, openSUSE, and Ubuntu

    sudo mkfs -t ext4 /dev/md127

    b.b. SLES 11SLES 11

    sudo mkfs -t ext3 /dev/md127

    c.c. SLES 11 - 启用 boot.md 并创建 mdadm.confSLES 11 - enable boot.md and create mdadm.conf

    sudo -i chkconfig --add boot.md
    sudo echo 'DEVICE /dev/sd*[0-9]' >> /etc/mdadm.conf


    在 SUSE 系统中完成这些更改之后,可能需要重新引导。A reboot may be required after making these changes on SUSE systems. 在 SLES 12 中,此步骤是必需的。This step is not required on SLES 12.

将新文件系统添加到 /etc/fstabAdd the new file system to /etc/fstab


错误地编辑 /etc/fstab 文件可能会导致系统无法引导。Improperly editing the /etc/fstab file could result in an unbootable system. 如果没有把握,请参考分发的文档来获取有关如何正确编辑该文件的信息。If unsure, refer to the distribution's documentation for information on how to properly edit this file. 另外,建议在编辑之前创建 /etc/fstab 文件的备份。It is also recommended that a backup of the /etc/fstab file is created before editing.

  1. 为新文件系统创建需要的装入点,例如:Create the desired mount point for your new file system, for example:

    sudo mkdir /data
  2. 在编辑 /etc/fstab 时,UUID 应该用于引用文件系统而不是设备名称。When editing /etc/fstab, the UUID should be used to reference the file system rather than the device name. 使用 blkid 实用程序来确定新文件系统的 UUID:Use the blkid utility to determine the UUID for the new file system:

    sudo /sbin/blkid
    /dev/md127: UUID="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" TYPE="ext4"
  3. 在文本编辑器中打开 /etc/fstab 并为新文件系统添加新条目,例如:Open /etc/fstab in a text editor and add an entry for the new file system, for example:

    UUID=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee  /data  ext4  defaults  0  2

    或在 SLES 11 上:Or on SLES 11:

    /dev/disk/by-uuid/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee  /data  ext3  defaults  0  2

    然后,保存并关闭 /etc/fstab。Then, save and close /etc/fstab.

  4. 测试该 /etc/fstab 条目是否正确:Test that the /etc/fstab entry is correct:

    sudo mount -a

    如果此命令导致错误消息,请检查 /etc/fstab 文件中的语法。If this command results in an error message, please check the syntax in the /etc/fstab file.

    接下来,运行 mount 命令以确保文件系统已装入:Next run the mount command to ensure the file system is mounted:

    /dev/md127 on /data type ext4 (rw)
  5. (可选)故障安全引导参数(Optional) Failsafe Boot Parameters

    fstab 配置fstab configuration

    许多分发版包括 nobootwaitnofail 装入参数,这些参数可以添加到 /etc/fstab 文件中。Many distributions include either the nobootwait or nofail mount parameters that may be added to the /etc/fstab file. 这些参数允许装入某特定文件系统时失败,并且允许 Linux 系统继续引导,即使它无法正确装入 RAID 文件系统也无妨。These parameters allow for failures when mounting a particular file system and allow the Linux system to continue to boot even if it is unable to properly mount the RAID file system. 请参阅分发版文档,了解有关这些参数的详细信息。Refer to your distribution's documentation for more information on these parameters.

    示例 (Ubuntu):Example (Ubuntu):

    UUID=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee  /data  ext4  defaults,nobootwait  0  2

    Linux 引导参数Linux boot parameters

    除了以上参数,还可以使用内核参数“bootdegraded=true”来启用系统引导功能,即使发现 RAID 已损坏或降级(例如,由于无意中从虚拟机中移除了数据驱动器而发现这种情况)也无妨。In addition to the above parameters, the kernel parameter "bootdegraded=true" can allow the system to boot even if the RAID is perceived as damaged or degraded, for example if a data drive is inadvertently removed from the virtual machine. 默认情况下,这样也可能会导致系统无法引导。By default this could also result in a non-bootable system.

    请参阅分发的文档,了解如何正确编辑内核参数。Please refer to your distribution's documentation on how to properly edit kernel parameters. 例如,在许多分发(CentOS、Oracle Linux、SLES 11)中,可以手动将这些参数添加到“/boot/grub/menu.lst”文件。For example, in many distributions (CentOS, Oracle Linux, SLES 11) these parameters may be added manually to the "/boot/grub/menu.lst" file. 在 Ubuntu 上,此参数可添加到“/etc/default/grub”上的 GRUB_CMDLINE_LINUX_DEFAULT 变量中。On Ubuntu this parameter can be added to the GRUB_CMDLINE_LINUX_DEFAULT variable on "/etc/default/grub".


某些 Linux 内核支持 TRIM/UNMAP 操作以放弃磁盘上未使用的块。Some Linux kernels support TRIM/UNMAP operations to discard unused blocks on the disk. 这些操作主要适用于标准存储,以通知 Azure 已删除的页不再有效可以丢弃。These operations are primarily useful in standard storage to inform Azure that deleted pages are no longer valid and can be discarded. 如果创建了较大的文件,然后将其删除,则放弃页可以节省成本。Discarding pages can save cost if you create large files and then delete them.


如果将数组的块区大小设置为小于默认值 (512 KB),则 RAID 可能不会发出丢弃命令。RAID may not issue discard commands if the chunk size for the array is set to less than the default (512KB). 这是因为“主机”上的 unmap 粒度也是 512KB。This is because the unmap granularity on the Host is also 512KB. 如果通过 mdadm 的 --chunk= 参数修改数组的块区大小,则 TRIM/unmap 请求可能被内核忽略。If you modified the array's chunk size via mdadm's --chunk= parameter, then TRIM/unmap requests may be ignored by the kernel.

在 Linux VM 中有两种方法可以启用 TRIM 支持。There are two ways to enable TRIM support in your Linux VM. 与往常一样,有关建议的方法,请参阅分发:As usual, consult your distribution for the recommended approach:

  • /etc/fstab 中使用 discard 装载选项,例如:Use the discard mount option in /etc/fstab, for example:

    UUID=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee  /data  ext4  defaults,discard  0  2
  • 在某些情况下,discard 选项可能会影响性能。In some cases the discard option may have performance implications. 此处,还可以从命令行手动运行 fstrim 命令,或将其添加到 crontab 以定期运行:Alternatively, you can run the fstrim command manually from the command line, or add it to your crontab to run regularly:


    # sudo apt-get install util-linux
    # sudo fstrim /data


    # sudo yum install util-linux
    # sudo fstrim /data