Swapping the OS Disk of an Azure Virtual Machine
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 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.
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.
$VM = Get-AzureRmVM -ResourceGroupName petri -Name vm-petri-01
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:
$Disk = Get-AzureRmDisk -ResourceGroupName petri -Name vm-petri-01-restoredosdisk
Reconfigure the virtual machine to use the new managed disk as the OS disk:
Set-AzureRmVMOSDisk -VM $VM -ManagedDiskId $disk.Id -Name $Disk.Name
Save the configuration change to the virtual machine by running Update-AzureRmVM:
Update-AzureRmVM -ResourceGroupName petri -VM $VM
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.
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.
More in Microsoft Azure
How to Select Hardware for Azure Stack HCI
Jul 5, 2022 | Flo Fox
How to Install Azure Stack HCI Single-Node Clusters
Jun 3, 2022 | Flo Fox
Microsoft Now Lets IT Admins Review & Remove Inactive Azure AD Users
May 27, 2022 | Rabia Noureen
Build 2022: Microsoft's Intelligent Data Platform Combines Data and Analytics
May 25, 2022 | Rabia Noureen
Microsoft Revises Restrictive Cloud Licensing Policies to Avoid EU Antitrust Probe
May 19, 2022 | Rabia Noureen
Microsoft's Azure AD Conditional Access Service Can Now Require Reauthentication
May 13, 2022 | Rabia Noureen
Most popular on petri