Azure VM Disk Swap

If you’re coming from on-premises, you know that before you do any changes on a virtual machine (updates, upgrades, configuration changes, etc.), you do a snapshot of the disk just in case of a failure. This is something we’ve all ignored up to a point until it bit us badly and we’ve then just ran snapshots for every change.

The problem in the cloud is that you do not have the same easy way to do VM snapshots and restores in case of an issue. This changed in Azure when they introduced the OS Disk swap feature which allows administrators to run snapshots of a virtual machine, do their thing and if anything goes wrong they just restore the checkpoint.

As I mentioned above, if anything goes wrong with a VM that you’re maintaining then you have a few options available depending on the situation. Before OS Disk swap existed, your only solution to fixing a broken VM was to either restore it from Azure Backup, download the VHD and hope to fix it from a Hyper-V machine, and the worst case was to redeploy it. Now you have the option of just doing a snapshot of the VM, do your stuff and if something happens just swap in the good disk 🙂

How do I do it?

Swapping the OS Disks is a simple operation that can be done via PowerShell or CLI (no portal support yet). You need either the latest AzureRM PowerShell Module installed on your computer or just use the Azure Cloud Shell –

For this example, I will use my WorkStation VM in Azure located in the Workstation RG

Let’s start by setting the VM object in a variable:

Now it’s time to stop the VM in order to have a consistent snapshot. – You can do skip this step if you don’t want to stop the VM.

While the VM is running or stopped you can now proceed to snapshot the current OS Disk:

Caption of the Snapshot

Let’s assume that you utterly broke the VM, beyond repair so now you have to start the OS Disk Swap procedure. This part requires to stop the VM.

The Update command will start up the VM and you will have reverted a disaster 🙂

Managed Disk from Snapshot
Swap completed successfully.

After you RDP / SSH to the VM and validate that everything is working as before, you can just take the old disk image and play around with it in Hyper-V instance so you can see what happened or just delete it.

Signing off. Have a good one!

Converting existing VM deployment ARM templates to use Managed Disks

In my last blog post, I talked about why we should stop using regular storage accounts for our IaaS VMs and why should we use Managed Disks. In today’s blog post I will talk about how you can modify your existing ARM templates that deploy your VMS to use Managed Disks from now on.

Let’s take a look at a regular storage account based ARM Template:

We have a resources block where we specify a storage account, and we use that resource to create an OS Disk and a Data Disk for the particular VM.

If we want to add more disks then we copy paste the what’s between the dataDisks array a couple of times, modify the LUN and name and we’re happy.

Converting the template to a managed disk format is pretty easy. You first need to reference in the template the API Compute (do note that we’re not modifying storage API) version 2016-04-30-preview or a later version (never use -preview in your production templates!)

You change the storage profile to reference managed disks as shown the code snip below:

Hard? I don’t think so, MS made it very easy to convert existing templates to use Managed Disks. You basically remove some code from the template 🙂

This sample is for single VMs;

If you have multiple VMs in an Availability set, you need to add a “managed” property to the Availability Set block like shown below:

Hope this was usefull, if you have any comments, write them down below 🙂

Have a good one!

Pin It on Pinterest