使用 Azure CLI 从 Azure 中的 VM 创建映像版本Create an image version from a VM in Azure using the Azure CLI

如果要使用现有 VM 生成多个相同的 VM,可以通过 Azure CLI 使用该 VM 在共享映像库中创建映像。If you have an existing VM that you would like to use to make multiple, identical VMs, you can use that VM to create an image in a Shared Image Gallery using the Azure CLI. 还可以使用 Azure PowerShell 从 VM 创建映像。You can also create an image from a VM using Azure PowerShell.

使用共享映像库时,将使用 映像版本 来创建 VM。An image version is what you use 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 use an image version to create a VM, the image version is used to create disks for the new VM. 可以多次使用映像版本。Image versions can be used multiple times.

准备阶段Before you begin

若要完成本文,必须具有现有的共享映像库。To complete this article, you must have an existing Shared Image Gallery.

在 Azure 中,在你的库所在区域中还必须有一个现有 VM。You must also have an existing VM in Azure, in the same region as your gallery.

如果 VM 附加了数据磁盘,则数据磁盘大小不能超过 1 TB。If the VM has a data disk attached, the data disk size cannot be more than 1 TB.

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

获取有关 VM 的信息Get information about the VM

可以使用 az vm list 查看可用 VM 的列表。You can see a list of VMs that are available using az vm list.

az vm list --output table

知道 VM 的名称及其所在的资源组后,使用 az vm get-instance-view 获取 VM 的 ID。Once you know the VM name and what resource group it is in, get the ID of the VM using az vm get-instance-view.

az vm get-instance-view -g MyResourceGroup -n MyVm --query id

创建映像定义Create an image definition

映像定义为映像创建一个逻辑分组。Image definitions create a logical grouping for images. 它们用于管理有关映像版本的信息,这些版本是在其中创建的。They are used to manage information about the image versions that are created within them.

映像定义名称可以由大写或小写字母、数字、点、短划线和句点构成。Image definition names can be made up of uppercase or lowercase letters, digits, dots, dashes, and periods.

请确保映像定义的类型正确。Make sure your image definition is the right type. 如果已通用化 VM(使用适用于 Windows 的 Sysprep,或适用于 Linux 的 waagent -deprovision),则应使用 --os-state generalized 创建通用化映像定义。If you have generalized the VM (using Sysprep for Windows, or waagent -deprovision for Linux) then you should create a generalized image definition using --os-state generalized. 若要在不删除现有用户帐户的情况下使用 VM,请使用 --os-state specialized 创建专用化映像定义。If you want to use the VM without removing existing user accounts, create a specialized image definition using --os-state specialized.

若要详细了解可为映像定义指定的值,请参阅映像定义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.

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

创建映像版本Create the image version

使用 az image gallery create-image-version 从 VM 创建映像版本。Create an image version from the VM 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,并且我们打算使用本地冗余存储在“中国北部”区域创建 2 个副本,在“中国东部区域”创建 1 个副本,在“中国东部 2”区域创建 1 个副本。In this example, the version of our image is 1.0.0 and we are going to create 2 replicas in the China North region, 1 replica in the China East region and 1 replica in the China East 2 region using locally-redundant storage. 复制区域必须包含源 VM 所在的区域。The replication regions must include the region the source VM is located.

请将此示例中的 --managed-image 值替换为上一步的 VM ID。Replace the value of --managed-image in this example with the ID of your VM from the previous step.

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


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

使用 Azure CLI 从通用化映像创建 VM。Create a VM from the generalized image using the Azure CLI.

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