diff options
author | yanqingx.li <yanqingx.li@intel.com> | 2012-09-26 14:47:55 +0800 |
---|---|---|
committer | Gui Chen <gui.chen@intel.com> | 2012-09-26 15:08:22 +0800 |
commit | fee503b47d468cbc7c539910796269b65cbff750 (patch) | |
tree | ae593364b7fe4f4e064e3d526009ace86f60accf | |
parent | ab277036676eb8cc77d21ce1b9366da5ee8c67d2 (diff) | |
download | mic-fee503b47d468cbc7c539910796269b65cbff750.tar.gz mic-fee503b47d468cbc7c539910796269b65cbff750.tar.bz2 mic-fee503b47d468cbc7c539910796269b65cbff750.zip |
support running specified command for mic chroot
usage: mic chroot CHROOT [COMMAND [ARGS]]
this feature enables running command with non-interactive
Change-Id: Ic7e74a00c83e3cef5684c2ee94a755389ba4276d
-rw-r--r-- | plugins/imager/fs_plugin.py | 10 | ||||
-rw-r--r-- | plugins/imager/livecd_plugin.py | 10 | ||||
-rw-r--r-- | plugins/imager/liveusb_plugin.py | 10 | ||||
-rw-r--r-- | plugins/imager/loop_plugin.py | 20 | ||||
-rw-r--r-- | plugins/imager/raw_plugin.py | 10 | ||||
-rwxr-xr-x | tools/mic | 19 |
6 files changed, 49 insertions, 30 deletions
diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index e2ee82b..70985b7 100644 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -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) diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index 275f5c1..98b6dbb 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -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) diff --git a/plugins/imager/liveusb_plugin.py b/plugins/imager/liveusb_plugin.py index 02cfb45..e0f079a 100644 --- a/plugins/imager/liveusb_plugin.py +++ b/plugins/imager/liveusb_plugin.py @@ -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) diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index c1bccba..78cde65 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -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) diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 3cb15ed..9b76d6f 100644 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -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) @@ -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: |