如何扩展虚拟机的 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 (the number depends on the SKU you chose) and we recommend installing applications and CPU-intensive workloads on these addendum disks, often, customers need to expand the OS drive to support specific scenarios:

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

重要

若要重设 Azure 虚拟机的 OS 磁盘或数据磁盘的大小,需要将虚拟机解除分配。Resizing an OS or Data Disk of an Azure Virtual Machine requires the virtual machine to be deallocated.

不支持收缩现有磁盘,收缩现有磁盘可能导致数据丢失。Shrinking an existing disk isn't supported, and can potentially result in data loss.

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

重设 Azure 门户中的托管磁盘的大小Resize a managed disk in the Azure portal

  1. Azure 门户中,转到要在其中扩展磁盘的虚拟机。In the Azure portal, go to the virtual machine in which you want to expand the disk. 选择“停止”以解除分配 VM。Select Stop to deallocate the VM.

  2. 停止 VM 后,在“设置”下的左侧菜单中,选择“磁盘” 。When the VM is stopped, in the left menu under Settings, select Disks.

    显示在菜单的“设置”部分中选择了“磁盘”选项的屏幕截图。

  3. 在“磁盘名称”下,选择要调整大小的磁盘。Under Disk name, select the disk you want to resize.

    显示“磁盘”窗格的屏幕截图,其中选择了一个磁盘名称。

  4. 在左侧菜单中的“设置”下,选择“大小 + 性能” 。In the left menu under Settings, select Size + performance.

:::image type="content" source="./media/expand-os-disk/configuration.png" alt-text="Screenshot that shows the Configuration option selected in the Settings section of the menu.":::
  1. 在“大小 (GiB)”中,选择所需的磁盘大小。In Size (GiB), select the disk size you want.

    警告

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

    显示“配置”窗格的屏幕截图,其中选择了磁盘大小。

  2. 选择“调整大小”。Select Resize.

:::image type="content" source="./media/expand-os-disk/save.png" alt-text="Screenshot that shows the Configuration pane with the Save button selected.":::

使用 PowerShell 重设托管磁盘的大小Resize a managed disk by using PowerShell

在管理模式下打开 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:

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

    $rgName = 'my-resource-group-name'
    $vmName = 'my-vm-name'
    
  3. 获取对 VM 的引用:Obtain a reference to your VM:

    $vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
    
  4. 在重设磁盘大小之前停止 VM:Stop the VM before resizing the disk:

    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:

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

    警告

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

  6. 更新 VM 可能需要几秒钟时间。Updating the VM might take a few seconds. 命令执行完后,请重启 VM:When the command finishes executing, restart the VM:

    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.

使用 PowerShell 重设非托管磁盘的大小Resize an unmanaged disk by using PowerShell

在管理模式下打开 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:

    Connect-AzAccount -Environment AzureChinaCloud
    Select-AzSubscription -SubscriptionName 'my-subscription-name'
    
  2. 设置资源组名称和 VM 名称:Set your resource group name and VM names:

    $rgName = 'my-resource-group-name'
    $vmName = 'my-vm-name'
    
  3. 获取对 VM 的引用:Obtain a reference to your VM:

    $vm = Get-AzVM -ResourceGroupName $rgName -Name $vmName
    
  4. 在重设磁盘大小之前停止 VM:Stop the VM before resizing the disk:

    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:

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

    警告

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

  6. 更新 VM 可能需要几秒钟时间。Updating the VM might take a few seconds. 命令执行完后,请重启 VM:When the command finishes executing, restart the VM:

    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

同样,可以使用索引(如上所示)或磁盘的“Name”属性引用附加到 VM 的其他数据磁盘:Similarly, you can 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 并扩展卷,使之包含新的空间。When 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. 打开命令提示符并键入 diskpartOpen 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.