support running specified command for mic chroot
authoryanqingx.li <yanqingx.li@intel.com>
Wed, 26 Sep 2012 06:47:55 +0000 (14:47 +0800)
committerGui Chen <gui.chen@intel.com>
Wed, 26 Sep 2012 07:08:22 +0000 (15:08 +0800)
usage: mic chroot CHROOT [COMMAND [ARGS]]
this feature enables running command with non-interactive

Change-Id: Ic7e74a00c83e3cef5684c2ee94a755389ba4276d

plugins/imager/fs_plugin.py
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py
tools/mic

index e2ee82b..70985b7 100644 (file)
@@ -126,13 +126,15 @@ class FsPlugin(ImagerPlugin):
         return 0
 
     @classmethod
-    def do_chroot(self, target):#chroot.py parse opts&args
+    def do_chroot(self, target, cmd):#chroot.py parse opts&args
             try:
-                envcmd = fs_related.find_binary_inchroot("env", target)
-                if envcmd:
-                    cmdline = "%s HOME=/root /bin/bash" % envcmd
+                if len(cmd) != 0:
+                    cmdline = ' '.join(cmd)
                 else:
                     cmdline = "/bin/bash"
+                envcmd = fs_related.find_binary_inchroot("env", target)
+                if envcmd:
+                    cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
                 chroot.chroot(target, None, cmdline)
             finally:
                 chroot.cleanup_after_chroot("dir", None, None, None)
index 275f5c1..98b6dbb 100644 (file)
@@ -118,7 +118,7 @@ class LiveCDPlugin(ImagerPlugin):
         return 0
 
     @classmethod
-    def do_chroot(cls, target):
+    def do_chroot(cls, target, cmd):
         os_image = cls.do_unpack(target)
         os_image_dir = os.path.dirname(os_image)
 
@@ -150,11 +150,13 @@ class LiveCDPlugin(ImagerPlugin):
             raise
 
         try:
-            envcmd = fs_related.find_binary_inchroot("env", extmnt)
-            if envcmd:
-                cmdline = "%s HOME=/root /bin/bash" % envcmd
+            if len(cmd) != 0:
+                cmdline = ' '.join(cmd)
             else:
                 cmdline = "/bin/bash"
+            envcmd = fs_related.find_binary_inchroot("env", extmnt)
+            if envcmd:
+                cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
             chroot.chroot(extmnt, None, cmdline)
         except:
             raise errors.CreatorError("Failed to chroot to %s." %target)
index 02cfb45..e0f079a 100644 (file)
@@ -119,7 +119,7 @@ class LiveUSBPlugin(ImagerPlugin):
         return 0
 
     @classmethod
-    def do_chroot(cls, target):
+    def do_chroot(cls, target, cmd):
         os_image = cls.do_unpack(target)
         os_image_dir = os.path.dirname(os_image)
 
@@ -151,11 +151,13 @@ class LiveUSBPlugin(ImagerPlugin):
             raise
 
         try:
-            envcmd = fs_related.find_binary_inchroot("env", extmnt)
-            if envcmd:
-                cmdline = "%s HOME=/root /bin/bash" % envcmd
+            if len(cmd) != 0:
+                cmdline = ' '.join(cmd)
             else:
                 cmdline = "/bin/bash"
+            envcmd = fs_related.find_binary_inchroot("env", extmnt)
+            if envcmd:
+                cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
             chroot.chroot(extmnt, None, cmdline)
         except:
             raise errors.CreatorError("Failed to chroot to %s." %target)
index c1bccba..78cde65 100644 (file)
@@ -131,7 +131,7 @@ class LoopPlugin(ImagerPlugin):
         return 0
 
     @classmethod
-    def _do_chroot_tar(cls, target):
+    def _do_chroot_tar(cls, target, cmd):
         mountfp_xml = os.path.splitext(target)[0] + '.xml'
         if not os.path.exists(mountfp_xml):
             raise errors.CreatorError("No mount point file found for this tar "
@@ -178,7 +178,11 @@ class LoopPlugin(ImagerPlugin):
             loops.append(loop)
 
         try:
-            chroot.chroot(mntdir, None, "/usr/bin/env HOME=/root /bin/bash")
+            if len(cmd) != 0:
+                cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd)
+            else:
+                cmdline = "/usr/bin/env HOME=/root /bin/bash"
+            chroot.chroot(mntdir, None, cmdline)
         except:
             raise errors.CreatorError("Failed to chroot to %s." % target)
         finally:
@@ -188,11 +192,11 @@ class LoopPlugin(ImagerPlugin):
             shutil.rmtree(tmpdir, ignore_errors=True)
 
     @classmethod
-    def do_chroot(cls, target):
+    def do_chroot(cls, target, cmd):
         if target.endswith('.tar'):
             import tarfile
             if tarfile.is_tarfile(target):
-                LoopPlugin._do_chroot_tar(target)
+                LoopPlugin._do_chroot_tar(target, cmd)
                 return
             else:
                 raise errors.CreatorError("damaged tarball for loop images")
@@ -225,11 +229,13 @@ class LoopPlugin(ImagerPlugin):
             raise
 
         try:
-            envcmd = fs_related.find_binary_inchroot("env", extmnt)
-            if envcmd:
-                cmdline = "%s HOME=/root /bin/bash" % envcmd
+            if len(cmd) != 0:
+                cmdline = ' '.join(cmd)
             else:
                 cmdline = "/bin/bash"
+            envcmd = fs_related.find_binary_inchroot("env", extmnt)
+            if envcmd:
+                cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
             chroot.chroot(extmnt, None, cmdline)
         except:
             raise errors.CreatorError("Failed to chroot to %s." % img)
index 3cb15ed..9b76d6f 100644 (file)
@@ -125,7 +125,7 @@ class RawPlugin(ImagerPlugin):
         return 0
 
     @classmethod
-    def do_chroot(cls, target):
+    def do_chroot(cls, target, cmd):
         img = target
         imgsize = misc.get_file_size(img) * 1024L * 1024L
         partedcmd = fs_related.find_binary_path("parted")
@@ -221,11 +221,13 @@ class RawPlugin(ImagerPlugin):
             raise
 
         try:
-            envcmd = fs_related.find_binary_inchroot("env", imgmnt)
-            if envcmd:
-                cmdline = "%s HOME=/root /bin/bash" % envcmd
+            if len(cmd) != 0:
+                cmdline = ' '.join(cmd)
             else:
                 cmdline = "/bin/bash"
+            envcmd = fs_related.find_binary_inchroot("env", imgmnt)
+            if envcmd:
+                cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
             chroot.chroot(imgmnt, None, cmdline)
         except:
             raise errors.CreatorError("Failed to chroot to %s." %img)
index d0c6fed..e45b1ec 100755 (executable)
--- a/tools/mic
+++ b/tools/mic
@@ -69,6 +69,13 @@ class MicCmd(cmdln.Cmdln):
 
         self.print_version()
 
+    def optparser_setup(func):
+        """Setup optparser for a function"""
+        if not hasattr(func, "optparser"):
+            func.optparser = cmdln.SubCmdOptionParser()
+            func.optparser.disable_interspersed_args()
+        return func
+
     def help_create(self):
         cr = creator.Creator()
         cr.optparser = cr.get_optparser()
@@ -166,11 +173,12 @@ class MicCmd(cmdln.Cmdln):
     @cmdln.option('-s', '--saveto',
                   action='store', dest='saveto', default=None,
                   help="Save the unpacked image to specified dir")
+    @optparser_setup
     def do_chroot(self, subcmd, opts, *args):
         """${cmd_name}: chroot into an image
 
         Usage:
-            mic chroot <imagefile>
+            mic chroot [options] <imagefile> [command [arg]...]
 
         ${cmd_option_list}
         """
@@ -181,11 +189,8 @@ class MicCmd(cmdln.Cmdln):
             if hasattr(handler, "optparser"):
                 handler.optparser.print_help()
             return 1
-
-        if len(args) == 1:
-            targetimage = args[0]
-        else:
-            raise errors.Usage("Extra argument given")
+        
+        targetimage = args[0]
 
         if not os.path.exists(targetimage):
             raise errors.CreatorError("Cannot find the image: %s"
@@ -209,7 +214,7 @@ class MicCmd(cmdln.Cmdln):
             raise errors.CreatorError("Cannot support image type: %s" \
                                       % imagetype)
 
-        chrootclass.do_chroot(targetimage)
+        chrootclass.do_chroot(targetimage, args[1:])
 
 if __name__ == "__main__":
     try: