Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59308 > unrolled thread

chroot to install packages

Started byHimanshu Garg <hgarg.india@gmail.com>
First post2013-11-13 06:31 -0800
Last post2013-11-14 01:41 -0800
Articles 9 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  chroot to install packages Himanshu Garg <hgarg.india@gmail.com> - 2013-11-13 06:31 -0800
    Re: chroot to install packages Chris Angelico <rosuav@gmail.com> - 2013-11-14 10:40 +1100
      Re: chroot to install packages Himanshu Garg <hgarg.india@gmail.com> - 2013-11-13 17:52 -0800
        Re: chroot to install packages Chris Angelico <rosuav@gmail.com> - 2013-11-14 13:00 +1100
        Re: chroot to install packages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-14 02:12 +0000
          Re: chroot to install packages Chris Angelico <rosuav@gmail.com> - 2013-11-14 13:26 +1100
    Re: chroot to install packages Himanshu Garg <hgarg.india@gmail.com> - 2013-11-14 01:29 -0800
      Re: chroot to install packages Chris Angelico <rosuav@gmail.com> - 2013-11-14 20:32 +1100
        Re: chroot to install packages Himanshu Garg <hgarg.india@gmail.com> - 2013-11-14 01:41 -0800

#59308 — chroot to install packages

FromHimanshu Garg <hgarg.india@gmail.com>
Date2013-11-13 06:31 -0800
Subjectchroot to install packages
Message-ID<510a8652-1097-4327-9dda-56454142cda4@googlegroups.com>
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?

[toc] | [next] | [standalone]


#59356

FromChris Angelico <rosuav@gmail.com>
Date2013-11-14 10:40 +1100
Message-ID<mailman.2562.1384386029.18130.python-list@python.org>
In reply to#59308
On Thu, Nov 14, 2013 at 1:31 AM, Himanshu Garg <hgarg.india@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

[toc] | [prev] | [next] | [standalone]


#59385

FromHimanshu Garg <hgarg.india@gmail.com>
Date2013-11-13 17:52 -0800
Message-ID<7ec1dd11-45f9-414f-9ee7-906a2b265677@googlegroups.com>
In reply to#59356
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.india@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

How can I do that?  Can you guide me?

[toc] | [prev] | [next] | [standalone]


#59387

FromChris Angelico <rosuav@gmail.com>
Date2013-11-14 13:00 +1100
Message-ID<mailman.2575.1384394449.18130.python-list@python.org>
In reply to#59385
On Thu, Nov 14, 2013 at 12:52 PM, Himanshu Garg <hgarg.india@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

[toc] | [prev] | [next] | [standalone]


#59390

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-11-14 02:12 +0000
Message-ID<528431a0$0$29975$c3e8da3$5496439d@news.astraweb.com>
In reply to#59385
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.india@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

[toc] | [prev] | [next] | [standalone]


#59394

FromChris Angelico <rosuav@gmail.com>
Date2013-11-14 13:26 +1100
Message-ID<mailman.2578.1384396383.18130.python-list@python.org>
In reply to#59390
On Thu, Nov 14, 2013 at 1:12 PM, Steven D'Aprano
<steve+comp.lang.python@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

[toc] | [prev] | [next] | [standalone]


#59418

FromHimanshu Garg <hgarg.india@gmail.com>
Date2013-11-14 01:29 -0800
Message-ID<c8b18088-d97d-4022-95e1-b14ab3a29125@googlegroups.com>
In reply to#59308
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.

[toc] | [prev] | [next] | [standalone]


#59419

FromChris Angelico <rosuav@gmail.com>
Date2013-11-14 20:32 +1100
Message-ID<mailman.2587.1384421553.18130.python-list@python.org>
In reply to#59418
On Thu, Nov 14, 2013 at 8:29 PM, Himanshu Garg <hgarg.india@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

[toc] | [prev] | [next] | [standalone]


#59420

FromHimanshu Garg <hgarg.india@gmail.com>
Date2013-11-14 01:41 -0800
Message-ID<309a78bb-0481-4b0b-92b8-d154d07fa51a@googlegroups.com>
In reply to#59419
> os.chdir("/") immediately afterwards. In any case, it's worth a try.
> ChrisA

Very thanks. the trick worked.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web