在 Azure Stack Hub 中创建 VM 磁盘存储Create VM disk storage in Azure Stack Hub

本文介绍如何使用 Azure Stack Hub 门户或 PowerShell 创建虚拟机 (VM) 磁盘存储。This article describes how to create virtual machine (VM) disk storage by using the Azure Stack Hub portal or by using PowerShell.

概述Overview

从版本 1808 开始,Azure Stack Hub 支持在 VM 上使用托管磁盘和非托管磁盘,作为操作系统 (OS) 磁盘和数据磁盘。Beginning with version 1808, Azure Stack Hub supports the use of managed disks and unmanaged disks in VMs, as both an operating system (OS) and a data disk. 在版本 1808 之前,仅支持非托管磁盘。Before version 1808, only unmanaged disks are supported.

托管磁盘通过管理与 VM 磁盘关联的存储帐户简化了 Azure IaaS VM 的磁盘管理。Managed disks simplify disk management for Azure IaaS VMs by managing the storage accounts associated with the VM disks. 只需指定所需的磁盘大小,Azure Stack Hub 即可为你创建和管理磁盘。You only have to specify the size of disk you need, and Azure Stack Hub creates and manages the disk for you.

非托管磁盘要求创建存储帐户用于存储磁盘。Unmanaged disks require that you create a storage account to store the disks. 你创建的磁盘称为 VM 磁盘并且存储在存储帐户中的容器中。The disks you create are referred to as VM disks and are stored in containers in the storage account.

最佳做法准则Best practice guidelines

建议对 VM 使用托管磁盘,以便更轻松地进行管理和容量均衡。It is recommended that you use managed disks for VMs for easier management and capacity balance. 不需要在使用托管磁盘之前准备存储帐户和容器。You don't have to prepare a storage account and containers before using managed disks. 创建多个托管磁盘时,会将这些磁盘分配到多个卷中,这有助于平衡卷的容量。When creating multiple managed disks, the disks are distributed into multiple volumes, which helps to balance the capacity of volumes.

对于非托管磁盘,为了改进性能并降低总体成本,建议将每个非托管磁盘置于单独的容器中。For unmanaged disks, to improve performance and reduce the overall costs, we recommend that you place each unmanaged disk in a separate container. 虽然可以将 OS 磁盘和数据磁盘放置在同一个容器中,但最佳做法是让一个容器容纳一个 OS 磁盘或一个数据磁盘,而不是同时容纳这二者。Although you can put both OS disks and data disks in the same container, the best practice is that one container should hold either an OS disk or a data disk, but not both at the same time.

如果向 VM 添加一个或多个数据磁盘,请使用更多的容器作为存储这些磁盘的位置。If you add one or more data disks to a VM, use additional containers as a location to store these disks. 附加 VM 的 OS 磁盘也应当位于其自己的容器中。The OS disk for additional VMs should also be in their own containers.

创建 VM 时,可将同一存储帐户重复用于每个新虚拟机。When you create VMs, you can reuse the same storage account for each new virtual machine. 只有创建的容器才应是唯一的。Only the containers you create should be unique.

添加新磁盘Adding new disks

下表概述了如何使用门户以及 PowerShell 添加磁盘:The following table summarizes how to add disks by using the portal, and by using PowerShell:

方法Method 选项Options
用户门户User portal - 向现有 VM 添加新的数据磁盘。- Add new data disks to an existing VM. 新磁盘由 Azure Stack Hub 创建。New disks are created by Azure Stack Hub.

- 将现有的磁盘 (.vhd) 文件添加到以前创建的 VM。- Add an existing disk (.vhd) file to a previously created VM. 若要执行此操作,必须准备 .vhd,然后将该文件上传到 Azure Stack Hub。To do so, you must prepare the .vhd and then upload the file to Azure Stack Hub.
PowerShellPowerShell - 使用 OS 磁盘创建新的 VM,同时向该 VM 添加一个或多个数据磁盘。- Create a new VM with an OS disk, and at the same time add one or more data disks to that VM.

使用门户向 VM 添加磁盘Use the portal to add disks to a VM

默认情况下,使用门户为大多数市场项创建 VM 时,只会创建 OS 磁盘。By default, when you use the portal to create a VM for most marketplace items, only the OS disk is created.

创建 VM 后,可以使用门户执行以下操作:After you create a VM, you can use the portal to:

  • 创建新的数据磁盘并将其附加到 VM。Create a new data disk and attach it to the VM.
  • 上传现有的数据磁盘并将其附加到 VM。Upload an existing data disk and attach it to the VM.

添加的每个非托管磁盘都应当放置在单独的容器中。Each unmanaged disk you add should be put in a separate container.

Note

由 Azure 创建并管理的磁盘称为托管磁盘Disks created and managed by Azure are called managed disks.

使用门户创建并附加新的数据磁盘Use the portal to create and attach a new data disk

  1. 在门户中,依次选择“所有服务”、“虚拟机”。 In the portal, select All services, then Virtual machines. 示例:VM 仪表板Example: VM dashboard

  2. 选择前面创建的 VM。Select a VM that has previously been created. 示例:在仪表板中选择 VMExample: Select a VM in the dashboard

  3. 对于 VM,依次选择“磁盘”、“添加数据磁盘”。 For the VM, select Disks, then Add data disk. 示例:将新磁盘附加到 VMExample: Attach a new disk to the vm

  4. 对于“数据磁盘”:For the Data disk:

    • 输入 LUNEnter the LUN. LUN 必须是有效的编号。The LUN must be a valid number.
    • 选择“创建磁盘”。Select Create disk. 示例:将新磁盘附加到 VMExample: Attach a new disk to the vm
  5. 在“创建托管磁盘”边栏选项卡中:In the Create managed disk blade:

    • 输入磁盘的名称Enter the Name of the disk.
    • 选择现有的资源组,或创建一个新的资源组。Select an existing Resource group or create a new one.
    • 选择“位置”。Select the Location. 默认情况下,位置设置为 OS 磁盘所在的同一容器。By default, the location is set to the same container that holds the OS disk.
    • 选择“帐户类型”。Select the Account type. 示例:将新磁盘附加到 VMExample: Attach a new disk to the vm

    Note

    高级磁盘 (SSD) 和标准磁盘 (HDD) 由 Azure Stack Hub 中的同一存储基础结构提供支持。Premium disks (SSD) and standard disks (HDD) are backed by the same storage infrastructure in Azure Stack Hub. 这两种磁盘提供相同的性能。They provide the same performance.

    • 选择“源类型”。Select the Source type.

      从其他磁盘的快照、存储帐户的 Blob 中创建磁盘,或创建空磁盘。Create a disk from a snapshot of another disk, a blob in a storage account, or create an empty disk.

      快照:选择一个快照(如果有)。Snapshot: Select a snapshot, if it's available. 该快照必须在 VM 的订阅和位置中可用。The snapshot must be in available in the VM's subscription and location.

      存储 BlobStorage blob:

      • 添加包含磁盘映像的存储 Blob 的 URI。Add the URI of the storage blob that contains the disk image.
      • 选择“浏览”打开“存储帐户”边栏选项卡。Select Browse to open the storage accounts blade. 有关说明,请参阅从存储帐户添加数据磁盘For instructions, see Add a data disk from a storage account.
      • 选择映像的 OS 类型:“Windows”、“Linux”或“无(数据磁盘)”。 Select the OS type of the image: Windows, Linux, or None (data disk).
    • 选择“大小(GiB)”。Select the Size (GiB).

      磁盘的费用会随着磁盘大小的增大而提高。Disk costs increase based on the size of the disk.

    • 选择“创建” 。Select Create. Azure Stack Hub 将创建并验证托管磁盘。Azure Stack Hub creates and validates the managed disk.

  6. 在 Azure Stack Hub 创建磁盘并将磁盘附加到 VM 之后,新磁盘列在“数据磁盘”下的 VM 磁盘设置中。After Azure Stack Hub creates the disk and attaches it to the VM, the new disk is listed in the VM disk settings under DATA DISKS.

    示例:查看磁盘

从存储帐户添加数据磁盘Add a data disk from a storage account

有关如何在 Azure Stack Hub 中使用存储帐户的详细信息,请参阅 Azure Stack Hub 存储简介For more information about working with storage accounts in Azure Stack Hub, see Introduction to Azure Stack Hub storage.

  1. 选择要使用的存储帐户Select the Storage account to use.

  2. 选择要在其中放置数据磁盘的容器Select the Container where you want to put the data disk. 在“容器”边栏选项卡中,可根据需要创建新的容器。From the Containers blade, you can create a new container if you want. 然后,可以将新磁盘的位置更改为其自己的容器。You can then change the location for the new disk to its own container. 为每个磁盘使用单独的容器时,数据磁盘的位置是分散的,这样可以改进性能。When you use a separate container for each disk, you distribute the placement of the data disk which improves performance.

  3. 选择“选择”以保存所做的选择。Choose Select to save the selection.

    示例:选择容器

将现有数据磁盘附加到 VMAttach an existing data disk to a VM

  1. 准备一个 .vhd 文件,用作 VM 的数据磁盘。Prepare a .vhd file for use as data disk for a VM. 将该 .vhd 文件上传到与要附加 .vhd 文件的 VM 一起使用的存储帐户。Upload that .vhd file to a storage account that you use with the VM to which you want to attach the .vhd file.

    示例:上传 VHD 文件

  2. 上传 .vhd 文件以后,即可将该 VHD 附加到 VM。After the .vhd file is uploaded, you're ready to attach the VHD to a VM. 在左侧菜单中选择“虚拟机”。In the menu on the left, select Virtual machines.
    示例:在仪表板中选择 VMExample: Select a VM in the dashboard

  3. 从列表中选择 VM。Choose the VM from the list.

    示例:在仪表板中选择 VM

  4. 在 VM 页上,依次选择“磁盘”、“附加现有”。 On the page for the VM, select Disks, then select Attach existing.

    示例:附加现有磁盘

  5. 在“附加现有磁盘”页面中,选择“VHD 文件”。 In the Attach existing disk page, select VHD File. 此时会打开“存储帐户”页。The Storage accounts page opens.

    示例:选择 VHD 文件

  6. 在“存储帐户”下选择要使用的帐户,然后选择一个容器,用于容纳以前上传的 .vhd 文件。Under Storage accounts, select the account to use, and then choose a container that holds the .vhd file you previously uploaded. 选择该 .vhd 文件,然后选择“选择”以保存所做的选择。Select the .vhd file, and then choose Select to save the selection.

    示例:选择容器

  7. 在“附加现有磁盘”下,选择的文件列在“VHD 文件”下。 Under Attach existing disk, the file you selected is listed under VHD File. 更新磁盘的“主机缓存”设置,然后选择“确定”以保存 VM 的新磁盘配置。 Update the Host caching setting of the disk, and then select OK to save the new disk configuration for the VM.

    示例:附加 VHD 文件

  8. 在 Azure Stack Hub 创建磁盘并将磁盘附加到 VM 之后,新磁盘列在“数据磁盘”下的 VM 磁盘设置中。After Azure Stack Hub creates the disk and attaches it to the VM, the new disk is listed in the VM's disk settings under Data Disks.

    示例:完成磁盘附加操作

使用 PowerShell 将多个磁盘添加到 VMUse PowerShell to add multiple disks to a VM

可以使用 PowerShell 来预配某个 VM,然后添加新的数据磁盘,或者附加预先存在的托管磁盘或 .vhd 文件作为数据磁盘。You can use PowerShell to provision a VM and add new data disks, or attach a pre-existing managed disk or .vhd file as a data disk.

Add-AzureRmVMDataDisk cmdlet 可以向 VM 添加数据磁盘。The Add-AzureRmVMDataDisk cmdlet adds a data disk to a VM. 可以在创建 VM 时添加数据磁盘,也可以向现有的 VM 添加数据磁盘。You can add a data disk when you create a VM, or you can add a data disk to an existing VM. 对于非托管磁盘,请指定 VhdUri 参数,将磁盘分配到不同的容器。For an unmanaged disk, specify the VhdUri parameter to distribute the disks to different containers.

将数据磁盘添加到 VMAdd data disks to a new VM

以下示例使用 PowerShell 命令创建包含三个数据磁盘的 VM。The following examples use PowerShell commands to create a VM with three data disks. 由于使用托管磁盘或非托管磁盘时存在细微差别,因此为此命令提供了多个部件。The commands are provided with several parts due to the minor differences when using managed disks or unmanaged disks.

创建虚拟机配置和网络资源Create virtual machine configuration and network resources

以下脚本创建 VM 对象,然后将它存储在 $VirtualMachine 变量中。The following script creates a VM object, and then stores it in the $VirtualMachine variable. 这些命令为 VM 指定名称和大小,然后创建 VM 的网络资源(虚拟网络、子网、虚拟网络适配器、NSG 和公共 IP 地址):The commands assign a name and size to the VM, then create the network resources (virtual network, subnet, virtual network adapter, NSG, and public IP address) for the VM:

# Create new virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName "VirtualMachine" `
                                      -VMSize "Standard_A2"

# Set variables
$rgName = "myResourceGroup"
$location = "local"

# Create a subnet configuration
$subnetName = "mySubNet"
$singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24

# Create a vnet configuration
$vnetName = "myVnetName"
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location `
                                  -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet

# Create a public IP
$ipName = "myIP"
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location `
                                  -AllocationMethod Dynamic

# Create a network security group configuration
$nsgName = "myNsg"
$rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name myRdpRule -Description "Allow RDP" `
                                                -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 `
                                                -SourceAddressPrefix Internet -SourcePortRange * `
                                                -DestinationAddressPrefix * -DestinationPortRange 3389
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location `
                                       -Name $nsgName -SecurityRules $rdpRule

# Create a NIC configuration
$nicName = "myNicName"
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName `
                                   -Location $location -SubnetId $vnet.Subnets[0].Id `
                                   -NetworkSecurityGroupId $nsg.Id -PublicIpAddressId $pip.Id

添加托管磁盘Add managed disks

下面的三个命令将托管数据磁盘添加到 $VirtualMachine 中存储的虚拟机。The following three commands add managed data disks to the virtual machine stored in $VirtualMachine. 每个命令都会指定磁盘的名称和其他属性:Each command specifies the name and additional properties of the disk:

$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk1' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 0 `
                                        -CreateOption Empty
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk2' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 1 `
                                        -CreateOption Empty
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk3' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 2 `
                                        -CreateOption Empty

以下命令将 OS 磁盘作为托管磁盘添加到 $VirtualMachine 中存储的虚拟机。The following command adds an OS disk as a managed disk to the virtual machine stored in $VirtualMachine.

# Set OS Disk
$osDiskName = "osDisk"
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $osDiskName  `
                                      -CreateOption FromImage -Windows

添加非托管磁盘Add unmanaged disks

接下来的三个命令将三个非托管数据磁盘的路径分配给 $DataDiskVhdUri01$DataDiskVhdUri02$DataDiskVhdUri03 变量。The next three commands assign paths of three unmanaged data disks to the $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03 variables. 在 URL 中定义另一路径名称,使磁盘分布到不同的容器:Define a different path name in the URL to distribute the disks to different containers:

$DataDiskVhdUri01 = "https://contoso.blob.local.azurestack.external/test1/data1.vhd"
$DataDiskVhdUri02 = "https://contoso.blob.local.azurestack.external/test2/data2.vhd"
$DataDiskVhdUri03 = "https://contoso.blob.local.azurestack.external/test3/data3.vhd"

下面的三个命令将数据磁盘添加到 $VirtualMachine 中存储的虚拟机。The following three commands add data disks to the virtual machine stored in $VirtualMachine. 每个命令都会指定磁盘的名称和其他属性。Each command specifies the name, and additional properties of the disk. 每个磁盘的 URI 分别存储在 $DataDiskVhdUri01$DataDiskVhdUri02$DataDiskVhdUri03 中:The URI of each disk is stored in $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03:

$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk1' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 10 -Lun 0 `
                                        -VhdUri $DataDiskVhdUri01 -CreateOption Empty
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk2' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 11 -Lun 1 `
                                        -VhdUri $DataDiskVhdUri02 -CreateOption Empty
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name 'DataDisk3' `
                                        -Caching 'ReadOnly' -DiskSizeInGB 12 -Lun 2 `
                                        -VhdUri $DataDiskVhdUri03 -CreateOption Empty

以下命令将非托管 OS 磁盘添加到 $VirtualMachine 中存储的虚拟机。The following commands add an unmanaged OS disk to the virtual machine stored in $VirtualMachine.

# Set OS Disk
$osDiskUri = "https://contoso.blob.local.azurestack.external/vhds/osDisk.vhd"
$osDiskName = "osDisk"
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $osDiskName -VhdUri $osDiskUri `
                                      -CreateOption FromImage -Windows

创建新的虚拟机Create new virtual machine

使用以下 PowerShell 命令设置 OS 映像,将网络配置添加到 VM,然后启动新 VM:Use the following PowerShell commands to set OS image, add network configuration to the VM, and then start the new VM:

#Create the new VM
$VirtualMachine = Set-AzureRmVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName VirtualMachine -ProvisionVMAgent | `
                  Set-AzureRmVMSourceImage -PublisherName MicrosoftWindowsServer -Offer WindowsServer `
                  -Skus 2016-Datacenter -Version latest | Add-AzureRmVMNetworkInterface -Id $nic.Id

New-AzureRmVM -ResourceGroupName $rgName -Location $location -VM $VirtualMachine

向现有 VM 添加新的数据磁盘Add data disks to an existing VM

以下示例使用 PowerShell 命令将三个数据磁盘添加到现有 VM:The following examples use PowerShell commands to add three data disks to an existing VM:

获取虚拟机Get virtual machine

第一个命令通过 Get-AzureRmVM cmdlet 获取名为 VirtualMachine 的 VM。The first command gets the VM named VirtualMachine by using the Get-AzureRmVM cmdlet. 此命令将 VM 存储在 $VirtualMachine 变量中:The command stores the VM in the $VirtualMachine variable:

$VirtualMachine = Get-AzureRmVM -ResourceGroupName "myResourceGroup" `
                                -Name "VirtualMachine"

添加托管磁盘Add managed disk

接下来的三个命令将托管数据磁盘添加到 $VirtualMachine 变量中存储的 VM。The next three commands add the managed data disks to the VM stored in the $VirtualMachine variable. 每个命令都会指定磁盘的名称和其他属性:Each command specifies the name and additional properties of the disk:

Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk1" -Lun 0 `
                      -Caching ReadOnly -DiskSizeinGB 10 -CreateOption Empty
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk2" -Lun 1 `
                      -Caching ReadOnly -DiskSizeinGB 11 -CreateOption Empty
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk3" -Lun 2 `
                      -Caching ReadOnly -DiskSizeinGB 12 -CreateOption Empty

添加非托管磁盘Add unmanaged disk

接下来的三个命令将三个数据磁盘的路径分配给 $DataDiskVhdUri01$DataDiskVhdUri02$DataDiskVhdUri03 变量。The next three commands assign paths for three data disks to the $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03 variables. VHD URI 中的不同路径名称表示用于放置磁盘的不同容器。The different path names in the VHD URIs indicate different containers for the disk placement:

$DataDiskVhdUri01 = "https://contoso.blob.local.azurestack.external/test1/data1.vhd"
$DataDiskVhdUri02 = "https://contoso.blob.local.azurestack.external/test2/data2.vhd"
$DataDiskVhdUri03 = "https://contoso.blob.local.azurestack.external/test3/data3.vhd"

接下来的三个命令将数据磁盘添加到 $VirtualMachine 变量中存储的 VM。The next three commands add the data disks to the VM stored in the $VirtualMachine variable. 每个命令都会指定磁盘的名称、位置和其他属性。Each command specifies the name, location, and additional properties of the disk. 每个磁盘的 URI 分别存储在 $DataDiskVhdUri01$DataDiskVhdUri02$DataDiskVhdUri03 中:The URI of each disk is stored in $DataDiskVhdUri01, $DataDiskVhdUri02, and $DataDiskVhdUri03:

Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk1" `
                      -VhdUri $DataDiskVhdUri01 -LUN 0 `
                      -Caching ReadOnly -DiskSizeinGB 10 -CreateOption Empty
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk2" `
                      -VhdUri $DataDiskVhdUri02 -LUN 1 `
                      -Caching ReadOnly -DiskSizeinGB 11 -CreateOption Empty
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name "DataDisk3" `
                      -VhdUri $DataDiskVhdUri03 -LUN 2 `
                      -Caching ReadOnly -DiskSizeinGB 12 -CreateOption Empty

更新虚拟机状态Update virtual machine state

以下命令更新 -ResourceGroupName$VirtualMachine 中存储的 VM 的状态:This command updates the state of the VM stored in $VirtualMachine in -ResourceGroupName:

Update-AzureRmVM -ResourceGroupName "myResourceGroup" -VM $VirtualMachine

后续步骤Next steps

若要详细了解 Azure Stack Hub VM,请参阅 Azure Stack Hub 中虚拟机的注意事项To learn more about Azure Stack Hub VMs, see Considerations for Virtual Machines in Azure Stack Hub.