预览版:从 VM 创建映像Preview: Create an image from a VM

如果要使用现有 VM 生成多个相同的 VM,你可以使用该 VM 在共享映像库中通过 Azure PowerShell 创建映像。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 Azure PowerShell. 还可以使用 Azure CLI 从 VM 创建映像。You can also create an image from a VM using the Azure CLI.

你可以使用 Azure PowerShell 从专用化和通用化 VM 捕获映像。You can capture an image from both specialized and generalized VMs using 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

若要完成本文中的操作,必须拥有现有的共享映像库,以及 Azure 中现有的 VM 作为源。To complete this article, you must have an existing Shared Image Gallery, and an existing VM in Azure to use as the source.

如果 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.

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

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

找到正确的库和映像定义后,为它们创建变量以供稍后使用。Once you find the right gallery and image definitions, create variables for them to use later. 此示例会获取 myResourceGroup 资源组中名为 myGallery 的库 。This example gets the gallery named myGallery in the myResourceGroup resource group.

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

获取 VMGet the VM

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

$sourceVm = Get-AzVM `
   -Name sourceVM `
   -ResourceGroupName myResourceGroup

在使用 Stop-AzVM 创建映像之前,建议停止/解除分配 VM。It is a best practice to stop\deallocate the VM before creating an image using Stop-AzVM.

Stop-AzVM `
   -ResourceGroupName $sourceVm.ResourceGroupName `
   -Name $sourceVm.Name `
   -Force

创建映像定义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(使用适用于 Windows 的 Sysprep,或适用于 Linux 的 waagent -deprovision),则应使用 -OsState generalized 创建映像定义。If you generalized the VM (using Sysprep for Windows, or waagent -deprovision for Linux) then you should create an image definition using -OsState generalized. 如果未通用化 VM,请使用 -OsState specialized 创建映像定义。If you didn't generalized the VM, create an image definition using -OsState specialized.

若要详细了解可以为映像定义指定的值,请参阅映像定义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 的专用化 VM。In this example, the image definition is named myImageDefinition, and is for a specialized VM running Windows. 若要为使用 Linux 的映像创建定义,请使用 -OsType LinuxTo create a definition for images using Linux, use -OsType Linux.

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

创建映像版本Create an image version

使用 New-AzGalleryImageVersion 创建映像版本。Create an image version 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.

若要从 VM 创建映像版本,请对 -Source 使用 $vm.Id.ToString()To create an image version from the VM, use $vm.Id.ToString() for the -Source.

   $region1 = @{Name='China North';ReplicaCount=1}
   $region2 = @{Name='China East';ReplicaCount=2}
   $targetRegions = @($region1,$region2)

$job = $imageVersion = New-AzGalleryImageVersion `
   -GalleryImageDefinitionName $imageDefinition.Name`
   -GalleryImageVersionName '1.0.0' `
   -GalleryName $gallery.Name `
   -ResourceGroupName $gallery.ResourceGroupName `
   -Location $gallery.Location `
   -TargetRegion $targetRegions  `
   -Source $sourceVm.Id.ToString() `
   -PublishingProfileEndOfLifeDate '2020-12-01' `
   -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 of the job, 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 在高级存储中存储映像,或者通过添加 -StorageAccountType Standard_LRS 在本地冗余存储中存储映像。You can also store your image in Premium storage by adding -StorageAccountType Premium_LRS, or Locally Redundant Storage by adding -StorageAccountType Standard_LRS when you create the image version.

后续步骤Next steps

验证新映像版本正常工作后,即可创建 VM。Once you have verified that you new image version is working correctly, you can create a VM. 专用化映像版本通用化映像版本创建 VM。Create a VM from a specialized image version or a generalized image version.