Skip to content

Instantly share code, notes, and snippets.

@BuonOmo
Last active January 17, 2018 12:27
Show Gist options
  • Save BuonOmo/90e2a6c98a16400b62ddbcdd2425da8f to your computer and use it in GitHub Desktop.
Save BuonOmo/90e2a6c98a16400b62ddbcdd2425da8f to your computer and use it in GitHub Desktop.
A tiny script to provide a container
#!/bin/zsh
# Copyright (c) 2016 Ulysse Buonomo <buonomo.ulysse@gmail.com> (MIT license)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -e
# This is the path of your container (or jail)
jail_path=$(mktemp -d /tmp/jail.XXX)
# These are all commands you want to provide, you should have those commands on you computer
coms=(/bin/bash /bin/ls) # name commands is already taken
echo "== Jail is at path $jail_path"
dependencies=($(echo $coms | xargs ldd | grep -oe '/lib[^ ]*' | sort -u))
directories=($(echo $coms $dependencies | xargs dirname | sort -u))
echo "== Importing commands.."
for i in $directories;do
mkdir -p $jail_path$i
done
for i in $coms $dependencies;do
cp $i $jail_path$i
done
echo "== Here is your jail ready for chroot"
# tree is not always given with linux OS, so there is a fallback on ls
tree $jail_path 2> /dev/null || ls -R
echo "\n== You can run the container with command \`chroot $jail_path <a_command>\`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment