def cleanup_iscsi(): """ Logout all iscsi target and restart tgtd service """ iscsi_session = iscsi.iscsi_get_sessions() if iscsi_session: utils.run("iscsiadm -m node -u") utils.run("service tgtd restart") for image in [src_emulated_image, dest_emulated_image]: if image: image = os.path.join(test.tmpdir, image) if os.path.exists(image): utils.run("rm -f %s" % image)
def create_iscsi_device(test, device_size="2G"): """ Create a iscsi device. """ imgname = "emulated_iscsi" device_name = utlv.setup_or_cleanup_iscsi(is_setup=True, emulated_image=imgname, image_size=device_size) # Verify if expected iscsi device has been set for iscsi_node in iscsi.iscsi_get_sessions(): if iscsi_node[1].count(imgname): # Remove port for pool operations ip_addr = iscsi_node[0].split(":3260")[0] iscsi_device = (ip_addr, iscsi_node[1]) break if iscsi_device == (): test.fail("No matched iscsi device.") check_ret = process.run("ls %s" % device_name, shell=True) if check_ret.exit_status: test.fail("Can not find provided device:%s" % check_ret) return device_name
def create_iscsi_device(device_size="2G"): """ Create a iscsi device. """ imgname = "emulated_iscsi" device_name = utlv.setup_or_cleanup_iscsi(is_setup=True, emulated_image=imgname, image_size=device_size) # Verify if expected iscsi device has been set for iscsi_node in iscsi.iscsi_get_sessions(): if iscsi_node[1].count(imgname): # Remove port for pool operations ip_addr = iscsi_node[0].split(":3260")[0] iscsi_device = (ip_addr, iscsi_node[1]) break if iscsi_device == (): raise error.TestFail("No matched iscsi device.") check_ret = utils.run("ls %s" % device_name) if check_ret.exit_status: raise error.TestFail("Can not find provided device:%s" % check_ret) return device_name
def pre_pool(self, pool_name, pool_type, pool_target, emulated_image, image_size="100M", pre_disk_vol=[]): """ Preapare the specific type pool Note: 1. For scsi type pool, it only could be created from xml file 2. Other type pools can be created by pool_creat_as function 3. Disk pool will not allow to create volume with virsh commands So we can prepare it before pool created :param pool_name: created pool name :param pool_type: dir, disk, logical, fs, netfs or else :param pool_target: target of storage pool :param emulated_image: use an image file to simulate a scsi disk it could be used for disk, logical pool :param image_size: the size for emulated image :param pre_disk_vol: a list include partition size to be created no more than 4 partition because msdos label """ extra = "" if pool_type == "dir": logging.info("Pool path:%s", self.tmpdir) pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) elif pool_type == "disk": device_name = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) # If pre_vol is None, disk pool will have no volume if type(pre_disk_vol) == list and len(pre_disk_vol): for vol in pre_disk_vol: mk_part(device_name, vol) extra = " --source-dev %s" % device_name elif pool_type == "fs": device_name = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd = "mkfs.ext4 -F %s" % device_name pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) extra = " --source-dev %s" % device_name utils.run(cmd) elif pool_type == "logical": logical_device = setup_or_cleanup_iscsi( is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd_pv = "pvcreate %s" % logical_device vg_name = "vg_%s" % pool_type cmd_vg = "vgcreate %s %s" % (vg_name, logical_device) extra = "--source-name %s" % vg_name utils.run(cmd_pv) utils.run(cmd_vg) # Create a small volume for verification # And VG path will not exist if no any volume in.(bug?) cmd_lv = "lvcreate --name default_lv --size 1M %s" % vg_name utils.run(cmd_lv) elif pool_type == "netfs": nfs_server_dir = self.params.get("nfs_server_dir", "nfs-server") nfs_path = os.path.join(self.tmpdir, nfs_server_dir) if not os.path.exists(nfs_path): os.mkdir(nfs_path) pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) setup_or_cleanup_nfs(is_setup=True, export_options="rw,async,no_root_squash", mount_src=nfs_path) source_host = self.params.get("source_host", "localhost") extra = "--source-host %s --source-path %s" % (source_host, nfs_path) elif pool_type == "iscsi": logical_device = setup_or_cleanup_iscsi( is_setup=True, emulated_image=emulated_image, image_size=image_size) iscsi_session = iscsi.iscsi_get_sessions() iscsi_device = () for iscsi_node in iscsi_session: if iscsi_node[1].count(emulated_image): iscsi_device = iscsi_node break if iscsi_device == (): raise error.TestFail("No iscsi device.") if "::" in iscsi_device[0]: iscsi_device = ('localhost', iscsi_device[1]) extra = " --source-host %s --source-dev %s" % iscsi_device elif pool_type == "scsi": scsi_xml_file = self.params.get("scsi_xml_file") if not os.path.exists(scsi_xml_file): scsi_xml_file = os.path.join(self.tmpdir, scsi_xml_file) logical_device = setup_or_cleanup_iscsi( is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd = ("iscsiadm -m session -P 3 |grep -B3 %s| grep Host|awk " "'{print $3}'" % logical_device.split('/')[2]) scsi_host = utils.system_output(cmd) scsi_xml = """ <pool type='scsi'> <name>%s</name> <source> <adapter type='scsi_host' name='host%s'/> </source> <target> <path>/dev/disk/by-path</path> </target> </pool> """ % (pool_name, scsi_host) logging.debug("Prepare the scsi pool xml: %s", scsi_xml) xml_object = open(scsi_xml_file, 'w') xml_object.write(scsi_xml) xml_object.close() # Create pool if pool_type == "scsi": re_v = virsh.pool_create(scsi_xml_file) else: re_v = virsh.pool_create_as(pool_name, pool_type, pool_target, extra) if not re_v: raise error.TestFail("Create pool failed.") # Check the created pool check_actived_pool(pool_name)
def pre_pool(self, pool_name, pool_type, pool_target, emulated_image, image_size="100M", pre_disk_vol=[]): """ Preapare the specific type pool Note: 1. For scsi type pool, it only could be created from xml file 2. Other type pools can be created by pool_creat_as function 3. Disk pool will not allow to create volume with virsh commands So we can prepare it before pool created :param pool_name: created pool name :param pool_type: dir, disk, logical, fs, netfs or else :param pool_target: target of storage pool :param emulated_image: use an image file to simulate a scsi disk it could be used for disk, logical pool :param image_size: the size for emulated image :param pre_disk_vol: a list include partition size to be created no more than 4 partition because msdos label """ extra = "" if pool_type == "dir": logging.info("Pool path:%s", self.tmpdir) pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) elif pool_type == "disk": device_name = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) # If pre_vol is None, disk pool will have no volume if type(pre_disk_vol) == list and len(pre_disk_vol): for vol in pre_disk_vol: mk_part(device_name, vol) extra = " --source-dev %s" % device_name elif pool_type == "fs": device_name = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd = "mkfs.ext4 -F %s" % device_name pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) extra = " --source-dev %s" % device_name utils.run(cmd) elif pool_type == "logical": logical_device = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd_pv = "pvcreate %s" % logical_device vg_name = "vg_%s" % pool_type cmd_vg = "vgcreate %s %s" % (vg_name, logical_device) extra = "--source-name %s" % vg_name utils.run(cmd_pv) utils.run(cmd_vg) # Create a small volume for verification # And VG path will not exist if no any volume in.(bug?) cmd_lv = "lvcreate --name default_lv --size 1M %s" % vg_name utils.run(cmd_lv) elif pool_type == "netfs": nfs_server_dir = self.params.get("nfs_server_dir", "nfs-server") nfs_path = os.path.join(self.tmpdir, nfs_server_dir) if not os.path.exists(nfs_path): os.mkdir(nfs_path) pool_target = os.path.join(self.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) setup_or_cleanup_nfs(is_setup=True, export_options="rw,async,no_root_squash", mount_src=nfs_path) source_host = self.params.get("source_host", "localhost") extra = "--source-host %s --source-path %s" % (source_host, nfs_path) elif pool_type == "iscsi": logical_device = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) iscsi_session = iscsi.iscsi_get_sessions() iscsi_device = () for iscsi_node in iscsi_session: if iscsi_node[1].count(emulated_image): iscsi_device = iscsi_node break if iscsi_device == (): raise error.TestFail("No iscsi device.") if "::" in iscsi_device[0]: iscsi_device = ('localhost', iscsi_device[1]) extra = " --source-host %s --source-dev %s" % iscsi_device elif pool_type == "scsi": scsi_xml_file = self.params.get("scsi_xml_file") if not os.path.exists(scsi_xml_file): scsi_xml_file = os.path.join(self.tmpdir, scsi_xml_file) logical_device = setup_or_cleanup_iscsi(is_setup=True, emulated_image=emulated_image, image_size=image_size) cmd = ("iscsiadm -m session -P 3 |grep -B3 %s| grep Host|awk " "'{print $3}'" % logical_device.split('/')[2]) scsi_host = utils.system_output(cmd) scsi_xml = """ <pool type='scsi'> <name>%s</name> <source> <adapter type='scsi_host' name='host%s'/> </source> <target> <path>/dev/disk/by-path</path> </target> </pool> """ % (pool_name, scsi_host) logging.debug("Prepare the scsi pool xml: %s", scsi_xml) xml_object = open(scsi_xml_file, 'w') xml_object.write(scsi_xml) xml_object.close() # Create pool if pool_type == "scsi": re_v = virsh.pool_create(scsi_xml_file) else: re_v = virsh.pool_create_as(pool_name, pool_type, pool_target, extra) if not re_v: raise error.TestFail("Create pool failed.") # Check the created pool check_actived_pool(pool_name)
def pre_pool(pool_name, pool_type, pool_target, emulated_image): """ Preapare the specific type pool Note: 1. For scsi type pool, it only could be created from xml file 2. Other type pools can be created by pool_creat_as function """ extra = "" if pool_type == "dir": logging.info(test.tmpdir) pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) elif pool_type == "disk": device_name = login_iscsi(emulated_image, "1G") mk_part(device_name) extra = " --source-dev %s" % device_name elif pool_type == "fs": device_name = login_iscsi(emulated_image, "1G") cmd = "mkfs.ext4 -F %s" % device_name pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) extra = " --source-dev %s" % device_name utils.run(cmd) elif pool_type == "logical": logical_device = login_iscsi(emulated_image, "1G") cmd_pv = "pvcreate %s" % logical_device vg_name = "vg_%s" % pool_type cmd_vg = "vgcreate %s %s" % (vg_name, logical_device) extra = "--source-name %s" % vg_name utils.run(cmd_pv) utils.run(cmd_vg) elif pool_type == "netfs": nfs_path = os.path.join(test.tmpdir, nfs_server_dir) if not os.path.exists(nfs_path): os.mkdir(nfs_path) pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) set_nfs_server("%s *(rw,async,no_root_squash)" % nfs_path) extra = "--source-host %s --source-path %s" % (source_host, nfs_path) elif pool_type == "iscsi": logical_device = login_iscsi(emulated_image, "100M") iscsi_session = iscsi.iscsi_get_sessions() iscsi_device = () for iscsi_node in iscsi_session: if iscsi_node[1].count(emulated_image): iscsi_device = iscsi_node break if iscsi_device == (): raise error.TestFail("No iscsi device.") if "::" in iscsi_device[0]: iscsi_device = ('localhost', iscsi_device[1]) extra = " --source-host %s --source-dev %s" % iscsi_device elif pool_type == "scsi": scsi_xml_file = params.get("scsi_xml_file") if not os.path.exists(scsi_xml_file): scsi_xml_file = os.path.join(test.tmpdir, scsi_xml_file) logical_device = login_iscsi(emulated_image, "100M") cmd = "iscsiadm -m session -P 3 |grep -B3 %s| \ grep Host|awk '{print $3}'" % logical_device.split('/')[2] scsi_host = utils.system_output(cmd) scsi_xml = """ <pool type='scsi'> <name>%s</name> <source> <adapter type='scsi_host' name='host%s'/> </source> <target> <path>/dev/disk/by-path</path> </target> </pool> """ % (pool_name, scsi_host) logging.debug("Prepare the scsi pool xml: %s", scsi_xml) xml_object = open(scsi_xml_file, 'w') xml_object.write(scsi_xml) xml_object.close() # Create pool if pool_type == "scsi": re_v = virsh.pool_create(scsi_xml_file) else: re_v = virsh.pool_create_as(pool_name, pool_type, pool_target, extra) if not re_v: raise error.TestFail("Create pool failed.") # Check the created pool if not check_pool(pool_name): raise error.TestFail("Can't find active pool: %s", pool_name)
def pre_pool(pool_name, pool_type, pool_target, emulated_image): """ Preapare the specific type pool Note: 1. For scsi type pool, it only could be created from xml file 2. Other type pools can be created by pool_creat_as function """ extra = "" if pool_type == "dir": logging.info(test.tmpdir) pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) elif pool_type == "disk": device_name = login_iscsi(emulated_image, "1G") mk_part(device_name) extra = " --source-dev %s" % device_name elif pool_type == "fs": device_name = login_iscsi(emulated_image, "1G") cmd = "mkfs.ext4 -F %s" % device_name pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) extra = " --source-dev %s" % device_name utils.run(cmd) elif pool_type == "logical": logical_device = login_iscsi(emulated_image, "1G") cmd_pv = "pvcreate %s" % logical_device vg_name = "vg_%s" % pool_type cmd_vg = "vgcreate %s %s" % (vg_name, logical_device) extra = "--source-name %s" % vg_name utils.run(cmd_pv) utils.run(cmd_vg) elif pool_type == "netfs": nfs_path = os.path.join(test.tmpdir, nfs_server_dir) if not os.path.exists(nfs_path): os.mkdir(nfs_path) pool_target = os.path.join(test.tmpdir, pool_target) if not os.path.exists(pool_target): os.mkdir(pool_target) set_nfs_server("%s *(rw,async,no_root_squash)" % nfs_path) extra = "--source-host %s --source-path %s" % (source_host, nfs_path) elif pool_type == "iscsi": logical_device = login_iscsi(emulated_image, "100M") iscsi_session = iscsi.iscsi_get_sessions() iscsi_device = () for iscsi_node in iscsi_session: if iscsi_node[1].count(emulated_image): iscsi_device = iscsi_node break if iscsi_device == (): raise error.TestFail("No iscsi device.") if "::" in iscsi_device[0]: iscsi_device = ('localhost', iscsi_device[1]) extra = " --source-host %s --source-dev %s" % iscsi_device elif pool_type == "scsi": scsi_xml_file = params.get("scsi_xml_file") if not os.path.exists(scsi_xml_file): scsi_xml_file = os.path.join(test.tmpdir, scsi_xml_file) logical_device = login_iscsi(emulated_image, "100M") cmd = "iscsiadm -m session -P 3 |grep -B3 %s| \ grep Host|awk '{print $3}'" % logical_device.split( '/')[2] scsi_host = utils.system_output(cmd) scsi_xml = """ <pool type='scsi'> <name>%s</name> <source> <adapter type='scsi_host' name='host%s'/> </source> <target> <path>/dev/disk/by-path</path> </target> </pool> """ % (pool_name, scsi_host) logging.debug("Prepare the scsi pool xml: %s", scsi_xml) xml_object = open(scsi_xml_file, 'w') xml_object.write(scsi_xml) xml_object.close() # Create pool if pool_type == "scsi": re_v = virsh.pool_create(scsi_xml_file) else: re_v = virsh.pool_create_as(pool_name, pool_type, pool_target, extra) if not re_v: raise error.TestFail("Create pool failed.") # Check the created pool if not check_pool(pool_name): raise error.TestFail("Can't find active pool: %s", pool_name)