50
50
class Image(object):
51
51
__metaclass__ = abc.ABCMeta
54
def __init__(self, instance, name, suffix):
53
def __init__(self, source_type, driver_format, is_block_dev=False):
55
54
"""Image initialization.
57
:instance: Instance name.
59
:suffix: Suffix for image name.
56
:source_type: block or file
57
:driver_format: raw or qcow2
60
self.source_type = source_type
61
self.driver_format = driver_format
62
self.is_block_dev = is_block_dev
63
64
@abc.abstractmethod
64
65
def create_image(self, prepare_template, base, size, *args, **kwargs):
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.
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
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
84
97
def cache(self, fn, fname, size=None, *args, **kwargs):
85
98
"""Creates image from template.
111
124
class Raw(Image):
112
def __init__(self, instance, name, suffix):
125
def __init__(self, instance, name):
126
super(Raw, self).__init__("file", "raw", is_block_dev=False)
115
128
self.path = os.path.join(FLAGS.instances_path,
116
instance, name + suffix)
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
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)
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
149
def __init__(self, instance, name):
150
super(Qcow2, self).__init__("file", "qcow2", is_block_dev=False)
152
self.path = os.path.join(FLAGS.instances_path,
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('_', '__')
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
178
def __init__(self, instance, name):
179
super(Lvm, self).__init__("block", "raw", is_block_dev=True)
183
def __init__(self, instance, name, 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))
193
188
self.path = os.path.join('/dev', self.vg, self.lv)
194
189
self.sparse = FLAGS.libvirt_sparse_logical_volumes
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,
240
235
def image(self, instance, name,
241
suffix=None, image_type=None):
242
237
"""Constructs image for selected backend
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.
252
246
image = self.BACKEND.get(image_type)
254
248
raise RuntimeError(_('Unknown image_type=%s') % image_type)
255
return image(instance, name, suffix)
249
return image(instance, name)