Skip to content

Commit

Permalink
ebsnvme-id: fix flake8 warnings
Browse files Browse the repository at this point in the history
No functional changes

Note that there are a few lines that still fail the "line too long" check,
mostly due to comments or embedded human-readable text.  In my opinion, fixing
these errors had a negative impact on readability, so I left the lines in place
and suppressed the warnings.

Signed-off-by: Noah Meyerhans <nmeyerha@amzn.com>
  • Loading branch information
Noah Meyerhans authored and Noah Meyerhans committed Apr 30, 2020
1 parent 8d15420 commit 5d4c487
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions ebsnvme-id
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ the volume.
"""

import argparse
from ctypes import *
from ctypes import Structure, c_uint8, c_uint16, \
c_uint32, c_uint64, c_char, addressof, sizeof
from fcntl import ioctl
import sys

Expand All @@ -22,6 +23,7 @@ NVME_IOCTL_ADMIN_CMD = 0xC0484E41
AMZN_NVME_VID = 0x1D0F
AMZN_NVME_EBS_MN = "Amazon Elastic Block Store"


class nvme_admin_command(Structure):
_pack_ = 1
_fields_ = [("opcode", c_uint8), # op code
Expand All @@ -41,11 +43,13 @@ class nvme_admin_command(Structure):
("cdw15", c_uint32),
("reserved1", c_uint64)]


class nvme_identify_controller_amzn_vs(Structure):
_pack_ = 1
_fields_ = [("bdev", c_char * 32), # block device name
("reserved0", c_char * (1024 - 32))]


class nvme_identify_controller_psd(Structure):
_pack_ = 1
_fields_ = [("mp", c_uint16), # maximum power
Expand All @@ -58,6 +62,7 @@ class nvme_identify_controller_psd(Structure):
("rwl", c_uint8), # relative write latency
("reserved1", c_char * 16)]


class nvme_identify_controller(Structure):
_pack_ = 1
_fields_ = [("vid", c_uint16), # PCI Vendor ID
Expand All @@ -77,7 +82,7 @@ class nvme_identify_controller(Structure):
("lpa", c_uint8), # Log Page Attributes
("elpe", c_uint8), # Error Log Page Entries
("npss", c_uint8), # Number of Power States Support
("avscc", c_uint8), # Admin Vendor Specific Command Configuration
("avscc", c_uint8), # Admin Vendor Specific Command Configuration # noqa
("reserved1", c_uint8 * (512 - 265)),
("sqes", c_uint8), # Submission Queue Entry Size
("cqes", c_uint8), # Completion Queue Entry Size
Expand All @@ -89,22 +94,23 @@ class nvme_identify_controller(Structure):
("vwc", c_uint8), # Volatile Write Cache
("awun", c_uint16), # Atomic Write Unit Normal
("awupf", c_uint16), # Atomic Write Unit Power Fail
("nvscc", c_uint8), # NVM Vendor Specific Command Configuration
("nvscc", c_uint8), # NVM Vendor Specific Command Configuration # noqa
("reserved3", c_uint8 * (704 - 531)),
("reserved4", c_uint8 * (2048 - 704)),
("psd", nvme_identify_controller_psd * 32), # Power State Descriptor
("psd", nvme_identify_controller_psd * 32), # Power State Descriptor # noqa
("vs", nvme_identify_controller_amzn_vs)] # Vendor Specific


class ebs_nvme_device:
def __init__(self, device):
self.device = device
self.ctrl_identify()

def _nvme_ioctl(self, id_response, id_len):
admin_cmd = nvme_admin_command(opcode = NVME_ADMIN_IDENTIFY,
addr = id_response,
alen = id_len,
cdw10 = 1)
admin_cmd = nvme_admin_command(opcode=NVME_ADMIN_IDENTIFY,
addr=id_response,
alen=id_len,
cdw10=1)

with open(self.device, "r+") as nvme:
ioctl(nvme, NVME_IOCTL_ADMIN_CMD, admin_cmd)
Expand All @@ -113,8 +119,9 @@ class ebs_nvme_device:
self.id_ctrl = nvme_identify_controller()
self._nvme_ioctl(addressof(self.id_ctrl), sizeof(self.id_ctrl))

if self.id_ctrl.vid != AMZN_NVME_VID or self.id_ctrl.mn.decode().strip() != AMZN_NVME_EBS_MN:
raise TypeError("[ERROR] Not an EBS device: '{0}'".format(self.device))
if self.id_ctrl.vid != AMZN_NVME_VID \
or self.id_ctrl.mn.decode().strip() != AMZN_NVME_EBS_MN:
raise TypeError("[ERROR] Not an EBS device: '{0}'".format(self.device)) # noqa

def get_volume_id(self):
vol = self.id_ctrl.sn.decode()
Expand All @@ -132,17 +139,19 @@ class ebs_nvme_device:

return dev


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Reads EBS information from NVMe devices.")
parser = \
argparse.ArgumentParser(description="Reads EBS information from NVMe devices.") # noqa
parser.add_argument("device", nargs=1, help="Device to query")

display = parser.add_argument_group("Display Options")
display.add_argument("-v", "--volume", action="store_true",
help="Return volume-id")
help="Return volume-id")
display.add_argument("-b", "--block-dev", action="store_true",
help="Return block device mapping")
help="Return block device mapping")
display.add_argument("-u", "--udev", action="store_true",
help="Output data in format suitable for udev rules")
help="Output data in format suitable for udev rules")

if len(sys.argv) < 2:
parser.print_help()
Expand Down

0 comments on commit 5d4c487

Please sign in to comment.