Posted in : Azure, Microsoft Av Tobias Vuorenmaa Översätt med Google ⟶

6 years ago

Recently a new capability was released for Azure Virtual Machines using Managed disks.
We have been missing the possibility to change OS disk of VMs using Managed disks. Until now that has only been possible for Unmanaged disks. Before release of this feature we have been forced to recreate the Virtual Machine if we want to use the snapshot and managed disk.
This feature come in handy while performing updates and or changes to OS or applications and where you might want to rollback to previous state on existing VM.
As of today Azure backup only supports restore to a new VM. With this capability we can hope to see a change for this in the feature. But as for now we can use Powershell to change OS disk of VM and restore a older version of that OS disk on existing VM.

In the exemple below we are:

  • Initiating a Snapshot
  • Creating a Managed disk from snapshot using the same name as the original disk but adds creation date.
  • Stop the VM – The server must be stop deallocated state.
  • Swap OS disk of existing VM
  • Start the VM
$resourceGroupName = 'rg-test-01'
$location = 'west europe'
$vmName = 'TESTIMG01'
$VirtualNetworkName = "vnet-test-01"
$snapshotName = 'snapshot-testimg01-2018-04-30'
$vm = Get-AzureRmVm -ResourceGroupName $resourceGroupName -Name $vmName
$osDiskName = ($vm.StorageProfile.OsDisk.Name)+('-')+(Get-Date -Format yyyy/MM/dd)
#Create Snapshot Config
$snapshot =  New-AzureRmSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -Location $location -CreateOption copy
#Take Snapshot
New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $snapshotName -ResourceGroupName $resourceGroupName
#Create a managed disk from created Snapshot
$VMSnapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$diskConfig = New-AzureRmDiskConfig -Location $VMsnapshot.Location -SourceResourceId $VMsnapshot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $osDiskName
#Stop VM
Stop-AzureRmVm -Name $vmName -ResourceGroupName $resourceGroupName
#Switch OS disk to the one created from Snapshot
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -Name $osDiskName
Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $disk.Id -Name $disk.Name
Update-AzureRmVM -ResourceGroupName $resourceGroupName -VM $vm
#Start VM after OS disk change
Start-AzureRmVm -Name $vmName -ResourceGroupName $resourceGroupName
#Verify OS disk
$vm.StorageProfile.OsDisk.Name

Source: https://azure.microsoft.com/en-us/blog/os-disk-swap-managed-disks/

Tags : Azure, Azure Backup, Change OS disk, Managed Disk

Personlig rådgivning

Vi erbjuder personlig rådgivning med författaren för 1400 SEK per timme. Anmäl ditt intresse i här så återkommer vi så snart vi kan.

Add comment

Your comment will be revised by the site if needed.