def _start_per_host_pxe_server(tftp_root, vlan_id, server_address, client_address): parent_interface = FLAGS.baremetal_pxe_parent_interface pxe_interface = vlan.ensure_vlan(vlan_id, parent_interface) from nova.network import linux_net chain = 'bm-%s' % pxe_interface iptables = linux_net.iptables_manager f = iptables.ipv4['filter'] f.add_chain(chain) f.add_rule('INPUT', '-i %s -j $%s' % (pxe_interface, chain)) f.add_rule(chain, '--proto udp --sport=68 --dport=67 -j ACCEPT') f.add_rule(chain, '-s %s -j ACCEPT' % client_address) f.add_rule(chain, '-j DROP') iptables.apply() utils.execute('ip', 'address', 'add', server_address + '/24', 'dev', pxe_interface, run_as_root=True) utils.execute('ip', 'route', 'add', client_address, 'scope', 'host', 'dev', pxe_interface, run_as_root=True) shutil.copyfile(FLAGS.baremetal_pxelinux_path, os.path.join(tftp_root, 'pxelinux.0')) utils.ensure_tree(os.path.join(tftp_root, 'pxelinux.cfg')) _start_dnsmasq(interface=pxe_interface, tftp_root=tftp_root, client_address=client_address, pid_path=_dnsmasq_pid_path(pxe_interface), lease_path=_dnsmasq_lease_path(pxe_interface))
def _cache_image(fn, target, fname, cow=False, *args, **kwargs): """Wrapper for a method that creates an image that caches the image. This wrapper will save the image into a common store and create a copy for use by the hypervisor. The underlying method should specify a kwarg of target representing where the image will be saved. fname is used as the filename of the base image. The filename needs to be unique to a given image. If cow is True, it will make a CoW image instead of a copy. """ if not os.path.exists(target): base_dir = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(base_dir): utils.ensure_tree(base_dir) base = os.path.join(base_dir, fname) @utils.synchronized(fname) def call_if_not_exists(base, fn, *args, **kwargs): if not os.path.exists(base): fn(target=base, *args, **kwargs) call_if_not_exists(base, fn, *args, **kwargs) if cow: libvirt_utils.create_cow_image(base, target) else: libvirt_utils.copy_image(base, target)
def create_image(self, var, context, image_meta, node, instance, injected_files=None, admin_password=None): image_root = var['image_root'] network_info = var['network_info'] ami_id = str(image_meta['id']) utils.ensure_tree(image_root) image_path = os.path.join(image_root, 'disk') LOG.debug("fetching image id=%s target=%s", ami_id, image_path) _cache_image_x(context=context, target=image_path, image_id=ami_id, user_id=instance['user_id'], project_id=instance['project_id']) LOG.debug("injecting to image id=%s target=%s", ami_id, image_path) self._inject_to_image(context, image_path, node, instance, network_info, injected_files=injected_files, admin_password=admin_password) var['image_path'] = image_path LOG.debug("fetching images all done")
def cache(self, fn, fname, size=None, *args, **kwargs): """Creates image from template. Ensures that template and image not already exists. Ensures that base directory exists. Synchronizes on template fetching. :fn: function, that creates template. Should accept `target` argument. :fname: Template name :size: Size of created image in bytes (optional) """ @utils.synchronized(fname, external=True, lock_path=self.lock_path) def call_if_not_exists(target, *args, **kwargs): if not os.path.exists(target): fn(target=target, *args, **kwargs) if not os.path.exists(self.path): base_dir = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(base_dir): utils.ensure_tree(base_dir) base = os.path.join(base_dir, fname) self.create_image(call_if_not_exists, base, size, *args, **kwargs)
def create_iscsi_target(self, name, tid, lun, path, **kwargs): try: utils.ensure_tree(FLAGS.volumes_dir) # grab the volume id vol_id = name.split(':')[1] volume_conf = """ <target %s> backing-store %s </target> """ % (name, path) LOG.info(_('Creating volume: %s') % vol_id) volume_path = os.path.join(FLAGS.volumes_dir, vol_id) if not os.path.isfile(volume_path): f = open(volume_path, 'w+') f.write(volume_conf) f.close() self._execute('tgt-admin', '--execute', '--conf', volume_path, '--update', vol_id, run_as_root=True) except Exception as ex: LOG.exception(ex) raise exception.NovaException(_('Failed to create volume: %s') % vol_id)
def create_iscsi_target(self, name, tid, lun, path, **kwargs): try: utils.ensure_tree(FLAGS.volumes_dir) # grab the volume id vol_id = name.split(':')[1] volume_conf = """ <target %s> backing-store %s </target> """ % (name, path) LOG.info(_('Creating volume: %s') % vol_id) volume_path = os.path.join(FLAGS.volumes_dir, vol_id) if not os.path.isfile(volume_path): f = open(volume_path, 'w+') f.write(volume_conf) f.close() self._execute('tgt-admin', '--execute', '--conf', volume_path, '--update', vol_id, run_as_root=True) except Exception as ex: LOG.exception(ex) raise exception.NovaException( _('Failed to create volume: %s') % vol_id)
def create_iscsi_target(self, name, tid, lun, path, **kwargs): # Note(jdg) tid and lun aren't used by TgtAdm but remain for # compatibility utils.ensure_tree(FLAGS.volumes_dir) vol_id = name.split(':')[1] volume_conf = """ <target %s> backing-store %s </target> """ % (name, path) LOG.info(_('Creating volume: %s') % vol_id) volumes_dir = FLAGS.volumes_dir volume_path = os.path.join(volumes_dir, vol_id) f = open(volume_path, 'w+') f.write(volume_conf) f.close() try: (out, err) = self._execute('tgt-admin', '--update', name, run_as_root=True) except exception.ProcessExecutionError, e: LOG.error( _("Failed to create iscsi target for volume " "id:%(vol_id)s.") % locals()) #Don't forget to remove the persistent file we created os.unlink(volume_path) raise exception.ISCSITargetCreateFailed(volume_id=vol_id)
def _cache_image(fetch_func, target, fname, cow=False, *args, **kwargs): """Wrapper for a method that creates an image that caches the image. This wrapper will save the image into a common store and create a copy for use by the hypervisor. The underlying method should specify a kwarg of target representing where the image will be saved. fname is used as the filename of the base image. The filename needs to be unique to a given image. If cow is True, it will make a CoW image instead of a copy. """ if not os.path.exists(target): base_dir = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(base_dir): utils.ensure_tree(base_dir) base = os.path.join(base_dir, fname) @utils.synchronized(fname) def call_if_not_exists(base, fetch_func, *args, **kwargs): if not os.path.exists(base): fetch_func(target=base, *args, **kwargs) call_if_not_exists(base, fetch_func, *args, **kwargs) if cow: libvirt_utils.create_cow_image(base, target) else: libvirt_utils.copy_image(base, target)
def create_iscsi_target(self, name, tid, lun, path, **kwargs): # Note(jdg) tid and lun aren't used by TgtAdm but remain for # compatibility utils.ensure_tree(FLAGS.volumes_dir) vol_id = name.split(':')[1] volume_conf = """ <target %s> backing-store %s </target> """ % (name, path) LOG.info(_('Creating volume: %s') % vol_id) volume_path = os.path.join(FLAGS.volumes_dir, vol_id) f = open(volume_path, 'w+') f.write(volume_conf) f.close() try: (out, err) = self._execute('tgt-admin', '--conf', volume_path, '--update', name, run_as_root=True) except exception.ProcessExecutionError, e: LOG.error(_("Failed to create iscsi target for volume " "id:%(vol_id)s.") % locals()) #Don't forget to remove the persistent file we created os.unlink(volume_path) raise exception.ISCSITargetCreateFailed(volume_id=vol_id)
def _setup_image(self, context, instance): base_dir = os.path.join(FLAGS.instances_path, FLAGS.base_dir_name) if not os.path.exists(base_dir): ensure_tree(base_dir) path = os.path.abspath(os.path.join(base_dir, instance["image_ref"])) if not os.path.exists(path): images.fetch_to_raw(context, instance["image_ref"], path, instance["user_id"], instance["project_id"]) return path
def put(self, bucket_name): path = os.path.abspath(os.path.join( self.application.directory, bucket_name)) if (not path.startswith(self.application.directory) or os.path.exists(path)): self.set_status(403) return utils.ensure_tree(path) self.finish()
def _setup_image(self, context, instance): base_dir = os.path.join(FLAGS.instances_path, FLAGS.base_dir_name) if not os.path.exists(base_dir): ensure_tree(base_dir) path = os.path.abspath(os.path.join(base_dir, instance["image_ref"])) if not os.path.exists(path): images.fetch_to_raw( context, instance["image_ref"], path, instance["user_id"], instance["project_id"]) return path
def ensure_ca_filesystem(): """Ensure the CA filesystem exists.""" ca_dir = ca_folder() if not os.path.exists(ca_path()): genrootca_sh_path = os.path.join(os.path.dirname(__file__), 'CA', 'genrootca.sh') start = os.getcwd() utils.ensure_tree(ca_dir) os.chdir(ca_dir) utils.execute("sh", genrootca_sh_path) os.chdir(start)
def __init__(self, root_directory, bucket_depth=0, mapper=None): if mapper is None: mapper = routes.Mapper() mapper.connect('/', controller=lambda *a, **kw: RootHandler(self)(*a, **kw)) mapper.connect('/{bucket}/{object_name}', controller=lambda *a, **kw: ObjectHandler(self)(*a, **kw)) mapper.connect('/{bucket_name}/', controller=lambda *a, **kw: BucketHandler(self)(*a, **kw)) self.directory = os.path.abspath(root_directory) utils.ensure_tree(self.directory) self.bucket_depth = bucket_depth super(S3Application, self).__init__(mapper)
def create_image(self, var, context, image_meta, node, instance, injected_files=None, admin_password=None): image_root = var['image_root'] ami_id = str(image_meta['id']) utils.ensure_tree(image_root) image_path = os.path.join(image_root, 'disk') LOG.debug("fetching image id=%s target=%s", ami_id, image_path) _cache_image_x(context=context, target=image_path, image_id=ami_id, user_id=instance['user_id'], project_id=instance['project_id']) var['image_path'] = image_path LOG.debug("PR_VIRT bitpkg is fetched successfully! fetching images all done.")
def setup_key_pair(self, context): key_name = '%s%s' % (context.project_id, FLAGS.vpn_key_suffix) try: keypair_api = compute.api.KeypairAPI() result = keypair_api.create_key_pair(context, context.user_id, key_name) private_key = result['private_key'] key_dir = os.path.join(FLAGS.keys_path, context.user_id) utils.ensure_tree(key_dir) key_path = os.path.join(key_dir, '%s.pem' % key_name) with open(key_path, 'w') as f: f.write(private_key) except (exception.Duplicate, os.error, IOError): pass return key_name
def write_stored_info(target, field=None, value=None): """Write information about an image.""" if not field: return info_file = get_info_filename(target) utils.ensure_tree(os.path.dirname(info_file)) d = read_stored_info(info_file) d[field] = value serialized = jsonutils.dumps(d) LOG.info(_('Writing image info file: %s'), info_file) LOG.info(_('Wrote: %s'), serialized) f = open(info_file, 'w') f.write(serialized) f.close()
def put(self, bucket, object_name): object_name = urllib.unquote(object_name) bucket_dir = os.path.abspath(os.path.join( self.application.directory, bucket)) if (not bucket_dir.startswith(self.application.directory) or not os.path.isdir(bucket_dir)): self.set_status(404) return path = self._object_path(bucket, object_name) if not path.startswith(bucket_dir) or os.path.isdir(path): self.set_status(403) return directory = os.path.dirname(path) utils.ensure_tree(directory) object_file = open(path, "w") object_file.write(self.request.body) object_file.close() self.set_header('ETag', '"%s"' % hashlib.md5(self.request.body).hexdigest()) self.finish()
def _sign_csr(csr_text, ca_folder): with utils.tempdir() as tmpdir: inbound = os.path.join(tmpdir, 'inbound.csr') outbound = os.path.join(tmpdir, 'outbound.csr') with open(inbound, 'w') as csrfile: csrfile.write(csr_text) LOG.debug(_('Flags path: %s'), ca_folder) start = os.getcwd() # Change working dir to CA utils.ensure_tree(ca_folder) os.chdir(ca_folder) utils.execute('openssl', 'ca', '-batch', '-out', outbound, '-config', './openssl.cnf', '-infiles', inbound) out, _err = utils.execute('openssl', 'x509', '-in', outbound, '-serial', '-noout') serial = string.strip(out.rpartition('=')[2]) os.chdir(start) with open(outbound, 'r') as crtfile: return (serial, crtfile.read())
def cache(self, fetch_func, filename, size=None, *args, **kwargs): """Creates image from template. Ensures that template and image not already exists. Ensures that base directory exists. Synchronizes on template fetching. :fetch_func: Function that creates the base image Should accept `target` argument. :filename: Name of the file in the image directory :size: Size of created image in bytes (optional) """ @utils.synchronized(filename, external=True, lock_path=self.lock_path) def call_if_not_exists(target, *args, **kwargs): if not os.path.exists(target): fetch_func(target=target, *args, **kwargs) if not os.path.exists(self.path): base_dir = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(base_dir): utils.ensure_tree(base_dir) base = os.path.join(base_dir, filename) self.create_image(call_if_not_exists, base, size, *args, **kwargs)
def test_update_dhcp_for_nw01(self): self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(self.driver, 'write_to_file') self.mox.StubOutWithMock(utils, 'ensure_tree') self.mox.StubOutWithMock(os, 'chmod') self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) utils.ensure_tree(mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) os.chmod(mox.IgnoreArg(), mox.IgnoreArg()) self.mox.ReplayAll() self.driver.update_dhcp(self.context, "eth0", networks[0])
def _ra_file(dev, kind): """Return path to a pid or conf file for a bridge/device.""" utils.ensure_tree(FLAGS.networks_path) return os.path.abspath("%s/nova-ra-%s.%s" % (FLAGS.networks_path, dev, kind))
def setup(self): """Ensure the keychains and folders exist.""" # NOTE(vish): One of the drawbacks of doing this in the api is # the keys will only be on the api node that launched # the cloudpipe. utils.ensure_tree(FLAGS.keys_path)
def test_ensure_tree(self): with utils.tempdir() as tmpdir: testdir = '%s/foo/bar/baz' % (tmpdir,) utils.ensure_tree(testdir) self.assertTrue(os.path.isdir(testdir))
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 _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 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='') 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'] 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 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 _add_file(self, path, data): filepath = os.path.join(self.tempdir, path) dirname = os.path.dirname(filepath) utils.ensure_tree(dirname) with open(filepath, 'w') as f: f.write(data)
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 _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 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='') 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(fetch_func=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(fetch_func=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(fetch_func=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'] 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 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)