def _get_scsi_standard_inquiry_the_right_way(): allocation_length = STANDARD_INQUIRY_MINIMAL_DATA_LENGTH with self.asi_context() as asi: command = StandardInquiryCommand(allocation_length=allocation_length) result = sync_wait(command.execute(asi)) if result.additional_length >= 0: allocation_length += result.additional_length command = StandardInquiryCommand(allocation_length=allocation_length) result = sync_wait(command.execute(asi)) return result
def get_alua_state(self): from infi.asi.cdb.rtpg import RTPGCommand from infi.asi.cdb.inquiry.vpd_pages.device_identification import DeviceIdentificationVPDPageCommand from infi.asi.coroutines.sync_adapter import sync_wait rtpg_command = RTPGCommand() device_identification_command = DeviceIdentificationVPDPageCommand() with self.asi_context() as asi: rtpg_result = sync_wait(rtpg_command.execute(asi)) device_identification_result = sync_wait(device_identification_command.execute(asi)) target_port_group = device_identification_result.designators_list[-1].target_port_group [device_alua_state] = [descriptor.asymetric_access_state for descriptor in rtpg_result.descriptor_list if descriptor.target_port_group == target_port_group] return device_alua_state
def get_scsi_inquiry_pages(self): """Returns an immutable dict-like object of available inquiry pages from this device. For example: >>> device.get_scsi_inquiry_pages()[0x80].product_serial_number """ from infi.asi.cdb.inquiry.vpd_pages import INQUIRY_PAGE_SUPPORTED_VPD_PAGES from infi.asi.cdb.inquiry.vpd_pages import SupportedVPDPagesCommand from infi.asi import AsiCheckConditionError from infi.asi.coroutines.sync_adapter import sync_wait command = SupportedVPDPagesCommand() page_dict = {} try: with self.asi_context() as asi: data = sync_wait(command.execute(asi)) page_dict[INQUIRY_PAGE_SUPPORTED_VPD_PAGES] = data for page_code in data.vpd_parameters: page_dict[page_code] = None except AsiCheckConditionError, e: (key, code) = (e.sense_obj.sense_key, e.sense_obj.additional_sense_code.code_name) if (key, code) == ('ILLEGAL_REQUEST', 'INVALID FIELD IN CDB'): # There are devices such as virtual USB disk controllers (bladecenter stuff) that don't support this # (mandatory!) command. In this case we simply return an empty dict. pass else: raise
def get_scsi_standard_inquiry(self): """:returns: the standard inquiry data""" from infi.asi.cdb.inquiry.standard import StandardInquiryCommand from infi.asi.coroutines.sync_adapter import sync_wait with self.asi_context() as asi: command = StandardInquiryCommand() return sync_wait(command.execute(asi))
def test_inquiry_command_on_path(self, path, command): from infi.asi import create_platform_command_executer, AsiCheckConditionError from infi.asi.coroutines.sync_adapter import sync_wait from os import open, O_RDWR from infi.asi.unix import UnixFile for scsi_id_path in glob('/sys/class/scsi_disk/*'): if scsi_id_path.split('/')[-1] == path.split('/')[-1]: block_device = glob(scsi_id_path + '/device/block/*')[0].split('/')[-1] if not block_device.startswith('sd'): raise unittest.SkipTest() break f = UnixFile(open(path, O_RDWR)) executer = create_platform_command_executer(f) cdb = command() try: _ = sync_wait(cdb.execute(executer)) except AsiCheckConditionError as e: # Some devices does not support some pages # for example, VMware on hba02 does not support 0x83 if e.sense_obj.sense_key == 'ILLEGAL_REQUEST' \ and e.sense_obj.additional_sense_code.code_name == 'INVALID FIELD IN CDB': pass else: raise f.close()
def register_key_example(device): with asi_context_linux(device) as asi: command = PersistentReserveOutCommand( service_action=PERSISTENT_RESERVE_OUT_SERVICE_ACTION_CODES.REGISTER, service_action_reservation_key=0xABBA) response = sync_wait(command.execute(asi)) return response
def get_scsi_standard_inquiry(access_path): from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.inquiry.standard import StandardInquiryCommand, STANDARD_INQUIRY_MINIMAL_DATA_LENGTH with asi_context(access_path) as asi: command = StandardInquiryCommand(allocation_length=STANDARD_INQUIRY_MINIMAL_DATA_LENGTH) result = sync_wait(command.execute(asi)) return result
def get_scsi_test_unit_ready(self): """:returns: True if the device is ready """ from infi.asi.cdb.tur import TestUnitReadyCommand from infi.asi.coroutines.sync_adapter import sync_wait with self.asi_context() as asi: command = TestUnitReadyCommand() return sync_wait(command.execute(asi))
def test_simple_coroutines(): def bar(): yield 5 def foo(): i = yield bar() yield i res = sync_wait(foo()) assert res == 5
def _get_scsi_standard_inquiry_the_fastest_way(allocation_length=219): try: with self.asi_context() as asi: command = StandardInquiryCommand(allocation_length=allocation_length) return sync_wait(command.execute(asi)) except AsiCheckConditionError, e: (key, code) = (e.sense_obj.sense_key, e.sense_obj.additional_sense_code.code_name) if (key, code) == ('ILLEGAL_REQUEST', 'INVALID FIELD IN CDB'): return raise
def get_scsi_serial(access_path): from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.inquiry.vpd_pages.unit_serial_number import UnitSerialNumberVPDPageCommand with asi_context(access_path) as asi: command = UnitSerialNumberVPDPageCommand() try: result = sync_wait(command.execute(asi)) except: return "" return result.product_serial_number
def get_scsi_serial(access_path): from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.inquiry.vpd_pages.unit_serial_number import UnitSerialNumberVPDPageCommand with asi_context(access_path) as asi: command = UnitSerialNumberVPDPageCommand() try: result = sync_wait(command.execute(asi)) except: return '' return result.product_serial_number
def get_capacity_in_bytes(access_path): from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.read_capacity import ReadCapacity16Command with asi_context(access_path) as asi: command = ReadCapacity16Command() try: result = sync_wait(command.execute(asi)) except: return "" return result.last_logical_block_address * result.block_length_in_bytes
def get_size_in_bytes(self): from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.read_capacity import ReadCapacity10Command, ReadCapacity16Command with self.asi_context() as asi: for command in [ReadCapacity16Command, ReadCapacity10Command]: try: result = sync_wait(command().execute(asi)) return result.last_logical_block_address * result.block_length_in_bytes except: pass return 0
def test_report_luns_command(): from infi.asi.unix import UnixFile import os if not os.path.exists("/dev/sg1"): return from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.report_luns import ReportLunsCommand from infi.asi import create_platform_command_executer handle = UnixFile(os.open("/dev/sg1", os.O_RDWR)) executer = create_platform_command_executer(handle) cdb = ReportLunsCommand(select_report=0) result = sync_wait(cdb.execute(executer)) assert result.lun_list != [] assert 0 in result.lun_list
def get_scsi_test_unit_ready(self): """Returns True if the device is ready, False if got NOT_READY check condition """ from infi.asi.cdb.tur import TestUnitReadyCommand from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi import AsiCheckConditionError with self.asi_context() as asi: try: command = TestUnitReadyCommand() return sync_wait(command.execute(asi)) except AsiCheckConditionError, e: (key, code) = (e.sense_obj.sense_key, e.sense_obj.additional_sense_code.code_name) if key in ('NOT_READY', 'ILLEGAL_REQUEST'): return False raise
def get_scsi_ses_pages(self, helper=None): """Returns an immutable dict-like object of available SES pages from this device. """ from infi.asi.cdb.diagnostic.ses_pages import DIAGNOSTIC_PAGE_SUPPORTED_PAGES from infi.asi.cdb.diagnostic.ses_pages import SupportedDiagnosticPagesCommand from infi.asi.coroutines.sync_adapter import sync_wait command = SupportedDiagnosticPagesCommand() page_dict = {} with self.asi_context() as asi: data = sync_wait(command.execute(asi)) page_dict[DIAGNOSTIC_PAGE_SUPPORTED_PAGES] = data.supported_pages[:data.page_length] for page in range(data.page_length): page_dict[data.supported_pages[page]] = None return SupportedSesPagesDict(page_dict, self, helper)
def test_using_generator(): def kar(): return 5 def bar(): for i in range(5): yield i def foo(): i = yield kar() for n in bar(): i += 1 yield i res = sync_wait(foo()) assert res == 10
def test_report_luns_command(): from infi.os_info import get_platform_string from infi.asi.unix import UnixFile import os if 'ubuntu' not in get_platform_string() or not os.path.exists("/dev/sg1"): # on some of our other environments, sg0 is the cdrom and sg1 is the local disk, and on others it's the # other way around. just test this on Ubuntu only. return from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi.cdb.report_luns import ReportLunsCommand from infi.asi import create_platform_command_executer handle = UnixFile(os.open("/dev/sg1", os.O_RDWR)) executer = create_platform_command_executer(handle) cdb = ReportLunsCommand(select_report=0) result = sync_wait(cdb.execute(executer)) assert result.lun_list != [] assert 0 in result.lun_list
def __test_exception(): def kar(): yield 5 def bar(): i = yield kar() raise Exception("my bad") def foo(): try: i = yield bar() except Exception: i = 7 yield i res = sync_wait(foo()) assert res == 7
def run_cmd(os_file, cmd, helper=None): executer = create_platform_command_executer(os_file) cdb = cmd(helper) if helper else cmd() data = sync_wait(cdb.execute(executer)) return data
class OSFile(object): def __init__(self, fd): self.fd = fd def read(self, size): return os.read(self.fd, size) def write(self, buffer): return os.write(self.fd, buffer) def close(self): os.close(self.fd) f = OSFile(os.open(path, os.O_RDWR)) try: executer = create_platform_command_executer(f) possible_commands = {10: ReadCapacity10Command, 16: ReadCapacity16Command} cdb = possible_commands[cdb_size](logical_block_address=0, pmi=0) data = sync_wait(cdb.execute(executer)) print(repr(data)) f.close() except: print_exc()
def func(sg_device, cdb): with asi_context(sg_device) as executer: return sync_wait(cdb.execute(executer))
from infi.asi import create_platform_command_executer from infi.asi.cdb.write import Write6Command, Write10Command from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi import create_os_file from infi.exceptools import print_exc if len(sys.argv) not in (5, 6): sys.stderr.write("usage: %s device_name offset length cdb_size [char='\x00']\n" % sys.argv[0]) sys.exit(1) path, offset, length, cdb_size = (sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])) char = sys.argv[5][0] if len(sys.argv) == 6 else "\x00" f = create_os_file(path) try: executer = create_platform_command_executer(f) possible_commands = {6: Write6Command, 10: Write10Command} cdb = possible_commands[cdb_size](logical_block_address=offset, buffer=char * (length * 512)) sync_wait(cdb.execute(executer)) f.close() except: print_exc()
from infi.exceptools import print_exc if len(sys.argv) != 5: sys.stderr.write("usage: %s device_name offset length cdb_size\n" % sys.argv[0]) sys.exit(1) path, offset, length, cdb_size = (sys.argv[1], int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])) f = create_os_file(path) try: executer = create_platform_command_executer(f) possible_commands = {6: Read6Command, 10: Read10Command, 12: Read12Command, 16: Read16Command } cdb = possible_commands[cdb_size](logical_block_address=offset, transfer_length=length) data = sync_wait(cdb.execute(executer)) print(repr(data)) f.close() except: print_exc()
def _create_value(self, page_code): from infi.asi.cdb.diagnostic.ses_pages import get_ses_page from infi.asi.coroutines.sync_adapter import sync_wait with self.device.asi_context() as asi: diagnostic_command = get_ses_page(page_code)(self.helper) if self.helper else get_ses_page(page_code)() return sync_wait(diagnostic_command.execute(asi))
import platform from infi.asi import create_platform_command_executer from infi.asi.cdb.compare_and_write import CompareAndWriteCommand from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi import create_os_file from infi.exceptools import print_exc import sys (path, offset, compare_buffer_path, write_buffer_path) = (sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4]) f = create_os_file(path) with open(compare_buffer_path) as p: compare_buffer = p.read() with open(write_buffer_path) as p: write_buffer = p.read() try: executer = create_platform_command_executer(f) cdb = CompareAndWriteCommand(logical_block_address=offset, buffer=compare_buffer + write_buffer, number_of_logical_blocks=1) sync_wait(cdb.execute(executer)) f.close() except: print_exc()
import sys from infi.asi import create_platform_command_executer from infi.asi.cdb.inquiry.standard import StandardInquiryCommand from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi import create_os_file from infi.exceptools import print_exc if len(sys.argv) != 2: sys.stderr.write("usage: %s device_name\n" % sys.argv[0]) sys.exit(1) path = sys.argv[1] f = create_os_file(path) try: executer = create_platform_command_executer(f) inquiry = StandardInquiryCommand() data = sync_wait(inquiry.execute(executer)) print(data) f.close() except: print_exc()
def func(sg_device, cdb): with asi_context(sg_device) as executer: queue.put(sync_wait(cdb.execute(executer)))
def _create_value(self, page_code): from infi.asi.cdb.inquiry.vpd_pages import get_vpd_page from infi.asi.coroutines.sync_adapter import sync_wait with self.device.asi_context() as asi: inquiry_command = get_vpd_page(page_code)() return sync_wait(inquiry_command.execute(asi))
def _send_null_write(self, device): from infi.asi.cdb.write import Write10Command from infi.asi.coroutines.sync_adapter import sync_wait cdb = Write10Command(0, '') # empty write with device.asi_context() as asi: sync_wait(cdb.execute(asi))
def read_keys_example(device): with asi_context_linux(device) as asi: command = PersistentReserveInCommand(service_action=PERSISTENT_RESERVE_IN_SERVICE_ACTION_CODES.READ_KEYS) response = sync_wait(command.execute(asi)) return response
import sys from infi.asi import create_platform_command_executer from infi.asi.cdb.tur import TestUnitReadyCommand from infi.asi.coroutines.sync_adapter import sync_wait from infi.asi import create_os_file from infi.exceptools import print_exc if len(sys.argv) != 2: sys.stderr.write("usage: %s device_name\n" % sys.argv[0]) sys.exit(1) path = sys.argv[1] f = create_os_file(path) try: executer = create_platform_command_executer(f) tur = TestUnitReadyCommand() data = sync_wait(tur.execute(executer)) print(data) f.close() except: print_exc()