使用 CLI 基于相同订阅的存储帐户中的 VHD 文件创建托管磁盘 (Linux)

此脚本基于相同订阅的存储帐户中的 VHD 文件创建托管磁盘。 使用此脚本将专用(未经过通用化/sysprep 处理)的 VHD 导入到托管 OS 磁盘以创建虚拟机。 或者使用它将数据 VHD 导入到托管数据磁盘。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

先决条件

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用的是本地安装,请使用 az login 命令登录 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

示例脚本

登录 Azure

使用以下脚本通过其他订阅登录,将 <Subscription ID> 替换为 Azure 订阅 ID。 如果没有 Azure 试用版订阅,请在开始前创建 Azure 试用版订阅

az cloud set -n AzureChinaCloud
az login

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

有关详细信息,请参阅设置有效的订阅登录

运行脚本

# Verified per Raman Kumar as of 2/23/2022

# <FullScript>
#Provide the subscription Id
subscriptionId="<subscriptionId>"

#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

#If you're creating an OS disk, uncomment the following lines and replace the values
#osType = 'yourOSType' #Acceptable values are either Windows or Linux
#hyperVGeneration = 'yourHyperVGen' #Acceptable values are either v1 or v2

#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 
#If you're creating an OS disk, add the following: --os-type $osType -hyper-v-generation $hyperVGeneration 
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --location $location --size-gb $diskSize --source $vhdUri
# </FullScript>

清理资源

运行以下命令来删除资源组、VM 和所有相关资源。

az group delete --name myResourceGroupName

示例参考

此脚本使用以下命令基于 VHD 创建托管磁盘。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az disk create 使用相同订阅的存储帐户中的 VHD 的 URI 创建托管磁盘

后续步骤

通过将托管磁盘附加为 OS 磁盘来创建虚拟机

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。