使用 Azure CLI 从共享映像库中的托管磁盘或快照创建映像Create an image from a Managed Disk or snapshot in a Shared Image Gallery using the Azure CLI

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

映像库中的映像具有两个组件,我们将在此示例中创建这两个组件: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 a snapshot or Managed Disk.

如果要包含数据磁盘,则数据磁盘大小不能超过 1 TB。If you want to include a data disk, the data disk size cannot be more than 1 TB.

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

查找快照或托管磁盘Find the snapshot or Managed Disk

可以使用 az snapshot list 查看资源组中可用的快照列表。You can see a list of snapshots that are available in a resource group using az snapshot list.

az snapshot list --query "[].[name, id]" -o tsv

还可使用托管磁盘代替快照。You can also use a Managed Disk instead of a snapshot. 若要获取托管磁盘,请使用 az disk listTo get a Managed Disk, use az disk list.

az disk list --query "[].[name, id]" -o tsv

获得快照或托管磁盘的 ID 后,将其分配给名为 $source 的变量,供以后使用。Once you have the ID of the snapshot or Managed Disk and assign it to a variable called $source to be used later.

可以使用相同的过程获取要包含在映像中的任何数据磁盘。You can use the same process to get any data disks that you want to include in your image. 将其分配给变量,稍后在创建映像版本时会使用这些变量。Assign them to variables, then use those variables later when you create the image version.

若要创建映像定义,需要有关映像库的信息。You will need information about the image gallery in order to create the image definition.

使用 az sig list 列出有关可用映像库的信息。List information about the available image galleries using az sig list. 请记下库的名称及库所在的资源组,以供以后使用。Note the gallery name which resource group the gallery is in to use later.

az sig list -o table

创建映像定义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. 在此示例中,我们假设快照或托管磁盘来自正在使用但尚未通用化的 VM。In this example, we are assuming that the snapshot or Managed Disk is from a VM that is in use, and hasn't been generalized. 如果托管磁盘或快照是从通用化 OS 获取的(在 Windows 上运行 Sysprep 或在 Linux 上运行 waagent-deprovision-deprovision+user 后),则将 -OsState 更改为 generalizedIf the Managed Disk or snapshot was taken of a generalized OS (after running Sysprep for Windows or waagent -deprovision or -deprovision+user for Linux) then change the -OsState to generalized.

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

使用 az sig image-definition create 在库中创建一个映像定义。Create an image definition in the gallery using az sig image-definition create.

在此示例中,映像定义名为 myImageDefinition,适用于专用化 Linux OS 映像。In this example, the image definition is named myImageDefinition , and is for a specialized Linux OS image. 若要使用 Windows OS 创建映像的定义,请使用 --os-type WindowsTo create a definition for images using a Windows OS, use --os-type Windows.

在此示例中,库名为 myGallery,它位于 myGalleryRG 资源组中,映像定义名称将为 mImageDefinition 。In this example, the gallery is named myGallery , it is in the myGalleryRG resource group, and the image definition name will be mImageDefinition .

resourceGroup=myGalleryRG
gallery=myGallery
imageDef=myImageDefinition
az sig image-definition create \
   --resource-group $resourceGroup \
   --gallery-name $gallery \
   --gallery-image-definition $imageDef \
   --publisher myPublisher \
   --offer myOffer \
   --sku mySKU \
   --os-type Linux \
   --os-state specialized

创建映像版本Create the image version

使用 az image gallery create-image-version 创建映像版本。Create an image version using az image gallery create-image-version.

允许用于映像版本的字符为数字和句点。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 zone-redundant storage. 选择复制的目标区域时,请记住,还需包含托管磁盘或快照的源区域作为复制的目标。When choosing target regions for replication, remember that you also have to include the source region of the Managed Disk or snapshot as a target for replication.

--os-snapshot 参数中传入快照或托管磁盘的 ID。Pass the ID of the snapshot or Managed Disk in the --os-snapshot parameter.

az sig image-version create \
   --resource-group $resourceGroup \
   --gallery-name $gallery \
   --gallery-image-definition $imageDef \
   --gallery-image-version 1.0.0 \
   --target-regions "chinaeast=1" "chinaeast2=1=standard_lrs" \
   --replica-count 2 \
   --os-snapshot $source

如果要在映像中包含数据磁盘,则需要同时包含设置为 LUN 编号的 --data-snapshot-luns 参数和设置为数据磁盘或快照 ID 的 --data-snapshots 参数。If you want to include data disks in the image, then you need to include both the --data-snapshot-luns parameter set to the LUN number and the --data-snapshots set to the ID of the data disk or snapshot.

备注

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

后续步骤Next steps

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

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