Exemple #1
0
    def test_copy_image_rsync_ipv6(self, mock_execute):
        libvirt_utils.copy_image('src', 'dest', host='2600::')

        mock_execute.assert_has_calls([
            self._rsync_call('--dry-run', 'src', '[2600::]:dest'),
            self._rsync_call('src', '[2600::]:dest'),
        ])
        self.assertEqual(2, mock_execute.call_count)
Exemple #2
0
    def test_copy_image_rsync_ipv6(self, mock_execute):
        libvirt_utils.copy_image('src', 'dest', host='2600::')

        mock_execute.assert_has_calls([
            self._rsync_call('--dry-run', 'src', '[2600::]:dest'),
            self._rsync_call('src', '[2600::]:dest'),
        ])
        self.assertEqual(2, mock_execute.call_count)
Exemple #3
0
 def create_ploop_image(base, target, size):
     image_path = os.path.join(target, "root.hds")
     libvirt_utils.copy_image(base, image_path)
     utils.execute('ploop', 'restore-descriptor', '-f', self.pcs_format,
                   target, image_path)
     if size:
         dd_path = os.path.join(self.path, "DiskDescriptor.xml")
         utils.execute('ploop', 'grow', '-s', '%dK' % (size >> 10),
                       dd_path, run_as_root=True)
Exemple #4
0
 def create_ploop_image(base, target, size):
     image_path = os.path.join(target, "root.hds")
     libvirt_utils.copy_image(base, image_path)
     utils.execute('ploop', 'restore-descriptor', '-f', self.pcs_format,
                   target, image_path)
     if size:
         dd_path = os.path.join(self.path, "DiskDescriptor.xml")
         utils.execute('ploop',
                       'grow',
                       '-s',
                       '%dK' % (size >> 10),
                       dd_path,
                       run_as_root=True)
Exemple #5
0
    def test_copy_image_scp(self, mock_execute):
        mock_execute.side_effect = [
            processutils.ProcessExecutionError,
            mock.DEFAULT,
        ]

        libvirt_utils.copy_image('src', 'dest', host='host')

        mock_execute.assert_has_calls([
            self._rsync_call('--dry-run', 'src', 'host:dest'),
            mock.call('scp', 'src', 'host:dest'),
        ])
        self.assertEqual(2, mock_execute.call_count)
Exemple #6
0
    def test_copy_image_scp(self, mock_execute):
        mock_execute.side_effect = [
            processutils.ProcessExecutionError,
            mock.DEFAULT,
        ]

        libvirt_utils.copy_image('src', 'dest', host='host')

        mock_execute.assert_has_calls([
            self._rsync_call('--dry-run', 'src', 'host:dest'),
            mock.call('scp', 'src', 'host:dest'),
        ])
        self.assertEqual(2, mock_execute.call_count)
Exemple #7
0
    def test_copy_image(self):
        dst_fd, dst_path = tempfile.mkstemp()
        try:
            os.close(dst_fd)

            src_fd, src_path = tempfile.mkstemp()
            try:
                with os.fdopen(src_fd, 'w') as fp:
                    fp.write('canary')

                libvirt_utils.copy_image(src_path, dst_path)
                with open(dst_path, 'r') as fp:
                    self.assertEqual(fp.read(), 'canary')
            finally:
                os.unlink(src_path)
        finally:
            os.unlink(dst_path)
Exemple #8
0
    def test_copy_image(self):
        dst_fd, dst_path = tempfile.mkstemp()
        try:
            os.close(dst_fd)

            src_fd, src_path = tempfile.mkstemp()
            try:
                with os.fdopen(src_fd, 'w') as fp:
                    fp.write('canary')

                libvirt_utils.copy_image(src_path, dst_path)
                with open(dst_path, 'r') as fp:
                    self.assertEqual(fp.read(), 'canary')
            finally:
                os.unlink(src_path)
        finally:
            os.unlink(dst_path)
Exemple #9
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        filename = self._get_lock_name(base)

        @utils.synchronized(filename, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size, use_cow=True)

        # Download the unmodified base image unless we already have a copy.
        if not os.path.exists(base):
            prepare_template(target=base, max_size=size, *args, **kwargs)
        self.verify_base_size(base, size)

        legacy_backing_size = None
        legacy_base = base

        # Determine whether an existing qcow2 disk uses a legacy backing by
        # actually looking at the image itself and parsing the output of the
        # backing file it expects to be using.
        if os.path.exists(self.path):
            backing_path = libvirt_utils.get_disk_backing_file(self.path)
            if backing_path is not None:
                backing_file = os.path.basename(backing_path)
                backing_parts = backing_file.rpartition('_')
                if backing_file != backing_parts[-1] and \
                        backing_parts[-1].isdigit():
                    legacy_backing_size = int(backing_parts[-1])
                    legacy_base += '_%d' % legacy_backing_size
                    legacy_backing_size *= units.Gi

        # Create the legacy backing file if necessary.
        if legacy_backing_size:
            if not os.path.exists(legacy_base):
                with fileutils.remove_path_on_error(legacy_base):
                    libvirt_utils.copy_image(base, legacy_base)
                    disk.extend(legacy_base, legacy_backing_size, use_cow=True)

        if not os.path.exists(self.path):
            with fileutils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #10
0
    def create_image(self, prepare_template, base, size, *args, **kwargs):
        filename = self._get_lock_name(base)

        @utils.synchronized(filename, external=True, lock_path=self.lock_path)
        def copy_qcow2_image(base, target, size):
            # TODO(pbrady): Consider copying the cow image here
            # with preallocation=metadata set for performance reasons.
            # This would be keyed on a 'preallocate_images' setting.
            libvirt_utils.create_cow_image(base, target)
            if size:
                disk.extend(target, size, use_cow=True)

        # Download the unmodified base image unless we already have a copy.
        if not os.path.exists(base):
            prepare_template(target=base, max_size=size, *args, **kwargs)
        self.verify_base_size(base, size)

        legacy_backing_size = None
        legacy_base = base

        # Determine whether an existing qcow2 disk uses a legacy backing by
        # actually looking at the image itself and parsing the output of the
        # backing file it expects to be using.
        if os.path.exists(self.path):
            backing_path = libvirt_utils.get_disk_backing_file(self.path)
            if backing_path is not None:
                backing_file = os.path.basename(backing_path)
                backing_parts = backing_file.rpartition('_')
                if backing_file != backing_parts[-1] and \
                        backing_parts[-1].isdigit():
                    legacy_backing_size = int(backing_parts[-1])
                    legacy_base += '_%d' % legacy_backing_size
                    legacy_backing_size *= units.Gi

        # Create the legacy backing file if necessary.
        if legacy_backing_size:
            if not os.path.exists(legacy_base):
                with fileutils.remove_path_on_error(legacy_base):
                    libvirt_utils.copy_image(base, legacy_base)
                    disk.extend(legacy_base, legacy_backing_size, use_cow=True)

        if not os.path.exists(self.path):
            with fileutils.remove_path_on_error(self.path):
                copy_qcow2_image(base, self.path, size)
Exemple #11
0
    def download(self, context, url_parts, dst_file, metadata, **kwargs):
        self.filesystems = self._get_options()
        if not self.filesystems:
            # NOTE(jbresnah) when nothing is configured assume legacy behavior
            patron_mountpoint = '/'
            glance_mountpoint = '/'
        else:
            self._verify_config()
            fs_descriptor = self._file_system_lookup(metadata, url_parts)
            if fs_descriptor is None:
                msg = (_('No matching ID for the URL %s was found.') %
                        url_parts.geturl())
                raise exception.ImageDownloadModuleError(reason=msg,
                                                    module=str(self))
            patron_mountpoint = fs_descriptor['mountpoint']
            glance_mountpoint = metadata['mountpoint']

        source_file = self._normalize_destination(patron_mountpoint,
                                                  glance_mountpoint,
                                                  url_parts.path)
        lv_utils.copy_image(source_file, dst_file)
        LOG.info(_LI('Copied %(source_file)s using %(module_str)s'),
                 {'source_file': source_file, 'module_str': str(self)})
Exemple #12
0
 def copy_raw_image(base, target, size):
     libvirt_utils.copy_image(base, target)
     if size:
         # class Raw is misnamed, format may not be 'raw' in all cases
         use_cow = self.driver_format == 'qcow2'
         disk.extend(target, size, use_cow=use_cow)
Exemple #13
0
 def copy_raw_image(base, target, size):
     libvirt_utils.copy_image(base, target)
     if size:
         # class Raw is misnamed, format may not be 'raw' in all cases
         use_cow = self.driver_format == 'qcow2'
         disk.extend(target, size, use_cow=use_cow)
Exemple #14
0
 def test_copy_image_local_cp(self, mock_execute):
     libvirt_utils.copy_image('src', 'dest')
     mock_execute.assert_called_once_with('cp', 'src', 'dest')
Exemple #15
0
 def test_copy_image_local_cp(self, mock_execute):
     libvirt_utils.copy_image('src', 'dest')
     mock_execute.assert_called_once_with('cp', 'src', 'dest')