corrected errors in sizes, still need cleanup with same units
authorPaul Tonelli <paul.tonelli@logilab.fr>
Tue, 05 Mar 2013 18:57:42 +0100
changeset 25 18936b962551
parent 24 5a66ef9f5377
child 26 2c46e9a89857
corrected errors in sizes, still need cleanup with same units
_states/tmp_func.py
--- a/_states/tmp_func.py	Tue Mar 05 18:19:24 2013 +0100
+++ b/_states/tmp_func.py	Tue Mar 05 18:57:42 2013 +0100
@@ -73,7 +73,7 @@
                 ret['comment'] = output
     return ret
 
-def resize(name, new_size=0):
+def resize(name, size=0):
     '''
     Resize partition and filesystem on a file with an ext* fs whenever
     possible (size in Mb) and check the integrity (for ext* fs)
@@ -91,7 +91,7 @@
     name
         name of the filesystem to resize
 
-    new_size
+    size
         new size, (can use suffixes like K, M...). If this size is too small,
         the filesystem will be minimized
     '''
@@ -100,7 +100,7 @@
             'result': True,
             'comment':  ''}
     block_size = os.statvfs(name).f_frsize
-    new_size = _convert_to_units(new_size)
+    new_size = _convert_to_units(size)
     current_size = os.stat(name).st_size / block_size
     if __opts__['test']:
         ret['result'] = None
@@ -110,7 +110,6 @@
         ret['result'] = False
         ret['comment'] = '{0} is not a file'.format(name, new_size)
         return ret
-    change_value = new_size - current_size
     additionnal_flags = ''
     minimize = ''
     info = __salt__['cmd.run_all']('e2fsck -n -f ' + name)
@@ -118,26 +117,26 @@
         ret['result'] = False
         ret['comment'] = 'Error while checking fs {0}: {1}'.format(name, info['stderr'])
         return ret
+    change_value = new_size - current_size * block_size
     minimum_size = int(__salt__['cmd.run_all']('resize2fs -P -f ' + name)['stdout'].split(':')[-1])
-    if new_size == current_size or minimum_size == current_size:
+    if abs(change_value) < pow(1024, 2) or (new_size < minimum_size and minimum_size == current_size):
         ret['result'] = True
         ret['comment'] = 'Filesystem {0} is already at the correct/smaller size'.format(name)
         return ret
-    if new_size > current_size: #increase size
+    if change_value > 0: #increase size
         if not _has_enough_free_space(name, change_value):
             ret['result'] = False
             ret['comment'] = 'Not enough free space'
             return ret
-        log.info('increasing size of ' + name + ' to ' + str(new_size) + 'Gb')
         info = __salt__['cmd.run_all']('dd if=/dev/zero bs=1024k count='
-                + str(increase_value/pow(1024,2)) + ' >> ' + name)
+                + str(change_value / pow(1024, 2)) + ' >> ' + name)
         if info['retcode']:
             ret['result'] = False
             ret['comment'] = 'Error while adding space to {0}: {1}'.format(name, info['stderr'])
             return ret
     else:
-        if minimum_size < change_value: #decrease size to what asked
-            additionnal_flags = ' ' + str(new_size)
+        if minimum_size < abs(change_value): #decrease size to what asked
+            additionnal_flags = ' ' + str(new_size / block_size)
         else: #minimise to min possible
             minimize = '-M '
     info = __salt__['cmd.run_all']('resize2fs -f ' + minimize + name + additionnal_flags)
@@ -146,7 +145,7 @@
         ret['comment'] = 'Error while resizing {0}: {1}'.format(name, info['stderr'])
         return ret
     ret['changes']['resized'] = True
-    ret['comment'] = 'Resized filesystem {0} from {1} to {2}'.format(name, str(current_size), str(max(new_size,minimum_size)))
+    ret['comment'] = 'Resized filesystem {0} from {1} to {2}'.format(name, str(current_size * block_size), str(max(new_size, minimum_size)))
     return ret
 
 def _has_enough_free_space(fs, space):