azure - 如何将 azure VM 镜像移动到其他位置

标签 azure powershell azure-storage azure-virtual-machine azure-cli

我在美国东部有一个带有托管磁盘的 azure 虚拟机镜像,我想将其移动/复制到西欧。

有简单的方法吗?

我看到有一个名为 az-image-copy 的 azure cli 扩展,但它对我不起作用,因为它输出一个错误,表明它找不到操作系统磁盘(即使资源 ID 是正确的并且我可以在 azure 门户中看到它)

ERROR: Resource ServerLinux_OsDisk_1_c208679747734937b10a1525aa84a7d7 is not found

那么还有其他办法吗?

最佳答案

您可以使用azure powershell复制托管镜像,创建快照并将其复制到另一个区域,然后创建镜像。这是similar issue .

创建快照:

<# -- Create a snapshot of the OS (and optionally data disks) from the generalized VM -- #>
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
$disk = Get-AzureRmDisk -ResourceGroupName $resourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $region

$snapshotName = $imageName + "-" + $region + "-snap"

New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -Snapshot $snapshot -SnapshotName $snapshotName

复制快照:

# Create the name of the snapshot, using the current region in the name.
$snapshotName = $imageName + "-" + $region + "-snap"

# Get the source snapshot
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName

# Create a Shared Access Signature (SAS) for the source snapshot
$snapSasUrl = Grant-AzureRmSnapshotAccess -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -DurationInSecond 3600 -Access Read

# Set up the target storage account in the other region
$targetStorageContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
New-AzureStorageContainer -Name $imageContainerName -Context $targetStorageContext -Permission Container

# Use the SAS URL to copy the blob to the target storage account (and thus region)
Start-AzureStorageBlobCopy -AbsoluteUri $snapSasUrl.AccessSAS -DestContainer $imageContainerName -DestContext $targetStorageContext -DestBlob $imageBlobName
Get-AzureStorageBlobCopyState -Container $imageContainerName -Blob $imageBlobName -Context $targetStorageContext -WaitForComplete

# Get the full URI to the blob
$osDiskVhdUri = ($targetStorageContext.BlobEndPoint + $imageContainerName + "/" + $imageBlobName)

# Build up the snapshot configuration, using the target storage account's resource ID
$snapshotConfig = New-AzureRmSnapshotConfig -AccountType StandardLRS `
                                            -OsType Windows `
                                            -Location $targetRegionName `
                                            -CreateOption Import `
                                            -SourceUri $osDiskVhdUri `
                                            -StorageAccountId "/subscriptions/${sourceSubscriptionId}/resourceGroups/${resourceGroupName}/providers/Microsoft.Storage/storageAccounts/${storageAccountName}"

# Create the new snapshot in the target region
$snapshotName = $imageName + "-" + $targetRegionName + "-snap"
$snap2 = New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName -Snapshot $snapshotConfig

创建图像:

<# -- In the second subscription, create a new Image from the copied snapshot --#>
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId

$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName

$imageConfig = New-AzureRmImageConfig -Location $destinationRegion

Set-AzureRmImageOsDisk -Image $imageConfig `
                        -OsType Windows `
                        -OsState Generalized `
                        -SnapshotId $snap.Id

New-AzureRmImage -ResourceGroupName $resourceGroupName `
                 -ImageName $imageName `
                 -Image $imageConfig

更多详情请引用此link .

关于azure - 如何将 azure VM 镜像移动到其他位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737157/

相关文章:

asp.net-mvc - ASP.NET MVC 4 - 如何在通过 Team Foundation Service 部署到 Azure 时转换数据库连接字符串?

TFS 持续交付直接到 Azure 生产环境

arrays - Powershell验证我的数组元素类型

azure - 如何集成 Active Directory 和 sql azure 以验证用户访问 azure 上运行的应用程序

azure - 如何在 Microsoft Azure 的 Jupiter Notebook 中为 Spark 集群安排 pyspark 作业?

azure - 尝试使用 AKS 在 Azure 上创建托管 Kubernetes 集群时出现 "Incorrect padding"

powershell - 在 foreach 语句内的字符串中引用对象的属性

Powershell 自定义对象 toString 值用作 Note 属性

azure - 如何限制 azure blob 容器的大小?

azure - 使用 Bicep 设置 Azure 存储帐户装载路径