Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Update based on suggestions: narrow which devices we're targeting, us…
Browse files Browse the repository at this point in the history
…e the PATH for binaries and link to scripts rather than recreating them.
  • Loading branch information
jacobappleton-orbis committed Jan 7, 2020
1 parent 4b655a2 commit d8175df
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 49 deletions.
2 changes: 1 addition & 1 deletion 999-aws-ebs-nvme.rules
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SUBSYSTEM=="block", KERNEL=="nvme[0-9]*n[0-9]*", ATTRS{model}=="Amazon Elastic Block Store", PROGRAM+="/usr/local/sbin/ebs-nvme-mapping.sh /dev/%k" SYMLINK+="%c"
SUBSYSTEM=="block", KERNEL=="nvme[0-26]n1", ATTRS{model}=="Amazon Elastic Block Store", PROGRAM+="/usr/local/sbin/ebs-nvme-mapping.sh /dev/%k" SYMLINK+="%c"
46 changes: 4 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ So, let's trim the trailing spaces for a viable block device name.
/dev/xvdf
```

We now have our desired block device name.
We now have our desired block device name.

## The Hacky Solution

Expand Down Expand Up @@ -378,52 +378,14 @@ your configuation.

I've picked `ATTRS{model}`.

Let's combine what we've found into a shell script...

```
[ec2-user@ip-10-81-66-128 ~]$ cat <<EOF> ebs-nvme-mapping.sh
> #!/bin/bash
>
> if [[ ! -x /usr/sbin/nvme ]]; then
> echo "ERROR: NVME tools not installed." >> /dev/stderr
> exit 1
> fi
>
> if [[ ! -b ${1} ]]; then
> echo "ERROR: cannot find block device ${1}" >> /dev/stderr
> exit 1
> fi
>
> # capture 32 bytes at an offset of 3072 bytes from the raw-binary data
> # not all block devices are extracted with /dev/ prefix
> # use `xvd` prefix instead of `sd`
> # remove all trailing space
> nvme_link=$( \
> /usr/sbin/nvme id-ctrl --raw-binary "${1}" | \
> /usr/bin/cut -c3073-3104 | \
> /bin/sed 's/^\/dev\///g'| \
> /bin/sed 's/^sd/xvd/'| \
> /usr/bin/tr -d '[:space:]' \
> );
> echo $nvme_link;
> EOF
[ec2-user@ip-10-81-66-128 ~]$ sudo install -m 0755 ebs-nvme-mapping.sh /usr/local/sbin/
[ec2-user@ip-10-81-66-128 ~]$
```
Let's combine what we've found into a [shell script](ebs-nvme-mapping.sh), and a [udev rule](999-aws-ebs-nvme.rules)...

...and a udev rule...
...and finally reload the `udev` rules and trigger it...

```
[ec2-user@ip-10-81-66-128 ~]$ cat <<EOF> 999-aws-ebs-nvme.rules
> SUBSYSTEM=="block", KERNEL=="nvme[0-9]*n[0-9]*", ATTRS{model}=="Amazon Elastic Block Store", PROGRAM+="/usr/local/sbin/ebs-nvme-mapping.sh /dev/%k" SYMLINK+="%c"
> EOF
[ec2-user@ip-10-81-66-128 ~]$ sudo install -m 0644 999-aws-ebs-nvme.rules /etc/udev/rules.d/
[ec2-user@ip-10-81-66-128 ~]$
[ec2-user@ip-10-81-66-128 ~]$ sudo udevadm control --reload-rules && udevadm trigger
```

`udev` will automatically reload rules upon changes to files in the rules directory. So we're locked
and loaded.

Now, when we attach and detach EBS volumes, our shell script will run.

## A Test Run
Expand Down
12 changes: 6 additions & 6 deletions ebs-nvme-mapping.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# To be used with the udev rule: /etc/udev/rules.d/999-aws-ebs-nvme.rules

if [[ ! -x /usr/sbin/nvme ]]; then
if [[ ! -x nvme ]]; then
echo "ERROR: NVME tools not installed." >> /dev/stderr
exit 1
fi
Expand All @@ -16,10 +16,10 @@ fi
# use `xvd` prefix instead of `sd`
# remove all trailing space
nvme_link=$( \
/usr/sbin/nvme id-ctrl --raw-binary "${1}" | \
/usr/bin/cut -c3073-3104 | \
/bin/sed 's/^\/dev\///g'| \
/bin/sed 's/^sd/xvd/'| \
/usr/bin/tr -d '[:space:]' \
nvme id-ctrl --raw-binary "${1}" | \
cut -c3073-3104 | \
sed 's/^\/dev\///g'| \
sed 's/^sd/xvd/'| \
tr -d '[:space:]' \
);
echo $nvme_link;

0 comments on commit d8175df

Please sign in to comment.