Debian Codegraph @

ebsnvme-id

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