使用 CLI 基于相同订阅的存储帐户中的 VHD 文件创建托管磁盘 (Linux)Create a managed disk from a VHD file in a storage account in the same subscription with CLI (Linux)

此脚本基于相同订阅的存储帐户中的 VHD 文件创建托管磁盘。This script creates a managed disk from a VHD file in a storage account in the same subscription. 使用此脚本将专用(未经过通用化/sysprep 处理)的 VHD 导入到托管 OS 磁盘以创建虚拟机。Use this script to import a specialized (not generalized/sysprepped) VHD to managed OS disk to create a virtual machine. 或者使用它将数据 VHD 导入到托管数据磁盘。Or, use it to import a data VHD to managed data disk.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

示例脚本Sample script

#Provide the subscription Id
subscriptionId=mySubscriptionId

#Provide the name of your resource group.
#Ensure that resource group is already created 
resourceGroupName=myResourceGroupName

#Provide the name of the Managed Disk
diskName=myDiskName

#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128

#Provide the URI of the VHD file that will be used to create Managed Disk. 
# VHD file can be deleted as soon as Managed Disk is created.
# e.g. https://contosostorageaccount1.blob.core.chinacloudapi.cn/vhds/contosovhd123.vhd 
vhdUri=https://contosostorageaccount1.blob.core.chinacloudapi.cn/vhds/contosoumd78620170425131836.vhd

#Provide the storage type for the Managed Disk. Premium_LRS or Standard_LRS.
storageType=Premium_LRS

#Provide the Azure location (e.g. chinanorth) where Managed Disk will be located. 
#The location should be same as the location of the storage account where VHD file is stored.
#Get all the Azure location supported for your subscription using command below:
#az account list-locations
location=chinanorth

#Set the context to the subscription Id where Managed Disk will be created
az account set --subscription $subscriptionId

#Create the Managed disk from the VHD file 
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --location $location --size-gb $diskSize --source $vhdUri

脚本说明Script explanation

此脚本使用以下命令基于 VHD 创建托管磁盘。This script uses following commands to create a managed disk from a VHD. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az disk createaz disk create 使用相同订阅的存储帐户中的 VHD 的 URI 创建托管磁盘Creates a managed disk using URI of a VHD in a storage account in the same subscription

后续步骤Next steps

通过将托管磁盘附加为 OS 磁盘来创建虚拟机Create a virtual machine by attaching a managed disk as OS disk

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.