您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 中创建通用 VM 的托管映像Create a managed image of a generalized VM in Azure

可通过在存储帐户中存储为托管/非托管磁盘的通用虚拟机 (VM) 创建托管的映像资源。A managed image resource can be created from a generalized virtual machine (VM) that is stored as either a managed disk or an unmanaged disk in a storage account. 随后,该映像可用于创建多个 VM。The image can then be used to create multiple VMs. 有关托管映像如何计费的信息,请参阅托管磁盘定价For information on how managed images are billed, see Managed Disks pricing.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

使用 Sysprep 通用化 Windows VMGeneralize the Windows VM using Sysprep

Sysprep 将删除所有个人帐户和安全信息,并准备好要用作映像的计算机。Sysprep removes all your personal account and security information, and then prepares the machine to be used as an image. 有关 Sysprep 的信息,请参阅 Sysprep 概述For information about Sysprep, see Sysprep overview.

确保 Sysprep 支持计算机上运行的服务器角色。Make sure the server roles running on the machine are supported by Sysprep. 有关详细信息, 请参阅Sysprep 对服务器角色的支持不支持的方案For more information, see Sysprep support for server roles and Unsupported scenarios.

重要

在 VM 上运行 Sysprep 后,该 VM 将被视为已通用化而无法重启。After you have run Sysprep on a VM, that VM is considered generalized and cannot be restarted. 通用化 VM 的过程是不可逆的。The process of generalizing a VM is not reversible. 如果需要保持原始 VM 正常运行,请创建 VM 的副本并将其副本通用化。If you need to keep the original VM functioning, you should create a copy of the VM and generalize its copy.

如果计划在首次将虚拟硬盘 (VHD) 上传到 Azure 之前运行 Sysprep,请确保先准备好 VMIf you plan to run Sysprep before uploading your virtual hard disk (VHD) to Azure for the first time, make sure you have prepared your VM.

若要将 Windows VM 通用化,请执行以下步骤:To generalize your Windows VM, follow these steps:

  1. 登录到 Windows VM。Sign in to your Windows VM.

  2. 以管理员身份打开“命令提示符”窗口。Open a Command Prompt window as an administrator. 将目录切换到 %windir%\system32\sysprep,然后运行 sysprep.exeChange the directory to %windir%\system32\sysprep, and then run sysprep.exe.

  3. 在“系统准备工具”对话框中,选择“进入系统全新体验(OOBE)”,并选中“通用”复选框。In the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE) and select the Generalize check box.

  4. 在“关机选项”中选择“关机”。For Shutdown Options, select Shutdown.

  5. 选择“确定”。Select OK.

    启动 Sysprep

  6. Sysprep 在完成运行后会关闭 VM。When Sysprep completes, it shuts down the VM. 请勿重启 VM。Do not restart the VM.

在门户中创建托管映像Create a managed image in the portal

  1. 打开 Azure 门户Open the Azure portal.

  2. 在左侧菜单中,选择“虚拟机”,然后从列表中选择 VM。In the menu on the left, select Virtual machines and then select the VM from the list.

  3. 在 VM 的“虚拟机”页面的上方菜单中,选择“捕获”。In the Virtual machine page for the VM, on the upper menu, select Capture.

    将显示“创建映像”页面。The Create image page appears.

  4. 对于“名称”,可以使用预填充的名称或输入想要为映像使用的名称。For Name, either accept the pre-populated name or enter a name that you would like to use for the image.

  5. 对于“资源组”,选择“新建”并输入名称,或选择“使用现有”并从下拉列表中选择要使用的资源组。For Resource group, either select Create new and enter a name, or select Use existing and select a resource group to use from the drop-down list.

  6. 如果想要在创建映像后删除源 VM,选择“创建映像后自动删除此虚拟机”。If you want to delete the source VM after the image has been created, select Automatically delete this virtual machine after creating the image.

  7. 如果希望能够在任何可用性区域中使用映像,请为“区域复原”选择“打开”。If you want the ability to use the image in any availability zone, select On for Zone resiliency.

  8. 选择“创建”以创建映像。Select Create to create the image.

  9. 创建映像后,在资源组的资源列表中,你会看到它作为“映像”资源而出现。After the image is created, you can find it as an Image resource in the list of resources in the resource group.

使用 PowerShell 创建 VM 映像Create an image of a VM using Powershell

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

直接从 VM 创建映像,可确保映像包含所有与该 VM 关联的磁盘,包括 OS 磁盘和任何数据磁盘。Creating an image directly from the VM ensures that the image includes all of the disks associated with the VM, including the OS disk and any data disks. 本示例演示如何从使用托管磁盘的 VM 创建托管映像。This example shows how to create a managed image from a VM that uses managed disks.

在开始之前,请确保有最新版本的 Azure PowerShell 模块。Before you begin, make sure that you have the latest version of the Azure PowerShell module. 若要查找版本,请在 PowerShell 中运行 Get-Module -ListAvailable AzTo find the version, run Get-Module -ListAvailable Az in PowerShell. 如需升级,请参阅使用 PowerShellGet 在 Windows 上安装 Azure PowerShellIf you need to upgrade, see Install Azure PowerShell on Windows with PowerShellGet. 如果在本地运行 PowerShell,则运行 Connect-AzAccount 以创建与 Azure 的连接。If you are running PowerShell locally, run Connect-AzAccount to create a connection with Azure.

备注

如果希望将映像存储在具有区域冗余能力的存储中,需要在支持可用性区域的区域中创建该映像并在映像配置中包括 -ZoneResilient 参数(New-AzImageConfig 命令)。If you would like to store your image in zone-redundant storage, you need to create it in a region that supports availability zones and include the -ZoneResilient parameter in the image configuration (New-AzImageConfig command).

若要创建 VM 映像,请遵循下列步骤:To create a VM image, follow these steps:

  1. 创建若干变量。Create some variables.

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $imageName = "myImage"
    
  2. 确保 VM 已解除分配。Make sure the VM has been deallocated.

    Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
    
  3. 将虚拟机的状态设置为“通用化”。Set the status of the virtual machine to Generalized.

    Set-AzVm -ResourceGroupName $rgName -Name $vmName -Generalized
    
  4. 获取虚拟机。Get the virtual machine.

    $vm = Get-AzVM -Name $vmName -ResourceGroupName $rgName
    
  5. 创建映像配置。Create the image configuration.

    $image = New-AzImageConfig -Location $location -SourceVirtualMachineId $vm.Id 
    
  6. 创建映像。Create the image.

    New-AzImage -Image $image -ImageName $imageName -ResourceGroupName $rgName
    

使用 PowerShell 从托管磁盘创建映像Create an image from a managed disk using PowerShell

如果仅想创建 OS 磁盘的映像,则将托管磁盘 ID 指定为 OS 磁盘:If you want to create an image of only the OS disk, specify the managed disk ID as the OS disk:

  1. 创建若干变量。Create some variables.

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $imageName = "myImage"
    
  2. 获取 VM。Get the VM.

    $vm = Get-AzVm -Name $vmName -ResourceGroupName $rgName
    
  3. 获取托管磁盘的 ID。Get the ID of the managed disk.

    $diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id
    
  4. 创建映像配置。Create the image configuration.

    $imageConfig = New-AzImageConfig -Location $location
    $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $diskID
    
  5. 创建映像。Create the image.

    New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

使用 PowerShell 从快照创建映像Create an image from a snapshot using Powershell

通过执行以下步骤,可以从通用 VM 的快照创建托管映像:You can create a managed image from a snapshot of a generalized VM by following these steps:

  1. 创建若干变量。Create some variables.

    $rgName = "myResourceGroup"
    $location = "EastUS"
    $snapshotName = "mySnapshot"
    $imageName = "myImage"
    
  2. 获取快照。Get the snapshot.

    $snapshot = Get-AzSnapshot -ResourceGroupName $rgName -SnapshotName $snapshotName
    
  3. 创建映像配置。Create the image configuration.

    $imageConfig = New-AzImageConfig -Location $location
    $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -SnapshotId $snapshot.Id
    
  4. 创建映像。Create the image.

    New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

从存储帐户中的 VHD 创建映像Create an image from a VHD in a storage account

从存储帐户中的通用 OS VHD 创建托管映像。Create a managed image from a generalized OS VHD in a storage account. 需要存储帐户中 VHD 的 URI,其格式如下: https://mystorageaccount.blob.core.windows.net/vhdcontainer/vhdfilename.vhdYou need the URI of the VHD in the storage account, which is in the following format: https://mystorageaccount.blob.core.windows.net/vhdcontainer/vhdfilename.vhd. 在本示例中,VHD 位于名为 vhdcontainer 的容器中的 mystorageaccount 中,且 VHD 文件名为 vhdfilename.vhd。In this example, the VHD is in mystorageaccount, in a container named vhdcontainer, and the VHD filename is vhdfilename.vhd.

  1. 创建若干变量。Create some variables.

    $vmName = "myVM"
    $rgName = "myResourceGroup"
    $location = "EastUS"
    $imageName = "myImage"
    $osVhdUri = "https://mystorageaccount.blob.core.windows.net/vhdcontainer/vhdfilename.vhd"
    
  2. 停止/解除分配 VM。Stop/deallocate the VM.

    Stop-AzVM -ResourceGroupName $rgName -Name $vmName -Force
    
  3. 将 VM 标记为通用化。Mark the VM as generalized.

    Set-AzVm -ResourceGroupName $rgName -Name $vmName -Generalized  
    
  4. 使用通用化的 OS VHD 创建映像。Create the image by using your generalized OS VHD.

    $imageConfig = New-AzImageConfig -Location $location
    $imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -BlobUri $osVhdUri
    $image = New-AzImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
    

后续步骤Next steps