From 9520282a4b147043551e47f88334219ad5cb1f7b Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sun, 17 Apr 2016 23:41:40 +0200 Subject: [PATCH] Add possibility to configure HDD cache mode during instance creation --- create/forms.py | 1 + create/templates/create_instance.html | 20 ++++++++++++++++++++ create/views.py | 7 ++++++- vrtManager/create.py | 21 ++++++++++++++++----- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/create/forms.py b/create/forms.py index 5b9fab5..fccf944 100644 --- a/create/forms.py +++ b/create/forms.py @@ -40,6 +40,7 @@ class NewVMForm(forms.Form): storage = forms.CharField(max_length=20, required=False) template = forms.CharField(required=False) images = forms.CharField(required=False) + cache_mode = forms.CharField(error_messages={'required': _('Please select HDD cache mode')}) hdd_size = forms.IntegerField(required=False) meta_prealloc = forms.BooleanField(required=False) virtio = forms.BooleanField(required=False) diff --git a/create/templates/create_instance.html b/create/templates/create_instance.html index 37189b7..f9a0692 100644 --- a/create/templates/create_instance.html +++ b/create/templates/create_instance.html @@ -77,6 +77,16 @@ +
+ +
+ +
+
@@ -159,6 +169,16 @@
+
+ +
+ +
+
diff --git a/create/views.py b/create/views.py index 5b03f8c..c3e1139 100644 --- a/create/views.py +++ b/create/views.py @@ -42,6 +42,7 @@ def create_instance(request, compute_id): networks = sorted(conn.get_networks()) instances = conn.get_instances() get_images = sorted(conn.get_storages_images()) + cache_modes = sorted(conn.get_cache_modes().items()) mac_auto = util.randomMAC() except libvirtError as lib_err: error_messages.append(lib_err) @@ -123,11 +124,15 @@ def create_instance(request, compute_id): volumes[path] = conn.get_volume_type(path) except libvirtError as lib_err: error_messages.append(lib_err.message) + if data['cache_mode'] not in conn.get_cache_modes(): + error_msg = _("Invalid cache mode") + error_messages.append(error_msg) if not error_messages: uuid = util.randomUUID() try: conn.create_instance(data['name'], data['memory'], data['vcpu'], data['host_model'], - uuid, volumes, data['networks'], data['virtio'], data['mac']) + uuid, volumes, data['cache_mode'], data['networks'], data['virtio'], + data['mac']) create_instance = Instance(compute_id=compute_id, name=data['name'], uuid=uuid) create_instance.save() return HttpResponseRedirect(reverse('instance', args=[compute_id, data['name']])) diff --git a/vrtManager/create.py b/vrtManager/create.py index d9f5350..cc1f7f2 100644 --- a/vrtManager/create.py +++ b/vrtManager/create.py @@ -48,6 +48,17 @@ class wvmCreate(wvmConnect): """Get guest capabilities""" return util.get_xml_path(self.get_cap_xml(), "/capabilities/host/cpu/arch") + def get_cache_modes(self): + """Get cache available modes""" + return { + 'default': 'Default', + 'none': 'Disabled', + 'writethrough': 'Write through', + 'writeback': 'Write back', + 'directsync': 'Direct sync', # since libvirt 0.9.5 + 'unsafe': 'Unsafe', # since libvirt 0.9.7 + } + def create_volume(self, storage, name, size, format='qcow2', metadata=False): size = int(size) * 1073741824 stg = self.get_storage(storage) @@ -129,7 +140,7 @@ class wvmCreate(wvmConnect): vol = self.get_volume_by_path(path) vol.delete() - def create_instance(self, name, memory, vcpu, host_model, uuid, images, networks, virtio, mac=None): + def create_instance(self, name, memory, vcpu, host_model, uuid, images, cache_mode, networks, virtio, mac=None): """ Create VM function """ @@ -172,11 +183,11 @@ class wvmCreate(wvmConnect): if stg_type == 'rbd': ceph_user, secret_uuid, ceph_hosts = get_rbd_storage_data(stg) xml += """ - + - """ % (img_type, ceph_user, secret_uuid, image) + """ % (img_type, cache_mode, ceph_user, secret_uuid, image) if isinstance(ceph_hosts, list): for host in ceph_hosts: if host.get('port'): @@ -189,8 +200,8 @@ class wvmCreate(wvmConnect): """ else: xml += """ - - """ % (img_type, image) + + """ % (img_type, cache_mode, image) if virtio: xml += """""" % (disk_letters.pop(0),)