~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/virt/libvirt/imagebackend.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
class Image(object):
51
51
    __metaclass__ = abc.ABCMeta
52
52
 
53
 
    @abc.abstractmethod
54
 
    def __init__(self, instance, name, suffix):
 
53
    def __init__(self, source_type, driver_format, is_block_dev=False):
55
54
        """Image initialization.
56
55
 
57
 
        :instance: Instance name.
58
 
        :name: Image name.
59
 
        :suffix: Suffix for image name.
 
56
        :source_type: block or file
 
57
        :driver_format: raw or qcow2
 
58
        :is_block_dev:
60
59
        """
61
 
        pass
 
60
        self.source_type = source_type
 
61
        self.driver_format = driver_format
 
62
        self.is_block_dev = is_block_dev
62
63
 
63
64
    @abc.abstractmethod
64
65
    def create_image(self, prepare_template, base, size, *args, **kwargs):
73
74
        """
74
75
        pass
75
76
 
76
 
    @abc.abstractmethod
77
 
    def libvirt_info(self, device_type):
 
77
    def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode):
78
78
        """Get `LibvirtConfigGuestDisk` filled for this image.
79
79
 
 
80
        :disk_dev: Disk bus device name
 
81
        :disk_bus: Disk bus type
80
82
        :device_type: Device type for this image.
 
83
        :cache_mode: Caching mode for this image
81
84
        """
82
 
        pass
 
85
        info = config.LibvirtConfigGuestDisk()
 
86
        info.source_type = self.source_type
 
87
        info.source_device = device_type
 
88
        info.target_bus = disk_bus
 
89
        info.target_dev = disk_dev
 
90
        info.driver_cache = cache_mode
 
91
        info.driver_format = self.driver_format
 
92
        driver_name = libvirt_utils.pick_disk_driver_name(self.is_block_dev)
 
93
        info.driver_name = driver_name
 
94
        info.source_path = self.path
 
95
        return info
83
96
 
84
97
    def cache(self, fn, fname, size=None, *args, **kwargs):
85
98
        """Creates image from template.
109
122
 
110
123
 
111
124
class Raw(Image):
112
 
    def __init__(self, instance, name, suffix):
113
 
        if not suffix:
114
 
            suffix = ''
 
125
    def __init__(self, instance, name):
 
126
        super(Raw, self).__init__("file", "raw", is_block_dev=False)
 
127
 
115
128
        self.path = os.path.join(FLAGS.instances_path,
116
 
                                 instance, name + suffix)
117
 
 
118
 
    def libvirt_info(self, device_type):
119
 
        info = config.LibvirtConfigGuestDisk()
120
 
        info.source_type = 'file'
121
 
        info.source_device = device_type
122
 
        info.driver_format = 'raw'
123
 
        info.source_path = self.path
124
 
        return info
 
129
                                 instance, name)
125
130
 
126
131
    def create_image(self, prepare_template, base, size, *args, **kwargs):
127
132
        @utils.synchronized(base)
140
145
                copy_raw_image(base, self.path, size)
141
146
 
142
147
 
143
 
class Qcow2(Raw):
144
 
    def libvirt_info(self, device_type):
145
 
        info = config.LibvirtConfigGuestDisk()
146
 
        info.source_type = 'file'
147
 
        info.source_device = device_type
148
 
        info.driver_format = 'qcow2'
149
 
        info.source_path = self.path
150
 
        return info
 
148
class Qcow2(Image):
 
149
    def __init__(self, instance, name):
 
150
        super(Qcow2, self).__init__("file", "qcow2", is_block_dev=False)
 
151
 
 
152
        self.path = os.path.join(FLAGS.instances_path,
 
153
                                 instance, name)
151
154
 
152
155
    def create_image(self, prepare_template, base, size, *args, **kwargs):
153
156
        @utils.synchronized(base)
172
175
    def escape(fname):
173
176
        return fname.replace('_', '__')
174
177
 
175
 
    def libvirt_info(self, device_type):
176
 
        info = config.LibvirtConfigGuestDisk()
177
 
        info.source_type = 'block'
178
 
        info.source_device = device_type
179
 
        info.driver_format = 'raw'
180
 
        info.source_path = self.path
181
 
        return info
 
178
    def __init__(self, instance, name):
 
179
        super(Lvm, self).__init__("block", "raw", is_block_dev=True)
182
180
 
183
 
    def __init__(self, instance, name, suffix):
184
 
        if not suffix:
185
 
            suffix = ''
186
181
        if not FLAGS.libvirt_images_volume_group:
187
182
            raise RuntimeError(_('You should specify'
188
183
                               ' libvirt_images_volume_group'
189
184
                               ' flag to use LVM images.'))
190
185
        self.vg = FLAGS.libvirt_images_volume_group
191
186
        self.lv = '%s_%s' % (self.escape(instance),
192
 
                             self.escape(name + suffix))
 
187
                             self.escape(name))
193
188
        self.path = os.path.join('/dev', self.vg, self.lv)
194
189
        self.sparse = FLAGS.libvirt_sparse_logical_volumes
195
190
 
196
191
    def create_image(self, prepare_template, base, size, *args, **kwargs):
197
192
        @utils.synchronized(base)
198
193
        def create_lvm_image(base, size):
199
 
            base_size = disk.get_image_virtual_size(base)
 
194
            base_size = disk.get_disk_size(base)
200
195
            resize = size > base_size
201
196
            size = size if resize else base_size
202
197
            libvirt_utils.create_lvm_image(self.vg, self.lv,
238
233
        }
239
234
 
240
235
    def image(self, instance, name,
241
 
              suffix=None, image_type=None):
 
236
              image_type=None):
242
237
        """Constructs image for selected backend
243
238
 
244
239
        :instance: Instance name.
245
240
        :name: Image name.
246
 
        :suffix: Suffix for image name (optional).
247
241
        :image_type: Image type.
248
242
        Optional, is FLAGS.libvirt_images_type by default.
249
243
        """
252
246
        image = self.BACKEND.get(image_type)
253
247
        if not image:
254
248
            raise RuntimeError(_('Unknown image_type=%s') % image_type)
255
 
        return image(instance, name, suffix)
 
249
        return image(instance, name)