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

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

准备阶段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 位于可用性集中,请参阅下一部分。)(If your VMs are in an availability set, see the next section.)

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

    $rgName = "myResourceGroup"
    $vmName = "myVM"
    Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
    
  2. 使用 ConvertTo-AzVMManagedDisk cmdlet 将 VM 转换为托管磁盘。Convert the VM to managed disks by using the ConvertTo-AzVMManagedDisk cmdlet. 以下过程转换之前的 VM,包括 OS 磁盘和任何数据磁盘,并启用虚拟机:The following process converts the previous VM, including the OS disk and any data disks, and starts the Virtual Machine:

    ConvertTo-AzVMManagedDisk -ResourceGroupName $rgName -VMName $vmName
    

在可用性集中转换 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.

  1. 使用 Update-AzAvailabilitySet cmdlet 转换可用性集。Convert the availability set by using the Update-AzAvailabilitySet cmdlet. 以下示例在名为 myResourceGroup 的资源组中更新名为 myAvailabilitySet 的可用性集:The following example updates the availability set named myAvailabilitySet in the resource group named myResourceGroup:

    $rgName = 'myResourceGroup'
    $avSetName = 'myAvailabilitySet'
    
    $avSet = Get-AzAvailabilitySet -ResourceGroupName $rgName -Name $avSetName
    Update-AzAvailabilitySet -AvailabilitySet $avSet -Sku Aligned 
    

    如果可用性集所在的区域只有 2 个托管容错域,但却有 3 个非托管容错域,则此命令会显示类似于“指定的容错域计数 3 必须在 1 到 2 这个范围内”的错误消息。If the region where your availability set is located has only 2 managed fault domains but the number of unmanaged fault domains is 3, this command shows an error similar to "The specified fault domain count 3 must fall in the range 1 to 2." 若要解决此错误,请将容错域更新为 2,并按如下所示将 Sku 更新为 AlignedTo resolve the error, update the fault domain to 2 and update Sku to Aligned as follows:

    $avSet.PlatformFaultDomainCount = 2
    Update-AzAvailabilitySet -AvailabilitySet $avSet -Sku Aligned
    
  2. 解除分配 VM,并转换可用性集中的 VM。Deallocate and convert the VMs in the availability set. 以下脚本使用 Stop-AzVM cmdlet 解除分配每个 VM,使用 ConvertTo-AzVMManagedDisk 进行转换,并在转换进程结束后自动将其重启:The following script deallocates each VM by using the Stop-AzVM cmdlet, converts it by using ConvertTo-AzVMManagedDisk, and restarts it automatically as apart of the conversion process:

    $avSet = Get-AzAvailabilitySet -ResourceGroupName $rgName -Name $avSetName
    
    foreach($vmInfo in $avSet.VirtualMachinesReferences)
    {
     $vm = Get-AzVM -ResourceGroupName $rgName | Where-Object {$_.Id -eq $vmInfo.id}
     Stop-AzVM -ResourceGroupName $rgName -Name $vm.Name -Force
     ConvertTo-AzVMManagedDisk -ResourceGroupName $rgName -VMName $vm.Name
    }
    

故障排除Troubleshooting

如果转换过程中出现错误,或先前转换中的问题导致 VM 处于“失败”状态,请再次运行 ConvertTo-AzVMManagedDisk cmdlet。If there is an error during conversion, or if a VM is in a failed state because of issues in a previous conversion, run the ConvertTo-AzVMManagedDisk cmdlet again. 简单的重试通常可以解决这种情况。A simple retry usually unblocks the situation. 在转换之前,确保所有 VM 扩展都处于“配置成功”状态,否则转换将失败,并出现错误代码 409。Before converting, make sure all the VM extensions are in the 'Provisioning succeeded' state or the conversion will fail with the error code 409.

使用 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

将标准托管磁盘转换为高级托管磁盘Convert standard managed disks to premium

使用快照获取 VM 的只读副本。Take a read-only copy of a VM by using snapshots.