Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor repo cleanup and python3 support #1

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
Expand Down
53 changes: 31 additions & 22 deletions ebsnvme-id
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python2.7
#!/usr/bin/env python

# Copyright 2017 Amazon.com, Inc. and its affiliates. All Rights Reserved.
# Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
Expand All @@ -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,60 +94,64 @@ 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, "rw") as nvme:
with open(self.device, "r+") as nvme:
ioctl(nvme, NVME_IOCTL_ADMIN_CMD, admin_cmd)

def ctrl_identify(self):
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.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
vol = self.id_ctrl.sn.decode()

if vol.startswith("vol") and vol[3] != "-":
vol = "vol-" + vol[3:]

return vol

def get_block_device(self, stripped=False):
dev = self.id_ctrl.vs.bdev.strip()
dev = self.id_ctrl.vs.bdev.decode().strip()

if stripped and dev.startswith("/dev/"):
dev = dev[5:]

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 All @@ -155,10 +164,10 @@ if __name__ == "__main__":
try:
dev = ebs_nvme_device(args.device[0])
except (IOError, TypeError) as err:
print >> sys.stderr, err
print(err, file=sys.stderr)
sys.exit(1)

if get_all or args.volume:
print "Volume ID: {0}".format(dev.get_volume_id())
print("Volume ID: {0}".format(dev.get_volume_id()))
if get_all or args.block_dev or args.udev:
print dev.get_block_device(args.udev)
print(dev.get_block_device(args.udev))
2 changes: 1 addition & 1 deletion ec2nvme-nsid
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Copyright 2016 Amazon.com, Inc. and its affiliates. All Rights Reserved.
# Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
#
# Licensed under the MIT License. See the LICENSE accompanying this file
# for the specific language governing permissions and limitations under
Expand Down