Skip to content
Related Articles

Related Articles

Microsoft Azure – Copy an Azure OS Managed Disk

Improve Article
Save Article
  • Last Updated : 29 Nov, 2022
Improve Article
Save Article

In this article, we are going to learn about how to copy an azure OS disk to a new disk. To perform this activity you 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 an azure PowerShell for copying data from an existing disk to a new disk. Let’s get started.

This activity/scenario can be used for the following:

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

Pre-requisite:

  • You must have an existing OS-managed disk in azure to copy.
  • Either Owner/Contributor and Storage Blob Owner/Storage Blob Data Contributor access privileges are required.
  • Optional: If you are using Windows PowerShell then you should install AzCopy v10 on your Windows OS.

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

# Switch to desired subscription
Set-AzureRmContext -SubscriptionName "_add_your_subscription_name_"

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

$targetDiskName = "_add_target_datadisk_name_"
$targetRG = "_add_target_rg_name_"
$targetLocate = "_add_target_location_"
$targetOS = "_add_system_ostype_"  #Windows/Linux

$HyperVGeneration = "_add_vm_gen_" #V1/V2
$SKUName = '_add_disk_sku_type_' #Premium_LRS/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

# Grant Disk Access Read
$sourceDiskSas = Grant-AzDiskAccess `
-ResourceGroupName $sourceRG `
-DiskName $sourceDiskName `
-DurationInSecond 60000 
-Access 'Read'

# Grant Disk Access Write
$targetDiskSas = Grant-AzDiskAccess `
-ResourceGroupName $targetRG `
-DiskName $targetDiskName `
-DurationInSecond 60000 `
-Access 'Write'

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

# Revoke Disk Access
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 
    • Note: V1 Gen OS Cannot be Swapped with V2 Gen
  • $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 a new 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
Related Articles

Start Your Coding Journey Now!