将驱动器号映射到 VolumeID

将驱动器号映射到 VolumeID

我收到了投诉...

救命!我的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可以看到Properties > General磁盘 2(驱动器号I)的Location值为Bus Number 0, Target Id 6, LUN 0。在Windows 卷映射表从同一页面,我们看到相应的DeviceNamexvdg,它映射到vol-0abc

所以...太好了...现在我每次需要执行此操作时只需要单击大约 5 个对话框(或者只是从 AWS 控制台和 Windows 资源管理器进行大小模糊匹配)。

是否有我可以执行的终端命令(或一系列命令)来快速可靠地检索此映射?

我目前正在通过各种迭代来尝试解决这个wmic问题,但是......这个问题有解决的可能吗?

答案1

是的,有。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

答案2

以下函数在 EC2 主机上下文中执行时,返回给定驱动器号的BlockDeviceNameEBS 。它可以处理跨多个磁盘的驱动器,但不能处理跨多个 EBS 卷的磁盘。VolumeID

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给定磁盘(第一个卷)的EBSSerialNumberWin32_DiskDrive. 如上所述@bjoster的回答BlockDeviceName,您还可以根据先前链接的表格检索查找所需的总线、目标和 lun 值AWS 文档

但令人沮丧的是,这并不容易映射回实际的驱动器或者姓名您可能会在实际用户投诉中收到这些属性。要关联这些属性(可在Win32_LogicalDiskWin32_LogicalDiskToPartition在我的例子中,集合是一个集合,您可以通过和集合上的前件/从属映射以菊花链形式返回Win32_DiskDriveToDiskPartition

在拼凑这些时,我注意到我的安装Win32_DiskDriveToDiskPartition映射为 1:1 磁盘。虽然Win32_LogicalDiskToPartition确实匹配 EBS 卷的数量(在每个磁盘有多个卷的情况下),但我还没有费心去弄清楚如何关联获取BlockDeviceName非主底层卷(如果存在)所需的总线、目标和 lun。如果检索到这些,但由于某种原因,非主卷的 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

相关内容