def create_pool(user_pool=False, pool_name=pool_name, pool_target=pool_target): """ Create libvirt pool as the output storage """ if output_uri == "qemu:///session" or user_pool: target_path = os.path.join("/home", v2v_user, pool_target) cmd = su_cmd + "'mkdir -p %s'" % target_path process.system(cmd, verbose=True) # Sometimes pool_creat_as returns sucess, but the pool can # not be found in user session. virsh.pool_create_as(pool_name, 'dir', target_path, unprivileged_user=v2v_user, debug=True) res = virsh.pool_info(pool_name, unprivileged_user=v2v_user, debug=True) if res.exit_status != 0: return False else: pvt.pre_pool(pool_name, pool_type, pool_target, emulated_img) return True
def run(test, params, env): ''' Test the command virsh pool-create-as (1) Call virsh pool-create-as (2) Call virsh -c remote_uri pool-create-as (3) Call virsh pool-create-as with an unexpected option ''' # Run test case if not params.has_key('pool_name') or not params.has_key('pool_target'): logging.error("Please give a 'name' and 'target'") pool_options = params.get('pool_options', '') pool_name = params.get('pool_name') pool_type = params.get('pool_type') pool_target = params.get('pool_target') if not os.path.isdir(pool_target): if os.path.isfile(pool_target): logging.error('<target> must be a directory') else: os.makedirs(pool_target) logging.info('Creating a %s type pool %s', pool_type, pool_name) status = virsh.pool_create_as(pool_name, pool_type, pool_target, extra=pool_options, uri=virsh.canonical_uri()) # Check status_error status_error = params.get('status_error') if status_error == 'yes': if status: raise error.TestFail("%d not a expected command return value" % status) else: logging.info("It's an expected error") elif status_error == 'no': result = virsh.pool_info(pool_name, uri=virsh.canonical_uri()) if result.exit_status: raise error.TestFail('Failed to check pool information') else: logging.info('Pool %s is running', pool_name) if not status: raise error.TestFail('%d not a expected command return value' % status) else: logging.info('Succeed to create pool %s', pool_name) # Clean up libvirt_pool = libvirt_storage.StoragePool() if libvirt_pool.pool_exists(pool_name): libvirt_pool.delete_pool(pool_name)
def prepare_scsi_pool(pool_name, wwnn, wwpn, parent_scsi, pool_target): """ Create a scsi pool with pool-create-as :params pool_name: name of the pool :params wwnn: wwnn to be used for vhba :params wwpn: wwpn to be used for vhba :params parent_scsi: vhba will be created under parent scsi_host :params pool_target: the target dir of the pool """ extra = "--adapter-wwnn %s --adapter-wwpn %s " extra += "--adapter-parent %s" extra %= (wwnn, wwpn, parent_scsi) if not virsh.pool_create_as(pool_name, "scsi", pool_target, extra): raise exceptions.TestFail("Failed to prepare pool:%s", 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(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)