Azure – How to Get Drive Letter Attached to a Data Disk

azurehard drivevirtual-machines

I am trying to get certain details of disks attached to a VM in azure using Azure python SDK. I came to know that:

A VM will have two disks attached to it when created:

  1. OS disk (OS disks are attached with a drive letter of C: by default)
  2. Temporary disk (Temporary disks are attached with a drive letter of D: by default)

Apart from these we can add extra data disks to a VM, if we need. The problem is when we add a Data disk, we don't know which letter is attached to that disk.

I get the disk utilization details for a disk along with it's drive letter from Azure log analytics but i don't get the disk name in that logs. so, i am not able to identify which disk's utilization logs they are.

Using Azure's Python sdk, I am able to get the disk name and disk size but i am not able to get the Disk letter.

I want to know the letter of a disk so that i get to know the utilization details of a particular disk.
Can someone please help me with this?

Best Answer

As mentioned in the comment , Its not possible to map Azure Disks with Windows Guest Disk in a Single Script.

You can use the LUN for the Disk to get mapping of the two .

Step -1 : Remote into the VM , open powershell and run the below command:

  `get-disk  | format-list number, path`

You will get the list of drives with their drive number (slot) and a path present in your VM.

For the data disks the path will look something like: ?\scsi#disk&ven_msft&prod_virtual_disk#000001#{57f56307-b6bf-19d0-94f2-00a0c91efb8b}

Note : The disk#000001# is the LUN part. In this case it's LUN 1.

Or

  • Connect to the VM and open Disk Management
  • In the lower pane, right-click any of the Disks and choose "Properties"
  • The LUN will be listed in the "Location" property on the "General" tab

Step -2 : Now to get the details of the Azure Disks you can run the below command in CLI:

  `az vm show -g myResourceGroup -n myVM --query "storageProfile.dataDisks"`

Or

Using Powershell:

$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myVM
$vm.StorageProfile.DataDisks | ft

Or

Using Python SDK:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import AzureCliCredential
credential = AzureCliCredential()
rg = 'ResourceGroupName'
name = 'VMName'
subscription_id = "SubID"
compute_client = ComputeManagementClient(credential, subscription_id)
vm = compute_client.virtual_machines.get(rg,name)
##get OS disk size(GB)
print (vm.storage_profile.os_disk.name,vm.storage_profile.os_disk.disk_size_gb)
datadisks = vm.storage_profile.data_disks
##get data disk size(GB)
for i in datadisks:
    print (i.lun,i.name,i.disk_size_gb)

Reference:

How to map Azure Disks to Windows VM guest disks - Azure Virtual Machines | Microsoft Docs