10

I need to be able to provide the /bin and /lib directories inside a chroot jail so that programs can dynamically link properly.

Is there a way to accomplish this without making a copy of the /bin and /lib dirs to the chroot jail?

I've tried symlinks, and they don't work from inside chroot jails, and directories can not be hardlinked.

14

You could use mount to remount the directories you need in your jail:

# mount --bind /bin /chroot/bin
# mount --bind /lib /chroot/lib
# chroot /chroot

For use in /etc/fstab:

/bin /chroot/bin none bind
/lib /chroot/lib none bind

Cheers!

  • 2
    Thanks a lot. Linux just keeps getting better and better the more I play with it :p – anonymous coward Dec 17 '10 at 2:17
  • I'm glad it worked out well! Cheers! – jgr Dec 17 '10 at 11:00
3

If you didn't want to mount the directories as jgr said, you can use cp to recursivly copy directories and create hardlinks for all files:

cp -alf /bin /chroot/bin
cp -alf /lib /chroot/lib
chroot /chroot

This way your chroot's /bin and /lib can have slightly different structure / contents than the main directories.

  • 1
    Good idea, but if the /chroot is on a different device this won't work. You can't hardlink across devices. – AllenKll May 6 '16 at 18:23
1
#!/bin/bash

copy_file_and_dependencies() {
    PROGRAM="$1"
    DEPENDENCIES="$(ldd "$PROGRAM" | awk '{ print $3 }' | grep -v '(' | grep -v 'not a dynamic executable')"

    mkdir -p "${JAIL}$(dirname $PROGRAM)"
    cp -Lv "$PROGRAM" "${JAIL}${PROGRAM}"

    for f in $DEPENDENCIES; do
        mkdir -p "${JAIL}$(dirname $f)"
        cp -Lv "$f" "${JAIL}${f}"
    done
}

export -f copy_file_and_dependencies

copy_file_and_dependencies /etc/ld.so.cache
copy_file_and_dependencies /bin/sh
# ...
  • 1
    The intent of the question seems to be to provide access to the existing files rather than copying them into the chroot. – psusi May 18 '15 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.