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)
- Generalize Source VM and Stop the VM
- Create managed disks snapshots
- Move the snapshots to another resource group / subscription / region
- Create new managed disks from snapshots in the new Resource Group
- Deploy new vm using using managed disks in the new Resource Group
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 Azure
Login-AzureRmAccount
## If you move the managed disk to another subscription, Select a target account first
Select-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 copy
Start-AzureStorageBlobCopy -AbsoluteUri $snapSASURL -DestContainer “ContainerName” -DestContext $targetStorageContext -DestBlob “xxxxxx.vhd”
# Get the progress
Get-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 US”
Set-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.
2.
https://stackoverflow.com/questions/42720630/how-do-you-move-vms-with-managed-disks-between-resource-groups
3.
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.
'Azure Tech' 카테고리의 다른 글
[Azure] Plan이 부여된 Managed VM 이동 방법 (0) | 2018.04.01 |
---|