def __init__(self, get_connection, resources= None): #self.libvirt_gen_drv = libvirt_vif.LibvirtGenericVIFDriver(get_connection) self.libvirt_gen_drv = libvirt_vif.LibvirtGenericVIFDriver() self.resources = jsonutils.loads(resources['pci_passthrough_devices']) self.specs = self._prepare_pci_instance_json() utils.write_to_file(CONF.fsl_sriov.pci_devices, jsonutils.dumps(self.specs)) self.neutron_client = neutron_api.API()
def test_load_file(self): dst_fd, dst_path = tempfile.mkstemp() try: os.close(dst_fd) # We have a test for write_to_file. If that is sound, this suffices libvirt_utils.write_to_file(dst_path, 'hello') self.assertEqual(libvirt_utils.load_file(dst_path), 'hello') finally: os.unlink(dst_path)
def test_write_to_file(self): dst_fd, dst_path = tempfile.mkstemp() try: os.close(dst_fd) libvirt_utils.write_to_file(dst_path, 'hello') with open(dst_path, 'r') as fp: self.assertEqual(fp.read(), 'hello') finally: os.unlink(dst_path)
def test_file_open(self): dst_fd, dst_path = tempfile.mkstemp() try: os.close(dst_fd) # We have a test for write_to_file. If that is sound, this suffices libvirt_utils.write_to_file(dst_path, 'hello') with libvirt_utils.file_open(dst_path, 'r') as fp: self.assertEqual(fp.read(), 'hello') finally: os.unlink(dst_path)
def test_write_to_file_with_umask(self): dst_fd, dst_path = tempfile.mkstemp() try: os.close(dst_fd) os.unlink(dst_path) libvirt_utils.write_to_file(dst_path, 'hello', umask=0o277) with open(dst_path, 'r') as fp: self.assertEqual(fp.read(), 'hello') mode = os.stat(dst_path).st_mode self.assertEqual(mode & 0o277, 0) finally: os.unlink(dst_path)
def _is_pci_device_available(self, instance_uuid): pci_devs = jsonutils.loads(utils.load_file(CONF.fsl_sriov.pci_devices)) assignable_pci_device = None for dev in pci_devs: if dev['instance_uuid'] is None: assignable_pci_device = dev['dev_id'] dev['instance_uuid'] = instance_uuid pfname = dev['pf_vf_name'] intbridge = CONF.neutron.ovs_bridge utils.execute('ovs-vsctl', '--no-wait', 'add-port', intbridge , pfname , '--', 'set', 'interface', pfname, 'type=vf_inic', run_as_root=True) break utils.write_to_file(CONF.fsl_sriov.pci_devices, jsonutils.dumps(pci_devs)) return assignable_pci_device
def _deallocate_pcidevice_for_instance(self, instance): """ Remove the UUID from PCI device spec. """ if CONF.pci_passthrough_whitelist and instance: pci_devs = jsonutils.loads(utils.load_file(CONF.fsl_sriov.pci_devices)) for dev in pci_devs: if instance.uuid == dev['instance_uuid']: dev['instance_uuid'] = None pfname = dev['pf_vf_name'] intbridge = CONF.neutron.ovs_bridge utils.execute('ovs-vsctl', '--no-wait', 'del-port', intbridge , pfname , run_as_root=True) utils.write_to_file(CONF.fsl_sriov.pci_devices, jsonutils.dumps(pci_devs))
def _create_image(self, context, inst, xml, suffix="", disk_images=None, network_info=None, block_device_info=None): if not suffix: suffix = "" # syntactic nicety def basepath(fname="", suffix=suffix): return os.path.join(FLAGS.instances_path, inst["name"], fname + suffix) # ensure directories exist and are writable libvirt_utils.ensure_tree(basepath(suffix="")) utils.execute("chmod", "0777", basepath(suffix="")) LOG.info(_("instance %s: Creating image"), inst["name"], instance=inst) if FLAGS.baremetal_type == "lxc": container_dir = "%s/rootfs" % basepath(suffix="") libvirt_utils.ensure_tree(container_dir) # NOTE(vish): No need add the suffix to console.log libvirt_utils.write_to_file(basepath("console.log", ""), "", 007) if not disk_images: disk_images = { "image_id": inst["image_ref"], "kernel_id": inst["kernel_id"], "ramdisk_id": inst["ramdisk_id"], } if disk_images["kernel_id"]: fname = disk_images["kernel_id"] self._cache_image( fn=libvirt_utils.fetch_image, context=context, target=basepath("kernel"), fname=fname, cow=False, image_id=disk_images["kernel_id"], user_id=inst["user_id"], project_id=inst["project_id"], ) if disk_images["ramdisk_id"]: fname = disk_images["ramdisk_id"] self._cache_image( fn=libvirt_utils.fetch_image, context=context, target=basepath("ramdisk"), fname=fname, cow=False, image_id=disk_images["ramdisk_id"], user_id=inst["user_id"], project_id=inst["project_id"], ) root_fname = hashlib.sha1(str(disk_images["image_id"])).hexdigest() size = inst["root_gb"] * 1024 * 1024 * 1024 inst_type_id = inst["instance_type_id"] inst_type = instance_types.get_instance_type(inst_type_id) if inst_type["name"] == "m1.tiny" or suffix == ".rescue": size = None root_fname += "_sm" else: root_fname += "_%d" % inst["root_gb"] self._cache_image( fn=libvirt_utils.fetch_image, context=context, target=basepath("root"), fname=root_fname, cow=False, # FLAGS.use_cow_images, image_id=disk_images["image_id"], user_id=inst["user_id"], project_id=inst["project_id"], ) # For now, we assume that if we're not using a kernel, we're using a # partitioned disk image where the target partition is the first # partition target_partition = None if not inst["kernel_id"]: target_partition = "1" if FLAGS.baremetal_type == "lxc": target_partition = None if inst["key_data"]: key = str(inst["key_data"]) else: key = None net = None nets = [] ifc_template = open(FLAGS.injected_network_template).read() ifc_num = -1 have_injected_networks = False admin_context = nova_context.get_admin_context() for (network_ref, mapping) in network_info: ifc_num += 1 if not network_ref["injected"]: continue have_injected_networks = True address = mapping["ips"][0]["ip"] netmask = mapping["ips"][0]["netmask"] address_v6 = None gateway_v6 = None netmask_v6 = None if FLAGS.use_ipv6: address_v6 = mapping["ip6s"][0]["ip"] netmask_v6 = mapping["ip6s"][0]["netmask"] gateway_v6 = mapping["gateway_v6"] net_info = { "name": "eth%d" % ifc_num, "address": address, "netmask": netmask, "gateway": mapping["gateway"], "broadcast": mapping["broadcast"], "dns": " ".join(mapping["dns"]), "address_v6": address_v6, "gateway_v6": gateway_v6, "netmask_v6": netmask_v6, } nets.append(net_info) if have_injected_networks: net = str(Template(ifc_template, searchList=[{"interfaces": nets, "use_ipv6": FLAGS.use_ipv6}])) metadata = inst.get("metadata") if any((key, net, metadata)): inst_name = inst["name"] injection_path = basepath("root") img_id = inst.image_ref disable_auto_fsck = True for injection in ("metadata", "key", "net"): if locals()[injection]: LOG.info( _("instance %(inst_name)s: injecting " "%(injection)s into image %(img_id)s"), locals(), instance=inst, ) try: disk.inject_data( injection_path, key, net, metadata, partition=target_partition, use_cow=False, # FLAGS.use_cow_images, disable_auto_fsck=disable_auto_fsck, ) except Exception as e: # This could be a windows image, or a vmdk format disk LOG.warn( _("instance %(inst_name)s: ignoring error injecting" " data into image %(img_id)s (%(e)s)") % locals(), instance=inst, )
def _create_image(self, context, inst, xml, suffix='', disk_images=None, network_info=None, block_device_info=None): if not suffix: suffix = '' # syntactic nicety def basepath(fname='', suffix=suffix): return os.path.join(FLAGS.instances_path, inst['name'], fname + suffix) # ensure directories exist and are writable libvirt_utils.ensure_tree(basepath(suffix='')) utils.execute('chmod', '0777', basepath(suffix='')) LOG.info(_('instance %s: Creating image'), inst['name']) if FLAGS.baremetal_type == 'lxc': container_dir = '%s/rootfs' % basepath(suffix='') libvirt_utils.ensure_tree(container_dir) # NOTE(vish): No need add the suffix to console.log libvirt_utils.write_to_file(basepath('console.log', ''), '', 007) if not disk_images: disk_images = {'image_id': inst['image_ref'], 'kernel_id': inst['kernel_id'], 'ramdisk_id': inst['ramdisk_id']} if disk_images['kernel_id']: fname = disk_images['kernel_id'] self._cache_image(fn=libvirt_utils.fetch_image, context=context, target=basepath('kernel'), fname=fname, cow=False, image_id=disk_images['kernel_id'], user_id=inst['user_id'], project_id=inst['project_id']) if disk_images['ramdisk_id']: fname = disk_images['ramdisk_id'] self._cache_image(fn=libvirt_utils.fetch_image, context=context, target=basepath('ramdisk'), fname=fname, cow=False, image_id=disk_images['ramdisk_id'], user_id=inst['user_id'], project_id=inst['project_id']) root_fname = hashlib.sha1(str(disk_images['image_id'])).hexdigest() size = inst['root_gb'] * 1024 * 1024 * 1024 inst_type_id = inst['instance_type_id'] inst_type = instance_types.get_instance_type(inst_type_id) if inst_type['name'] == 'm1.tiny' or suffix == '.rescue': size = None root_fname += "_sm" else: root_fname += "_%d" % inst['root_gb'] self._cache_image(fn=libvirt_utils.fetch_image, context=context, target=basepath('root'), fname=root_fname, cow=False, # FLAGS.use_cow_images, image_id=disk_images['image_id'], user_id=inst['user_id'], project_id=inst['project_id'], size=size) # For now, we assume that if we're not using a kernel, we're using a # partitioned disk image where the target partition is the first # partition target_partition = None if not inst['kernel_id']: target_partition = "1" if FLAGS.baremetal_type == 'lxc': target_partition = None if inst['key_data']: key = str(inst['key_data']) else: key = None net = None nets = [] ifc_template = open(FLAGS.injected_network_template).read() ifc_num = -1 have_injected_networks = False admin_context = nova_context.get_admin_context() for (network_ref, mapping) in network_info: ifc_num += 1 if not network_ref['injected']: continue have_injected_networks = True address = mapping['ips'][0]['ip'] netmask = mapping['ips'][0]['netmask'] address_v6 = None gateway_v6 = None netmask_v6 = None if FLAGS.use_ipv6: address_v6 = mapping['ip6s'][0]['ip'] netmask_v6 = mapping['ip6s'][0]['netmask'] gateway_v6 = mapping['gateway_v6'] net_info = {'name': 'eth%d' % ifc_num, 'address': address, 'netmask': netmask, 'gateway': mapping['gateway'], 'broadcast': mapping['broadcast'], 'dns': ' '.join(mapping['dns']), 'address_v6': address_v6, 'gateway_v6': gateway_v6, 'netmask_v6': netmask_v6} nets.append(net_info) if have_injected_networks: net = str(Template(ifc_template, searchList=[{'interfaces': nets, 'use_ipv6': FLAGS.use_ipv6}])) metadata = inst.get('metadata') if any((key, net, metadata)): inst_name = inst['name'] injection_path = basepath('root') img_id = inst.image_ref disable_auto_fsck = True for injection in ('metadata', 'key', 'net'): if locals()[injection]: LOG.info(_('instance %(inst_name)s: injecting ' '%(injection)s into image %(img_id)s') % locals()) try: disk.inject_data(injection_path, key, net, metadata, partition=target_partition, use_cow=False, # FLAGS.use_cow_images, disable_auto_fsck=disable_auto_fsck) except Exception as e: # This could be a windows image, or a vmdk format disk LOG.warn(_('instance %(inst_name)s: ignoring error injecting' ' data into image %(img_id)s (%(e)s)') % locals())
def activate_bootloader(self, var, context, node, instance): tftp_root = var['tftp_root'] image_path = var['image_path'] deploy_aki_id = FLAGS.baremetal_deploy_kernel deploy_ari_id = FLAGS.baremetal_deploy_ramdisk aki_id = str(instance['kernel_id']) ari_id = str(instance['ramdisk_id']) images = [ (deploy_aki_id, 'deploy_kernel'), (deploy_ari_id, 'deploy_ramdisk'), (aki_id, 'kernel'), (ari_id, 'ramdisk'), ] utils.ensure_tree(tftp_root) if FLAGS.baremetal_pxe_vlan_per_host: tftp_paths = [i[1] for i in images] else: tftp_paths = [ os.path.join(str(instance['uuid']), i[1]) for i in images ] utils.ensure_tree(os.path.join(tftp_root, str(instance['uuid']))) LOG.debug("tftp_paths=%s", tftp_paths) def _cache_image_b(image_id, target): LOG.debug("fetching id=%s target=%s", image_id, target) _cache_image_x(context=context, image_id=image_id, target=target, user_id=instance['user_id'], project_id=instance['project_id']) for image, path in zip(images, tftp_paths): target = os.path.join(tftp_root, path) _cache_image_b(image[0], target) pxe_config_dir = os.path.join(tftp_root, 'pxelinux.cfg') pxe_config_path = os.path.join(pxe_config_dir, self._pxe_cfg_name(node)) root_mb = instance['root_gb'] * 1024 inst_type_id = instance['instance_type_id'] inst_type = instance_types.get_instance_type(inst_type_id) swap_mb = inst_type['swap'] if swap_mb < 1024: swap_mb = 1024 pxe_ip = None if FLAGS.baremetal_pxe_vlan_per_host: pxe_ip_id = bmdb.bm_pxe_ip_associate(context, node['id']) pxe_ip = bmdb.bm_pxe_ip_get(context, pxe_ip_id) deployment_key = _random_alnum(32) deployment_id = bmdb.bm_deployment_create(context, deployment_key, image_path, pxe_config_path, root_mb, swap_mb) deployment_iscsi_iqn = "iqn-%s" % str(instance['uuid']) iscsi_portal = None if FLAGS.baremetal_pxe_append_iscsi_portal: if pxe_ip: iscsi_portal = pxe_ip['server_address'] pxeconf = _build_pxe_config(deployment_id, deployment_key, deployment_iscsi_iqn, deployment_aki_path=tftp_paths[0], deployment_ari_path=tftp_paths[1], aki_path=tftp_paths[2], ari_path=tftp_paths[3], iscsi_portal=iscsi_portal) utils.ensure_tree(pxe_config_dir) libvirt_utils.write_to_file(pxe_config_path, pxeconf) if FLAGS.baremetal_pxe_vlan_per_host: vlan_id = node['prov_vlan_id'] server_address = pxe_ip['server_address'] client_address = pxe_ip['address'] _start_per_host_pxe_server(tftp_root, vlan_id, server_address, client_address)
def activate_bootloader(self, var, context, node, instance): tftp_root = var['tftp_root'] image_path = var['image_path'] deploy_aki_id = FLAGS.baremetal_deploy_kernel deploy_ari_id = FLAGS.baremetal_deploy_ramdisk aki_id = str(instance['kernel_id']) ari_id = str(instance['ramdisk_id']) images = [(deploy_aki_id, 'deploy_kernel'), (deploy_ari_id, 'deploy_ramdisk'), (aki_id, 'kernel'), (ari_id, 'ramdisk'), ] utils.ensure_tree(tftp_root) if FLAGS.baremetal_pxe_vlan_per_host: tftp_paths = [i[1] for i in images] else: tftp_paths = [os.path.join(str(instance['uuid']), i[1]) for i in images] utils.ensure_tree( os.path.join(tftp_root, str(instance['uuid']))) LOG.debug("tftp_paths=%s", tftp_paths) def _cache_image_b(image_id, target): LOG.debug("fetching id=%s target=%s", image_id, target) _cache_image_x(context=context, image_id=image_id, target=target, user_id=instance['user_id'], project_id=instance['project_id']) for image, path in zip(images, tftp_paths): target = os.path.join(tftp_root, path) _cache_image_b(image[0], target) pxe_config_dir = os.path.join(tftp_root, 'pxelinux.cfg') pxe_config_path = os.path.join(pxe_config_dir, self._pxe_cfg_name(node)) root_mb = instance['root_gb'] * 1024 inst_type_id = instance['instance_type_id'] inst_type = instance_types.get_instance_type(inst_type_id) swap_mb = inst_type['swap'] if swap_mb < 1024: swap_mb = 1024 pxe_ip = None if FLAGS.baremetal_pxe_vlan_per_host: pxe_ip_id = bmdb.bm_pxe_ip_associate(context, node['id']) pxe_ip = bmdb.bm_pxe_ip_get(context, pxe_ip_id) deployment_key = _random_alnum(32) deployment_id = bmdb.bm_deployment_create(context, deployment_key, image_path, pxe_config_path, root_mb, swap_mb) deployment_iscsi_iqn = "iqn-%s" % str(instance['uuid']) iscsi_portal = None if FLAGS.baremetal_pxe_append_iscsi_portal: if pxe_ip: iscsi_portal = pxe_ip['server_address'] pxeconf = _build_pxe_config(deployment_id, deployment_key, deployment_iscsi_iqn, deployment_aki_path=tftp_paths[0], deployment_ari_path=tftp_paths[1], aki_path=tftp_paths[2], ari_path=tftp_paths[3], iscsi_portal=iscsi_portal) utils.ensure_tree(pxe_config_dir) libvirt_utils.write_to_file(pxe_config_path, pxeconf) if FLAGS.baremetal_pxe_vlan_per_host: vlan_id = node['prov_vlan_id'] server_address = pxe_ip['server_address'] client_address = pxe_ip['address'] _start_per_host_pxe_server(tftp_root, vlan_id, server_address, client_address)