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

在 Azure 中的 Linux VM 上配置 LVMConfigure LVM on a Linux VM in Azure

本文介绍如何在 Azure 虚拟机中配置逻辑卷管理器 (LVM)。This document will discuss how to configure Logical Volume Manager (LVM) in your Azure virtual machine. 尽管可以在任何连接到虚拟机的磁盘上配置 LVM,但默认情况下,大多数云映像不会在 OS 磁盘上配置 LVM。While it is feasible to configure LVM on any disk attached to the virtual machine, by default most cloud images will not have LVM configured on the OS disk. 这是为了防止重复卷组相关的问题,因为 OS 磁盘可能曾经连接到相同分发和类型的 VM(例如在执行恢复方案期间)。This is to prevent problems with duplicate volume groups if the OS disk is ever attached to another VM of the same distribution and type, i.e. during a recovery scenario. 因此建议只在数据磁盘上使用 LVM。Therefore it is recommended only to use LVM on the data disks.

线性与条带化逻辑卷Linear vs. striped logical volumes

LVM 可用于将多个物理磁盘合并成单个存储卷。LVM can be used to combine a number of physical disks into a single storage volume. 默认情况下,LVM 通常会创建线性逻辑卷,这意味着,物理存储是串连在一起的。By default LVM will usually create linear logical volumes, which means that the physical storage is concatenated together. 在此情况下,读取/写入操作通常只发送到单个磁盘。In this case read/write operations will typically only be sent to a single disk. 相比之下,我们也可以创建条带化逻辑卷,其中的读取和写入将分布到卷组(类似于 RAID0)中的多个磁盘。In contrast, we can also create striped logical volumes where reads and writes are distributed to multiple disks contained in the volume group (i.e. similar to RAID0). 出于性能的考虑,你可能希望创建条带化逻辑卷,以便读取和写入操作利用所有附加的数据磁盘。For performance reasons it is likely you will want to stripe your logical volumes so that reads and writes utilize all your attached data disks.

本文档介绍如何将多个数据磁盘合并成单个卷组,然后创建条带化逻辑卷。This document will describe how to combine several data disks into a single volume group, and then create a striped logical volume. 下面是通用化的步骤,适用于大多数分发。The steps below are somewhat generalized to work with most distributions. 在大多数情况下,Azure 上用于管理 LVM 的实用工具和工作流与其他环境中的基本上相同。In most cases the utilities and workflows for managing LVM on Azure are not fundamentally different than other environments. 像往常一样,另请咨询 Linux 供应商配合特定分发使用 LVM 的文档和最佳实践。As usual, please also consult your Linux vendor for documentation and best practices for using LVM with your particular distribution.

附加数据磁盘Attaching data disks

使用 LVM 时,通常一开始用二个或更多的空数据磁盘。One will usually want to start with two or more empty data disks when using LVM. 根据 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 will not go into detail on how to provision and attach data disks to a Linux virtual machine. 请参阅 Microsoft Azure 文章附加磁盘,详细了解如何在 Azure 上为 Linux 虚拟机附加空数据磁盘。Please 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.

安装 LVM 实用工具Install the LVM utilities

  • UbuntuUbuntu

    sudo apt-get update
    sudo apt-get install lvm2
  • RHEL、CentOS 和 Oracle LinuxRHEL, CentOS & Oracle Linux

    sudo yum install lvm2
  • SLES 12 和 openSUSESLES 12 and openSUSE

    sudo zypper install lvm2
  • SLES 11SLES 11

    sudo zypper install lvm2

    在 SLES11 上,还必须编辑 /etc/sysconfig/lvm 并将 LVM_ACTIVATED_ON_DISCOVERED 设置为“enable”:On SLES11 you must also edit /etc/sysconfig/lvm and set LVM_ACTIVATED_ON_DISCOVERED to "enable":


配置 LVMConfigure LVM

本指南假设已附加三个数据磁盘,分别为 /dev/sdc/dev/sdd/dev/sdeIn this guide we will assume you have attached three data disks, which we'll refer to as /dev/sdc, /dev/sdd and /dev/sde. 请注意,VM 中的路径名称不一定与上述相同。Note that these may not always be the same path names in your VM. 可以运行“sudo fdisk -l”或类似命令列出可用磁盘。You can run 'sudo fdisk -l' or similar command to list your available disks.

  1. 准备物理卷:Prepare the physical volumes:

    sudo pvcreate /dev/sd[cde]
    Physical volume "/dev/sdc" successfully created
    Physical volume "/dev/sdd" successfully created
    Physical volume "/dev/sde" successfully created
  2. 创建卷组。Create a volume group. 在本例中,我们将调用卷组 data-vg01In this example we are calling the volume group data-vg01:

    sudo vgcreate data-vg01 /dev/sd[cde]
    Volume group "data-vg01" successfully created
  3. 创建一个或多个逻辑卷。Create the logical volume(s). 以下命令将创建跨整个卷组的名为 data-lv01 的单个逻辑卷,但请注意,在卷组中创建多个逻辑卷也是可行的。The command below we will create a single logical volume called data-lv01 to span the entire volume group, but note that it is also feasible to create multiple logical volumes in the volume group.

    sudo lvcreate --extents 100%FREE --stripes 3 --name data-lv01 data-vg01
    Logical volume "data-lv01" created.
  4. 格式化逻辑卷Format the logical volume

    sudo mkfs -t ext4 /dev/data-vg01/data-lv01


    在 SLES11 上,请使用 -t ext3 而不是 ext4。With SLES11 use -t ext3 instead of ext4. SLES11 仅支持对 ext4 文件系统进行只读访问。SLES11 only supports read-only access to ext4 filesystems.

将新文件系统添加到 /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, please 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. 查找逻辑卷路径Locate the logical volume path

    --- Logical volume ---
    LV Path                /dev/data-vg01/data-lv01
  3. 在文本编辑器中打开 /etc/fstab,并为新文件系统添加条目,例如:Open /etc/fstab in a text editor and add an entry for the new file system, for example:

    /dev/data-vg01/data-lv01  /data  ext4  defaults  0  2

    然后,保存并关闭 /etc/fstabThen, 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/mapper/data--vg01-data--lv01 on /data type ext4 (rw)
  5. (可选)/etc/fstab 中的防故障引导参数(Optional) Failsafe boot parameters in /etc/fstab

    许多分发包括 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. 请参阅分发的相关文档,以了解有关这些参数的详细信息。Please refer to your distribution's documentation for more information on these parameters.

    示例 (Ubuntu):Example (Ubuntu):

    /dev/data-vg01/data-lv01  /data  ext4  defaults,nobootwait  0  2


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

在 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:

    /dev/data-vg01/data-lv01  /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 /datadrive


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