Skip to content
Related Articles

Related Articles

Microsoft Azure – Copy a Azure OS Managed Disk

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Mar, 2022

In this article, we must need an azure-managed OS disk from that OS disk we are copying the data to the azure page blob, and from that disk, we will transfer data to the new managed disk. Here, we will be using Azure PowerShell for copying data from an existing disk to a new disk.

This scenario can be used for the following activities:

  • Building/Cloning the azure servers with the same OS Configuration 
  • Creating the same application in different locations or datacentres.
  • Data Transfer from one region to another region
  • In a few cases, this process can also be used for versioning.

Note: V1 Gen OS Cannot be Swapped with V2 Gen

Pre-requisite:

  • You must have an Existing OS Managed Disk to Copy in Azure
  • Owner, Contributor Access is required.
  • If you were using Windows PowerShell you should install AzCopy v10.

Implementation:

Follow the below steps to copy the OS managed disk:1

Step 1: Log in to Azure Portal.

Step 2: Once you have logged from azure menu access Azure Cloud Shell >> Switch to PowerShell Mode.

Step 3: Now, Use the below following command create a new PowerShell File. Let’s say “copy-managed-disk.ps1”.

touch copy-managed-disk.ps1

Step 4: To write into a file or to edit the file use the below command

code copy-managed-data-disk.ps1

Copy paste the below PowerShell Script in copy-managed-data-disk.ps1

$SubscriptionName = "_add_subscription_name"
Set-AzureRmContext -SubscriptionName "$SubscriptionName" | Out-Null

$sourceRG = "_add_source_rg_name_"
$sourceDiskName = "_add_source_datadisk_name_"

$targetDiskName = "_add_target_datadisk_name_"
$targetRG = "_add_target_rg_name_"
$targetLocate = "_add_target_location_"

#Expected value for OS is either "Windows" or "Linux"
$targetOS = "_add_system_ostype_"
$HyperVGeneration = "_add_vm_gen_" #either V1 or V2

$SKUName = '_add_disk_sku_type_' #Premium_LRS or Standard_LRS

$sourceDisk = Get-AzDisk -ResourceGroupName $sourceRG -DiskName $sourceDiskName

# Adding the sizeInBytes with the 512 offset, and the -Upload flag
$targetDiskconfig = New-AzDiskConfig -SkuName -osType $targetOS `
-UploadSizeInBytes $($sourceDisk.DiskSizeBytes+512) `
-Location $targetLocate -CreateOption 'Upload' `
-HyperVGeneration $HyperVGeneration

$targetDisk = New-AzDisk -ResourceGroupName $targetRG -DiskName $targetDiskName `
-Disk $targetDiskconfig

$sourceDiskSas = Grant-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName `
-DurationInSecond 86400 -Access 'Read'

$targetDiskSas = Grant-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName `
-DurationInSecond 86400 -Access 'Write'

azcopy copy $sourceDiskSas.AccessSAS $targetDiskSas.AccessSAS --blob-type PageBlob

Revoke-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName

Revoke-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName

Now, fill up the details of the following in script

  • $SubscriptionName = “_add_subscription_name”
  • $sourceRG = “_add_source_rg_name_”
  • $sourceDiskName = “_add_source_datadisk_name_”
  • $targetDiskName = “_add_target_datadisk_name_”
  • $targetRG = “_add_target_rg_name_”
  • $targetLocate = “_add_target_location_”
  • $targetOS = “add_system_ostype_” #either “Windows” or “Linux”
  • $HyperVGeneration = “_add_vm_gen_” #either V1 or V2
  • $SKUName = ‘_add_disk_sku_type_’ #either “Premium_LRS” or “Standard_LRS” or any other.

Note: Modify the changes according to your requirement for copying data to disk.

Step 5: After you make the changes in the script, use the below command to run the PowerShell script

./copy-managed-data-disk.ps1

Note: execution process will take time to depending on the size of the OS managed disk. On an average, for 50 GB of data it will process in 15-20, if you are using a Premium LRS. In case of Standard LRS it might take 30-40 minutes on an average for copying data from one disk to another. It might even take more time if you are using a HDD.

Output: Once, the copying is done. Then the result will look the following.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!