Reputation: 12213
I am trying to attach an EBS volume on EC2 (RHEL) instance. This is how my attach-volume command looks like:
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 "
}
this is the output of 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
Even though I specified device name as /dev/sdf
, it shows up as nvme5n1
. This makes it difficult for me to identify the newly attached EBS volume and mount it.
I tried aws ec2 attach-volume --volume-id vol-xxxxxxxxxxxxxxxxx --instance-id i-yyyyyyyyyyyyyyyyy --device /dev/nvme5n1
but that gives me an error saying /dev/nvme5n1
is not a valid EBS device name.
Is there a way I can identify the right name of the EBS volume I just attached so that I can mount it to the directory I desire?
Upvotes: 6
Views: 6780
Reputation: 31
Rather than iterating potential devices, READLINK will neatly resolve the proper path for both symlink and direct devices. So your 'userdata' boot script could include:
aws ec2 attach-volume --volume-id ${VOLUME_ID} --device /dev/sdf --instance-id ${INSTANCE_ID} --region ${REGION}
BLOCKDEV=$(readlink -f /dev/sdf)
[[ "$(file -s ${BLOCKDEV})" =~ "${BLOCKDEV}: data" ]] && mkfs -t xfs ${BLOCKDEV}
[ -d /foo ] || mkdir /foo
mount ${BLOCKDEV} /foo
This will attach a designated EBS volume as 'sdf', but resolve it to the actual symlinked name. If the volume happens to be brand new -- "/dev/nvme1n1: data" -- it will initialize an XFS filesystem. Regardless, the XFS partition will then be mounted.
Upvotes: -1
Reputation: 1336
EBS volumes are exposed as NVMe block devices on instances built on the Nitro System.
On an older instance type you might see something like:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda 202:0 0 160G 0 disk
└─xvda1 202:1 0 160G 0 part /
On a Nitro-based instance you'll see something similar to what you provided above.
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 thesudo ebsnvme-id /dev/nvme0n1 -u
command to determine the NVMe device name.
$ sudo ebsnvme-id /dev/nvme0n1
xvda
You could enumerate all the NVMe devices and check for the one that has the associated device name. Perhaps with something like:
$ DEV=xvda
$ lsblk | grep disk | awk '{print $1}' | while read disk; do echo -n "$disk " && sudo ebsnvme-id -b /dev/$disk; done | grep $DEV | awk '{print $1}'
nvme0n1
For more information on device naming see the Amazon EBS and NVMe on Linux instances documentation.
Upvotes: 6