On Wed, 13 Nov 2013 17:52:42 -0800, Himanshu Garg wrote: > On Thursday, November 14, 2013 5:10:20 AM UTC+5:30, Chris Angelico > wrote: >> On Thu, Nov 14, 2013 at 1:31 AM, Himanshu Garg <hgarg.in...@gmail.com> >> wrote: >> >> > I am writing a python script to run chroot command to chroot to a >> > linux distro and then run commands. How can I do this, as after >> > chrooting, the script runs the commands relative to the outside not >> > inside the chrooted env? >> >> Probably the easiest way to do this is to divide your script into two >> pieces: one piece runs inside the chroot, the other doesn't. Then you >> have the "outer" script invoke the "inner" script as a separate >> process. When the inner process terminates, the outer continues, and >> since the outer wasn't chrooted, it's now running commands relative to >> the outside. >> >> One convenient way to manage this is to have one script that invokes >> itself with arguments. Another way is to have two actually separate >> script files. It really depends how much work you're doing in each >> half.
> > How can I do that? Can you guide me? That's an extremely open-ended question that basically means "please write my code for me", but I'll give it a go. Note that I haven't done this before, so take what I say with a grain of salt, and I look forward to corrections from others. Suppose you have a function that you want to run on the inside of the chroot. So you build a module like this: # module inside.py def function(a, b, c): ... if __name__ == '__main__': args = sys.argv[1:] function(*args) Make sure this module is executable: on Linux systems, you would use chmod u+x inside.py The next bit is the part I have no idea about... use your operating system tools to set up a chroot jail for "inside.py". Google is your friend there, I'm sure there will be many, many websites that discuss chroot jails. Finally, you have your outside script that calls the inner one: # module outside.py from subprocess import call return_code = call(["path/to_jail/inside.py", "first arg", "second arg", "third arg"]) For more complicated usage, you should read the docs for the subprocess module. For quick and dirty uses, you can use: import sys sys.system('path/to_jail/inside.py "first arg" "second arg" "third arg"') but I recommend against that except for throw-away scripts. Corrections welcome! -- Steven -- https://mail.python.org/mailman/listinfo/python-list