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

使用 Azure CLI 从自定义磁盘创建 Linux VMCreate a Linux VM from a custom disk with the Azure CLI

本文介绍如何上传自定义虚拟硬盘 (VHD),以及如何在 Azure 中复制现有的 VHD。This article shows you how to upload a customized virtual hard disk (VHD), and how to copy an existing VHD in Azure. 然后,使用新建的 VHD 来创建新的 Linux 虚拟机 (VM)。The newly created VHD is then used to create new Linux virtual machines (VMs). 可根据要求安装并配置 Linux 分发版,然后使用该 VHD 创建新的 Azure 虚拟机。You can install and configure a Linux distro to your requirements and then use that VHD to create a new Azure virtual machine.

若要从自定义磁盘创建多个 VM,请先从 VM 或 VHD 创建映像。To create multiple VMs from your customized disk, first create an image from your VM or VHD. 有关详细信息,请参阅使用 CLI 创建 Azure VM 的自定义映像For more information, see Create a custom image of an Azure VM by using the CLI.

可使用两个选项创建自定义磁盘:You have two options to create a custom disk:

  • 上载 VHDUpload a VHD
  • 复制现有的 Azure VMCopy an existing Azure VM

要求Requirements

若要完成以下步骤,需要:To complete the following steps, you'll need:

备注

Azure 不支持更新的 VHDX 格式。The newer VHDX format is not supported in Azure. 创建 VM 时,请将 VHD 指定为映像格式。When you create a VM, specify VHD as the format. 如果需要,可以使用 qemu-img convertConvert-VHD PowerShell cmdlet 将 VHDX 磁盘转换为 VHD。If needed, you can convert VHDX disks to VHD with qemu-img convert or the Convert-VHD PowerShell cmdlet. Azure 不支持上传动态 VHD,因此,上传之前,你需要将此类磁盘转换为静态 VHD。Azure does not support uploading dynamic VHDs, so you'll need to convert such disks to static VHDs before uploading. 可以使用 Azure VHD Utilities for GO 等工具在将动态磁盘上传到 Azure 的过程中转换磁盘。You can use tools such as Azure VHD Utilities for GO to convert dynamic disks during the process of uploading them to Azure.

  • 确保已安装了最新的 Azure CLI 并已使用 az login 登录到 Azure 帐户。Make sure that you have the latest Azure CLI installed and you are signed in to an Azure account with az login.

在下面的示例中,请将示例参数名称替换为自己的值,例如 myResourceGroupmystorageaccountmydisksIn the following examples, replace example parameter names with your own values, such as myResourceGroup, mystorageaccount, and mydisks.

准备 VMPrepare the VM

Azure 支持各种 Linux 分发(请参阅 Endorsed Distributions(认可的分发))。Azure supports various Linux distributions (see Endorsed Distributions). 以下文章介绍了如何准备 Azure 支持的各种 Linux 分发版:The following articles describe how to prepare the various Linux distributions that are supported on Azure:

另请参阅 Linux 安装说明,获取更多有关如何为 Azure 准备 Linux 映像的一般提示。Also see the Linux Installation Notes for more general tips on preparing Linux images for Azure.

备注

只有在使用某个认可的分发的时候也使用 Azure 认可的分发中的 Linux 中“支持的版本”下指定的配置详细信息时,Azure 平台 SLA 才适用于运行 Linux 的 VM。The Azure platform SLA applies to VMs running Linux only when one of the endorsed distributions is used with the configuration details as specified under "Supported Versions" in Linux on Azure-Endorsed Distributions.

选项 1:上传 VHDOption 1: Upload a VHD

你现在可以将 VHD 直接上传到托管磁盘。You can now upload VHD straight into a managed disk. 有关说明,请参阅使用 Azure CLI 将 VHD 上传到 AzureFor instructions, see Upload a VHD to Azure using Azure CLI.

选项 2:复制现有 VMOption 2: Copy an existing VM

也可以在 Azure 中创建自定义的 VM,然后复制 OS 磁盘并将其附加到新 VM 以创建另一个副本。You can also create a customized VM in Azure and then copy the OS disk and attach it to a new VM to create another copy. 这种做法在测试中不会有任何问题,但若要将现有 Azure VM 作为多个新 VM 的模型,请改为创建映像。This is fine for testing, but if you want to use an existing Azure VM as the model for multiple new VMs, create an image instead. 有关从现有 Azure VM 创建映像的详细信息,请参阅使用 CLI 创建 Azure VM 的自定义映像For more information about creating an image from an existing Azure VM, see Create a custom image of an Azure VM by using the CLI.

如果要将现有 VM 复制到其他区域,你可能想要使用 azcopy在另一区域中的磁盘副本If you want to copy an existing VM to another region, you might want to use azcopy to creat a copy of a disk in another region.

否则,你应拍摄 VM 的快照,然后从快照创建新的操作系统 VHD。Otherwise, you should take a snapshot of the VM and then create a new OS VHD from the snapshot.

创建快照Create a snapshot

此示例在资源组 myResourceGroup 中创建名为 myVM 的 VM 的快照,并创建名为 osDiskSnapshot 的快照。This example creates a snapshot of a VM named myVM in resource group myResourceGroup and creates a snapshot named osDiskSnapshot.

osDiskId=$(az vm show -g myResourceGroup -n myVM --query "storageProfile.osDisk.managedDisk.id" -o tsv)
az snapshot create \
    -g myResourceGroup \
    --source "$osDiskId" \
    --name osDiskSnapshot

创建托管磁盘Create the managed disk

从快照创建新的托管磁盘。Create a new managed disk from the snapshot.

获取快照的 ID。Get the ID of the snapshot. 在此示例中,快照名为 osDiskSnapshot,位于 myResourceGroup 资源组中。In this example, the snapshot is named osDiskSnapshot and it is in the myResourceGroup resource group.

snapshotId=$(az snapshot show --name osDiskSnapshot --resource-group myResourceGroup --query [id] -o tsv)

创建托管磁盘。Create the managed disk. 此示例将从快照创建名为 myManagedDisk 的托管磁盘,该磁盘位于标准存储中,大小为 128 GB。In this example, we will create a managed disk named myManagedDisk from our snapshot, where the disk is in standard storage and sized at 128 GB.

az disk create \
    --resource-group myResourceGroup \
    --name myManagedDisk \
    --sku Standard_LRS \
    --size-gb 128 \
    --source $snapshotId

创建 VMCreate the VM

使用 az vm create 创建 VM,并将托管磁盘附加为 OS 磁盘 (--attach-os-disk)。Create your VM with az vm create and attach (--attach-os-disk) the managed disk as the OS disk. 以下示例使用基于上传的 VHD 创建的托管磁盘创建名为 myNewVM 的 VM:The following example creates a VM named myNewVM using the managed disk you created from your uploaded VHD:

az vm create \
    --resource-group myResourceGroup \
    --location eastus \
    --name myNewVM \
    --os-type linux \
    --attach-os-disk myManagedDisk

现在,应该可以使用凭据通过 SSH 从源 VM 连接到该 VM。You should be able to SSH into the VM with the credentials from the source VM.

后续步骤Next steps

准备好并上传自定义虚拟磁盘之后,可以阅读有关使用 Resource Manager 和模板的详细信息。After you have prepared and uploaded your custom virtual disk, you can read more about using Resource Manager and templates. 可能还需要向新 VM 添加数据磁盘You may also want to add a data disk to your new VMs. 如果需要访问在 VM 上运行的应用程序,请务必打开端口和终结点If you have applications running on your VMs that you need to access, be sure to open ports and endpoints.