将 Linux 虚拟机从非托管磁盘转换为托管磁盘Convert a Linux virtual machine from unmanaged disks to managed disks

如果有使用非托管磁盘的现有 Linux 虚拟机 (VM),可以将这些 VM 转换为使用 Azure 托管磁盘If you have existing Linux virtual machines (VMs) that use unmanaged disks, you can convert the VMs to use Azure Managed Disks. 此过程会同时转换 OS 磁盘和任何附加的数据磁盘。This process converts both the OS disk and any attached data disks.

本文介绍如何使用 Azure CLI 转换 VM。This article shows you how to convert VMs by using the Azure CLI. 如果需要安装或升级它,请参阅安装 Azure CLIIf you need to install or upgrade it, see Install Azure CLI.

准备阶段Before you begin

  • 该转换需要重启 VM,因此请在预先存在的维护时段内计划 VM 迁移。The conversion requires a restart of the VM, so schedule the migration of your VMs during a pre-existing maintenance window.

  • 转换是不可逆的。The conversion is not reversible.

  • 请注意,任何具有虚拟机参与者角色的用户将不能更改 VM 大小(因为它们可以预转换)。Be aware that any users with the Virtual Machine Contributor role will not be able to change the VM size (as they could pre-conversion). 这是因为包含托管磁盘的 VM 要求用户对 OS 磁盘具有 Microsoft.Compute/disks/write 权限。This is because VMs with managed disks require the user to have the Microsoft.Compute/disks/write permission on the OS disks.

  • 请务必测试转换。Be sure to test the conversion. 在生产环境中执行迁移之前迁移测试性虚拟机。Migrate a test virtual machine before you perform the migration in production.

  • 在转换期间,将解除分配 VM。During the conversion, you deallocate the VM. 转换完成后,VM 在启动时会接收新的 IP 地址。The VM receives a new IP address when it is started after the conversion. 如果需要,可向 VM 分配静态 IP 地址If needed, you can assign a static IP address to the VM.

  • 查看 Azure VM 代理支持转换过程所需的最低版本。Review the minimum version of the Azure VM agent required to support the conversion process. 有关如何检查和更新代理版本的信息,请参阅 Azure 中 VM 代理的最低版本支持For information on how to check and update your agent version, see Minimum version support for VM agents in Azure

  • 不会删除在转换之前由 VM 使用的原始 VHD 和存储帐户。The original VHDs and the storage account used by the VM before conversion are not deleted. 它们会继续产生费用。They continue to incur charges. 若要避免这些项目产生的费用,请在验证转换已完成后删除原始 VHD Blob。To avoid being billed for these artifacts, delete the original VHD blobs after you verify that the conversion is complete. 如果需要找到这些未附加的磁盘以删除它们,请参阅我们的文章查找并删除未附加的 Azure 托管和非托管磁盘If you need to find these unattached disks in order to delete them, see our article Find and delete unattached Azure managed and unmanaged disks.

转换单实例 VMConvert single-instance VMs

本节介绍如何将单实例 Azure VM 从非托管磁盘转换为托管磁盘。This section covers how to convert single-instance Azure VMs from unmanaged disks to managed disks. (如果 VM 位于可用性集中,请参阅下一部分。)可通过此过程,将 VM 从高级 (SSD) 非托管磁盘转换为高级托管磁盘,或从标准 (HDD) 非托管磁盘转换为标准托管磁盘。(If your VMs are in an availability set, see the next section.) You can use this process to convert the VMs from premium (SSD) unmanaged disks to premium managed disks, or from standard (HDD) unmanaged disks to standard managed disks.

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

  1. 使用 az vm deallocate 解除分配 VM。Deallocate the VM by using az vm deallocate. 以下示例在名为 myResourceGroup 的资源组中解除分配名为 myVM 的 VM:The following example deallocates the VM named myVM in the resource group named myResourceGroup:

    az vm deallocate --resource-group myResourceGroup --name myVM
    
  2. 使用 az vm convert 将 VM 转换为托管磁盘。Convert the VM to managed disks by using az vm convert. 以下过程转换名为 myVM 的 VM,包括 OS 磁盘和任何数据磁盘:The following process converts the VM named myVM, including the OS disk and any data disks:

    az vm convert --resource-group myResourceGroup --name myVM
    
  3. 使用 az vm start 在转换为托管磁盘后启动 VM。Start the VM after the conversion to managed disks by using az vm start. 以下示例启动名为 myResourceGroup 的资源组中名为 myVM 的 VM。The following example starts the VM named myVM in the resource group named myResourceGroup.

    az vm start --resource-group myResourceGroup --name myVM
    

转换可用性集中的 VMConvert VMs in an availability set

如果要转换为托管磁盘的 VM 位于可用性集中,则需要先将可用性集转换为托管可用性集。If the VMs that you want to convert to managed disks are in an availability set, you first need to convert the availability set to a managed availability set.

可用性集中的所有 VM 都必须在转换可用性集之前解除分配。All VMs in the availability set must be deallocated before you convert the availability set. 可用性集本身转换为托管可用性集后,计划将所有 VM 转换为托管磁盘。Plan to convert all VMs to managed disks after the availability set itself has been converted to a managed availability set. 然后,启动所有 VM,并继续照常操作。Then, start all the VMs and continue operating as normal.

  1. 使用 az vm availability-set list 列出可用性集中的所有 VM。List all VMs in an availability set by using az vm availability-set list. 以下示例列出了名为 myResourceGroup 的资源组中名为 myAvailabilitySet 的可用性集中的所有 VM:The following example lists all VMs in the availability set named myAvailabilitySet in the resource group named myResourceGroup:

    az vm availability-set show \
        --resource-group myResourceGroup \
        --name myAvailabilitySet \
        --query [virtualMachines[*].id] \
        --output table
    
  2. 使用 az vm deallocate 解除分配所有 VM。Deallocate all the VMs by using az vm deallocate. 以下示例在名为 myResourceGroup 的资源组中解除分配名为 myVM 的 VM:The following example deallocates the VM named myVM in the resource group named myResourceGroup:

    az vm deallocate --resource-group myResourceGroup --name myVM
    
  3. 使用 az vm availability-set convert 转换可用性集。Convert the availability set by using az vm availability-set convert. 以下示例转换名为 myResourceGroup 的资源组中名为 myAvailabilitySet 的可用性集:The following example converts the availability set named myAvailabilitySet in the resource group named myResourceGroup:

    az vm availability-set convert \
        --resource-group myResourceGroup \
        --name myAvailabilitySet
    
  4. 使用 az vm convert 将所有 VM 转换为托管磁盘。Convert all the VMs to managed disks by using az vm convert. 以下过程转换名为 myVM 的 VM,包括 OS 磁盘和任何数据磁盘:The following process converts the VM named myVM, including the OS disk and any data disks:

    az vm convert --resource-group myResourceGroup --name myVM
    
  5. 使用 az vm start 在转换为托管磁盘后启动所有 VM。Start all the VMs after the conversion to managed disks by using az vm start. 以下示例在名为 myResourceGroup 的资源组中启动名为 myVM 的 VM:The following example starts the VM named myVM in the resource group named myResourceGroup:

    az vm start --resource-group myResourceGroup --name myVM
    

使用 Azure 门户进行转换Convert using the Azure portal

还可以使用 Azure 门户将非托管磁盘转换为托管磁盘。You can also convert unmanaged disks to managed disks using the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 从门户的 VM 列表中选择 VM。Select the VM from the list of VMs in the portal.
  3. 在 VM 的边栏选项卡中,从菜单中选择“磁盘”。In the blade for the VM, select Disks from the menu.
  4. 在“磁盘”边栏选项卡的顶部,选择“迁移到托管磁盘”。At the top of the Disks blade, select Migrate to managed disks.
  5. 如果 VM 位于可用性集中,则“迁移到托管磁盘”边栏选项卡上会出现“首先需要转换可用性集”的警告。If your VM is in an availability set, there will be a warning on the Migrate to managed disks blade that you need to convert the availability set first. 此警告应该有一个链接,单击该链接即可转换可用性集。The warning should have a link you can click to convert the availability set. 转换可用性集后,或者如果 VM 不在可用性集中,请单击“迁移”以启动将磁盘迁移到托管磁盘的过程。Once the availability set is converted or if your VM is not in an availability set, click Migrate to start the process of migrating your disks to managed disks.

VM 将会停止并在完成迁移后重新启动。The VM will be stopped and restarted after migration is complete.

后续步骤Next steps

有关存储选项的详细信息,请参阅 Azure 托管磁盘概述For more information about storage options, see Azure Managed Disks overview.