Chrooting Python in OpenBSD 4.5
Just a quick technical entry today, on the off chance that this helps somebody, somewhere.
For reason’s I’ll go into another time, I’ve recently been trying to chroot a Python installation into the directory /srv/www on an OpenBSD 4.5 machine. I took the standard approach to this, which is to examine the output of ldd /usr/local/bin/python2.5:
/usr/local/bin/python2.5:
Start End Type Open Ref GrpRef Name
1c000000 3c004000 exe 1 0 0 /usr/local/bin/python2.5
05432000 2547c000 rlib 0 1 0 /usr/local/lib/libpython2.5.so.1.0
05dc3000 25dc7000 rlib 0 1 0 /usr/lib/libutil.so.11.0
0df1e000 2df44000 rlib 0 1 0 /usr/lib/libstdc++.so.47.0
0d543000 2d54d000 rlib 0 1 0 /usr/lib/libm.so.5.0
02a7c000 22a85000 rlib 0 1 0 /usr/lib/libpthread.so.11.1
06a56000 26a8f000 rlib 0 1 0 /usr/lib/libc.so.50.1
08db8000 08db8000 rtld 0 1 0 /usr/libexec/ld.so
and copy each of these files over to the corresponding location in the chroot directory (i.e. stuff from /usr/lib/ goes in /srv/www/usr/lib). But after doing this, testing the setup with chroot /srv/www python2.5 yielded the error: /usr/bin/local/python2.5: can't load library 'libpython2.5.so.1.0'.
I went near mad trying to figure out why this wasn’t working. The libpython file was clearly in the correct location. A PHP installation that I had chrooted into the very same place using the very same procedure worked flawlessly. There seemed to be no scope for rational explanation of why this wasn’t working.
In the end, I managed to solve this problem by copying the file /var/run/ld.so.hints into the corresponding location in the chroot directory. Everything worked perfectly after that. I don’t profess to have any idea why you need to do this (I discovered that it worked after much trial and error, based on random permutations of stuff found on the web), but you do. Hopefully this post saves some other poor hacker from wasting a few frustrated hours.
I note that the ld.so.hints file exists in the same location on recent NetBSD releases, so I assume this advice ports over to NetBSD as well. However, there is no such file in my install of Arch Linux, so things are probably different in Linux land. Linux distros do seem to have a file /etc/ld.so.conf which serves a similar purposes – it was actually reading about this that gave me the idea to look for something similar in my situation.

Hello dude, your article helps me so much, my python is now chrooted and works fine… but, i’m still having a little problem… may be you would help me…
when i try to import zlib, i get ImportError: Cannot load specified object
zlib.so is where supposed to be: usr/local/lib/python2.6/lib-dynload/zlib.so*
any kind off help will be welcome… thank you in advance
Hi Guido,
I’m really glad this post was able to help you. I’m sorry but I don’t think I can help with your zlib problems.
It was almost 2 years ago that I dealt with chrooting Python myself, so my memory is not exactly fresh, and unfortunately I don’t have an OpenBSD machine around anymore to have a tinker with. I did some Googling to see if I could find anything, and here’s the best I could find:
Here is a small open source program for chrooting Python on OpenBSD. I had a look at the source code and as far as I could tell from a quick glance it’s not doing anything particularly specical, just copying across library files. However, I did notice that right at the end it runs the command “chroot %s /sbin/ldconfig /usr/local/lib”, where %s gets replaced with your chroot directory. The comment for this bit of code says “Runs the command to generate a new ld.hints.so file in the chroot directory”. In other words, this script is not just copying the regular ld.hints file across like I did, but actually using some program to generate a new one. It is possible that this may make a difference for your problem, but it’s really just a guess. I hope it helps.