Migrate a Windows virtual machine from unmanaged disks to managed disks

Applies to: ✔️ Windows VMs

If you have existing Windows virtual machines (VMs) that use unmanaged disks, you can migrate the VMs to use managed disks through the Azure Managed Disks service. This process converts both the operating system (OS) disk and any attached data disks.

Before you begin

  • The migration will restart the VM, so schedule the migration of your VMs during a pre-existing maintenance window.

  • The migration isn't reversible.

  • Any users with the Virtual Machine Contributor role won't be able to change the VM size (as they could pre-migration). 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 migration. Migrate a test virtual machine before you perform the migration in production.

  • During the migration, you deallocate the VM. The VM receives a new IP address when it's started after the migration. If needed, you can assign a static IP address to the VM.

  • Review the minimum version of the Azure VM agent required to support the migration process. For information on how to check and update your agent version, see Minimum version support for VM agents in Azure

  • The original VHDs and the storage account used by the VM before migration are not deleted. They continue to incur charges. To avoid being billed for these artifacts, delete the original VHD blobs after you verify that the migration is complete. 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.

Migrate single-instance VMs

This section covers how to migrate single-instance Azure VMs from unmanaged disks to managed disks. (If your VMs are in an availability set, see the next section.)

  1. Deallocate the VM by using the Stop-AzVM cmdlet. 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. Migrate the VM to managed disks by using the ConvertTo-AzVMManagedDisk cmdlet. 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
    

Migrate VMs in an availability set

If the VMs that you want to migrate to managed disks are in an availability set, you first need to migrate the availability set to a managed availability set.

  1. Migrate the availability set by using the Update-AzAvailabilitySet cmdlet. 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 
    

    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." To 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. Deallocate and migrate the VMs in the availability set. 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 migration 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

  • Before converting, make sure all the VM extensions are in the 'Provisioning succeeded' state or the migration will fail with the error code 409.
  • If there is an error during migration, or if a VM is in a failed state because of issues in a previous migration, run the ConvertTo-AzVMManagedDisk cmdlet again. A simple retry usually unblocks the situation.
  • If you are converting a Linux VM to managed disks, use the latest version of the Azure Linux Agent. Operations using Azure Linux Agent versions '2.2.0' and earlier will likely fail. Running the migration on a generalized VM or a VM that belongs to a classic availability set is also not supported.
  • If the migration fails with the "SnapshotCountExceeded" error, delete some snapshots and attempt the operation again.

Migrate using the Azure portal

You can also migrate unmanaged disks to managed disks using the Azure portal.

  1. Sign in to the Azure portal.
  2. Select the VM from the list of VMs in the portal.
  3. 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. If your VM is in an availability set, there will be a warning on the Migrate to managed disks blade that you need to migrate the availability set first. The warning should have a link you can click to migrate the availability set. 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.

The VM will be stopped and restarted after migration is complete.

Next steps

Change the disk type of an Azure managed disk.

Take a read-only copy of a VM by using snapshots.