Can't use subprocess.Popen() after os.chroot() - why?

Nobody nobody at nowhere.com
Tue Sep 6 21:01:11 EDT 2011


On Sun, 04 Sep 2011 07:22:07 -0700, Erik wrote:

> I'm trying to do the following: 

> os.chroot("/tmp/my_chroot")
> p = Popen("/bin/date", stdin=PIPE, stdout=PIPE, stderr=PIPE)

> but the Popen call is dying with the following exception:

> LookupError: unknown encoding: string-escape
> 
> Am I missing something here? does the chroot environment need to be
> populated with more than just the date executable in this case?

Yes. It also needs to include any parts of the Python run-time which
Python will try to load while executing subsequent code. In this case, the
module which implements the string-escape encoding.

But fixing that will probably show up yet more files which need to exist
within the pseudo-root. E.g. any shared libraries which the executable
needs (probably at least libc), and any data files which those libraries
need (in the case of /bin/date, it may need /etc/timezone; many programs
may require locale data if you aren't in the "C" locale, and so on).

Whether from Python or from C, chroot() requires a good understanding of
the low-level details of your operating system. If you don't know how to
build a minimal Linux distribution from scratch, you're going to have to
learn many of those details in order to use chroot().

For any non-trivial chroot() usage, it's often easier to install a small
newlib+busybox-based Linux distribution under the pseudo-root than to try
to re-use files from and existing (presumably glibc+coreutils-based)
desktop/server distribution.




More information about the Python-list mailing list