通过 Azure CLI 从自定义磁盘上传并创建 Linux VMUpload and create a Linux VM from custom disk with the Azure CLI

本文介绍如何通过 Azure CLI 将虚拟硬盘 (VHD) 上传到 Azure 存储帐户,并从此自定义磁盘创建 Linux VM。This article shows you how to upload a virtual hard disk (VHD) to an Azure storage account with the Azure CLI and create Linux VMs from this custom disk. 此功能可让你安装并配置 Linux 分发以满足需求,然后使用该 VHD 快速创建 Azure 虚拟机 (VM)。This functionality allows you to install and configure a Linux distro to your requirements and then use that VHD to quickly create Azure virtual machines (VMs).

本主题使用存储帐户保存最终 VHD,但也可以使用托管磁盘执行这些步骤。This topic uses storage accounts for the final VHDs, but you can also do these steps using managed disks.

快速命令Quick commands

如果需要快速完成任务,请参阅以下部分,其中详细说明了用于将 VHD 上传到 Azure 的基本命令。If you need to quickly accomplish the task, the following section details the base commands to upload a VHD to Azure. 本文档的余下部分(从此处开始)提供了每个步骤的更详细信息和上下文。More detailed information and context for each step can be found the rest of the document, starting here.

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

备注

在 Azure China 中使用 Azure CLI 2.0 之前,请首先运行 az cloud set -n AzureChinaCloud 更改云环境。Before you can use Azure CLI 2.0 in Azure China, please run az cloud set -n AzureChinaCloud first to change the cloud environment. 如果要切换回全局 Azure,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Global Azure, run az cloud set -n AzureCloud again.

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

首先,使用 az group create 创建资源组。First, create a resource group with az group create. 以下示例在 chinanorth 位置创建名为 myResourceGroup 的资源组:The following example creates a resource group named myResourceGroup in the chinanorth location:

az group create --name myResourceGroup --location chinanorth

使用 az storage account create 创建一个用于存放虚拟磁盘的存储帐户。Create a storage account to hold your virtual disks with az storage account create. 以下示例创建名为 mystorageaccount 的存储帐户:The following example creates a storage account named mystorageaccount:

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

使用 az storage account keys list 列出存储帐户的访问密钥。List the access keys for your storage account with az storage account keys list. 记下 key1Make a note of key1:

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

通过 az storage container create 使用获取的存储密钥在存储帐户中创建一个容器。Create a container within your storage account using the storage key you obtained with az storage container create. 以下示例使用 key1 中的存储密钥值创建名为 mydisks 的容器:The following example creates a container named mydisks using the storage key value from key1:

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

最后,使用 az storage blob upload 将 VHD 上传到创建的容器。Finally, upload your VHD to the container you created with az storage blob upload. /path/to/disk/mydisk.vhd 下指定 VHD 的本地路径:Specify the local path to your VHD under /path/to/disk/mydisk.vhd:

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

使用 az vm create 指定磁盘的 URI (--image)。Specify the URI to your disk (--image) with az vm create. 以下示例使用前面上载的虚拟磁盘创建名为 myVM 的 VM:The following example creates a VM named myVM using the virtual disk previously uploaded:

az vm create --resource-group myResourceGroup --location chinanorth \
    --name myVM --storage-account mystorageaccount --os-type linux \
    --admin-username azureuser --ssh-key-value ~/.ssh/id_rsa.pub \
    --image https://mystorageaccount.blob.core.chinacloudapi.cn/mydisk/myDisks.vhd \
    --use-unmanaged-disk

目标存储帐户必须与虚拟磁盘上传到的位置相同。The destination storage account has to be the same as where you uploaded your virtual disk to. 还需要指定或根据提示输入 az vm create 命令所需的所有其他参数,例如虚拟网络、公共 IP 地址、用户名和 SSH 密钥。You also need to specify, or answer prompts for, all the additional parameters required by the az vm create command such as virtual network, public IP address, username, and SSH keys. 阅读有关可用经典 CLI Resource Manager 参数的详细信息。You can read more about the available classic CLI Resource Manager parameters.

要求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.

  • 从自定义磁盘创建的 VM 必须位于磁盘本身所在的存储帐户中VMs created from your custom disk must reside in the same storage account as the disk itself
    • 创建存储帐户和容器,用于存放自定义磁盘以及所创建的 VMCreate a storage account and container to hold both your custom disk and created VMs
    • 创建所有 VM 之后,可以安全地删除磁盘After you have created all your VMs, you can safely delete your disk

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

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

准备要上传的磁盘Prepare the disk to be uploaded

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.

创建资源组Create a resource group

资源组以逻辑方式将所有 Azure 资源(例如虚拟网络和存储)聚集在一起,以支持虚拟机。Resource groups logically bring together all the Azure resources to support your virtual machines, such as the virtual networking and storage. 有关资源组的详细信息,请参阅资源组概述For more information resource groups, see resource groups overview. 在上载自定义磁盘和创建 VM 之前,首先需要使用 az group create 创建一个资源组。Before uploading your custom disk and creating VMs, you first need to create a resource group with az group create.

以下示例在 chinanorth 位置创建名为 myResourceGroup 的资源组:The following example creates a resource group named myResourceGroup in the chinanorth location:

az group create --name myResourceGroup --location chinanorth

创建存储帐户Create a storage account

可以使用 az storage account create 为自定义磁盘和 VM 创建存储帐户。Create a storage account for your custom disk and VMs with az storage account create. 从自定义磁盘创建的、使用非托管磁盘的所有 VM 都必须位于该磁盘所在的同一存储帐户中。Any VMs with unmanaged disks that you create from your custom disk need to be in the same storage account as that disk.

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

az storage account create --resource-group myResourceGroup --location chinanorth \
  --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, such as to carry out write operations. 有关存储帐户访问密钥的详细信息,请参阅管理存储帐户访问密钥For more information about storage account access keys, see Manage storage account access keys. 可使用 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 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

创建 VMCreate the VM

若要使用非托管磁盘创建 VM,请使用 az vm create 指定磁盘的 URI (--image)。To create a VM with unmanaged disks, specify the URI to your disk (--image) with az vm create. 以下示例使用前面上载的虚拟磁盘创建名为 myVM 的 VM:The following example creates a VM named myVM using the virtual disk previously uploaded:

az vm create 中指定 --image 参数,指向自定义磁盘。You specify the --image parameter with az vm create to point to your custom disk. 确保 --storage-account 与用于存储自定义磁盘的存储帐户匹配。Ensure that --storage-account matches the storage account where your custom disk is stored. 不需要使用与自定义磁盘相同的容器来存储 VM。You do not have to use the same container as the custom disk to store your VMs. 上传自定义磁盘之前,请确保使用前面步骤中所述的相同方式创建任何附加容器。Make sure to create any additional containers in the same way as the earlier steps before uploading your custom disk.

以下示例从自定义磁盘创建名为 myVM 的 VM:The following example creates a VM named myVM from your custom disk:

az vm create --resource-group myResourceGroup --location chinanorth \
    --name myVM --storage-account mystorageaccount --os-type linux \
    --admin-username azureuser --ssh-key-value ~/.ssh/id_rsa.pub \
    --image https://mystorageaccount.blob.core.chinacloudapi.cn/mydisks/myDisk.vhd \
    --use-unmanaged-disk

仍需要指定或根据提示输入 az vm create 命令所需的所有其他参数,例如用户名和 SSH 密钥。You still need to specify, or answer prompts for, all the additional parameters required by the az vm create command such as username and SSH keys.

资源管理器模板Resource Manager template

Azure 资源管理器模板是用于定义希望生成的环境的 JavaScript 对象表示法 (JSON) 文件。Azure Resource Manager templates are JavaScript Object Notation (JSON) files that define the environment you wish to build. 这些模板细分为不同的资源提供程序,如计算或网络。The templates are broken down in to different resource providers such as compute or network. 可以使用现有模板,也可以编写自己的模板。You can use existing templates or write your own. 阅读有关使用 Resource Manager 和模板的详细信息。Read more about using Resource Manager and templates.

在模板的 Microsoft.Compute/virtualMachines 提供程序中有一个 storageProfile 节点,其中包含 VM 的配置详细信息。Within the Microsoft.Compute/virtualMachines provider of your template, you have a storageProfile node that contains the configuration details for your VM. 需要编辑的两个主要参数为 imagevhd URI,它们指向自定义磁盘和新 VM 的虚拟磁盘。The two main parameters to edit are the image and vhd URIs that point to your custom disk and your new VM's virtual disk. 下面显示了使用自定义磁盘的 JSON 示例:The following shows an example of the JSON for using a custom disk:

"storageProfile": {
          "osDisk": {
            "name": "myVM",
            "osType": "Linux",
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "image": {
              "uri": "https://mystorageaccount.blob.core.chinacloudapi.cn/mydisks/myDisk.vhd"
            },
            "vhd": {
              "uri": "https://mystorageaccount.blob.core.chinacloudapi.cn/vhds/newvmname.vhd"
            }
          }

可以使用此现有模板从自定义映像创建 VM,或阅读有关创建自己的 Azure 资源管理器模板的信息。You can use this existing template to create a VM from a custom image or read about creating your own Azure Resource Manager templates.

配置模板之后,使用 az group deployment create 创建 VM。Once you have a template configured, use az group deployment create to create your VMs. 使用 --template-uri 参数指定 JSON 模板的 URI:Specify the URI of your JSON template with the --template-uri parameter:

az group deployment create --resource-group myNewResourceGroup \
  --template-uri https://uri.to.template/mytemplate.json

如果在计算机上以本地方式存储了一个 JSON 文件,则可以改用 --template-file 参数:If you have a JSON file stored locally on your computer, you can use the --template-file parameter instead:

az group deployment create --resource-group myNewResourceGroup \
  --template-file /path/to/mytemplate.json

后续步骤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.