在 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. 请参阅 Azure 文章附加磁盘,详细了解如何在 Azure 上为 Linux 虚拟机附加空数据磁盘。See the Azure article attach a disk for detailed instructions on how to attach an empty data disk to a Linux virtual machine on Azure.

重要

请勿混合使用不同大小的磁盘,否则会导致 RAID 集的性能被限制为运行速度最慢磁盘的性能。Do not mix disks of different sizes, doing so would result in performance of the raidset to be limited to that of the slowest disk.

安装 mdadm 实用程序Install the mdadm utility

  • UbuntuUbuntu

    sudo apt-get update
    sudo apt-get install mdadm
    
  • CentOSCentOS

    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

    CentOS、SLES 12、openSUSE 和 UbuntuCentOS, SLES 12, openSUSE, and Ubuntu

    sudo mkfs -t ext4 /dev/md127
    

    SLES 11SLES 11

    sudo mkfs -t ext3 /dev/md127
    

    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:

    mount
    .................
    /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".

TRIM/UNMAP 支持TRIM/UNMAP support

某些 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:

    UbuntuUbuntu

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

    CentOSCentOS

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