如何重设使用 Azure 磁盘加密的逻辑卷管理设备的大小

适用于:✔️ Linux VM ✔️ 灵活规模集

本文介绍如何重设使用 Azure 磁盘加密的数据磁盘的大小。 若要重设磁盘大小,在 Linux 上使用逻辑卷管理 (LVM)。 这些步骤适用于多种场景。

你可以在以下环境中使用此重设大小过程:

  • Linux 发行版:
    • CentOS 7 或更高版本
    • Ubuntu 18.04 或更高版本
    • SUSE 12 或更高版本
  • Azure 磁盘加密版本:
    • 单通道扩展
    • 双通道扩展

先决条件

本文假设用户具备以下条件:

方案

本文中的过程适用于以下场景:

  • 传统的 LVM 和 LVM-on-crypt 配置
  • 传统的 LVM 加密
  • LVM-on-crypt

传统的 LVM 和 LVM-on-crypt 配置

当卷组 (VG) 有可用空间时,传统的 LVM 和 LVM-on-crypt 配置扩展逻辑卷 (LV)。

传统的 LVM 加密

在传统的 LVM 加密中,LV 已加密。 未加密整个磁盘。

通过使用传统的 LVM 加密,你可以:

  • 在添加新的物理卷 (PV) 时扩展 LV。
  • 在重设现有 PV 的大小时,扩展 LV。

LVM-on-crypt

推荐的磁盘加密方法是 LVM-on-encrypt。 该方法加密整个磁盘,而不仅仅是 LV。

通过使用 LVM-on-crypt,你可以:

  • 在添加新的 PV 时扩展 LV。
  • 在重设现有 PV 的大小时,扩展 LV。

注意

建议不要在同一个 VM 上混合使用传统的 LVM 加密和 LVM-on-crypt。

以下部分提供了如何使用 LVM 和 LVM-on-crypt 的示例。 这些示例使用磁盘、PV、VG、LV、文件系统、全局唯一标识符 (UUID) 和装入点的预先存在的值。 将这些值替换为自己的值,使其适合你的环境。

当 VG 有可用空间时扩展 LV

重设 LV 大小的传统方法是在 VG 有可用空间时扩展 LV。 可以将此方法用于非加密磁盘、传统的 LVM 加密卷和 LVM-on-crypt 配置。

  1. 验证要增大的文件系统的当前大小:

    df -h /mountpoint
    

    Screenshot showing code that checks the size of the file system. The command and the result are highlighted.

  2. 验证 VG 是否有足够的空间来增大 LV:

    sudo vgs
    

    Screenshot showing the code that checks for space on the VG. The command and the result are highlighted.

    也可使用 vgdisplay

    sudo vgdisplay vgname
    

    Screenshot showing the V G display code that checks for space on the VG. The command and the result are highlighted.

  3. 确定需要重设大小的 LV:

    sudo lsblk
    

    Screenshot showing the result of the l s b l k command. The command and the result are highlighted.

    对于 LVM-on-crypt,不同之处在于此输出显示加密层处于磁盘级别。

    Screenshot showing the result of the l s b l k command. The output is highlighted. It shows the encrypted layer.

  4. 检查 LV 大小:

    sudo lvdisplay lvname
    

    Screenshot showing the code that checks the logical volume size. The command and the result are highlighted.

  5. 通过使用 -r 重设在线文件系统的大小来增加 LV 大小:

    sudo lvextend -r -L +2G /dev/vgname/lvname
    

    Screenshot showing the code that increases the size of the logical volume. The command and the results are highlighted.

  6. 验证 LV 和文件系统的新大小:

    df -h /mountpoint
    

    Screenshot showing the code that verifies the size of the LV and the file system. The command and the result are highlighted.

    大小输出指示已成功重设 LV 和文件系统的大小。

可以再次检查 LV 信息以确认 LV 级别的更改:

sudo lvdisplay lvname

Screenshot showing the code that confirms the new sizes. The sizes are highlighted.

通过添加新的 PV 来扩展传统的 LVM 卷

当你需要添加一个新的磁盘来增加 VG 的大小时,可以通过添加一个新的 PV 来扩展传统的 LVM 卷。

  1. 验证要增大的文件系统的当前大小:

    df -h /mountpoint
    

    Screenshot showing the code that checks the current size of a file system. The command and the result are highlighted.

  2. 验证当前 PV 配置:

    sudo pvs
    

    Screenshot showing the code that checks the current PV configuration. The command and the result are highlighted.

  3. 查看当前的 VG 信息:

    sudo vgs
    

    Screenshot showing the code that checks the current volume group information. The command and the result are highlighted.

  4. 检查当前磁盘列表。 通过检查 /dev/disk/azure/scsi1/ 中的设备来标识数据磁盘。

    sudo ls -l /dev/disk/azure/scsi1/
    

    Screenshot showing the code that checks the current disk list. The command and the results are highlighted.

  5. 查看 lsblk 的输出:

    sudo lsbk
    

    Screenshot showing the code that checks the output of l s b l k. The command and the results are highlighted.

  6. 按照将数据磁盘附加到 Linux VM 中的说明,将新磁盘附加到 VM。

  7. 检查磁盘列表,并注意新磁盘。

    sudo ls -l /dev/disk/azure/scsi1/
    

    Screenshot showing the code that checks the disk list. The results are highlighted.

    sudo lsblk
    

    Screenshot showing the code that checks the disk list by using l s b l k. The command and the result are highlighted.

  8. 在新的数据磁盘上创建一个新 PV:

    sudo pvcreate /dev/newdisk
    

    Screenshot showing the code that creates a new PV. The result is highlighted.

    此方法使用整个磁盘作为没有分区的 PV。 或者,你可以使用 fdisk 创建一个分区,然后将该分区用于 pvcreate

  9. 验证 PV 是否已添加到 PV 列表中:

    sudo pvs
    

    Screenshot showing the code that shows the physical volume list. The result is highlighted.

  10. 通过向 VG 添加新的 PV 来扩展该 VG:

    sudo vgextend vgname /dev/newdisk
    

    Screenshot showing the code that extends the volume group. The result is highlighted.

  11. 检查新的 VG 大小:

    sudo vgs
    

    Screenshot showing the code that checks the volume group size. The results are highlighted.

  12. 使用 lsblk 标识需要重设大小的 LV:

    sudo lsblk
    

    Screenshot showing the code that identifies the local volume that needs to be resized. The results are highlighted.

  13. 通过使用 -r 增大在线文件系统来扩展 LV 大小:

    sudo lvextend -r -L +2G /dev/vgname/lvname
    

    Screenshot showing code that increases the size of the file system online. The results are highlighted.

  14. 验证 LV 和文件系统的新大小:

    df -h /mountpoint
    

    Screenshot showing the code that checks the sizes of the local volume and the file system. The command and the result are highlighted.

    重要

    对传统的 LVM 配置使用 Azure 数据加密时,加密层在 LV 级别(而不是磁盘级别)进行创建。

    此时,加密层已扩展到新磁盘。 实际的数据磁盘在平台级别没有加密设置,因此其加密状态不会更新。

    这些是推荐使用 LVM-on-crypt 方法的部分原因。

  15. 检查门户中的加密信息:

    Screenshot showing encryption information in the portal. The disk name and the encryption are highlighted.

    若要更新磁盘上的加密设置,请添加一个新的 LV 并在 VM 上启用扩展。

  16. 添加新的 LV,在其上创建一个文件系统,并将其添加到 /etc/fstab

  17. 再次设置加密扩展。 这次,在平台级别的新数据磁盘上标记加密设置。 下面是 CLI 示例:

    az vm encryption enable -g ${RGNAME} --name ${VMNAME} --disk-encryption-keyvault "<your-unique-keyvault-name>"
    
  18. 检查门户中的加密信息:

    Screenshot showing encryption information in the portal. The disk name and the encryption information are highlighted.

更新加密设置后,可以删除新的 LV。 同时从你创建的 /etc/fstab/etc/crypttab 中删除条目。

Screenshot showing the code that deletes the new logical volume. The deleted F S tab and crypt tab are highlighted.

请按照以下步骤完成清理:

  1. 卸载 LV:

    sudo umount /mountpoint
    
  2. 关闭卷的加密层:

    sudo cryptsetup luksClose /dev/vgname/lvname
    
  3. 删除 LV:

    sudo lvremove /dev/vgname/lvname
    

通过重设现有 PV 的大小来扩展传统的 LVM 卷

在某些情况下,你的限制可能需要你重设现有磁盘的大小。 方法如下:

  1. 标识加密的磁盘:

    sudo ls -l /dev/disk/azure/scsi1/
    

    Screenshot showing the code that identifies encrypted disks. The results are highlighted.

    sudo lsblk -fs
    

    Screenshot showing alternative code that identifies encrypted disks. The results are highlighted.

  2. 检查 PV 信息:

    sudo pvs
    

    Screenshot showing the code that checks information about the physical volume. The results are highlighted.

    图中的结果显示,所有 PV 上的所有空间当前都已使用。

  3. 检查 VG 信息:

    sudo vgs
    sudo vgdisplay -v vgname
    

    Screenshot showing the code that checks information about the volume group. The results are highlighted.

  4. 检查磁盘大小。 可以使用 fdisklsblk 列出驱动器大小。

    for disk in `sudo ls -l /dev/disk/azure/scsi1/* | awk -F/ '{print $NF}'` ; do echo "sudo fdisk -l /dev/${disk} | grep ^Disk "; done | bash
    
    sudo lsblk -o "NAME,SIZE"
    

    Screenshot showing the code that checks disk sizes. The results are highlighted.

    此处,我们通过使用 lsblk -fs 来标识哪些 PV 与哪些 LV 相关联。 可以通过运行 lvdisplay 来标识关联。

    sudo lvdisplay --maps VG/LV
    sudo lvdisplay --maps datavg/datalv1
    

    Screenshot showing an alternative way to identify physical volume associations with local volumes. The results are highlighted.

    在这种情况下,所有四个数据驱动器都属于同一个 VG 和单个 LV。 配置可能有所不同。

  5. 检查当前文件系统使用率:

    df -h /datalvm*
    

    Screenshot showing the code that checks file system utilization. The command and the results are highlighted.

  6. 按照展开 Azure 托管磁盘中的说明重设数据磁盘的大小。 可以使用门户、CLI 或 PowerShell。

    重要

    VM 正在运行时,无法重设虚拟磁盘的大小。 在此步骤中解除分配 VM。

  7. 启动 VM,并使用 fdisk 检查新的大小。

    for disk in `sudo ls -l /dev/disk/azure/scsi1/* | awk -F/ '{print $NF}'` ; do echo "sudo fdisk -l /dev/${disk} | grep ^Disk "; done | bash
    
    sudo lsblk -o "NAME,SIZE"
    

    Screenshot showing the code that checks disk size. The result is highlighted.

    在这种情况下,/dev/sdd 的大小从 5 G 调整到 20 G。

  8. 检查当前 PV 大小:

    sudo pvdisplay /dev/resizeddisk
    

    Screenshot showing the code that checks the size of the P V. The result is highlighted.

    即使磁盘已经重设大小,PV 仍然保持以前的大小。

  9. 重设 PV 大小:

    sudo pvresize /dev/resizeddisk
    

    Screenshot showing the code that resizes the physical volume. The result is highlighted.

  10. 检查 PV 大小:

    sudo pvdisplay /dev/resizeddisk
    

    Screenshot showing the code that checks the physical volume's size. The result is highlighted.

    对所有要重设大小的磁盘应用相同的过程。

  11. 检查 VG 信息。

    sudo vgdisplay vgname
    

    Screenshot showing the code that checks information for the volume group. The result is highlighted.

    VG 现在有足够的空间分配给 LV。

  12. 重设 LV 大小:

    sudo lvresize -r -L +5G vgname/lvname
    sudo lvresize -r -l +100%FREE /dev/datavg/datalv01
    

    Screenshot showing the code that resizes the L V. The results are highlighted.

  13. 检查文件系统的大小:

    df -h /datalvm2
    

    Screenshot showing the code that checks the size of the file system. The result is highlighted.

通过添加新的 PV 来扩展 LVM-on-crypt 卷

你也可以通过添加新的 PV 来扩展 LVM-on-crypt 卷。 此方法严格遵循在加密设备上配置 LVM 和 RAID 的步骤。 请参阅介绍如何添加新磁盘并在 LVM-on-crypt 配置中设置它的部分。

可以使用此方法向现有 LV 添加空间。 也可以创建新的 VG 或 LV。

  1. 验证 VG 的当前大小:

    sudo vgdisplay vgname
    

    Screenshot showing the code that checks the volume group size. Results are highlighted.

  2. 验证要扩展的文件系统和 LV 的大小:

    sudo lvdisplay /dev/vgname/lvname
    

    Screenshot showing the code that checks the size of the local volume. Results are highlighted.

    df -h mountpoint
    

    Screenshot showing the code that checks the file system's size. The result is highlighted.

  3. 向 VM 添加新的数据磁盘并标识它。

    添加新磁盘之前,请检查磁盘:

    sudo fdisk -l | egrep ^"Disk /"
    

    Screenshot showing the code that checks the size of the disks. The result is highlighted.

    下面是另一种在添加新磁盘之前检查磁盘的方法:

    sudo lsblk
    

    Screenshot showing an alternative code that checks the size of the disks. The results are highlighted.

    若要添加新磁盘,可以使用 PowerShell、Azure CLI 或 Azure 门户。 有关详细信息,请参阅将数据磁盘附加到 Linux VM

    内核名称方案适用于新添加的设备。 通常向新驱动器分配下一个可用的驱动器号。 在本例中,添加的磁盘是 sdd

  4. 检查磁盘以确保已添加新磁盘:

    sudo fdisk -l | egrep ^"Disk /"
    

    Screenshot showing the code that lists the disks. The results are highlighted.

    sudo lsblk
    

    Screenshot showing the newly added disk in the output.

  5. 在最近添加的磁盘上创建一个文件系统。 将磁盘与 /dev/disk/azure/scsi1/ 上的链接设备匹配。

    sudo ls -la /dev/disk/azure/scsi1/
    

    Screenshot showing the code that creates a file system. The results are highlighted.

    sudo mkfs.ext4 /dev/disk/azure/scsi1/${disk}
    

    Screenshot showing additional code that creates a file system and matches the disk to the linked devices. The results are highlighted.

  6. 为新添加的磁盘创建临时装入点:

    newmount=/data4
    sudo mkdir ${newmount}
    
  7. 将最近创建的文件系统添加到 /etc/fstab

    sudo blkid /dev/disk/azure/scsi1/lun4| awk -F\" '{print "UUID="$2" '${newmount}' "$4" defaults,nofail 0 0"}' >> /etc/fstab
    
  8. 装载新创建的文件系统:

    sudo mount -a
    
  9. 验证是否已装载新文件系统:

    df -h
    

    Screenshot showing the code that verifies that the file system is mounted. The result is highlighted.

    sudo lsblk
    

    Screenshot showing additional code that verifies that the file system is mounted. The result is highlighted.

  10. 重启以前为数据驱动器启动的加密。

    提示

    对于 LVM-on-crypt,建议使用 EncryptFormatAll。 否则,你可能会在设置其他磁盘时看到双重加密。

    有关详细信息,请参阅在加密设备上配置 LVM 和 RAID

    下面是一个示例:

    az vm encryption enable \
    --resource-group ${RGNAME} \
    --name ${VMNAME} \
    --disk-encryption-keyvault ${KEYVAULTNAME} \
    --key-encryption-key ${KEYNAME} \
    --key-encryption-keyvault ${KEYVAULTNAME} \
    --volume-type "DATA" \
    --encrypt-format-all \
    -o table
    

    加密完成后,你会在新添加的磁盘上看到一个加密层:

    sudo lsblk
    

    Screenshot showing the code that checks the crypt layer. The result is highlighted.

  11. 卸载新磁盘的加密层:

    sudo umount ${newmount}
    
  12. 查看当前的 PV 信息:

    sudo pvs
    

    Screenshot showing the code that checks information about the physical volume. The result is highlighted.

  13. 在磁盘的加密层上创建一个 PV。 从前面的 lsblk 命令获取设备名。 在设备名称前面添加 /dev/ 映射程序以创建 PV:

    sudo pvcreate /dev/mapper/mapperdevicename
    

    Screenshot showing the code that creates a physical volume on the encrypted layer. The results are highlighted.

    将显示一条关于擦除当前 ext4 fs 签名的警告。 此警告是在意料之内。 使用 y 回答此问题。

  14. 验证新 PV 是否已添加到 LVM 配置中:

    sudo pvs
    

    Screenshot showing the code that verifies that the physical volume was added to the LVM configuration. The result is highlighted.

  15. 将新的 PV 添加到需要增大的 VG 中。

    sudo vgextend vgname /dev/mapper/nameofhenewpv
    

    Screenshot showing the code that adds a physical volume to a volume group. The results are highlighted.

  16. 验证 VG 的新大小和可用空间:

    sudo vgdisplay vgname
    

    Screenshot showing the code that verifies the size and free space of the volume group. The results are highlighted.

    注意 Total PE 计数和 Free PE / Size 的增加。

  17. 增加 LV 和文件系统的大小。 在 lvextend 上使用 -r 选项。 在本例中,我们将把 VG 中的总可用空间添加到给定的 LV 中。

    sudo lvextend -r -l +100%FREE /dev/vgname/lvname
    

    Screenshot showing the code that increases the size of the local volume and the file system. The results are highlighted.

按照以下步骤验证更改。

  1. 验证 LV 的大小:

    sudo lvdisplay /dev/vgname/lvname
    

    Screenshot showing the code that verifies the new size of the local volume. The results are highlighted.

  2. 验证文件系统的新大小:

    df -h /mountpoint
    

    Screenshot showing the code that verifies the new size of the file system. The result is highlighted.

  3. 验证 LVM 层是否位于加密层之上:

    sudo lsblk
    

    Screenshot showing the code that verifies that the LVM layer is on top of the encrypted layer. The result is highlighted.

    如果你使用不带选项的 lsblk,则会多次看到装入点。 命令按设备和 LV 进行排序。

    你可能需要使用 lsblk -fs。 在此命令中,-fs 反转排序顺序,使装入点只显示一次。 磁盘显示多次。

    sudo lsblk -fs
    

    Screenshot showing alternative code that verifies that the LVM layer is on top of the encrypted layer. The result is highlighted.

通过重设现有 PV 的大小来扩展 LVM-on-crypt 卷

  1. 标识加密的磁盘:

    sudo lsblk
    

    Screenshot showing the code that identifies the encrypted disks. The results are highlighted.

    sudo lsblk -s
    

    Screenshot showing alternative code that identifies the encrypted disks. The results are highlighted.

  2. 检查 PV 信息:

    sudo pvs
    

    Screenshot showing the code that checks information for physical volumes. The results are highlighted.

  3. 检查 VG 信息:

    sudo vgs
    

    Screenshot showing the code that checks information for volume groups. The results are highlighted.

  4. 检查 LV 信息:

    sudo lvs
    

    Screenshot showing the code that checks information for the local volume. The result is highlighted.

  5. 检查文件系统使用率:

    df -h /mountpoint(s)
    

    Screenshot showing the code that checks how much of the file system is being used. The results are highlighted.

  6. 检查磁盘的大小:

    sudo fdisk
    sudo fdisk -l | egrep ^"Disk /"
    sudo lsblk
    

    Screenshot showing the code that checks the size of disks. The results are highlighted.

  7. 重设数据磁盘的大小。 可以使用门户、CLI 或 PowerShell。 有关详细信息,请参阅扩展 Linux VM 上的虚拟硬盘中的重设磁盘大小部分。

    重要

    VM 正在运行时,无法重设虚拟磁盘的大小。 在此步骤中解除分配 VM。

  8. 检查磁盘大小:

    sudo fdisk
    sudo fdisk -l | egrep ^"Disk /"
    sudo lsblk
    

    Screenshot showing code that checks disk sizes. The results are highlighted.

    在这种情况下,两个磁盘的大小都从 2 GB 调整到 4 GB。 但是文件系统、LV 和 PV 的大小保持不变。

  9. 检查当前 PV 大小。 请记住,在 LVM-on-crypt 上,PV 是 /dev/mapper/ 设备,而不是 /dev/sd* 设备。

    sudo pvdisplay /dev/mapper/devicemappername
    

    Screenshot showing the code that checks the size of the current physical volume. The results are highlighted.

  10. 重设 PV 大小:

    sudo pvresize /dev/mapper/devicemappername
    

    Screenshot showing the code that resizes the physical volume. The results are highlighted.

  11. 检查新的 PV 大小:

    sudo pvdisplay /dev/mapper/devicemappername
    

    Screenshot showing the code that checks the size of the physical volume. The results are highlighted.

  12. 重设 PV 上加密层的大小:

    sudo cryptsetup resize /dev/mapper/devicemappername
    

    对所有要重设大小的磁盘应用相同的过程。

  13. 检查 VG 信息:

    sudo vgdisplay vgname
    

    Screenshot showing the code that checks information for the volume group. The results are highlighted.

    VG 现在有足够的空间分配给 LV。

  14. 检查 LV 信息:

    sudo lvdisplay vgname/lvname
    

    Screenshot showing the code that checks information for the local volume. The results are highlighted.

  15. 检查文件系统使用率:

    df -h /mountpoint
    

    Screenshot showing the code that checks utilization of the file system. The results are highlighted.

  16. 重设 LV 大小:

    sudo lvresize -r -L +2G /dev/vgname/lvname
    

    Screenshot showing the code that resizes the local volume. The results are highlighted.

    此处,我们也使用 -r 选项来重设文件系统的大小。

  17. 检查 LV 信息:

    sudo lvdisplay vgname/lvname
    

    Screenshot showing the code that gets information about the local volume. The results are highlighted.

  18. 检查文件系统使用率:

    df -h /mountpoint
    

    Screenshot showing the code that checks the file system utilization. The results are highlighted.

对其他任何需要重设大小的 LV 应用相同的重设大小过程。

后续步骤

对 Azure 磁盘加密进行故障排除