从托管映像迁移到共享映像库映像Migrate from a managed image to a Shared Image Gallery image

如果你想要将现有托管磁盘迁移到共享映像库,可以直接从托管映像创建共享映像库映像。If you have an existing managed image that you would like to migrate into a Shared Image Gallery, you can create a Shared Image Gallery image directly from the managed image. 测试新映像后,可以删除源托管映像。Once you have tested your new image, you can delete the source managed image. 还可以使用 Azure CLI 从托管映像迁移到共享映像库。You can also migrate from a managed image to a Shared Image Gallery using the Azure CLI.

映像库中的映像具有两个组件,我们将在此示例中创建这两个组件:Images in an image gallery have two components, which we will create in this example:

  • “映像定义”包含有关映像及其使用要求的信息。An Image definition carries information about the image and requirements for using it. 这包括了该映像是 Windows 还是 Linux 映像、是专用映像还是通用映像、发行说明以及最低和最高内存要求。This includes whether the image is Windows or Linux, specialized or generalized, release notes, and minimum and maximum memory requirements. 它是某种映像类型的定义。It is a definition of a type of image.
  • 使用共享映像库时,将使用映像版本来创建 VM。An image version is what is used to create a VM when using a Shared Image Gallery. 可根据环境的需要创建多个映像版本。You can have multiple versions of an image as needed for your environment. 创建 VM 时,将使用该映像版本来为 VM 创建新磁盘。When you create a VM, the image version is used to create new disks for the VM. 可以多次使用映像版本。Image versions can be used multiple times.

准备阶段Before you begin

若要完成本文,必须有一个现有的托管映像。To complete this article, you must have an existing managed image. 如果托管映像包含数据磁盘,则数据磁盘大小不能超过 1 TB。If the managed image contains a data disk, the data disk size cannot be more than 1 TB.

通过本文进行操作时,请根据需要替换资源组和 VM 名称。When working through this article, replace the resource group and VM names where needed.

可以按名称列出所有库和映像定义。You can list all of the galleries and image definitions by name. 结果采用 gallery\image definition\image version 格式。The results are in the format gallery\image definition\image version.

Get-AzResource -ResourceType Microsoft.Compute/galleries | Format-Table

找到正确的库后,创建一个变量供稍后使用。Once you find the right gallery, create a variable to use later. 此示例获取 myResourceGroup 资源组中名为 myGallery 的库。This example gets the gallery named myGallery in the myResourceGroup resource group.

$gallery = Get-AzGallery `
   -Name myGallery `
   -ResourceGroupName myResourceGroup

创建映像定义Create an image definition

映像定义为映像创建一个逻辑分组。Image definitions create a logical grouping for images. 它们用于管理有关映像的信息。They are used to manage information about the image. 映像定义名称可能包含大写或小写字母、数字、点、短划线和句点。Image definition names can be made up of uppercase or lowercase letters, digits, dots, dashes and periods.

创建映像定义时,请确保它包含所有正确信息。When making your image definition, make sure is has all of the correct information. 由于托管映像始终会通用化,因此应设置 -OsState generalizedBecause managed images are always generalized, you should set -OsState generalized.

若要详细了解可以为映像定义指定的值,请参阅映像定义For more information about the values you can specify for an image definition, see Image definitions.

使用 New-AzGalleryImageDefinition 创建映像定义。Create the image definition using New-AzGalleryImageDefinition. 在此示例中,映像定义名为 myImageDefinition,适用于通用化 Windows OS。In this example, the image definition is named myImageDefinition, and is for a generalized Windows OS. 若要使用 Linux OS 创建映像的定义,请使用 -OsType LinuxTo create a definition for images using a Linux OS, use -OsType Linux.

$imageDefinition = New-AzGalleryImageDefinition `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -Name 'myImageDefinition' `
   -OsState generalized `
   -OsType Windows `
   -Publisher 'myPublisher' `
   -Offer 'myOffer' `
   -Sku 'mySKU'

获取托管映像Get the managed image

可使用 Get-AzImage 查看资源组中可用的映像列表。You can see a list of images that are available in a resource group using Get-AzImage. 了解映像名称及其所在的资源组后,就可以再次使用 Get-AzImage 来获取映像对象并将其存储在变量中以便以后使用。Once you know the image name and what resource group it is in, you can use Get-AzImage again to get the image object and store it in a variable to use later. 此示例将从“myResourceGroup”资源组获取名为“myImage”的映像,并将其分配给变量“$managedImage” 。This example gets an image named myImage from the "myResourceGroup" resource group and assigns it to the variable $managedImage.

$managedImage = Get-AzImage `
   -ImageName myImage `
   -ResourceGroupName myResourceGroup

创建映像版本Create an image version

使用 New-AzGalleryImageVersion 从托管映像创建映像版本。Create an image version from the managed image using New-AzGalleryImageVersion.

允许用于映像版本的字符为数字和句点。Allowed characters for image version are numbers and periods. 数字必须在 32 位整数范围内。Numbers must be within the range of a 32-bit integer. 格式:MajorVersion.MinorVersion.PatchFormat: MajorVersion.MinorVersion.Patch.

在此示例中,映像版本为 1.0.0,该版本被复制到中国北部和中国东部数据中心 。In this example, the image version is 1.0.0 and it's replicated to both China North and China East datacenters. 选择复制的目标区域时,请记住,你还需包括源区域作为复制的目标。When choosing target regions for replication, remember that you also have to include the source region as a target for replication.

$region1 = @{Name='China East';ReplicaCount=1}
$region2 = @{Name='China North';ReplicaCount=2}
$targetRegions = @($region1,$region2)
$job = $imageVersion = New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $imageDefinition.Name `
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $imageDefinition.ResourceGroupName `
   -Location $imageDefinition.Location `
   -TargetRegion $targetRegions  `
   -Source $managedImage.Id.ToString() `
   -PublishingProfileEndOfLifeDate '2020-12-31' `
   -asJob 

可能需要一段时间才能将该映像复制到所有目标区域,因此我们创建了作业,以便可以跟踪进度。It can take a while to replicate the image to all of the target regions, so we have created a job so we can track the progress. 若要查看进度,请键入 $job.StateTo see the progress, type $job.State.

$job.State

备注

需等待映像版本彻底生成并复制完毕,然后才能使用同一托管映像来创建另一映像版本。You need to wait for the image version to completely finish being built and replicated before you can use the same managed image to create another image version.

创建映像版本时,还可以通过添加 -StorageAccountType Premium_LRS 在高级存储中存储映像。You can also store your image in Premium storage by adding -StorageAccountType Premium_LRS when you create the image version.

删除托管映像Delete the managed image

确认新映像版本正常工作后,可以删除托管映像。Once you have verified that you new image version is working correctly, you can delete the managed image.

Remove-AzImage `
   -ImageName $managedImage.Name `
   -ResourceGroupName $managedImage.ResourceGroupName

后续步骤Next steps

确认复制完成后,可以从通用化映像创建 VM。Once you have verified that replication is complete, you can create a VM from the generalized image.

<!-Not Available on For information about how to supply purchase plan information, see Supply Azure Marketplace purchase plan information when creating images-->