Windows

將驅動器號映射到 VolumeID

  • March 7, 2019

我收到了投訴…

幫助!我的I驅動器已滿EC2-Server-1!請給摩爾空間!

但是,當我在擴展卷之前遠端訪問伺服器時,我發現我無法輕鬆確定需要擴展哪個EBS 卷。

當我執行aws ec2 describe-instances --filters "Name=tag:Name,Values=EC2-Server-1"時,顯示中的相關資訊$.Reservations.Instances.BlockDeviceMappings

DeviceName Ebs
---------- ---
/dev/sda1  @{VolumeId=vol-0123;...}
xvdf       @{VolumeId=vol-0456;...}
xvdj       @{VolumeId=vol-0789;...}
xvdg       @{VolumeId=vol-0abc;...}
...

按照這個指導,我可以在Disk Management磁碟2(驅動器號)Properties > General下看到該值為。在同一頁面的Windows 卷映射表上查找此值,我們看到對應的是,它映射到。I``Location``Bus Number 0, Target Id 6, LUN 0``DeviceName``xvdg``vol-0abc

所以……太好了……現在我每次需要執行此操作時只需點擊大約 5 個對話框(或者只是從 AWS 控制台和 Windows 資源管理器中模糊匹配大小)。

我可以執行終端命令(或一系列命令)來快速可靠地檢索此映射嗎?

我目前正在反復進行各種wmic迭代,但是…有可能這是一個已解決的問題嗎?

當在 EC2 主機的上下文中執行時,以下函式返回給定驅動器號的BlockDeviceNameEBS 。VolumeID它處理跨越多個磁碟的驅動器,但不適用於跨越多個 EBS 卷的磁碟。

function Get-EbsDeets {
   [cmdletbinding()]Param(
       [string[]]$DriveLetter
   )

   # $wmiVol = Get-WmiObject Win32_Volume      | Where DriveLetter -eq "$DriveLetter`:"
   $wmiLd = Get-WmiObject Win32_LogicalDisk | Where {($_.DeviceID).Trim(':') -in $DriveLetter}

   $wmiLd | ForEach-Object {
       $Letter =  $_.DeviceID

       $wmiLd2p = Get-WmiObject Win32_LogicalDiskToPartition | Where Dependent -in $_.__PATH

       $wmiDd2p = Get-WmiObject Win32_DiskDriveToDiskPartition | Where Dependent -in $wmiLd2p.Antecedent

       $wmiDsk = Get-WmiObject Win32_DiskDrive | Where __PATH -in $wmiDd2p.Antecedent 

       $wmiDsk | ForEach-Object {
           $EstimatedVolumeId = $_.SerialNumber.Insert(3,'-')
           $DeviceName = (ec2WinVolMap $_.SCSIBus -target $_.SCSITargetId -lun $_.SCSILogicalUnit).DeviceName
           $VolumeType = (ec2WinVolMap $_.SCSIBus -target $_.SCSITargetId -lun $_.SCSILogicalUnit).VolumeType
           $SizeGb = [Math]::Round(($_.Size / 1gb),2)

           [PSCustomObject]@{
               DriveLetter = $Letter 
               DiskNumber  = $_.DeviceId
               DeviceName  = $DeviceName
               VolumeType  = $VolumeType
               VolumeId    = $EstimatedVolumeId
               SizeGb      = $SizeGb
           }
       }
   }
}

Function ec2WinVolMap {
   [cmdletBinding()]Param(
        $bus
       ,$target
       ,$lun
   )
# https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-volumes.html#windows-volume-mapping
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html
   $Lookup = @"
Bus,Target,LUN,DeviceName,VolumeType
0,0,0,/dev/sda1,ebs
0,1,0,xvdb, ebs
0,2,0,xvdc, ebs
0,3,0,xvdd, ebs
0,4,0,xvde, ebs
0,5,0,xvdf, ebs
0,6,0,xvdg, ebs
0,7,0,xvdh, ebs
0,8,0,xvdi, ebs
0,9,0,xvdj, ebs
0,10,0,xvdk,ebs
0,11,0,xvdl,ebs
0,12,0,xvdm,ebs
0,13,0,xvdn,ebs
0,14,0,xvdo,ebs
0,15,0,xvdp,ebs
0,16,0,xvdq,ebs
0,17,0,xvdr,ebs
0,18,0,xvds,ebs
0,19,0,xvdt,ebs
0,20,0,xvdu,ebs
0,21,0,xvdv,ebs
0,22,0,xvdw,ebs
0,23,0,xvdx,ebs
0,24,0,xvdy,ebs
0,25,0,xvdz,ebs
0,78,0,xvdca,instance-store
0,79,0,xvdcb,instance-store
0,80,0,xvdcc,instance-store
0,81,0,xvdcd,instance-store
0,82,0,xvdce,instance-store
0,83,0,xvdcf,instance-store
0,84,0,xvdcg,instance-store
0,85,0,xvdch,instance-store
0,86,0,xvdci,instance-store
0,87,0,xvdcj,instance-store
0,88,0,xvdck,instance-store
0,89,0,xvdcl,instance-store
"@ | ConvertFrom-Csv

   $DeviceName = ($Lookup | Where-Object {
       $_.Bus    -eq $bus    -and `
       $_.Target -eq $target -and `
       $_.LUN    -eq $lun
   }).DeviceName

   $VolumeType = ($Lookup | Where-Object {
       $_.Bus    -eq $bus    -and `
       $_.Target -eq $target -and `
       $_.LUN    -eq $lun
   }).VolumeType

   [PSCustomObject]@{
       DeviceName = $DeviceName
       VolumeType = $VolumeType
   }
}

解釋

鑑於上述限制,您可以VolumeID通過檢查. 如答案中所述,您還可以檢索基於先前連結的AWS 文件中的表查找所需的匯流排、目標和 lun 值。SerialNumberWin32_DiskDrive@bjosterBlockDeviceName

但令人沮喪的是,這並不容易映射回您在實際使用者投訴中可能收到實際驅動器號或*名稱。*要關聯這些屬性(在我的例子中是在集合中找到的),您可以通過和集合Win32_LogicalDisk上的前因/從屬映射以菊花鏈形式返回。Win32_LogicalDiskToPartition``Win32_DiskDriveToDiskPartition

將這些拼湊在一起時,我注意到在我的安裝Win32_DiskDriveToDiskPartition映射中,磁碟是 1:1 的。雖然Win32_LogicalDiskToPartition確實與 EBS 卷的數量相匹配(在每個磁碟有多個卷的情況下),但我還沒有費心去弄清楚如何關聯獲取BlockDeviceName非主基礎卷所需的匯流排、目標和 lun (s) 當它們存在時。如果檢索到這些,但由於某種原因 EBSVolumeID不立即用於非主卷,則可能會考慮通過以下方式呼叫 AWSPowerShell 函式

$bdn = 'xvdg'

# assuming you've tagged your instance with the hostname
$ec2 = Get-EC2Instance -Filter @{Name='tag:Name';Values='EC2-Server-1'}

($ec2.Instances.BlockDeviceMappings | Where DeviceName -eq $bdn).Ebs

就在這裡。PowerShell(帶有 WMI)來救援:

Get-WmiObject Win32_DiskDrive | select-object DeviceID,size,scsiport,scsibus,scsitargetid,scsilogicalunit

這將為您留下:

DeviceID        : \\.\PHYSICALDRIVE9
size            : 234362882560
scsiport        : 3
scsibus         : 0
scsitargetid    : 2
scsilogicalunit : 4

引用自:https://serverfault.com/questions/954868