Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

chroot to install packages

80 views
Skip to first unread message

Himanshu Garg

unread,
Nov 13, 2013, 9:31:21 AM11/13/13
to
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?

Chris Angelico

unread,
Nov 13, 2013, 6:40:20 PM11/13/13
to pytho...@python.org
On Thu, Nov 14, 2013 at 1:31 AM, Himanshu Garg <hgarg...@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.

ChrisA

Himanshu Garg

unread,
Nov 13, 2013, 8:52:42 PM11/13/13
to
How can I do that? Can you guide me?

Chris Angelico

unread,
Nov 13, 2013, 9:00:39 PM11/13/13
to pytho...@python.org
On Thu, Nov 14, 2013 at 12:52 PM, Himanshu Garg <hgarg...@gmail.com> wrote:
> How can I do that? Can you guide me?

First off: Google Groups is making your posts very ugly. Please either
fix them before posting, or use a better client.

https://wiki.python.org/moin/GoogleGroupsPython

As to chrooting: It'd look something like this, in pseudocode:

if sys.argv[1] == "chroot":
chroot("/some/path/to/new/root")
do_stuff_in_chroot()
do_more_stuff_in_chroot()
exit(0)

do_stuff_not_in_chroot()
os.system("python my_script_name.py chroot")
do_more_stuff_not_in_chroot()


There are many ways to do things, but that's one of the simplest. You
have two completely separate processes.

ChrisA

Steven D'Aprano

unread,
Nov 13, 2013, 9:12:48 PM11/13/13
to
> 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

Chris Angelico

unread,
Nov 13, 2013, 9:26:51 PM11/13/13
to pytho...@python.org
On Thu, Nov 14, 2013 at 1:12 PM, Steven D'Aprano
<steve+comp....@pearwood.info> wrote:
> 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.

I think Python has os.chroot? Not sure.

ChrisA

Himanshu Garg

unread,
Nov 14, 2013, 4:29:55 AM11/14/13
to
I have done it but having a problem.

I have written a script

os.chroot("/lxc/test_container/rootfs")
subprocess.call(["apt-key", "add", "/root/package.key"])
subprocess.call(["apt-get", "update"])

os._exit(0)

Now, this script is working properly, entering the chroot jail and adding the apt key, but when the "apt-get update" command runs, it starts but throws me an error:
"E: Unable to change to (unreachable)/lxc/test_container/ - chdir (2: No such file or directory)"

However, when I manually chroot to "/lxc/test_container/rootfs" and run the update command, then it works perfectly with no errors.

Why the apt-get update command require to go to my "/lxc/test_container" when run in script.

Chris Angelico

unread,
Nov 14, 2013, 4:32:29 AM11/14/13
to pytho...@python.org
On Thu, Nov 14, 2013 at 8:29 PM, Himanshu Garg <hgarg...@gmail.com> wrote:
> I have written a script
>
> os.chroot("/lxc/test_container/rootfs")
> subprocess.call(["apt-key", "add", "/root/package.key"])
> subprocess.call(["apt-get", "update"])
>
> os._exit(0)
>
> Now, this script is working properly, entering the chroot jail and adding the apt key, but when the "apt-get update" command runs, it starts but throws me an error:
> "E: Unable to change to (unreachable)/lxc/test_container/ - chdir (2: No such file or directory)"

I'm not certain, but this might be due to chrooting without changing
directory. Unless os.chroot() does it for you, you'll want to
os.chdir("/") immediately afterwards. In any case, it's worth a try.

ChrisA

Himanshu Garg

unread,
Nov 14, 2013, 4:41:19 AM11/14/13
to
> os.chdir("/") immediately afterwards. In any case, it's worth a try.
> ChrisA

Very thanks. the trick worked.
0 new messages