mic chroot -s dest, unpack image and save to fs image
authorGui Chen <gui.chen@intel.com>
Tue, 17 Jan 2012 06:00:28 +0000 (14:00 +0800)
committerGui Chen <gui.chen@intel.com>
Tue, 17 Jan 2012 06:06:37 +0000 (14:06 +0800)
Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/chroot.py
mic/conf.py
tools/mic

index b4cffa5..781fcb7 100644 (file)
@@ -21,6 +21,7 @@ import shutil
 import subprocess
 
 from mic import msger
+from mic.conf import configmgr
 from mic.utils import misc, errors, runner, fs_related
 
 chroot_lockfd = -1
@@ -251,6 +252,34 @@ def chroot(chrootdir, bindmounts = None, execute = "/bin/bash"):
         os.chroot(chrootdir)
         os.chdir("/")
 
+    if configmgr.chroot['saveto']:
+        savefs = True
+        saveto = configmgr.chroot['saveto']
+        wrnmsg = "Can't save chroot fs for dir %s exists" % saveto
+        if saveto == chrootdir:
+            savefs = False
+            wrnmsg = "Dir %s is being used to chroot" % saveto
+        elif os.path.exists(saveto):
+            if msger.ask("Dir %s already exists, cleanup and continue?" %
+                         saveto):
+                shutil.rmtree(saveto, ignore_errors = True)
+                savefs = True
+            else:
+                savefs = False
+
+        if savefs:
+            msger.info("Saving image to directory %s" % saveto)
+            runner.quiet("cp -af %s %s" % (chrootdir, saveto))
+            devs = ['dev/fd',
+                    'dev/stdin',
+                    'dev/stdout',
+                    'dev/stderr',
+                    'etc/mtab']
+            ignlst = [os.path.join(saveto, x) for x in devs]
+            map(os.unlink, filter(os.path.exists, ignlst))
+        else:
+            msger.warning(wrnmsg)
+
     dev_null = os.open("/dev/null", os.O_WRONLY)
     files_to_check = ["/bin/bash", "/sbin/init"]
 
index 05832f6..112164c 100644 (file)
@@ -52,7 +52,9 @@ class ConfigMgr(object):
 
                     "runtime": None,
                 },
-                'chroot': {},
+                'chroot': {
+                    "saveto": None,
+                },
                 'convert': {
                     "shell": False,
                 },
index c55c5f1..87c0558 100755 (executable)
--- a/tools/mic
+++ b/tools/mic
@@ -136,6 +136,9 @@ class MicCmd(cmdln.Cmdln):
             destimager.do_pack(base_on)
 
     @cmdln.alias("ch")
+    @cmdln.option('-s', '--saveto',
+                  action='store', dest='saveto', default=None,
+                  help="Save the unpacked image to specified dir")
     def do_chroot(self, subcmd, opts, *args):
         """${cmd_name}: chroot into an image
 
@@ -163,6 +166,8 @@ class MicCmd(cmdln.Cmdln):
 
         self._root_confirm()
 
+        configmgr.chroot['saveto'] = opts.saveto
+
         imagetype = misc.get_image_type(targetimage)
         if imagetype in ("ext3fsimg", "ext4fsimg", "btrfsimg"):
             imagetype = "loop"