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

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

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

如果想要从自定义磁盘创建多个 VM,则应该从 VM 或 VHD 创建映像。If you want to create multiple VMs from your customized disk, you should create an image from your VM or VHD. 有关详细信息,请参阅使用 CLI 创建 Azure VM 的自定义映像For more information, see Create a custom image of an Azure VM using the CLI.

可以使用两个选项:You have two options:

快速命令Quick commands

使用 az vm create 从自定义或专用的磁盘创建新 VM 时,请附加该磁盘 (--attach-os-disk),而不要指定自定义映像或 Marketplace 映像 (--image)。When creating a new VM using az vm create from a customized or specialized disk you attach the disk (--attach-os-disk) instead of specifying a custom or marketplace image (--image). 以下示例使用从自定义 VHD 创建的、名为 myManagedDisk 的托管磁盘,创建名为 myVM 的 VM:The following example creates a VM named myVM using the managed disk named myManagedDisk created from your customized VHD:

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

要求Requirements

若要完成以下步骤,需要:To complete the following steps, you 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 using qemu-img convert or the Convert-VHD PowerShell cmdlet. 此外,Azure 不支持上传动态 VHD,因此,上传之前,你需要将此类磁盘转换为静态 VHD。Further, Azure does not support uploading dynamic VHDs, so you 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 to Azure.

在以下示例中,请将示例参数名称替换为自己的值。In the following examples, replace example parameter names with your own values. 示例参数名称包括 myResourceGroupmystorageaccountmydisksExample parameter names included myResourceGroup, mystorageaccount, and mydisks.

准备 VMPrepare the VM

Azure 支持各种 Linux 分发(请参阅 Endorsed Distributions(认可的分发))。Azure supports various Linux distributions (see Endorsed Distributions). 以下文章指导如何准备 Azure 上支持的各种 Linux 分发:The following articles guide you through 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 upload a customized VHD that you have running on a local machine or that you exported from another cloud. 若要使用 VHD 创建新的 Azure VM,需要将 VHD 上传到存储帐户,并从该 VHD 创建托管磁盘。To use the VHD to create a new Azure VM, you need to upload the VHD to a storage account and create a managed disk from the VHD.

创建资源组Create a resource group

在上载自定义磁盘和创建 VM 之前,首先需要使用 az group create 创建一个资源组。Before uploading your custom disk and creating VMs, you first need to create a resource group with az group create.

以下示例在 eastus 位置创建名为 myResourceGroup 的资源组:Azure 托管磁盘概述The following example creates a resource group named myResourceGroup in the eastus location: Azure Managed Disks overview

az group create \
    --name myResourceGroup \
    --location eastus

创建存储帐户Create a storage account

可以使用 az storage account create 为自定义磁盘和 VM 创建存储帐户。Create a storage account for your custom disk and VMs with az storage account create.

以下示例在前面创建的资源组中创建名为 mystorageaccount 的存储帐户:The following example creates a storage account named mystorageaccount in the resource group previously created:

az storage account create \
    --resource-group myResourceGroup \
    --location eastus \
    --name mystorageaccount \
    --kind Storage \
    --sku Standard_LRS

列出存储帐户密钥List storage account keys

Azure 将为每个存储帐户生成两个 512 位的访问密钥。Azure generates two 512-bit access keys for each storage account. 在向存储帐户进行身份验证以执行操作(例如执行写入操作)时,将使用这些访问密钥。These access keys are used when authenticating to the storage account, like carrying out write operations. 从此处了解有关管理对存储的访问的详细信息。Read more about managing access to storage here. 可使用 az storage account keys list 查看访问密钥。You view the access keys with az storage account keys list.

查看创建的存储帐户的访问密钥:View the access keys for the storage account you created:

az storage account keys list \
    --resource-group myResourceGroup \
    --account-name mystorageaccount

输出类似于:The output is similar to:

info:    Executing command storage account keys list
+ Getting storage account keys
data:    Name  Key                                                                                       Permissions
data:    ----  ----------------------------------------------------------------------------------------  -----------
data:    key1  d4XAvZzlGAgWdvhlWfkZ9q4k9bYZkXkuPCJ15NTsQOeDeowCDAdB80r9zA/tUINApdSGQ94H9zkszYyxpe8erw==  Full
data:    key2  Ww0T7g4UyYLaBnLYcxIOTVziGAAHvU+wpwuPvK4ZG0CDFwu/mAxS/YYvAQGHocq1w7/3HcalbnfxtFdqoXOw8g==  Full
info:    storage account keys list command OK

记下 key1,因为在后续步骤中需要使用它来与存储帐户交互。Make a note of key1 as you will use it to interact with your storage account in the next steps.

创建存储容器Create a storage container

在存储帐户中创建用于整理磁盘的容器的方式,与创建各种目录以便有条理地整理本地文件系统的方式相同。In the same way that you create different directories to logically organize your local file system, you create containers within a storage account to organize your disks. 一个存储帐户可以包含任意数目的容器。A storage account can contain any number of containers. 可以使用 az storage container create 创建容器。Create a container with az storage container create.

以下示例创建名为 mydisks 的容器:The following example creates a container named mydisks:

az storage container create \
    --account-name mystorageaccount \
    --name mydisks

上传 VHDUpload the VHD

现在,使用 az storage blob upload 上传自定义磁盘。Now upload your custom disk with az storage blob upload. 可以页 Blob 的形式上传和存储自定义磁盘。You upload and store your custom disk as a page blob.

指定访问密钥、在上一步中创建的容器,以及自定义磁盘在本地计算机上的路径:Specify your access key, the container you created in the previous step, and then the path to the custom disk on your local computer:

az storage blob upload --account-name mystorageaccount \
    --account-key key1 \
    --container-name mydisks \
    --type page \
    --file /path/to/disk/mydisk.vhd \
    --name myDisk.vhd

上传 VHD 可能需要一些时间。Uploading the VHD may take a while.

创建托管磁盘Create a managed disk

使用 az disk create 从 VHD 创建托管磁盘。Create a managed disk from the VHD using az disk create. 以下示例从已上传到命名存储帐户和容器的 VHD 创建名为 myManagedDisk 的托管磁盘:The following example creates a managed disk named myManagedDisk from the VHD you uploaded to your named storage account and container:

az disk create \
    --resource-group myResourceGroup \
    --name myManagedDisk \
  --source https://mystorageaccount.blob.core.windows.net/mydisks/myDisk.vhd

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

也可以在 Azure 中创建自定义的 VM,然后复制 OS 磁盘并将其附加到新 VM 以创建另一个副本。You can also create the 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, you really should 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 using the CLI

创建快照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. 在此示例中,我们将在标准存储中从快照创建大小为 128GB、名为 myManagedDisk 的托管磁盘。In this example, we will create a managed disk named myManagedDisk from our snapshot, that is 128GB in size in standard storage.

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)。Now, 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 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 using 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.