Skip to content

Commit 83f080f

Browse files
Change ec2nvme-nsid to use Bash built-ins
This simple change cuts the runtime by more than half and CPU by almost a third. It also makes the script more reliable when resources are constrained (the sort of situation where you might suddenly attach more EBS NVMe volumes) and helps improve the performance of the early boot process (initrd/initramfs), should you make these scripts part of that. Performance before: $ time bash -c 'for i in $(seq 1 10000); do bash ./ec2nvme-nsid nvme0n1234234p2 > /dev/null; done' bash -c 38.00s user 90.67s system 92% cpu 2:18.43 total Performance after: $ time bash -c 'for i in $(seq 1 10000); do bash ./ec2nvme-nsid nvme0n1234234p2 > /dev/null; done' bash -c 12.61s user 27.56s system 66% cpu 1:00.26 total Signed-off-by: Keith Gable <gablk@amazon.com>
1 parent 220d932 commit 83f080f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

amazon-ec2-utils.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Name: amazon-ec2-utils
22
Summary: A set of tools for running in EC2
3-
Version: 2.1.0
3+
Version: 2.2.0
44
Release: 1%{?dist}
55
License: MIT
66
Group: System Tools
@@ -75,6 +75,9 @@ rm -rf $RPM_BUILD_ROOT
7575
/etc/udev/rules.d/60-cdrom_id.rules
7676

7777
%changelog
78+
* Thu Jan 18 2024 Keith Gable <gablk@amazon.com> - 2.2.0-1
79+
- Change ec2nvme-nsid to use Bash string manipulation to improve
80+
performance and reliability
7881

7982
* Thu Apr 6 2023 Noah Meyerhans <nmeyerha@amazon.com> - 2.1.0-1
8083
- Add --quiet option to ec2-metadata

ec2nvme-nsid

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
# for the specific language governing permissions and limitations under
77
# the License.
88

9-
#Expected input if partition's kernel name like nvme0n1p2
10-
if [ $# -eq 0 ] ; then
9+
# Expected input is partition's kernel name like nvme0n1p2 or nvme0n1, output would be 1
10+
11+
if [[ $# -ne 1 ]]; then
1112
exit 1
1213
else
13-
# extract ns id from partition's kernel name and export it
14-
NSID=$(echo -n "$@" | cut -f 3 -d 'n' | cut -f 1 -d 'p')
14+
kernel_name=$1
15+
16+
# Remove nvme prefix (also deals with any /dev that might accidentally get passed in)
17+
prefix_removed=${kernel_name#*nvme*n}
18+
19+
# Remove partition suffix
20+
NSID=${prefix_removed%p*}
21+
1522
echo "_NS_ID=${NSID}"
1623
fi

0 commit comments

Comments
 (0)