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

如何创建虚拟机或 VHD 的映像How to create an image of a virtual machine or VHD

若要创建虚拟机 (VM) 的多个副本以便在 Azure 中使用,请捕获 VM 或 OS VHD 的映像。To create multiple copies of a virtual machine (VM) for use in Azure, capture an image of the VM or of the OS VHD. 若要创建用于部署的映像,需要删除个人帐户信息。To create an image for deployment, you'll need to remove personal account information. 执行下列步骤可取消预配现有 VM、将其解除分配,然后创建映像。In the following steps, you deprovision an existing VM, deallocate it and create an image. 可以使用此映像,在订阅内的任何资源组中创建 VM。You can use this image to create VMs across any resource group within your subscription.

若要创建一份现有 Linux VM 的副本以用于备份或调试,或从本地 VM 上传专用 Linux VHD,请参阅上传自定义磁盘映像并根据该映像创建 Linux VMTo create a copy of your existing Linux VM for backup or debugging, or to upload a specialized Linux VHD from an on-premises VM, see Upload and create a Linux VM from custom disk image.

还可使用“Packer”创建自定义配置。You can also use Packer to create your custom configuration. 有关详细信息,请参阅如何使用 Packer 在 Azure 中创建 Linux 虚拟机映像For more information, see How to use Packer to create Linux virtual machine images in Azure.

创建映像前需要以下项:You'll need the following items before creating an image:

快速命令Quick commands

有关本文的用于测试、评估或了解 Azure 中的 VM 的简化版本,请参阅使用 CLI 创建 Azure VM 的自定义映像For a simplified version of this article, and for testing, evaluating, or learning about VMs in Azure, see Create a custom image of an Azure VM by using the CLI.

步骤 1:取消设置 VMStep 1: Deprovision the VM

首先,使用 Azure VM 代理取消预配 VM 以删除计算机特定文件和数据。First you'll deprovision the VM by using the Azure VM agent to delete machine-specific files and data. 在源 Linux VM 上,使用带 -deprovision+user 参数的 waagent 命令。Use the waagent command with the -deprovision+user parameter on your source Linux VM. 有关详细信息,请参阅 Azure Linux 代理用户指南For more information, see the Azure Linux Agent user guide.

  1. 使用 SSH 客户端连接到 Linux VM。Connect to your Linux VM with an SSH client.

  2. 在 SSH 窗口中,输入以下命令:In the SSH window, enter the following command:

    sudo waagent -deprovision+user
    

    备注

    仅在将捕获为映像的 VM 上运行此命令。Only run this command on a VM that you'll capture as an image. 此命令无法保证映像中的所有敏感信息均已清除,或该映像适合再分发。This command does not guarantee that the image is cleared of all sensitive information or is suitable for redistribution. +user 参数还会删除上次预配的用户帐户。The +user parameter also removes the last provisioned user account. 要保留 VM 中的用户帐户凭据,请仅使用 -deprovisionTo keep user account credentials in the VM, use only -deprovision.

  3. y 继续。Enter y to continue. 添加 -force 参数即可免除此确认步骤。You can add the -force parameter to avoid this confirmation step.

  4. 该命令完成后,请输入“退出”以关闭 SSH 客户端。After the command completes, enter exit to close the SSH client.

步骤 2:创建 VM 映像Step 2: Create VM image

使用 Azure CLI 将 VM 标记为通用化并捕获映像。Use the Azure CLI to mark the VM as generalized and capture the image. 在以下示例中,请将示例参数名称替换为自己的值。In the following examples, replace example parameter names with your own values. 示例参数名称包括 myResourceGroupmyVnetmyVMExample parameter names include myResourceGroup, myVnet, and myVM.

  1. 对使用 az vm deallocate 取消设置的 VM 解除分配。Deallocate the VM that you deprovisioned with az vm deallocate. 以下示例在名为 myResourceGroup 的资源组中释放名为 myVM 的 VM。The following example deallocates the VM named myVM in the resource group named myResourceGroup.

    az vm deallocate \
      --resource-group myResourceGroup \
      --name myVM
    
  2. 使用 az vm generalize 将 VM 标记为通用化。Mark the VM as generalized with az vm generalize. 以下示例将名为 myResourceGroup 的资源组中名为 myVM 的 VM 标记为通用化。The following example marks the VM named myVM in the resource group named myResourceGroup as generalized.

    az vm generalize \
      --resource-group myResourceGroup \
      --name myVM
    
  3. 使用 az image create 创建 VM 资源的映像。Create an image of the VM resource with az image create. 以下示例使用名为 myVM 的 VM 资源在名为 myResourceGroup 的资源组中创建名为 myImage 的映像。The following example creates an image named myImage in the resource group named myResourceGroup using the VM resource named myVM.

    az image create \
      --resource-group myResourceGroup \
      --name myImage --source myVM
    

    备注

    该映像在与源 VM 相同的资源组中创建。The image is created in the same resource group as your source VM. 可以在订阅内的任何资源组中从此映像创建虚拟机。You can create VMs in any resource group within your subscription from this image. 从管理角度来看,你可能希望为 VM 资源和映像创建特定的资源组。From a management perspective, you may wish to create a specific resource group for your VM resources and images.

    如果希望将映像存储在具有区域复原能力的存储中,需要在支持可用性区域的区域中创建该快照并包括 --zone-resilient true 参数。If you would like to store your image in zone-resilient storage, you need to create it in a region that supports availability zones and include the --zone-resilient true parameter.

步骤 3:从捕获的映像创建 VMStep 3: Create a VM from the captured image

使用通过 az vm create 创建的映像来创建 VM。Create a VM by using the image you created with az vm create. 以下示例从名为 myImage 的映像创建名为 myVMDeployed 的 VM。The following example creates a VM named myVMDeployed from the image named myImage.

az vm create \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --image myImage\
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

在另一个资源组中创建 VMCreating the VM in another resource group

可在订阅内的任何资源组中根据映像创建 VM。You can create VMs from an image in any resource group within your subscription. 要在与映像不同的资源组中创建 VM,请指定映像的完整资源 ID。To create a VM in a different resource group than the image, specify the full resource ID to your image. 使用 az image list 查看映像列表。Use az image list to view a list of images. 输出类似于以下示例。The output is similar to the following example.

"id": "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage",
   "location": "westus",
   "name": "myImage",

以下示例使用 az vm create,通过指定映像资源 ID,在与源映像不同的资源组中创建 VM。The following example uses az vm create to create a VM in a resource group other than the source image, by specifying the image resource ID.

az vm create \
   --resource-group myOtherResourceGroup \
   --name myOtherVMDeployed \
   --image "/subscriptions/guid/resourceGroups/MYRESOURCEGROUP/providers/Microsoft.Compute/images/myImage" \
   --admin-username azureuser \
   --ssh-key-value ~/.ssh/id_rsa.pub

步骤 4:验证部署Step 4: Verify the deployment

将 SSH 连接到创建的虚拟机以验证部署并开始使用新的 VM。SSH into the virtual machine you created to verify the deployment and start using the new VM. 要通过 SSH 连接,请使用 az vm show 查找 VM 的 IP 地址或 FQDN。To connect via SSH, find the IP address or FQDN of your VM with az vm show.

az vm show \
   --resource-group myResourceGroup \
   --name myVMDeployed \
   --show-details

后续步骤Next steps

可以从源 VM 映像创建多个 VM。You can create multiple VMs from your source VM image. 更改映像:To make changes to your image:

  • 从映像创建 VM。Create a VM from your image.
  • 进行任何更新或配置更改。Make any updates or configuration changes.
  • 再次执行相关步骤,对 VM 执行取消预配、解除分配、通用化和创建操作。Follow the steps again to deprovision, deallocate, generalize, and create an image.
  • 将此新映像用于将来的部署。Use this new image for future deployments. 可以删除原始映像。You may delete the original image.

有关使用 CLI 管理 VM 的详细信息,请参阅 Azure CLIFor more information on managing your VMs with the CLI, see Azure CLI.