skip to Main Content

I need a solution to copy image along with the data disk or attach a data disk to the existing managed image.

I tried copying the image using az copy but it doesn’t copy attached data disk.

2

Answers


  1. Chosen as BEST ANSWER

    I created a VM using my image and then followed the above steps to copy the data disk and attach it to the VM. Then I took the snapshot of the VM in order to create the image which fulfilled my requirement.


  2. I tried copying the image using az copy and that was successful, but it doesn’t copy the attached data disk.

    To copy the disk across region, make use of below PowerShell script

    $sourceRG = <sourceResourceGroupHere>
    $sourceDiskName = <sourceDiskNameHere>
    $targetDiskName = <targetDiskNameHere>
    $targetRG = <targetResourceGroupHere>
    $targetLocate = <yourTargetLocationHere>
    $targetVmGeneration = "V1" # either V1 or V2
    #Expected value for OS is either "Windows" or "Linux"
    $targetOS = <yourOSTypeHere>
    
    $sourceDisk = Get-AzDisk -ResourceGroupName $sourceRG -DiskName $sourceDiskName
    
    # Adding the sizeInBytes with the 512 offset, and the -Upload flag
    $targetDiskconfig = New-AzDiskConfig -SkuName 'Standard_LRS' -osType $targetOS -UploadSizeInBytes $($sourceDisk.DiskSizeBytes+512) -Location $targetLocate -CreateOption 'Upload' -HyperVGeneration $targetVmGeneration
    
    $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  
    

    Output:

    enter image description here

    To confirm in portal disk copy to another region successfully like below:

    enter image description here

    References:

    Upload a VHD to Azure or copy a disk across regions – Azure PowerShell – Azure Virtual Machines | Microsoft Learn

    Attach a data disk to a Windows VM in Azure by using PowerShell – Azure Virtual Machines | Microsoft Learn

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search