space check before copy image
authorGui Chen <gui.chen@intel.com>
Wed, 15 Feb 2012 05:21:05 +0000 (13:21 +0800)
committerGui Chen <gui.chen@intel.com>
Wed, 15 Feb 2012 05:21:05 +0000 (13:21 +0800)
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/imager/baseimager.py
mic/imager/fs.py
mic/utils/misc.py

index 8e5821a..8cc5de4 100644 (file)
@@ -1079,6 +1079,7 @@ class BaseImageCreator(object):
         # Ensure all data is flushed to _outdir
         runner.quiet('sync')
 
+        misc.check_space_pre_cp(self._outdir, destdir)
         for f in os.listdir(self._outdir):
             shutil.move(os.path.join(self._outdir, f),
                         os.path.join(destdir, f))
index fdc9956..b7949f9 100644 (file)
@@ -19,7 +19,7 @@ import os, sys
 
 from baseimager import BaseImageCreator
 from mic import msger
-from mic.utils import runner
+from mic.utils import runner, misc
 from mic.utils.fs_related import *
 from subprocess import call
 
@@ -48,6 +48,7 @@ class FsImageCreator(BaseImageCreator):
         if self._img_compression_method == None:
             fsdir = os.path.join(destdir, self.name)
 
+            misc.check_space_pre_cp(self._instroot, destdir)
             msger.info("Copying %s to %s ..." % (self._instroot, fsdir))
             runner.show(['cp', "-af", self._instroot, fsdir])
 
index e47b3bb..9bec55b 100644 (file)
@@ -53,6 +53,22 @@ RPM_RE  = re.compile("(.*)\.(.*) (.*)-(.*)")
 RPM_FMT = "%(name)s.%(arch)s %(ver_rel)s"
 SRPM_RE = re.compile("(.*)-(\d+.*)-(\d+\.\d+).src.rpm")
 
+def human_size(size):
+    # make Bytes size readable by human
+    import math
+    measure = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
+    expo = int(math.log(size, 1024))
+    mant = float(size/math.pow(1024, expo))
+    return "{0:.1f}{1:s}".format(mant, measure[expo])
+
+def check_space_pre_cp(src, dst):
+    # check whether space is enough before 'cp'
+    srcsize = get_file_size(src) * 1024 * 1024
+    dstsize = get_filesystem_avail(dst)
+    if srcsize > dstsize:
+        raise CreatorError("Space on %s (%s) is not enough than needed (%s)"
+                           % (dst, human_size(dstsize), human_size(srcsize)))
+
 def get_md5sum(fpath):
     blksize = 65536 # should be optimized enough