Converting Azure VM Disks to Managed Disks
In this post, I will show you how to convert a virtual machine, not in an availability set, but with virtual hard disks in a storage account (without encryption) to a virtual machine using Managed Disks.
The Virtual Machine
There is nothing unusual about the Azure Resource Manager virtual machine that I will convert. It has an OS disk, a temp drive, and some data disks. Keep in mind that the Managed Disk is ARM only.
The OS disk and the data disks are stored in a storage account. These are the disks that will be converted. The temp drive is stored on the Hyper-V host in Azure, not in a storage account, and is not subject to conversion.
Preparing to Convert
We will be using PowerShell to do the conversion. Keep in mind, the necessary cmdlet is only available in the latest version of the PowerShell modules. Make sure you update the Azure PowerShell modules on your PC before you proceed. You will need to reboot if this is the first time you have installed the modules.
We will be using a cmdlet called ConvertTo-AzureRmVMManagedDisk to do the conversion. If this cmdlet is not available on your PC, then try updating your ARM Compute module with the below command in an elevated PowerShell window. Note that I wrote this article on February 18th. Therefore, you probably will not need to run the below command after the next update to the ARM PowerShell modules installer but here it is just in case.
Install-Module AzureRM.Compute -RequiredVersion 2.6.0 -AllowClobber
Converting the Disks
You can use the following small PowerShell script to convert the disks of a virtual machine from unmanaged disks to Managed Disks.
$ResourceGroupName = "rg-petri3"
$VMName = "vm-petri3"
$SubscriptionID = "1a2b3c45-aa12-1a23-12a3-a1bcdefg345h"
Select-AzureRmSubscription -SubscriptionId $SubscriptionID
Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force
ConvertTo-AzureRmVMManagedDisk -ResourceGroupName $ResourceGroupName -VMName $VMName
The first section of the script prepares three variables to store information about your environment:
- ResourceGroupName: The name of the resource group that contains the virtual machine you wish to convert to use Managed Disks
- VMName: The name of the virtual machine in question
- SubscriptionID: The GUID of the subscription that contains the resource group and the virtual machine
The first command will request your login credentials so that the current PowerShell session can sign you into your account.
The second line selects the subscription that contains the machine/disks that you wish to convert. I included this line because many people have access to more than one subscription when they sign in. We need to select the correct active subscription.
You cannot convert a virtual machine’s disks to Managed Disks without shutting it down first. I use the resource group name and the virtual machine name with Stop-AzureRMVM to shut the desired machine down.
The final step, ConvertTo-AzureRmVMManagedDisk, uses the resource group name and the virtual machine name to start the conversion of the machine’s disks.
The virtual machine is automatically started up again. At this time, the locks on the original virtual hard disk blobs in the storage account are released. Once you are happy with the conversion, you can delete the original blobs. If the storage account is not being used for anything else (such as other virtual machines or virtual machine diagnostics), then you can delete the storage account.
From the time I started the script to completion, it took approximately 5 minutes to convert my demo lab machine. Please note, the virtual machine was in a running state.
What About Availability Sets?
This post showed you a very simple example with a virtual machine that is not in an availability set. I’ll show you how to convert virtual machines in an availability set in a later post.