对 VM 操作系统磁盘进行故障排除Troubleshoot a VMs operating system disk

此脚本将失败或有问题的虚拟机的操作系统磁盘作为数据磁盘装载到第二个虚拟机。This script mounts the operating system disk of a failed or problematic virtual machine as a data disk to a second virtual machine. 排查磁盘问题或恢复数据时,此脚本会很有用。This can be useful when troubleshooting disk issues or recovering data.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

#!/bin/bash

# Sign in the Azure China Cloud
az cloud set -n AzureChinaCloud
az login

# Source virtual machine details.
sourcevm=<Replace with vm name>
resourcegroup=<Replace with resource group name>

# Get the disk id for the source VM operating system disk.
diskid="$(az vm show -g $resourcegroup -n $sourcevm --query [storageProfile.osDisk.managedDisk.id] -o tsv)"

# Delete the source virtual machine, this will not delete the disk.
az vm delete -g $resourcegroup -n $sourcevm --yes

# Create a new virtual machine, this creates SSH keys if not present.
az vm create --resource-group $resourcegroup --name myVM --image UbuntuLTS --generate-ssh-keys

# Attach disk as a data disk to the newly created VM.
az vm disk attach --resource-group $resourcegroup --vm-name myVM --disk $diskid

# Configure disk on new VM.
ip=$(az vm list-ip-addresses --resource-group $resourcegroup --name myVM --query '[].virtualMachine.network.publicIpAddresses[0].ipAddress' -o tsv)
ssh $ip 'sudo mkdir /mnt/remountedOsDisk'
ssh $ip 'sudo mount -t ext4 /dev/sdc1 /mnt/remountedOsDisk'

脚本说明Script explanation

此脚本使用以下命令创建资源组、虚拟机和所有相关资源。This script uses the following commands to create a resource group, virtual machine, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 注释Notes
az vm showaz vm show 返回虚拟机列表。Return a list of virtual machines. 在此示例中,查询选项用于返回虚拟机操作系统磁盘。In this case, the query option is used to return the virtual machine operating system disk. 然后,将此值添加到名为“uri”的变量。This value is then added to a variable name 'uri'.
az vm deleteaz vm delete 删除虚拟机。Deletes a virtual machine.
az vm createaz vm create 创建虚拟机。Creates a virtual machine.
az vm disk attachaz vm disk attach 将磁盘附加到虚拟机。Attaches a disk to a virtual machine.
az vm list-ip-addressesaz vm list-ip-addresses 返回虚拟机的 IP 地址。Returns the IP addresses of a virtual machine.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。Additional virtual machine CLI script samples can be found in the Azure Linux VM documentation.