Esempio n. 1
0
    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):
                libvirt_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)
Esempio n. 2
0
    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'])
        libvirt_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")
Esempio n. 3
0
    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):
                libvirt_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)
Esempio n. 4
0
    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)
        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):
                libvirt_utils.ensure_tree(base_dir)
            base = os.path.join(base_dir, fname)

            self.create_image(call_if_not_exists, base, size,
                               *args, **kwargs)
Esempio n. 5
0
    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)
        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):
                libvirt_utils.ensure_tree(base_dir)
            base = os.path.join(base_dir, fname)

            self.create_image(call_if_not_exists, base, size,
                               *args, **kwargs)
Esempio n. 6
0
    def _add_file(self, path, data, inject=False):
        if inject:
            path_id = '%03d' % self.next_inject_id
            path = 'openstack/files/%s' % path_id
            self.injected[path] = path_id
            self.next_inject_id += 1

        filepath = os.path.join(self.tempdir, path)
        dirname = os.path.dirname(filepath)
        virtutils.ensure_tree(dirname)
        with open(filepath, 'w') as f:
            f.write(data)
    def _add_file(self, path, data, inject=False):
        if inject:
            path_id = '%03d' % self.next_inject_id
            path = 'openstack/files/%s' % path_id
            self.injected[path] = path_id
            self.next_inject_id += 1

        filepath = os.path.join(self.tempdir, path)
        dirname = os.path.dirname(filepath)
        virtutils.ensure_tree(dirname)
        with open(filepath, 'w') as f:
            f.write(data)
Esempio n. 8
0
 def _add_file(self, path, data):
     filepath = os.path.join(self.tempdir, path)
     dirname = os.path.dirname(filepath)
     virtutils.ensure_tree(dirname)
     with open(filepath, 'w') as f:
         f.write(data)
Esempio n. 9
0
    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,
                )
Esempio n. 10
0
    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())
Esempio n. 11
0
    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())
Esempio n. 12
0
 def _add_file(self, path, data):
     filepath = os.path.join(self.tempdir, path)
     dirname = os.path.dirname(filepath)
     virtutils.ensure_tree(dirname)
     with open(filepath, 'w') as f:
         f.write(data)