chroot to install packages

H

Himanshu Garg

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?
 
C

Chris Angelico

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
 
H

Himanshu Garg

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?
 
C

Chris Angelico

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
 
S

Steven D'Aprano

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!
 
C

Chris Angelico

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
 
H

Himanshu Garg

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.
 
C

Chris Angelico

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,604
Members
47,223
Latest member
smithjens316

Latest Threads

Top