Migrate Azure Virtual Machines between Storage Accounts

10월 22, 2014에 게시됨

Program Manager, Azure Compute
One common task on Azure is to migrate a Virtual Machine from one storage account to another. Before we dive into these steps, it’s helpful to briefly review how Azure Virtual Machines are set up. When you create an Azure Virtual Machine, there are two services that work in tandem to create this machine: Compute and Storage. On the Storage side, a VHD is created in one of your storage accounts within the Azure Storage Service. The physical node that this VHD is stored on is located in the region you specified to place your Virtual Machine. On the compute side, we find a physical node in a second cluster to place your virtual machine. When the VM starts in that cluster, it establishes a connection with the Storage Service and boots from the VHD. When creating a Virtual Machine, we require that the VHD be located in a storage account in the same region where you are creating the VM. This is to ensure there is performance consistency when communicating between the Virtual Machine and the storage account. IaaSArchitecture With this context in mind, let’s walk through the steps to migrate the virtual machine from one region to another:
  1. Stop the Virtual Machine
  2. Copy the VHD blob from a storage account in the source region to a storage account in the destination region.
  3. Create an Azure Disk from the blob
  4. Boot the Virtual Machine from the Disk

Stop the Virtual Machine

Go to the Service Management Portal, select the Virtual Machine that you’d like to migrate, and select Shut Down from the control menu. ShutdownVm Alternatively, you can use the Azure Powershell cmdlet to accomplish the same task:
$servicename = "KenazTestService"
$vmname = "TestVM1"
Get-AzureVM -ServiceName $servicename -Name $vmname | Stop-AzureVM
Stopping the VM is a required step so that the file system is consistent when you do the copy operation. Azure does not support live migration at this time. This operation implies that you are migrating a specialized VM from one region to another. If you’d like to create a VM from a generalized image, sys-prep the Virtual Machine before stopping it.

Copy the VHD blob

The Azure Storage Service exposes the ability to move a blob from one storage account to another. To do this, we have to perform the following steps:
  1. Determine the source storage account information
  2. Determine the destination storage account information
  3. Ensure that the destination container exists in the destination storage account
  4. Perform the blob copy
NOTE: Copying blobs between storage accounts in different regions can take up to one hour or more depending on the size of the blob. The easiest way to do this is through Azure Powershell:
Select-AzureSubscription "kenazsubscription" 

# VHD blob to copy #
$blobName = "KenazTestService-TestVM1-2014-8-26-15-1-55-658-0.vhd" 

# Source Storage Account Information #
$sourceStorageAccountName = "kenazsa"
$sourceKey = "MySourceStorageAccountKey"
$sourceContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceKey  
$sourceContainer = "vhds"

# Destination Storage Account Information #
$destinationStorageAccountName = "kenazdestinationsa"
$destinationKey = "MyDestinationStorageAccountKey"
$destinationContext = New-AzureStorageContext –StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationKey  

# Create the destination container #
$destinationContainerName = "destinationvhds"
New-AzureStorageContainer -Name $destinationContainerName -Context $destinationContext 

# Copy the blob # 
$blobCopy = Start-AzureStorageBlobCopy -DestContainer $destinationContainerName `
                        -DestContext $destinationContext `
                        -SrcBlob $blobName `
                        -Context $sourceContext `
                        -SrcContainer $sourceContainer
This will initiate the blob copy from your source storage account to your destination storage account. At this point, you’ll probably have to wait a while for the blob to be fully copied. In order to check the status of the operation, you can try the following commands.
while(($blobCopy | Get-AzureStorageBlobCopyState).Status -eq "Pending")
    Start-Sleep -s 30
    $blobCopy | Get-AzureStorageBlobCopyState
Once the blob is finished copying, the status of the blob copy will be “Success”. For a more comprehensive copy VHD example, see "Azure Virtual Machine: Copy VHDs Between Storage Accounts."

Blob copy using AzCopy

Another option is to use the AzCopy utility (download here). Here is the equivalent blob copy between storage accounts:
AzCopy https://sourceaccount.blob.core.windows.net/mycontainer1 https://destaccount.blob.core.windows.net/mycontainer2 /sourcekey:key1 /destkey:key2 abc.txt
For more details on how to use AzCopy for different scenarios, check out “Getting Started with the AzCopy Command-Line Utility”.

Create an Azure Disk

At this point, the blob that you’ve copied into your destination storage account is still just a blob. In order to boot from it, you have to create an Azure Disk from this blob. Navigate to the Disks section of Virtual Machines and select Create. NOTE: These instructions are specific to specialized VMs. If you want to use the VHD as an image, you will need to restart the VM, sysprep it, copy the blob over, and then add as an Image (not a Disk). VirtualMachineDisks Use the VHD URL explorer to select the blob from the destination container that we copied the blob to. Select the toggle that says “The VHD contains an operating system.” This indicates to Azure that the disk object you’re creating is meant to be used as the OS disk rather than one of the data disks. CreateVHD NOTE: If you get an error that states “A lease conflict occurred with the blob …”, go back to the previous step to validate that the blob has finished copying. LeaseCOnflict Alternatively, you can use the Powershell cmdlets to perform the same operation:
Add-AzureDisk -DiskName "myMigratedTestVM" `
            -OS Linux `
            -MediaLocation "https://kenazdestinationsa.blob.core.windows.net/destinationvhds/KenazTestService-TestVM1-2014-8-26-16-16-48-522-0.vhd" `
Once complete, the Disk should show up under the Disks section of Virtual Machines.

Create the Virtual Machine

At this point, you can create the Virtual Machine using the disk object you just created. From the Service Management Portal, select Create Virtual Machine from Gallery and select the Disk that you created under My Disks. NOTE: If you are moving a VM that has a storage pool configured (or want the drive letter ordering to remain the same), make a note of the LUN number to VHD mapping on the source VM, and make sure the data disks are attached to the same LUNs on the destination VM. LinuxVM The Virtual Machine is now running in the destination storage account.