使用 Azure CLI 复制另一个库中的映像Copy an image from another gallery using the Azure CLI

如果你的组织中有多个库,则你还可以从其他库中存储的现有映像版本创建自己的映像版本。If you have multiple galleries in your organization, you can also create image versions from existing image versions stored in other galleries. 例如,你可能会使用一个开发和测试库用于创建和测试新映像。For example, you might have a development and test gallery for creating and testing new images. 准备好在生产环境中使用这些映像时,可以遵循本示例将它们复制到生产库中。When they are ready to be used in production, you can copy them into a production gallery using this example. 也可以使用 Azure PowerShell 从另一个库中的映像创建映像。You can also create an image from an image in another gallery using Azure PowerShell.

准备阶段Before you begin

若要完成本文,必须有一个现有的源库、映像定义和映像版本。To complete this article, you must have an existing source gallery, image definition, and image version. 此外,还应有一个目标库。You should also have a destination gallery.

必须将源映像版本复制到目标库所在的区域。The source image version must be replicated to the region where your destination gallery is located.

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

需要源映像定义中的信息,以便可以在新库中创建该映像的副本。You will need information from the source image definition so you can create a copy of it in your new gallery.

使用 az sig list 列出有关可用映像库的信息,以查找有关源库的信息。List information about the available image galleries using az sig list to find information about the source gallery.

az sig list -o table

使用 az sig image-definition list 列出库中的映像定义。List the image definitions in a gallery, using az sig image-definition list. 在此示例中,我们将搜索 myGalleryRG 资源组中名为 myGallery 的库内的映像定义。In this example, we are searching for image definitions in the gallery named myGallery in the myGalleryRG resource group.

az sig image-definition list \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   -o table

使用 az sig image-version list 列出库中映像的版本,以查找要复制到新库中的映像版本。List the versions of an image in a gallery, using az sig image-version list to find the image version that you want to copy into your new gallery. 在此示例中,我们将查找包含在 myImageDefinition 映像定义中的所有映像版本。In this example, we are looking for all of the image versions that are part of the myImageDefinition image definition.

az sig image-version list \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   --gallery-image-definition myImageDefinition \
   -o table

获取全部所需信息后,可以使用 az sig image-version show 获取源映像版本的 ID。Once you have all of the information you need, you can get the ID of the source image version using az sig image-version show.

az sig image-version show \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   --gallery-image-definition myImageDefinition \
   --gallery-image-version 1.0.0 \
   --query "id" -o tsv

创建映像定义Create the image definition

需要创建一个与源映像版本的映像定义相匹配的映像定义。You need to create an image definition that matches the image definition of your source image version. 可以使用 az sig image-definition show 查看在新库中重新创建映像定义所需的全部信息。You can see all of the information you need to recreate the image definition in your new gallery using az sig image-definition show.

az sig image-definition show \
   --resource-group myGalleryRG \
   --gallery-name myGallery \
   --gallery-image-definition myImageDefinition

输出将如下所示:The output will look something like this:

{
  "description": null,
  "disallowed": null,
  "endOfLifeDate": null,
  "eula": null,
  "hyperVgeneration": "V1",
  "id": "/subscriptions/1111abcd-1a23-4b45-67g7-1234567de123/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition",
  "identifier": {
    "offer": "myOffer",
    "publisher": "myPublisher",
    "sku": "mySKU"
  },
  "location": "chinaeast",
  "name": "myImageDefinition",
  "osState": "Specialized",
  "osType": "Linux",
  "privacyStatementUri": null,
  "provisioningState": "Succeeded",
  "purchasePlan": null,
  "recommended": null,
  "releaseNoteUri": null,
  "resourceGroup": "myGalleryRG",
  "tags": null,
  "type": "Microsoft.Compute/galleries/images"
}

使用上述输出中的信息,在新库中创建新的映像定义。Create a new image definition, in your new gallery, using the information from the output above.

az sig image-definition create \
   --resource-group myNewGalleryRG \
   --gallery-name myNewGallery \
   --gallery-image-definition myImageDefinition \
   --publisher myPublisher \
   --offer myOffer \
   --sku mySKU \
   --os-type Linux \
   --hyper-v-generation V1 \
   --os-state specialized 

创建映像版本Create the image version

使用 az image gallery create-image-version 创建版本。Create versions using az image gallery create-image-version. 你需要传入托管映像的 ID 以作为创建映像版本时要使用的基线。You will need to pass in the ID of the managed image to use as a baseline for creating the image version. 可以使用 az image list 获取资源组中的映像的相关信息。You can use az image list to get information about images that are in a resource group.

允许用于映像版本的字符为数字和句点。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,我们将使用本地冗余存储在“中国东部”区域创建 1 个副本,在“中国东部 2”区域创建 1 个副本。 In this example, the version of our image is 1.0.0 and we are going to create 1 replica in the China East region and 1 replica in the China East 2 region using locally-redundant storage.

az sig image-version create \
   --resource-group myNewGalleryRG \
   --gallery-name myNewGallery \
   --gallery-image-definition myImageDefinition \
   --gallery-image-version 1.0.0 \
   --target-regions "chinaeast=1" "chinaeast2=1=standard_lrs" \
   --replica-count 2 \
   --managed-image "/subscriptions/<Subscription ID>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"

备注

需等待映像版本彻底生成并复制完毕,然后才能使用同一托管映像来创建另一映像版本。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.

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

后续步骤Next steps

通用化专用化映像版本创建 VM。Create a VM from a generalized or a specialized image version.

有关如何提供购买计划信息的信息,请参阅在创建映像时提供 Azure 市场购买计划信息For information about how to supply purchase plan information, see Supply Azure Marketplace purchase plan information when creating images.