使用 Azure 虚拟机修复命令修复 Linux VMRepair a Linux VM by using the Azure Virtual Machine repair commands

如果 Linux 虚拟机 (VM) 在 Azure 中遇到启动或磁盘错误,可能需要对磁盘本身执行缓解操作。If your Linux virtual machine (VM) in Azure encounters a boot or disk error, you may need to perform mitigation on the disk itself. 一个常见示例是应用程序更新失败,使 VM 无法成功启动。A common example would be a failed application update that prevents the VM from being able to boot successfully. 本文详细介绍如何使用 Azure 虚拟机修复命令将磁盘连接到另一个 Linux VM 来修复所有错误,然后重新生成原始 VM。This article details how to use Azure Virtual Machine repair commands to connect the disk to another Linux VM to fix any errors, then rebuild your original VM.

重要

  • 本文中的脚本仅适用于使用 Azure 资源管理器的 VM。The scripts in this article only apply to the VMs that use Azure Resource Manager.
  • 运行脚本需要 VM 的出站连接(端口 443)。Outbound connectivity from the VM (port 443) is required for the script to run.
  • 一次只能运行一个脚本。Only one script may run at a time.
  • 无法取消正在运行的脚本。A running script cannot be canceled.
  • 脚本最多可以运行 90 分钟,之后它将超时。The maximum time a script can run is 90 minutes, after which it will time out.
  • 对于使用 Azure 磁盘加密的 VM,仅支持通过单次传递加密(带有或不带 KEK)加密的托管磁盘。For VMs using Azure Disk Encryption, only managed disks encrypted with single pass encryption (with or without KEK) are supported.

修复过程概述Repair process overview

现在可以使用 Azure 虚拟机修复命令更改 VM 的 OS 磁盘,而不再需要删除并重新创建 VM。You can now use Azure Virtual Machine repair commands to change the OS disk for a VM, and you no longer need to delete and recreate the VM.

请按照下列步骤排查 VM 问题:Follow these steps to troubleshoot the VM issue:

  1. 启动 Azure 本地 ShellLaunch Azure local Shell
  2. 运行 az extension add/updateRun az extension add/update
  3. 运行 az vm repair createRun az vm repair create
  4. 运行 az vm repair run 或执行缓解步骤。Run az vm repair run, or perform mitigation steps.
  5. 运行 az vm repair restoreRun az vm repair restore

有关其他文档和说明,请参阅 az vm repairFor additional documentation and instructions, see az vm repair.

修复过程示例Repair process example

  1. 启动 Azure 本地 ShellLaunch Azure local Shell

    如果希望在本地安装并使用 CLI,则本快速入门需要 Azure CLI 2.0.30 版或更高版本。When you prefer to install and use the CLI locally, this quickstart requires Azure CLI version 2.0.30 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要安装或升级 Azure CLI,请参阅安装 Azure CLIIf you need to install or upgrade your Azure CLI, see Install Azure CLI.

    如果需要使用与当前登录 Azure 门户所用帐户不同的帐户登录本地 Shell,可使用 az login az login referenceIf you need to login to local Shell with a different account than you are currently logged in to the Azure Portal with you can use az login az login reference. 若要在与你的帐户关联的订阅之间切换,可使用 az account set --subscription az account set referenceTo switch between subscriptions associated with your account you can use az account set --subscription az account set reference.

    <--Mooncake 自定义句子逻辑--><--Mooncake Customize on the sentence logic-->

  2. 如果是首次使用 az vm repair 命令,请添加 vm-repair CLI 扩展。If this is the first time you have used the az vm repair commands, add the vm-repair CLI extension.

    az extension add -n vm-repair
    

    如果以前使用过 az vm repair 命令,请将任何更新应用于 vm-repair 扩展。If you have previously used the az vm repair commands, apply any updates to the vm-repair extension.

    az extension update -n vm-repair
    
  3. 运行 az vm repair createRun az vm repair create. 此命令将为非功能性 VM 创建 OS 磁盘的副本,在新资源组中创建修复 VM,并附加 OS 磁盘副本。This command will create a copy of the OS disk for the non-functional VM, create a repair VM in a new Resource Group, and attach the OS disk copy. 修复 VM 的大小和区域将与指定的非功能性 VM 相同。The repair VM will be the same size and region as the non-functional VM specified. 所有步骤中使用的资源组和 VM 名称都将用于非功能性 VM。The Resource Group and VM name used in all steps will be for the non-functional VM. 如果 VM 使用的是 Azure 磁盘加密,该命令将尝试解锁已加密的磁盘,以便在附加到修复 VM 时可对其进行访问。If your VM is using Azure Disk Encryption the command will attempt to unlock the encrypted disk so that it is accessible when attached to the repair VM.

    az vm repair create -g MyResourceGroup -n myVM --repair-username username --repair-password password!234 --verbose
    
  4. 运行 az vm repair runRun az vm repair run. 此命令将通过修复 VM 在附加的磁盘上运行指定的修复脚本。This command will run the specified repair script on the attached disk via the repair VM. 如果你使用的故障排除指南指定了 run-id,请在此处使用它,否则可以使用 az vm repair list-scripts 来查看可用的修复脚本。If the troubleshooting guide you are using specified a run-id please use it here, otherwise you can use az vm repair list-scripts to see available repair scripts. 此处使用的资源组和 VM 名称适用于第 3 步中使用的非功能性 VM。The Resource Group and VM name used here are for the non-functional VM used in step 3. 有关修复脚本的其他信息可在修复脚本库中找到。Additional information about the repair scripts can be found in the repair script library.

    az vm repair run -g MyResourceGroup -n MyVM --run-on-repair --run-id lin-hello-world --verbose
    

    (可选)可以使用修复 VM 来执行任何所需的手动缓解步骤,然后继续执行步骤 5。Optionally you can perform any needed manual mitigation steps using the repair VM, then proceed to step 5.

  5. 运行 az vm repair restoreRun az vm repair restore. 此命令会将已修复的 OS 磁盘与 VM 的原始 OS 磁盘交换。This command will swap the repaired OS disk with the original OS disk of the VM. 此处使用的资源组和 VM 名称适用于第 3 步中使用的非功能性 VM。The Resource Group and VM name used here are for the non-functional VM used in step 3.

    az vm repair restore -g MyResourceGroup -n MyVM --verbose
    

验证和启用启动诊断Verify and enable boot diagnostics

以下示例在名为 myResourceGroup 的资源组中名为 myVMDeployed 的 VM 上启用诊断扩展:The following example enables the diagnostic extension on the VM named myVMDeployed in the resource group named myResourceGroup:

Azure CLIAzure CLI

az vm boot-diagnostics enable --name myVMDeployed --resource-group myResourceGroup --storage https://mystor.blob.core.chinacloudapi.cn/

后续步骤Next steps