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)
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)
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)
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)
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)
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 "
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:
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")
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)
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")
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)
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()
@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}
"""
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"
raise errors.CreatorError("Cannot support image type: %s" \
% imagetype)
- chrootclass.do_chroot(targetimage)
+ chrootclass.do_chroot(targetimage, args[1:])
if __name__ == "__main__":
try: