Skip to content

Commit 0ea9f0e

Browse files
committed
Initial import of ec2-utils
1 parent 760d15c commit 0ea9f0e

13 files changed

Lines changed: 554 additions & 0 deletions

51-ec2-hvm-devices.rules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2006 Amazon.com, Inc. and its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the MIT License. See the LICENSE accompanying this file
4+
# for the specific language governing permissions and limitations under
5+
# the License.
6+
7+
KERNEL=="xvd*", PROGRAM="/sbin/ec2udev-vbd %k", SYMLINK+="%c"

52-ec2-vcpu.rules

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2014 Amazon.com, Inc. and its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the MIT License. See the LICENSE accompanying this file
4+
# for the specific language governing permissions and limitations under
5+
# the License.
6+
7+
SUBSYSTEM=="cpu", ACTION=="add", RUN+="/sbin/ec2udev-vcpu $devpath"

60-cdrom_id.rules

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright 2016 Amazon.com, Inc. and its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the MIT License. See the LICENSE accompanying this file
4+
# for the specific language governing permissions and limitations under
5+
# the License.
6+
7+
# This is an empty placeholder file used to signal udev to
8+
# skip probing for cd/dvd drive capabilities.

70-ec2-nvme-devices.rules

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2006 Amazon.com, Inc. and its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the MIT License. See the LICENSE accompanying this file
4+
# for the specific language governing permissions and limitations under
5+
# the License.
6+
7+
#nvme-ns-* devices
8+
KERNEL=="nvme[0-9]*n[0-9]*", ENV{DEVTYPE}=="disk", ATTRS{serial}=="?*", ATTRS{model}=="?*", SYMLINK+="disk/by-id/nvme-$attr{model}_$attr{serial}-ns-%n", OPTIONS+="string_escape=replace"
9+
10+
#nvme partitions
11+
KERNEL=="nvme[0-9]*n[0-9]*p[0-9]*", ENV{DEVTYPE}=="partition", ATTRS{serial}=="?*", ATTRS{model}=="?*", IMPORT{program}="ec2nvme-nsid %k"
12+
KERNEL=="nvme[0-9]*n[0-9]*p[0-9]*", ENV{DEVTYPE}=="partition", ATTRS{serial}=="?*", ATTRS{model}=="?*", ENV{_NS_ID}=="?*", SYMLINK+="disk/by-id/nvme-$attr{model}_$attr{serial}-ns-$env{_NS_ID}-part%n", OPTIONS+="string_escape=replace"
13+
14+
# ebs nvme devices
15+
KERNEL=="nvme[0-9]*n[0-9]*", ENV{DEVTYPE}=="disk", ATTRS{model}=="Amazon Elastic Block Store", PROGRAM="/sbin/ebsnvme-id -u /dev/%k", SYMLINK+="%c"
16+
KERNEL=="nvme[0-9]*n[0-9]*p[0-9]*", ENV{DEVTYPE}=="partition", ATTRS{model}=="Amazon Elastic Block Store", PROGRAM="/sbin/ebsnvme-id -u /dev/%k", SYMLINK+="%c%n"

LICENSE

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MIT License
2+
Copyright (c) <year> <copyright holders>
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
Software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+

NOTICE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ec2-utils
2+
Copyright 2006-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+

ebsnvme-id

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#!/usr/bin/env python2.7
2+
3+
# Copyright 2017 Amazon.com, Inc. and its affiliates. All Rights Reserved.
4+
#
5+
# Licensed under the MIT License. See the LICENSE accompanying this file
6+
# for the specific language governing permissions and limitations under
7+
# the License.
8+
9+
"""
10+
Usage:
11+
Read EBS device information and provide information about
12+
the volume.
13+
"""
14+
15+
import argparse
16+
from ctypes import *
17+
from fcntl import ioctl
18+
import sys
19+
20+
NVME_ADMIN_IDENTIFY = 0x06
21+
NVME_IOCTL_ADMIN_CMD = 0xC0484E41
22+
AMZN_NVME_VID = 0x1D0F
23+
AMZN_NVME_EBS_MN = "Amazon Elastic Block Store"
24+
25+
class nvme_admin_command(Structure):
26+
_pack_ = 1
27+
_fields_ = [("opcode", c_uint8), # op code
28+
("flags", c_uint8), # fused operation
29+
("cid", c_uint16), # command id
30+
("nsid", c_uint32), # namespace id
31+
("reserved0", c_uint64),
32+
("mptr", c_uint64), # metadata pointer
33+
("addr", c_uint64), # data pointer
34+
("mlen", c_uint32), # metadata length
35+
("alen", c_uint32), # data length
36+
("cdw10", c_uint32),
37+
("cdw11", c_uint32),
38+
("cdw12", c_uint32),
39+
("cdw13", c_uint32),
40+
("cdw14", c_uint32),
41+
("cdw15", c_uint32),
42+
("reserved1", c_uint64)]
43+
44+
class nvme_identify_controller_amzn_vs(Structure):
45+
_pack_ = 1
46+
_fields_ = [("bdev", c_char * 32), # block device name
47+
("reserved0", c_char * (1024 - 32))]
48+
49+
class nvme_identify_controller_psd(Structure):
50+
_pack_ = 1
51+
_fields_ = [("mp", c_uint16), # maximum power
52+
("reserved0", c_uint16),
53+
("enlat", c_uint32), # entry latency
54+
("exlat", c_uint32), # exit latency
55+
("rrt", c_uint8), # relative read throughput
56+
("rrl", c_uint8), # relative read latency
57+
("rwt", c_uint8), # relative write throughput
58+
("rwl", c_uint8), # relative write latency
59+
("reserved1", c_char * 16)]
60+
61+
class nvme_identify_controller(Structure):
62+
_pack_ = 1
63+
_fields_ = [("vid", c_uint16), # PCI Vendor ID
64+
("ssvid", c_uint16), # PCI Subsystem Vendor ID
65+
("sn", c_char * 20), # Serial Number
66+
("mn", c_char * 40), # Module Number
67+
("fr", c_char * 8), # Firmware Revision
68+
("rab", c_uint8), # Recommend Arbitration Burst
69+
("ieee", c_uint8 * 3), # IEEE OUI Identifier
70+
("mic", c_uint8), # Multi-Interface Capabilities
71+
("mdts", c_uint8), # Maximum Data Transfer Size
72+
("reserved0", c_uint8 * (256 - 78)),
73+
("oacs", c_uint16), # Optional Admin Command Support
74+
("acl", c_uint8), # Abort Command Limit
75+
("aerl", c_uint8), # Asynchronous Event Request Limit
76+
("frmw", c_uint8), # Firmware Updates
77+
("lpa", c_uint8), # Log Page Attributes
78+
("elpe", c_uint8), # Error Log Page Entries
79+
("npss", c_uint8), # Number of Power States Support
80+
("avscc", c_uint8), # Admin Vendor Specific Command Configuration
81+
("reserved1", c_uint8 * (512 - 265)),
82+
("sqes", c_uint8), # Submission Queue Entry Size
83+
("cqes", c_uint8), # Completion Queue Entry Size
84+
("reserved2", c_uint16),
85+
("nn", c_uint32), # Number of Namespaces
86+
("oncs", c_uint16), # Optional NVM Command Support
87+
("fuses", c_uint16), # Fused Operation Support
88+
("fna", c_uint8), # Format NVM Attributes
89+
("vwc", c_uint8), # Volatile Write Cache
90+
("awun", c_uint16), # Atomic Write Unit Normal
91+
("awupf", c_uint16), # Atomic Write Unit Power Fail
92+
("nvscc", c_uint8), # NVM Vendor Specific Command Configuration
93+
("reserved3", c_uint8 * (704 - 531)),
94+
("reserved4", c_uint8 * (2048 - 704)),
95+
("psd", nvme_identify_controller_psd * 32), # Power State Descriptor
96+
("vs", nvme_identify_controller_amzn_vs)] # Vendor Specific
97+
98+
class ebs_nvme_device:
99+
def __init__(self, device):
100+
self.device = device
101+
self.ctrl_identify()
102+
103+
def _nvme_ioctl(self, id_response, id_len):
104+
admin_cmd = nvme_admin_command(opcode = NVME_ADMIN_IDENTIFY,
105+
addr = id_response,
106+
alen = id_len,
107+
cdw10 = 1)
108+
109+
with open(self.device, "rw") as nvme:
110+
ioctl(nvme, NVME_IOCTL_ADMIN_CMD, admin_cmd)
111+
112+
def ctrl_identify(self):
113+
self.id_ctrl = nvme_identify_controller()
114+
self._nvme_ioctl(addressof(self.id_ctrl), sizeof(self.id_ctrl))
115+
116+
if self.id_ctrl.vid != AMZN_NVME_VID or self.id_ctrl.mn.strip() != AMZN_NVME_EBS_MN:
117+
raise TypeError("[ERROR] Not an EBS device: '{0}'".format(self.device))
118+
119+
def get_volume_id(self):
120+
vol = self.id_ctrl.sn
121+
122+
if vol.startswith("vol") and vol[3] != "-":
123+
vol = "vol-" + vol[3:]
124+
125+
return vol
126+
127+
def get_block_device(self, stripped=False):
128+
dev = self.id_ctrl.vs.bdev.strip()
129+
130+
if stripped and dev.startswith("/dev/"):
131+
dev = dev[5:]
132+
133+
return dev
134+
135+
if __name__ == "__main__":
136+
parser = argparse.ArgumentParser(description="Reads EBS information from NVMe devices.")
137+
parser.add_argument("device", nargs=1, help="Device to query")
138+
139+
display = parser.add_argument_group("Display Options")
140+
display.add_argument("-v", "--volume", action="store_true",
141+
help="Return volume-id")
142+
display.add_argument("-b", "--block-dev", action="store_true",
143+
help="Return block device mapping")
144+
display.add_argument("-u", "--udev", action="store_true",
145+
help="Output data in format suitable for udev rules")
146+
147+
if len(sys.argv) < 2:
148+
parser.print_help()
149+
sys.exit(1)
150+
151+
args = parser.parse_args()
152+
153+
get_all = not (args.udev or args.volume or args.block_dev)
154+
155+
try:
156+
dev = ebs_nvme_device(args.device[0])
157+
except (IOError, TypeError) as err:
158+
print >> sys.stderr, err
159+
sys.exit(1)
160+
161+
if get_all or args.volume:
162+
print "Volume ID: {0}".format(dev.get_volume_id())
163+
if get_all or args.block_dev or args.udev:
164+
print dev.get_block_device(args.udev)

0 commit comments

Comments
 (0)