def mkdir_libvirt(dir): if not os.path.exists(dir): LOG.debug('does not exist %s', dir) utilities.make_directories(dir) os.chown(dir, libvirt_uid, libvirt_gid) os.chmod(dir, 0775) # ug+rwx, a+rx if not can_libvirt_write_access(dir): raise Exception("Directory %s is not writable by %s (uid=%d). " "If it already exists, make sure that it's " "writable and executable by %s." % (dir, FLAGS.libvirt_user, libvirt_uid, FLAGS.libvirt_user))
def pre_launch(self, context, new_instance_ref, network_info=None, block_device_info=None, migration=False, use_image_service=False, image_refs=[]): image_base_path = None if use_image_service: # We need to first download the descriptor and the disk files # from the image service. LOG.debug("Downloading images %s from the image service." % (image_refs)) image_base_path = os.path.join(FLAGS.instances_path, '_base') if not os.path.exists(image_base_path): LOG.debug('Base path %s does not exist. It will be created now.', image_base_path) utilities.make_directories(image_base_path) os.chown(image_base_path, self.openstack_uid, self.openstack_gid) image_service = nova.image.get_default_image_service() for image_ref in image_refs: image = image_service.show(context, image_ref) target = os.path.join(image_base_path, image['name']) if migration or not os.path.exists(target): # If the path does not exist fetch the data from the image # service. NOTE: We always fetch in the case of a # migration, as the descriptor may have changed from its # previous state. Migrating VMs are the only case where a # descriptor for an instance will not be a fixed constant. images.fetch(context, image_ref, target, new_instance_ref['user_id'], new_instance_ref['project_id']) os.chown(target, self.openstack_uid, self.openstack_gid) # (dscannell) Check to see if we need to convert the network_info # object into the legacy format. if network_info and self.libvirt_conn.legacy_nwinfo(): network_info = compute_utils.legacy_network_info(network_info) # We need to create the libvirt xml, and associated files. Pass back # the path to the libvirt.xml file. working_dir = os.path.join(FLAGS.instances_path, new_instance_ref['name']) disk_file = os.path.join(working_dir, "disk") libvirt_file = os.path.join(working_dir, "libvirt.xml") # Make sure that our working directory exists. if not(os.path.exists(working_dir)): os.makedirs(working_dir) if not(os.path.exists(disk_file)): # (dscannell) We will write out a stub 'disk' file so that we don't # end up copying this file when setting up everything for libvirt. # Essentially, this file will be removed, and replaced by vms as an # overlay on the blessed root image. f = open(disk_file, 'w') f.close() # (dscannell) We want to disable any injection. We do this by making a # copy of the instance and clearing out some entries. Since OpenStack # uses dictionary-list accessors, we can pass this dictionary through # that code. instance_dict = AttribDictionary(dict(new_instance_ref.iteritems())) # The name attribute is special and does not carry over like the rest # of the attributes. instance_dict['name'] = new_instance_ref['name'] instance_dict.os_type = new_instance_ref.os_type instance_dict['key_data'] = None instance_dict['metadata'] = [] for network_ref, mapping in network_info: network_ref['injected'] = False # (dscannell) This was taken from the core nova project as part of the # boot path for normal instances. We basically want to mimic this # functionality. xml = self.libvirt_conn.to_xml(instance_dict, network_info, False, block_device_info=block_device_info) self.libvirt_conn.firewall_driver.setup_basic_filtering(instance_dict, network_info) self.libvirt_conn.firewall_driver.prepare_instance_filter(instance_dict, network_info) self.libvirt_conn._create_image(context, instance_dict, xml, network_info=network_info, block_device_info=block_device_info) if not(migration): # (dscannell) Remove the fake disk file (if created). os.remove(disk_file) # Fix up the permissions on the files that we created so that they are owned by the # openstack user. os.chown(working_dir, self.openstack_uid, self.openstack_gid) for root, dirs, files in os.walk(working_dir, followlinks=True): for path in dirs + files: LOG.debug("chowning path=%s to openstack user %s" % (os.path.join(root, path), self.openstack_uid)) os.chown(os.path.join(root, path), self.openstack_uid, self.openstack_gid) # Return the libvirt file, this will be passed in as the name. This # parameter is overloaded in the management interface as a libvirt # special case. return (libvirt_file, image_base_path)