From 7869b756852f7d52588a8c3b5b712b8368258f9a Mon Sep 17 00:00:00 2001 From: Gui Chen Date: Wed, 15 Feb 2012 13:21:05 +0800 Subject: [PATCH] space check before copy image Signed-off-by: Gui Chen --- mic/imager/baseimager.py | 1 + mic/imager/fs.py | 3 ++- mic/utils/misc.py | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mic/imager/baseimager.py b/mic/imager/baseimager.py index 8e5821a..8cc5de4 100644 --- a/mic/imager/baseimager.py +++ b/mic/imager/baseimager.py @@ -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)) diff --git a/mic/imager/fs.py b/mic/imager/fs.py index fdc9956..b7949f9 100644 --- a/mic/imager/fs.py +++ b/mic/imager/fs.py @@ -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]) diff --git a/mic/utils/misc.py b/mic/utils/misc.py index e47b3bb..9bec55b 100644 --- a/mic/utils/misc.py +++ b/mic/utils/misc.py @@ -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 -- 2.7.4