[Kimchi-devel] [PATCH 1/2] Getting default disk format from template.conf

dhbarboza82 at gmail.com dhbarboza82 at gmail.com
Wed Jul 15 21:35:40 UTC 2015


From: Daniel Henrique Barboza <dhbarboza82 at gmail.com>

If the user change disk format in template.conf, templates are going to
be created correctly and vm xml as well. But, when the disk is created,
the format is always qcow2.

This patch considers the settings in the template.conf to define
the default volume format when creating the disks. User defined settings
will override any default (unless the storage type is logical,
iscsi or scsi. In those cases the format will always be 'raw').

If the user does not provide any format and template.conf does not
declare any default as well, the default value to be used as volume
type will be 'qcow2' (considering the storage type restriction
mentioned above).

Signed-off-by: Daniel Henrique Barboza <dhbarboza82 at gmail.com>
---
 src/kimchi/vmtemplate.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/src/kimchi/vmtemplate.py b/src/kimchi/vmtemplate.py
index 3e42971..1ad9131 100644
--- a/src/kimchi/vmtemplate.py
+++ b/src/kimchi/vmtemplate.py
@@ -24,11 +24,13 @@ import urlparse
 import uuid
 import platform
 
+from configobj import ConfigObj
 from lxml import etree
 from lxml.builder import E
 
 from kimchi import imageinfo
 from kimchi import osinfo
+from kimchi.config import paths
 from kimchi.exception import InvalidParameter, IsoFormatError, MissingParameter
 from kimchi.exception import ImageFormatError, OperationFailed
 from kimchi.isoinfo import IsoImage
@@ -158,6 +160,13 @@ class VMTemplate(object):
         dev, xml = get_disk_xml(params)
         return xml
 
+    def _get_default_disk0_format(self):
+        config_file = os.path.join(paths.conf_dir, 'template.conf')
+        config = ConfigObj(config_file)
+
+        default_vol_format = config['storage']['disk.0'].get('format', 'qcow2')
+        return default_vol_format
+
     def _get_disks_xml(self, vm_uuid):
         # Current implementation just allows to create disk in one single
         # storage pool, so we cannot mix the types (scsi volumes vs img file)
@@ -165,7 +174,9 @@ class VMTemplate(object):
         storage_path = self._get_storage_path()
 
         base_disk_params = {'type': 'disk', 'disk': 'file',
-                            'bus': self.info['disk_bus'], 'format': 'qcow2'}
+                            'bus': self.info['disk_bus'],
+                            'format': self._get_default_disk0_format()}
+
         logical_disk_params = {'format': 'raw'}
         iscsi_disk_params = {'disk': 'block', 'format': 'raw'}
 
@@ -193,8 +204,8 @@ class VMTemplate(object):
         return unicode(disks_xml, 'utf-8')
 
     def to_volume_list(self, vm_uuid):
+        default_vol_format = self._get_default_disk0_format()
         storage_path = self._get_storage_path()
-        fmt = 'raw' if self._get_storage_type() in ['logical'] else 'qcow2'
         ret = []
         for i, d in enumerate(self.info['disks']):
             index = d.get('index', i)
@@ -202,11 +213,11 @@ class VMTemplate(object):
 
             info = {'name': volume,
                     'capacity': d['size'],
-                    'format': fmt,
+                    'format': d.get('format', default_vol_format),
                     'path': '%s/%s' % (storage_path, volume)}
 
             if 'logical' == self._get_storage_type() or \
-               fmt not in ['qcow2', 'raw']:
+               info['format'] not in ['qcow2', 'raw']:
                 info['allocation'] = info['capacity']
             else:
                 info['allocation'] = 0
-- 
2.4.3




More information about the Kimchi-devel mailing list