正确指定 EBS 卷的设备名称,同时附加到 ec2 实例并稍后使用设备名称识别它

correctly specifying Device Name for EBS volume while attaching to an ec2 instance and identifying it later using Device name

我正在尝试在 EC2 (RHEL) 实例上附加一个 EBS 卷。这是我的附加卷命令的样子:

aws ec2 attach-volume --volume-id vol-xxxxxxxxxxxxxxxxx --instance-id i-yyyyyyyyyyyyyyyyy --device /dev/sdf
{
    "AttachTime": "2021-12-02T19:30:13.070000+00:00",
    "Device": "/dev/sdf",
    "InstanceId": "i-yyyyyyyyyyyyyyyyy ",
    "State": "attaching",
    "VolumeId": "vol-xxxxxxxxxxxxxxxxx "
}

这是lsblk

的输出
NAME                   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme1n1                259:0    0   5G  0 disk
└─aaaaa-aaa            253:2    0   5G  0 lvm  /logs
nvme0n1                259:1    0  10G  0 disk
├─nvme0n1p1            259:2    0   1M  0 part
└─nvme0n1p2            259:3    0  10G  0 part /
nvme3n1                259:4    0  35G  0 disk
├─bbbbb-bbb            253:3    0   8G  0 lvm  [SWAP]
├─bbbbb-ccc            253:4    0   4G  0 lvm  /var/tmp
├─bbbbb-ddd            253:5    0   4G  0 lvm  /var
├─bbbbb-eee            253:6    0   4G  0 lvm  /var/log
nvme2n1                259:5    0   5G  0 disk
└─ccccc-ffff           253:0    0   5G  0 lvm  /products
nvme4n1                259:6    0   5G  0 disk
└─ddddd-gggg           253:1    0   5G  0 lvm  /apps
nvme5n1                259:7    0  20G  0 disk

尽管我将设备名称指定为 /dev/sdf,但它显示为 nvme5n1。这让我很难识别新附加的 EBS 卷并挂载它。

我尝试了 aws ec2 attach-volume --volume-id vol-xxxxxxxxxxxxxxxxx --instance-id i-yyyyyyyyyyyyyyyyy --device /dev/nvme5n1,但出现错误提示 /dev/nvme5n1 不是有效的 EBS 设备名称。

有什么方法可以识别我刚刚附加的 EBS 卷的正确名称,以便我可以将它挂载到我想要的目录?

EBS 卷公开为 NVMe block devices on instances built on the Nitro System

在较旧的实例类型上,您可能会看到如下内容:

$ lsblk 
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvda    202:0    0  160G  0 disk 
└─xvda1 202:1    0  160G  0 part /

在基于 Nitro 的实例上,您会看到与上面提供的内容类似的内容。

With Amazon Linux AMIs later than version 2017.09.01, we provide a udev rule that reads this data and creates a symbolic link to the block-device mapping.

$ lsblk 
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
nvme0n1       259:0    0  120G  0 disk 
├─nvme0n1p1   259:1    0  120G  0 part /
└─nvme0n1p128 259:2    0    1M  0 part 
$ ls -l /dev/xvda
lrwxrwxrwx 1 root root 7 Dec  3 08:59 /dev/xvda -> nvme0n1

If you are using an Amazon Linux or FreeBSD AMI, use the sudo ebsnvme-id /dev/nvme0n1 -u command for a consistent NVMe device name. For other distributions, use the sudo ebsnvme-id /dev/nvme0n1 -u command to determine the NVMe device name.

$ sudo ebsnvme-id /dev/nvme0n1
xvda

您可以枚举所有 NVMe 设备并检查具有关联设备名称的设备。也许像这样:

$ DEV=xvda
$ lsblk | grep disk | awk '{print }' | while read disk; do echo -n "$disk " && sudo ebsnvme-id -b /dev/$disk; done | grep $DEV | awk '{print }'
nvme0n1

有关设备命名的详细信息,请参阅 Amazon EBS and NVMe on Linux instances 文档。