通过 Azure 门户将 OS 磁盘附加到恢复 VM,对 Windows VM 进行故障排除Troubleshoot a Windows VM by attaching the OS disk to a recovery VM through the Azure portal

如果 Azure 中的 Windows 虚拟机 (VM) 遇到启动错误或磁盘错误,可能需要对虚拟硬盘 (VHD) 执行故障排除步骤。If your Windows virtual machine (VM) in Azure encounters a startup or disk error, you might need to perform troubleshooting steps on the virtual hard disk (VHD). 一个常见示例是应用程序更新失败,导致 VM 无法成功启动。A common example is a failed application update that prevents the VM from starting successfully. 本文详细介绍如何使用 Azure 门户将虚拟硬盘连接到另一个 Windows VM 来修复所有错误,然后重新创建原始 VM。This article details how to use the Azure portal to connect your virtual hard disk to another Windows VM to fix any errors, and then re-create your original VM.

恢复过程概述Recovery process overview

故障排除过程如下:The troubleshooting process is as follows:

  1. 停止受影响的 VM。Stop the affected VM.
  2. 为 VM 的 OS 磁盘创建快照。Create a snapshot for the OS disk of the VM.
  3. 从快照创建虚拟硬盘。Create a virtual hard disk from the snapshot.
  4. 将虚拟硬盘附加并装入到另一个 Windows VM,以便进行故障排除。Attach and mount the virtual hard disk to another Windows VM for troubleshooting purposes.
  5. 连接到故障排除 VM。Connect to the troubleshooting VM. 编辑文件或运行任何工具来修复原始虚拟硬盘上的问题。Edit files or run any tools to fix problems on the original virtual hard disk.
  6. 从故障排除 VM 卸载并分离虚拟硬盘。Unmount and detach the virtual hard disk from the troubleshooting VM.
  7. 交换 VM 的 OS 磁盘。Swap the OS disk for the VM.

备注

本文不适用于包含非托管磁盘的 VM。This article does not apply to VMs that have unmanaged disks.

拍摄 OS 磁盘快照Take a snapshot of the OS disk

快照是虚拟硬盘的完整只读副本。A snapshot is a full, read-only copy of a virtual hard disk. 建议在创建快照之前完全关闭 VM,以清除正在运行的所有进程。We recommend that you cleanly shut down the VM before taking a snapshot, to clear out any processes that are in progress. 若要创建 OS 磁盘的快照,请执行以下步骤:To take a snapshot of an OS disk, follow these steps:

  1. 转到 Azure 门户Go to the Azure portal. 在边栏中选择“虚拟机”,然后选择有问题的 VM。Select Virtual machines from the sidebar, and then select the VM that has the problem.

  2. 在左窗格中选择“磁盘”,然后选择 OS 磁盘的名称。 On the left pane, select Disks, and then select the name of the OS disk.

    屏幕截图显示了磁盘设置中的 OS 磁盘的名称。

  3. 在 OS 磁盘的“概述”页上,选择“创建快照”。 On the Overview page of the OS disk, select Create snapshot.

  4. 在 OS 磁盘所在位置创建快照。Create a snapshot in the same location as the OS disk.

从快照创建磁盘Create a disk from the snapshot

若要从快照创建磁盘,请执行以下步骤:To create a disk from the snapshot, follow these steps:

  1. 运行以下 PowerShell 命令从快照创建托管磁盘。Run the following PowerShell commands to create a managed disk from the snapshot. 将示例名称替换为相应的名称。Replace the example names with the appropriate names.

    # Sign in the Azure China Cloud
    Connect-AzAccount -Environment AzureChinaCloud
    
    #Provide the name of your resource group.
    $resourceGroupName ='myResourceGroup'
    
    #Provide the name of the snapshot that will be used to create managed disks.
    $snapshotName = 'mySnapshot' 
    
    #Provide the name of the managed disk.
    $diskName = 'newOSDisk'
    
    #Provide the size of the disks in gigabytes. It should be greater than the VHD file size. In this example, the size of the snapshot is 127 GB. So we set the disk size to 128 GB.
    $diskSize = '128'
    
    #Provide the storage type for the managed disk: Premium_LRS or Standard_LRS.
    $storageType = 'Standard_LRS'
    
    #Provide the Azure region (for example, chinanorth) where the managed disks will be located.
    #This location should be the same as the snapshot location.
    #Get all the Azure locations by using this command:
    #Get-AzLocation
    $location = 'chinanorth'
    
    $snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName 
    
    $diskConfig = New-AzDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id
    
    New-AzDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName -DiskName $diskName
    
  2. 如果命令运行成功,则会在提供的资源组中看到新磁盘。If the commands run successfully, you'll see the new disk in the resource group that you provided.

将磁盘附加到另一个 VMAttach the disk to another VM

在后续几个步骤中,将使用另一个 VM 进行故障排除。For the next few steps, you use another VM for troubleshooting purposes. 将磁盘附加到故障排除 VM 后,可以浏览和编辑磁盘的内容。After you attach the disk to the troubleshooting VM, you can browse and edit the disk's content. 此过程允许用户更正任何配置错误或者查看其他应用程序或系统日志文件。This process allows you to correct any configuration errors or review additional application or system log files. 若要将磁盘附加到另一个 VM,请执行以下步骤:To attach the disk to another VM, follow these steps:

  1. 在门户中选择资源组,然后选择故障排除 VM。Select your resource group from the portal, and then select your troubleshooting VM. 选择“磁盘” > “编辑” > “添加数据磁盘”。 Select Disks > Edit > Add data disk.

    屏幕截图显示了门户中用于附加现有磁盘的选择。

  2. 在“数据磁盘” 列表中,选择所标识的 VM 的 OS 磁盘。In the Data disks list, select the OS disk of the VM that you identified. 如果看不到 OS 磁盘,请确保故障排除 VM 和 OS 磁盘位于同一区域(位置)。If you don't see the OS disk, make sure that the troubleshooting VM and the OS disk are in the same region (location).

  3. 选择“保存”应用所做的更改。 Select Save to apply the changes.

将附加的数据磁盘装载到 VMMount the attached data disk to the VM

  1. 与故障排除 VM 建立远程桌面连接。Open a Remote Desktop connection to the troubleshooting VM.

  2. 在故障排除 VM 中打开“服务器管理器”,然后选择“文件和存储服务”。 In the troubleshooting VM, open Server Manager, and then select File and Storage Services.

    屏幕截图显示了如何在“服务器管理器”中选择“文件和存储服务”。

  3. 系统会自动检测并附加数据磁盘。The data disk is automatically detected and attached. 若要查看已连接磁盘的列表,请选择“磁盘” 。To see a list of the connected disks, select Disks. 可选择要查看卷信息(包括驱动器号)的数据磁盘。You can select your data disk to view volume information, including the drive letter. 以下示例显示使用了驱动器 F 的附加数据磁盘。The following example shows the data disk attached and using drive F.

    屏幕截图显示了服务器管理器中的附加磁盘和卷信息。

修复原始虚拟硬盘上的问题Fix problems on the original virtual hard disk

装载现有虚拟硬盘后,可以根据需要执行任何维护和故障排除步骤。With the existing virtual hard disk mounted, you can now perform any maintenance and troubleshooting steps as needed. 解决所有错误后,请继续执行以下步骤。After you've resolved all errors, continue with the following steps.

卸载并分离原始虚拟硬盘Unmount and detach the original virtual hard disk

从故障排除 VM 中分离现有虚拟硬盘。Detach the existing virtual hard disk from your troubleshooting VM. 在解除将虚拟硬盘附加到故障排除 VM 的租约之前,不能将该虚拟硬盘与任何其他 VM 一起使用。You can't use your virtual hard disk with any other VM until the lease that attaches the virtual hard disk to the troubleshooting VM is released.

  1. 在与 VM 的远程桌面会话中,打开“服务器管理器”,然后选择“文件和存储服务”。From the Remote Desktop session to your VM, open Server Manager, and then select File and Storage Services.

    屏幕截图显示了在“服务器管理器”中选择“文件和存储服务”。

  2. 选择“磁盘”,右键单击你的数据磁盘,然后选择“脱机”。 Select Disks, right-click your data disk, and then select Take Offline.

    屏幕截图显示了在“服务器管理器”中将数据磁盘设置为脱机。

  3. 从 VM 中分离虚拟硬盘。Detach the virtual hard disk from the VM. 在 Azure 门户中选择你的 VM,然后选择“磁盘”。Select your VM in the Azure portal, and then select Disks.

  4. 依次选择“编辑”、附加的 OS 磁盘、“删除” 。Select Edit, select the OS disk that you attached, and then select Delete.

    屏幕截图显示了用于分离现有虚拟硬盘的选择。

    继续操作之前,请等待数据磁盘在 VM 中成功删除。Before you continue, wait until the data disk is successfully deleted in the VM.

交换 VM 的 OS 磁盘Swap the OS disk for the VM

Azure 门户现在支持更改 VM 的 OS 磁盘。Azure portal now supports changing the OS disk of the VM. 为此,请按照下列步骤进行操作:To do this, follow these steps:

  1. 转到 Azure 门户Go to the Azure portal. 在边栏中选择“虚拟机”,然后选择有问题的 VM。Select Virtual machines from the sidebar, and then select the VM that has the problem.

  2. 在左窗格中选择“磁盘”,然后选择“交换 OS 磁盘”。 On the left pane, select Disks, and then select Swap OS Disk.

    屏幕截图显示了 Azure 门户中用于交换 OS 磁盘的按钮。

  3. 选择已修复的新磁盘,然后输入 VM 的名称以确认更改。Choose the new disk that you repaired, and then enter the name of the VM to confirm the change. 如果在列表中看不到该磁盘,请在从故障排除 VM 中分离磁盘后等待 10 到 15 分钟。If you don't see the disk in the list, wait 10 to 15 minutes after you detach the disk from the troubleshooting VM. 另外,请确保该磁盘与 VM 位于同一位置。Also make sure that the disk is in the same location as the VM.

  4. 选择“确定” 。Select OK.

后续步骤Next steps

如果在连接到 VM 时遇到问题,请参阅排查 Azure 虚拟机的远程桌面连接问题If you're having problems connecting to your VM, see Troubleshoot Remote Desktop connections to an Azure VM. 如果在访问 VM 上运行的应用程序时遇到问题,请参阅排查 Windows VM 上的应用程序连接问题For problems with accessing applications running on your VM, see Troubleshoot application connectivity issues on a Windows VM.

有关使用 Azure 资源管理器的详细信息,请参阅 Azure 资源管理器概述For more information about using Azure Resource Manager, see the Azure Resource Manager overview.