Swapping the OS Disk of an Azure Virtual Machine

Posted on May 14, 2018 by Aidan Finn in Microsoft Azure with

In this post, I will explain how you can replace the OS disk of an Azure virtual machine with managed disks without touching the data disks.

 

 

Note that this solution can be useful when you need to restore an OS disk of a virtual machine from backup, without restoring the data disks and losing a day of operations. One example situation is when an anti-virus scanner corrupts the OS boot loader of an Azure virtual machine. The scenario below is yet another example of why data should be put in data disks!

Restore and swap the OS disks of an Azure virtual machine [Image Credit: Aidan Finn]

Restore and Swap the OS Disks of an Azure Virtual Machine [Image Credit: Aidan Finn]

Restore Disks from Backup

Azure Backup recently made a change to how virtual machines are backed up. Azure Backup has always used a snapshot mechanism as the first step of backing up a virtual machine. This was to help calculate the differences for the incremental backup before data was sent across the network to the recovery services vault. After you upgrade your subscription, Azure Backup maintains those snapshots for 7 days and they can be used to quickly restore virtual machines without copying data over a network. This greatly improves the time to restore operations.

You can open the virtual machine, click Backup under Operations, and click Restore VM to start the restoration process. Alternatively, you can go to Backup Items in the recovery services vault, click Azure Virtual Machine, and select the machine in question. You will probably select the most recent backup of your virtual machine to restore, depending on when your OS problem occurred.

Choosing an Azure VM backup to restore [Image Credit: Aidan Finn]

Choosing an Azure VM Backup to Restore [Image Credit: Aidan Finn]

Select a backup to restore and then click OK. In the Restore Configuration screen, choose “Restore Disks” as the Restore Type and select a storage account as the staging location. Click OK and then monitor the restore job to know when it has completed.

Create a Managed Disk

The disks will be restored as blobs into a storage account. If your virtual machine is using managed disks, then the OS disk is not in a usable state yet. You can very quickly create a new managed disk for the OS disk using a method shared here. You will only need to do this with the OS disk because you are not restoring the data disks in this scenario. You can delete the VHD blobs for the data disks.

Swap the OS Disks

This process requires that you shut down the virtual machine. Also, note that you cannot use this process to switch from Windows to Linux or vice versa.

Record the name of the virtual machine resource and resource group. Also, record the name of the new OS disk resource and resource group. Then you will run the following PowerShell, either in Azure’s Cloud Shell or on your PC, using the Latest Release Build of the Azure PowerShell Modules.

Then, stop the machine:

Store the virtual machine’s details in a variable. My virtual machine is called vm-petri-01 in a resource group called petri:

The restored disk’s details, a managed disk called vm-petri01-restoredosdisk in the petri resource group, are also stored in a variable:

Reconfigure the virtual machine to use the new managed disk as the OS disk:

Save the configuration change to the virtual machine by running Update-AzureRmVM:

You can now restart the virtual machine. Note that you have reverted the OS disk to one from a backup but you have not modified any of the data disks. There has been no data loss! There is a small chance that if this machine was a domain member, you’ll have to fix up the computer account of this machine in the domain.

Clean-Up

Once you’re happy that the process has been successful, you will have to clean up some left-behind resources:

  • The old OS disk (managed disk)
  • The restored VHD blobs in the stating location (storage account) – This includes the restored OS disk blob and the data disks that we didn’t restore.

Tagged with , , ,