如何扩展虚拟机的 OS 驱动器How to expand the OS drive of a virtual machine

在资源组中通过从 Azure 市场部署映像来创建新的虚拟机 (VM) 时,默认的 OS 驱动器通常为 127 GB(默认情况下,某些映像的 OS 磁盘大小更小)。When you create a new virtual machine (VM) in a Resource Group by deploying an image from Azure Marketplace, the default OS drive is often 127 GB (some images have smaller OS disk sizes by default). 尽管可以将数据磁盘添加到 VM(数量取决于所选择的 SKU),并且我们建议将应用程序和需要大量 CPU 的工作负荷安装在这些附加的磁盘上,但客户有时候还是需要扩展 OS 驱动器以支持特定的方案,例如:Even though it's possible to add data disks to the VM (how many depending upon the SKU you've chosen) and moreover it's recommended to install applications and CPU intensive workloads on these addendum disks, oftentimes customers need to expand the OS drive to support certain scenarios such as following:

  • 支持将组件安装在 OS 驱动器上的传统应用程序。Support legacy applications that install components on OS drive.
  • 从本地迁移具有较大 OS 驱动器的物理电脑或虚拟机。Migrate a physical PC or virtual machine from on-premises with a larger OS drive.

Important

若要调整 Azure 虚拟机的 OS 磁盘大小,需要解除分配虚拟机。Resizing the OS Disk of an Azure Virtual Machine requires the virtual machine to be deallocated.

扩展磁盘后,需要扩展 OS 中的卷才能使用更大的磁盘。After expanding the disks, you need to expand the volume within the OS to take advantage of the larger disk.

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

调整托管磁盘的大小Resize a managed disk

在管理模式下打开 Powershell ISE 或 Powershell 窗口,并遵循以下步骤:Open your Powershell ISE or Powershell window in administrative mode and follow the steps below:

  1. 在资源管理模式下登录 Azure 帐户,然后选择订阅,如下所示:Sign in to your Azure account in resource management mode and select your subscription as follows:

    Connect-AzAccount -Environment AzureChinaCloud
    Select-AzSubscription -SubscriptionName 'my-subscription-name'
    
  2. 设置资源组名称和 VM 名称,如下所示:Set your resource group name and VM name as follows:

    $rgName = 'my-resource-group-name'
    $vmName = 'my-vm-name'
    
  3. 获取对 VM 的引用,如下所示:Obtain a reference to your VM as follows:

    $vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
    
  4. 在调整磁盘大小之前停止 VM,如下所示:Stop the VM before resizing the disk as follows:

    Stop-AzVM -ResourceGroupName $rgName -Name $vmName
    
  5. 获取对托管 OS 磁盘的引用。Obtain a reference to the managed OS disk. 将托管 OS 磁盘的大小设置为所需值,并更新磁盘,如下所示:Set the size of the managed OS disk to the desired value and update the Disk as follows:

    $disk= Get-AzDisk -ResourceGroupName $rgName -DiskName $vm.StorageProfile.OsDisk.Name
    $disk.DiskSizeGB = 1023
    Update-AzDisk -ResourceGroupName $rgName -Disk $disk -DiskName $disk.Name
    

    Warning

    新大小应该大于现有磁盘大小。The new size should be greater than the existing disk size. 对于 OS 磁盘来说,允许的最大值为 2048 GB。The maximum allowed is 2048 GB for OS disks. (可以扩展 VHD Blob,使之超出该大小,但 OS 只能使用空间的头 2048 GB。)(It is possible to expand the VHD blob beyond that size, but the OS will only be able to work with the first 2048 GB of space.)

  6. 更新 VM 可能需要几秒钟时间。Updating the VM may take a few seconds. 命令完成执行后,请重新启动 VM,如下所示:Once the command finishes executing, restart the VM as follows:

    Start-AzVM -ResourceGroupName $rgName -Name $vmName
    

大功告成!And that's it! 现在,请通过 RDP 访问 VM,打开“计算机管理”(或“磁盘管理”),然后使用刚刚分配的空间扩展驱动器。Now RDP into the VM, open Computer Management (or Disk Management) and expand the drive using the newly allocated space.

非托管磁盘的大小Resize an unmanaged disk

在管理模式下打开 Powershell ISE 或 Powershell 窗口,并遵循以下步骤:Open your Powershell ISE or Powershell window in administrative mode and follow the steps below:

  1. 在资源管理模式下登录 Azure 帐户,然后选择订阅,如下所示:Sign in to your Azure account in resource management mode and select your subscription as follows:

    Connect-AzAccount -Environment AzureChinaCloud
    Select-AzSubscription -SubscriptionName 'my-subscription-name'
    
  2. 设置资源组名称和 VM 名称,如下所示:Set your resource group name and VM name as follows:

    $rgName = 'my-resource-group-name'
    $vmName = 'my-vm-name'
    
  3. 获取对 VM 的引用,如下所示:Obtain a reference to your VM as follows:

    $vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
    
  4. 在调整磁盘大小之前停止 VM,如下所示:Stop the VM before resizing the disk as follows:

    Stop-AzVM -ResourceGroupName $rgName -Name $vmName
    
  5. 将非托管 OS 磁盘的大小设置为所需值,并更新 VM,如下所示:Set the size of the unmanaged OS disk to the desired value and update the VM as follows:

    $vm.StorageProfile.OSDisk.DiskSizeGB = 1023
    Update-AzVM -ResourceGroupName $rgName -VM $vm
    

    Warning

    新大小应该大于现有磁盘大小。The new size should be greater than the existing disk size. 对于 OS 磁盘来说,允许的最大值为 2048 GB。The maximum allowed is 2048 GB for OS disks. (可以扩展 VHD Blob,使之超出该大小,但 OS 只能使用空间的头 2048 GB。)(It is possible to expand the VHD blob beyond that size, but the OS will only be able to work with the first 2048 GB of space.)

  6. 更新 VM 可能需要几秒钟时间。Updating the VM may take a few seconds. 命令完成执行后,请重新启动 VM,如下所示:Once the command finishes executing, restart the VM as follows:

    Start-AzVM -ResourceGroupName $rgName -Name $vmName
    

OS 磁盘的脚本Scripts for OS disk

以下是同时适用于托管磁盘和非托管磁盘的引用的完整脚本:Below is the complete script for your reference for both managed and unmanaged disks:

托管磁盘Managed disks

Connect-AzAccount -Environment AzureChinaCloud
Select-AzSubscription -SubscriptionName 'my-subscription-name'
$rgName = 'my-resource-group-name'
$vmName = 'my-vm-name'
$vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
Stop-AzVM -ResourceGroupName $rgName -Name $vmName
$disk= Get-AzDisk -ResourceGroupName $rgName -DiskName $vm.StorageProfile.OsDisk.Name
$disk.DiskSizeGB = 1023
Update-AzDisk -ResourceGroupName $rgName -Disk $disk -DiskName $disk.Name
Start-AzVM -ResourceGroupName $rgName -Name $vmName

非托管磁盘Unmanaged disks

Connect-AzAccount -Environment AzureChinaCloud
Select-AzSubscription -SubscriptionName 'my-subscription-name'
$rgName = 'my-resource-group-name'
$vmName = 'my-vm-name'
$vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
Stop-AzVM -ResourceGroupName $rgName -Name $vmName
$vm.StorageProfile.OSDisk.DiskSizeGB = 1023
Update-AzVM -ResourceGroupName $rgName -VM $vm
Start-AzVM -ResourceGroupName $rgName -Name $vmName

调整数据磁盘的大小Resizing data disks

虽然本文重介绍扩展 VM 的 OS 磁盘,但该脚本也可用于扩展附加到 VM 的数据磁盘。This article is focused primarily on expanding the OS disk of the VM, but the script can also be used for expanding the data disks attached to the VM. 例如,若要扩展附加到 VM 的第一个数据磁盘,请将 StorageProfileOSDisk 对象替换为 DataDisks 数组,并使用数字索引获取对第一个附加数据磁盘的引用,如下所示:For example, to expand the first data disk attached to the VM, replace the OSDisk object of StorageProfile with DataDisks array and use a numeric index to obtain a reference to first attached data disk, as shown below:

托管磁盘Managed disk

$disk= Get-AzDisk -ResourceGroupName $rgName -DiskName $vm.StorageProfile.DataDisks[0].Name
$disk.DiskSizeGB = 1023

非托管磁盘Unmanaged disk

$vm.StorageProfile.DataDisks[0].DiskSizeGB = 1023

同样,如上所示使用索引或磁盘的“名称属性”可以引用附加到 VM 的其他数据磁盘 :Similarly you may reference other data disks attached to the VM, either by using an index as shown above or the Name property of the disk:

托管磁盘Managed disk

(Get-AzDisk -ResourceGroupName $rgName -DiskName ($vm.StorageProfile.DataDisks | Where ({$_.Name -eq 'my-second-data-disk'})).Name).DiskSizeGB = 1023

非托管磁盘Unmanaged disk

($vm.StorageProfile.DataDisks | Where ({$_.Name -eq 'my-second-data-disk'}).DiskSizeGB = 1023

扩展 OS 中的卷Expand the volume within the OS

展开 VM 的磁盘后,需要转到 OS 并展开卷以包含新的空间。Once you have expanded the disk for the VM, you need to go into the OS and expand the volume to encompass the new space. 以下几种方法可用于扩展分区。There are several methods for expanding a partition. 本部分介绍了如何使用 RDP 连接来连接 VM 以使用 DiskPart 展开分区 。This section covers connecting the VM using an RDP connection to expand the partition using DiskPart.

  1. 打开与 VM 的 RDP 连接。Open an RDP connection to your VM.

  2. 打开命令提示符并键入 diskpart Open a command prompt and type diskpart.

  3. 在 DISKPART 提示符处,键入 list volumeAt the DISKPART prompt, type list volume. 记下要扩展的卷。Make note of the volume you want to extend.

  4. 在 DISKPART 提示符处,键入 select volume <volumenumber>At the DISKPART prompt, type select volume <volumenumber>. 这将选择将扩展到同一磁盘上的连续可用空间的卷 volumenumber 。This selects the volume volumenumber that you want to extend into contiguous, empty space on the same disk.

  5. 在 DISKPART 提示符处,键入 extend [size=<size>]At the DISKPART prompt, type extend [size=<size>]. 这将按大小(MB)扩展所选的卷 。This extends the selected volume by size in megabytes (MB).

后续步骤Next steps

还可以使用 Azure 门户来附加磁盘。You can also attach disks using the Azure portal.