Convert a Windows virtual machine from unmanaged disks to managed disks

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. This process converts both the OS disk and any attached data disks.

This article shows you how to convert VMs by using Azure PowerShell. If you need to install or upgrade it, see Install and configure Azure PowerShell.

Before you begin

  • If the unmanaged disk is in a storage account previously encrypted through Azure Storage Service Encryption, you can't convert it to a managed disk. For steps to copy and use these virtual hard disks (VHDs) in managed disks, see the Managed disks and Azure Storage Service Encryption section later in this article.

  • 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.

  • Be sure to test the conversion. Migrate a test virtual machine before you perform the migration in production.

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

  • The original VHDs and the storage account used by the VM before conversion 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 conversion is complete.

Convert single-instance VMs

This section covers how to convert 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-AzureRmVM cmdlet. The following example deallocates the VM named myVM in the resource group named myResourceGroup:

    $rgName = "myResourceGroup"
    $vmName = "myVM"
    Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force
    
  2. Convert the VM to managed disks by using the ConvertTo-AzureRmVMManagedDisk cmdlet. The following process converts the previous VM, including the OS disk and any data disks:

    ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $rgName -VMName $vmName
    
  3. Start the VM after the conversion to managed disks by using Start-AzureRmVM. The following example restarts the previous VM:

    Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName
    

Convert VMs in an availability set

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. Convert the availability set by using the Update-AzureRmAvailabilitySet cmdlet. The following example updates the availability set named myAvailabilitySet in the resource group named myResourceGroup:

    $rgName = 'myResourceGroup'
    $avSetName = 'myAvailabilitySet'
    
    $avSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $avSetName
    Update-AzureRmAvailabilitySet -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-AzureRmAvailabilitySet -AvailabilitySet $avSet -Sku Aligned
    
  2. Deallocate and convert the VMs in the availability set. The following script deallocates each VM by using the Stop-AzureRmVM cmdlet, converts it by using ConvertTo-AzureRmVMManagedDisk, and restarts it by using Start-AzureRmVM:

    $avSet = Get-AzureRmAvailabilitySet -ResourceGroupName $rgName -Name $avSetName
    
    foreach($vmInfo in $avSet.VirtualMachinesReferences)
    {
      $vm = Get-AzureRmVM -ResourceGroupName $rgName | Where-Object {$_.Id -eq $vmInfo.id}
      Stop-AzureRmVM -ResourceGroupName $rgName -Name $vm.Name -Force
      ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $rgName -VMName $vm.Name
      Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName
    }
    

Convert standard managed disks to premium

After you've converted your VM to managed disks, you can switch between the storage types. You can also have a mixture of disks that use standard and premium storage.

In the following example, we show how to switch from standard to premium storage. To use premium managed disks, your VM must use a VM size that supports premium storage. This example also switches to a size that supports premium storage.

$rgName = 'myResourceGroup'
$vmName = 'YourVM'
$size = 'Standard_DS2_v2'
$vm = Get-AzureRmVM -Name $vmName -rgName $resourceGroupName

# Stop and deallocate the VM before changing the size
Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Force

# Change the VM size to a size that supports premium storage
$vm.HardwareProfile.VmSize = $size
Update-AzureRmVM -VM $vm -ResourceGroupName $rgName

# Get all disks in the resource group of the VM
$vmDisks = Get-AzureRmDisk -ResourceGroupName $rgName 

# For disks that belong to the selected VM, convert to premium storage
foreach ($disk in $vmDisks)
{
    if ($disk.OwnerId -eq $vm.Id)
    {
        $diskUpdateConfig = New-AzureRmDiskUpdateConfig –AccountType PremiumLRS
        Update-AzureRmDisk -DiskUpdate $diskUpdateConfig -ResourceGroupName $rgName `
        -DiskName $disk.Name
    }
}

Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName

Troubleshooting

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-AzureRmVMManagedDisk cmdlet again. A simple retry usually unblocks the situation.

Managed disks and Azure Storage Service Encryption

You can't use the preceding steps to convert an unmanaged disk into a managed disk if the unmanaged disk is in a storage account that has ever been encrypted through Azure Storage Service Encryption. The following steps detail how to copy and use unmanaged disks that have been in an encrypted storage account:

  1. Copy the VHD by using AzCopy to a storage account that has never been enabled for Azure Storage Service Encryption.

  2. Use the copied VM in one of the following ways:

    • Create a VM that uses managed disks and specify that VHD file during creation by using New-AzureRmVm.

    • Attach the copied VHD by using Add-AzureRmVmDataDisk to a running VM that uses managed disks.

Next steps

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