본문 바로가기

Azure Tech

[Azure] How can we move VMs with managed disks between resource groups / Regions

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

How can we move VMs with managed disks between resource groups

By now, we're moving VMs with managed disk following this procedure: (Within 30min)

  1. Generalize Source VM and Stop the VM
  2. Create managed disks snapshots
  3. Move the snapshots to another resource group / subscription / region
  4. Create new managed disks from snapshots in the new Resource Group
  5. Deploy new vm using using managed disks in the new Resource Group

Let's do this real quick!


1. Generalize the Source VM

Run 'Sysprep' (command : sysprep') to generailze source VM.

Detailed generalizing steps - 
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/capture-image-resource 




2. Stop VM manually




3. Create a snapshot for OS Disk




4. Check the created snapshot



5. Go into the snapshot resources and click 'Export' button on the upper menu to generate a SAS url 




6. Set up your storage account

You need a storage account in the region that you’re migrating your VM to.

Once you have created your storage account create a container in it called “vhds1”.




7. Create a container in the storage account manually


* ex) vhds1




8. We use the PowerShell Tool to move Managed OS Disk around our Azure storage account. 

To migrate the OS Disk, we can use the following command.



# Sign-in AzureLogin-AzureRmAccount
## If you move the managed disk to another subscription, Select a target account firstSelect-AzureRmSubscription -Subscription ‘bbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbb’
# Declare variables$snapSASURL = “Snapshot_SAS_URL”$targetStorageContext = (Get-AzureRmStorageAccount -ResourceGroupName “StorageAccount_ResourceGroup_Name” -Name “StorageAccount_Name”).Context
# Start copyStart-AzureStorageBlobCopy -AbsoluteUri $snapSASURL -DestContainer “ContainerName” -DestContext $targetStorageContext -DestBlob “xxxxxx.vhd”
# Get the progressGet-AzureStorageBlobCopyState -Container “ContainerName” -Context $targetStorageContext -WaitForComplete
Blob Name : xxxxxx.vhd (input blob name)


- Ex)



- Results




9. Go into the copied resource(test2.vhd) and copy the blob URL and Target storage account's *Resource ID 

*Target Storage account - Properties - Resource ID



10. Create a snapshot from the copied VHD and next, create a image from the snapshot using the script as below.



# Set OS Disk VHD Uri$osDiskVhdUri = 'URL of Copied VHD'
## Set a new snapshot config using the os disk vhd uri$snapshotConfig = New-AzureRmSnapshotConfig -AccountType StandardLRS -OsType Windows -Location “East US” -CreateOption Import -SourceUri $osDiskVhdUri -StorageAccountID “Target Storage Account's Resource ID
# Set a new snapshot name$snapshotName2 = “New_Snapshot_Name
# Create a new snapshot$snap2 = New-AzureRmSnapshot -ResourceGroupName “Target Resource Group Name” -SnapshotName $snapshotName2 -Snapshot $snapshotConfig

# Store the newly created snapshot information to a new value$snap = Get-AzureRmSnapshot -ResourceGroupName “Target Resource Group Name” -SnapshotName $snapshotName2
# Set an image config$imageConfig = New-AzureRmImageConfig -Location “East USSet-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -SnapshotId $snap.Id
# Create an image (Need to set a image name)New-AzureRmImage -ResourceGroupName “Target Resource Group Name” -ImageName “newimage” -Image $imageconfig

- Ex)



- Results




11. Create a VM from the Image via Azure Portal




- Results




Reference:


1. 

https://docs.microsoft.com/en-us/azure/virtual-machines/scripts/virtual-machines-windows-powershell-sample-copy-managed-disks-to-same-or-different-subscription


2. 

https://stackoverflow.com/questions/42720630/how-do-you-move-vms-with-managed-disks-between-resource-groups 


3. 

https://blogs.msdn.microsoft.com/igorpag/2017/03/14/azure-managed-disks-deep-dive-lessons-learned-and-benefits/


4. 

https://github.com/mcollier/copy-azure-managed-disk-images/blob/master/copy-managed-disk-image.ps1


5. 

https://michaelcollier.wordpress.com/2017/05/03/copy-managed-images/


6. 

https://blog.kloud.com.au/2017/06/09/the-quickest-way-to-create-new-vms-in-azure-from-existing-vm-snapshots-mostly-with-powershell/

'Azure Tech' 카테고리의 다른 글

[Azure] Plan이 부여된 Managed VM 이동 방법  (0) 2018.04.01