From 3953aa868735774a550355b1c71cf1d626fe2687 Mon Sep 17 00:00:00 2001 From: JF Ding Date: Thu, 29 Dec 2011 17:38:10 +0800 Subject: [PATCH] some changes for save/load mountpoints info for taring loop --- mic/imager/loop.py | 41 ++++++++++++++++-- mic/plugin.py | 4 +- mic/utils/misc.py | 11 ----- plugins/imager/loop_plugin.py | 78 ++++++++++++++++++----------------- 4 files changed, 81 insertions(+), 53 deletions(-) diff --git a/mic/imager/loop.py b/mic/imager/loop.py index ae386be..c2f0178 100644 --- a/mic/imager/loop.py +++ b/mic/imager/loop.py @@ -27,6 +27,35 @@ from baseimager import BaseImageCreator FSLABEL_MAXLEN = 32 """The maximum string length supported for LoopImageCreator.fslabel.""" +def save_mountpoints(dpath, loops): + """Save mount points mapping to file + :loops, list of tuple (mp, label, name, size, fstype) + """ + + if not loops: + return None + + fname = ".mountpoints" + fp = os.path.join(dpath, fname) + + with open(fp, 'w') as wf: + for loop in loops: + wf.write(':'.join(map(str, loop)) + '\n') + + return fname + +def load_mountpoints(dpath): + """Load mount points mapping from file + """ + mps = [] + with open(os.path.join(dpath, '.mountpoints')) as f: + for line in f.readlines(): + try: + mps.append(tuple(line.strip().split(':'))) + except: + msger.warning("wrong format line in mountpoints mapping file") + return mps + class LoopImageCreator(BaseImageCreator): """Installs a system into a loopback-mountable filesystem image. @@ -308,9 +337,15 @@ class LoopImageCreator(BaseImageCreator): % item['name']) tar.add(item['name']) - # append mount map file to tar ball - mountmap = misc.write_mount_point(self._instloops) - tar.add(mountmap) + # append mount points mapping file to tar ball + mountmap_fp = save_mountpoints(self.__imgdir, + [(loop['mountpoint'], + loop['label'], + loop['name'], + loop['size'], + loop['fstype']) for loop in self._instloops]) + if mountmap_fp: + tar.add(mountmap_fp) tar.close() os.chdir(curdir) diff --git a/mic/plugin.py b/mic/plugin.py index cedcdc2..4654135 100644 --- a/mic/plugin.py +++ b/mic/plugin.py @@ -76,8 +76,8 @@ class PluginMgr(object): msger.debug("Plugin module %s:%s imported"\ % (mod, pymod.__file__)) except ImportError, e: - msger.warning('%s, skip plugin %s/%s'\ - %(str(e), os.path.basename(pdir), mod)) + msger.warning('Loading failed, skip plugin %s/%s'\ + % (os.path.basename(pdir), mod)) del(sys.path[0]) diff --git a/mic/utils/misc.py b/mic/utils/misc.py index 00a39c2..f5d8659 100644 --- a/mic/utils/misc.py +++ b/mic/utils/misc.py @@ -61,17 +61,6 @@ def get_md5sum(fpath): md5sum.update(data) return md5sum.hexdigest() -def write_mount_point(loops): - if not loops: - return - mountpoints = ".mountpoints" - with open(mountpoints, 'w') as f: - for loop in loops: - f.write("%s:%s:%s:%d:%s\n" % (loop['mountpoint'], loop['label'], - loop['name'], loop['size'], - loop['fstype'])) - return mountpoints - def save_ksconf_file(ksconf, release="latest", arch="ia32"): if not os.path.exists(ksconf): return diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index 63288db..e4b8661 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -23,7 +23,7 @@ from mic import chroot, msger from mic.utils import misc, fs_related, errors, cmdln from mic.conf import configmgr from mic.plugin import pluginmgr -import mic.imager.loop as loop +from mic.imager.loop import LoopImageCreator, load_mountpoints from mic.pluginbase import ImagerPlugin class LoopPlugin(ImagerPlugin): @@ -76,7 +76,7 @@ class LoopPlugin(ImagerPlugin): pkgmgrs = pluginmgr.get_plugins('backend').keys() raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs))) - creator = loop.LoopImageCreator(creatoropts, pkgmgr, opts.taring_to) + creator = LoopImageCreator(creatoropts, pkgmgr, opts.taring_to) if len(recording_pkgs) > 0: creator._recording_pkgs = recording_pkgs @@ -86,6 +86,7 @@ class LoopPlugin(ImagerPlugin): imagefile = "%s.tar" % os.path.join(creator.destdir, opts.taring_to) else: imagefile = "%s.img" % os.path.join(creator.destdir, creator.name) + if os.path.exists(imagefile): if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile): os.unlink(imagefile) @@ -113,47 +114,48 @@ class LoopPlugin(ImagerPlugin): return 0 @classmethod - def do_chroot_tar(cls, target): + def _do_chroot_tar(cls, target): import tarfile - tar = tarfile.open(target, 'r') tmpdir = misc.mkdtemp() tar.extractall(path=tmpdir) tar.close() - loops = [] mntdir = misc.mkdtemp() - with open(os.path.join(tmpdir, '.mountpoints'), 'r') as f: - for line in f.readlines(): - try: - mp, label, name, size, fstype = line.strip('\n').split(':') - except: - msger.error("Wrong format image tarball: %s" % target) - - if fstype in ("ext2", "ext3", "ext4"): - myDiskMount = fs_related.ExtDiskMount - elif fstype == "btrfs": - myDiskMount = fs_related.BtrfsDiskMount - elif fstype in ("vfat", "msdos"): - myDiskMount = fs_related.VfatDiskMount - else: - msger.error("Cannot support fstype: %s" % fstype) - - name = os.path.join(tmpdir, name) - size = int(size) * 1024L * 1024L - loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), - os.path.join(mntdir, mp.lstrip('/')), - fstype, size, label) - loops.append(loop) - - try: - msger.verbose("Mount %s to %s" % (mp, mntdir + mp)) - fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/'))) - loop.mount() - except: - loop.cleanup() - shutil.rmtree(mntdir, ignore_errors = True) - raise + + loops = [] + for (mp, label, name, size, fstype) in load_mountpoints(tmpdir): + if fstype in ("ext2", "ext3", "ext4"): + myDiskMount = fs_related.ExtDiskMount + elif fstype == "btrfs": + myDiskMount = fs_related.BtrfsDiskMount + elif fstype in ("vfat", "msdos"): + myDiskMount = fs_related.VfatDiskMount + else: + msger.error("Cannot support fstype: %s" % fstype) + + name = os.path.join(tmpdir, name) + size = int(size) * 1024L * 1024L + loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), + os.path.join(mntdir, mp.lstrip('/')), + fstype, size, label) + + try: + msger.verbose("Mount %s to %s" % (mp, mntdir + mp)) + fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/'))) + loop.mount() + + except: + loop.cleanup() + for lp in reversed(loops): + chroot.cleanup_after_chroot("img", lp, None, mntdir) + + shutil.rmtree(mntdir, ignore_errors = True) + shutil.rmtree(tmpdir, ignore_errors = True) + raise + + loops.append(loop) + try: chroot.chroot(mntdir, None, "/bin/env HOME=/root /bin/bash") except: @@ -161,14 +163,16 @@ class LoopPlugin(ImagerPlugin): finally: for loop in reversed(loops): chroot.cleanup_after_chroot("img", loop, None, mntdir) + shutil.rmtree(tmpdir, ignore_errors = True) @classmethod def do_chroot(cls, target):#chroot.py parse opts&args import tarfile if tarfile.is_tarfile(target): - LoopPlugin.do_chroot_tar(target) + LoopPlugin._do_chroot_tar(target) return + img = target imgsize = misc.get_file_size(img) * 1024L * 1024L imgtype = misc.get_image_type(img) -- 2.34.1