How to copy (only) needed libraries to a chroot

Discussion in 'all things UNIX' started by Gullible Jones, Aug 2, 2012.

Thread Status:
Not open for further replies.
  1. For those of you who might want to create a chroot sandbox, don't want to deal with the horrors of bind mounting, want things automated, and don't like dealing with the CLI, I'm throwing this hint out...

    # cp --parents `ldd BIN_PATH | cut -d " " -f 3` CHROOT_PATH
    Where BIN_PATH is the path to the binary you want to run, and CHROOT_PATH is your chroot.

    It's pretty simple how it works (which is good, because I'm not a UNIX wizard):

    - ldd produces line after line of output like this: => /lib/ (someaddress)

    - cut, invoked so that it takes the third token with space as the delimiter, just gives you the library path

    - the backticks make cp read the output therein

    - thus cp copies all the libraries from their original place to your chroot's library dir, and the --parents option makes it keep the directory hierarchy the same.

    So you end up with just the libraries you need in the chroot directory. Don't take my word on it though, there are probably situations where this wouldn't work. It's a hint, not a solution - so don't just copy and paste it!
Thread Status:
Not open for further replies.