Advertisement
Guest User

scratchbox

a guest
Apr 7th, 2010
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # failsafe
  4. #set -e
  5.  
  6. #
  7. # usage
  8. #
  9. function usage()
  10. {
  11.     ADDUSER_PATH="$SBOX_PATH/sbin/sbox_adduser"
  12.     cat <<EOF
  13.  
  14. Usage: $MYNAME
  15.        $MYNAME -|-s
  16.        $MYNAME <OPTIONS> <COMMAND> [<ARGS>]
  17.  
  18. Options:
  19.        -d <DIR> change cwd to DIR before running command
  20.        -e <MY_ENV> use MY_ENV as BASH_ENV. (see man bash)
  21.        -p print commands as they are executed
  22.        -k keep current environment
  23.  
  24. Starts a Scratchbox session.  If no arguments are given, an interactive login
  25. shell is launched.  If a dash or '-s' is specified, the stdin is executed as
  26. a shell script.  Otherwise the arguments are executed by the shell.  If the
  27. '-d' option is specified, the current directory will be changed first.
  28.  
  29. You need to have a Scratchbox user account in order to log in.  It can be
  30. created by root using $ADDUSER_PATH.
  31.  
  32. EOF
  33.     exit 1
  34. }
  35.  
  36. #
  37. # print welcome message
  38. #
  39. function print_welcome()
  40. {
  41.     echo
  42.     echo "Welcome to Scratchbox, the cross-compilation toolkit!"
  43.     echo
  44.     echo "Use 'sb-menu' to change your compilation target."
  45.     echo "See /scratchbox/doc/ for documentation."
  46.     echo
  47. }
  48.  
  49. #
  50. # exit point in case of error
  51. #
  52. function exit_error()
  53. {
  54.     echo -e "ERROR: "$@
  55.     exit 1
  56. }
  57.  
  58. #
  59. # do some basic checks to see if environment is ok
  60. #
  61. function sanity_check()
  62. {
  63.     if [ `id -u` = 0 ] ; then
  64.     exit_error "Not allowed to run this as root!"
  65.     fi
  66.     if [ "z$USER" = 'z' ] ; then
  67.     exit_error "Cannot determine user. \$USER is null. Please check your environment."
  68.     fi
  69.     if [ ! -d $SANDBOX ]; then
  70.     exit_error "You don't have a Scratchbox user account!"
  71.     fi
  72.     if [ ! -d $SANDBOX/scratchbox/tools/bin ] || [ ! -c $SANDBOX/dev/null ]; then
  73.     exit_error "Scratchbox is not properly set up!"
  74.     fi
  75. }
  76.  
  77. #
  78. # sudo mode initialization routines
  79. #
  80. function sudo_mode()
  81. {
  82.     SUDO=sudo
  83.     CHROOT_CMD="$SBOX_PATH/sbin/chroot-sudo-uid"
  84.  
  85.     if [ ! -f $CHROOT_SUDO ]; then
  86.         exit_error "ERROR: $CHROOT_SUDO is missing!"
  87.     fi
  88.  
  89.     # register arm binaries (only for sudomode)
  90.     $SUDO $SBOX_PATH/sbin/register_misc_runner
  91.  
  92.     # do mount --bind:ing (only for sudomode)
  93.     mkdir -p $SANDBOX/proc $SANDBOX/tmp $SANDBOX/scratchbox $SANDBOX/dev
  94.  
  95.     if ! mount | grep -q " on $SANDBOX/proc" ; then
  96.     $SUDO mount --bind /proc $SANDBOX/proc
  97.     fi
  98.     if ! mount | grep -q " on $SANDBOX/tmp" ; then
  99.     $SUDO mount --bind /tmp $SANDBOX/tmp
  100.     fi
  101.     if ! mount | grep -q " on $SANDBOX/scratchbox" ; then
  102.     $SUDO mount --bind $SBOX_PATH $SANDBOX/scratchbox
  103.     fi
  104.     if [ -d /scratchbox_local/dev ]; then
  105.     if ! mount | grep -q " on $SANDBOX/dev" ; then
  106.         $SUDO mount --bind /scratchbox_local/dev $SANDBOX/dev
  107.     fi
  108.     fi
  109.     if ! mount | grep -q " on $SANDBOX/dev/pts" ; then
  110.     $SUDO mount -t devpts none $SANDBOX/dev/pts
  111.     fi
  112.  
  113.     CHROOT_CMD="$SUDO $CHROOT_SUDO"
  114. }
  115.  
  116. #
  117. # normal mode initialization
  118. #
  119. function normal_mode()
  120. {
  121.     CHROOT_CMD="$SBOX_PATH/sbin/chroot-uid"
  122.     if [ ! -f "$CHROOT_CMD" ]; then
  123.     exit_error "'$CHROOT_CMD' is missing."
  124.     fi
  125.     if [ ! -u "$CHROOT_CMD" ]; then
  126.     exit_error "'$CHROOT_CMD' is not suid ROOT."
  127.     fi
  128.     if [ ! -x "$CHROOT_CMD" ]; then
  129.     exit_error "You don't have permission to run '$CHROOT_CMD'.\nMaybe you have not logged out and in again after being added to sbox group."
  130.     fi
  131. }
  132.  
  133. #
  134. # Create some stuff for user if running first time
  135. #
  136. function initialize_sbox()
  137. {
  138.     # Create stuff for user
  139.     if [ ! -L $SANDBOX/home/$USER/.terminfo ]; then
  140.     ln -s /scratchbox/etc/terminfo $SANDBOX/home/$USER/.terminfo
  141.     fi
  142.     create_target
  143. }
  144.  
  145. #
  146. # run sb-menu for user
  147. #
  148. create_target()
  149. {
  150.     if [ ! -L "$SANDBOX/targets/links/scratchbox.config" ] ; then
  151.     echo
  152.     echo "You dont have active target in scratchbox chroot.";
  153.     echo "Please create one by running \"sb-menu\" before continuing";
  154.     echo
  155.     fi
  156. }
  157.  
  158.  
  159.  
  160. #
  161. # set up scratchbox specific environment variables
  162. #
  163. function sboxify_environment()
  164. {
  165.     export _SBOX_USER_GROUPNAME=`groups | cut -d' ' -f1`
  166.  
  167.     export _SBOX_DIR=$SBOX_PATH
  168.  
  169.     export PATH=/scratchbox/tools/bin:/scratchbox/compilers/bin:/bin:/usr/bin
  170.  
  171.     export SBOX_REDIRECT_FROM_DIRS=/bin:/usr/bin
  172.     export SBOX_REDIRECT_TO_DIRS=/scratchbox/tools/bin
  173.  
  174.     export SBOX_PRELOAD=/scratchbox/tools/lib/libsb.so.0
  175.     if [ $MYENV ] ; then
  176.         export BASH_ENV=$MYENV
  177.         SBENV="$SBENV"' BASH_ENV="$BASH_ENV"'
  178.     fi
  179.     export HOME=/home/$USER
  180.  
  181.     SBENV="$SBENV"' _SBOX_USER_GROUPNAME="$_SBOX_USER_GROUPNAME" _SBOX_DIR="$SBOX_PATH" PATH="$PATH" SBOX_REDIRECT_FROM_DIRS="$SBOX_REDIRECT_FROM_DIRS"'
  182.     SBENV="$SBENV"' SBOX_REDIRECT_TO_DIRS="$SBOX_REDIRECT_TO_DIRS" SBOX_PRELOAD="$SBOX_PRELOAD" HOME="$HOME"'
  183.     SBENV="$SBENV"' USER="$USER" TERM="$TERM" LOGNAME="$LOGNAME"'
  184. }
  185.  
  186.  
  187. #
  188. # resolve where scratchbox is installed
  189. #
  190. function resolve_sbox_path()
  191. {
  192.     if ! which dirname > /dev/null 2>&1 ; then
  193.     exit_error "'dirname' command is needed to run this script"
  194.     fi
  195.     if ! which readlink > /dev/null 2>&1 ; then
  196.     exit_error "'readlink' command is needed to run this script"
  197.     fi
  198.     SBOX_PATH=`dirname $(readlink -f "$0")`
  199.     SANDBOX="$SBOX_PATH/users/$USER"
  200. }
  201.  
  202. #
  203. # run scratchbox command
  204. #
  205. function run_sbox_command()
  206. {
  207.     if $STDIN ; then
  208.     if $ECHO ; then
  209.         echo $RUN -s
  210.     fi
  211.     $RUN -s
  212.     RETURN=$?
  213.     else
  214.     if $ECHO ; then
  215.         echo $RUN /scratchbox/tools/bin/exec.sh "$INITIAL_DIR" "$@"
  216.     fi
  217.     $RUN /scratchbox/tools/bin/exec.sh "$INITIAL_DIR" "$@"
  218.     RETURN=$?
  219.     fi
  220. }
  221.  
  222. #
  223. # run interactive shell
  224. #
  225. function run_sbox_shell()
  226. {
  227.     print_welcome
  228.     export _SBOX_RESTART_FILE=/tmp/scratchbox-restart.$USER
  229.     SBENV="$SBENV"' _SBOX_RESTART_FILE="$_SBOX_RESTART_FILE"'
  230.     $RUN
  231.         RETURN=$?
  232.     while [ -f $_SBOX_RESTART_FILE ]; do
  233.     hash -r
  234.     reset
  235.     rm $_SBOX_RESTART_FILE
  236.     echo "Shell restarting..."
  237.     $RUN
  238.     RETURN=$?
  239.     done
  240. }
  241.  
  242. #
  243. # clean up
  244. # unmount sbox directories if there is no more active sessions left
  245. #
  246. function cleanup()
  247. {
  248.     PYTHON="$CHROOT_CMD $SANDBOX /scratchbox/tools/bin/python"
  249.     if [ "`$PYTHON /scratchbox/tools/bin/sb-conf ls -S|wc -l`" = 1 ]; then
  250.      $PYTHON /scratchbox/tools/bin/sbrsh-conf umount >/dev/null
  251.     fi
  252. }
  253.  
  254. #
  255. # ready, set, GO!
  256. #
  257. MYNAME=${0/*\//}
  258. INITIAL_DIR="/home/$USER"
  259. MYENV=""
  260. STDIN=false
  261. ECHO=false
  262. KEEPENV=false
  263.  
  264. resolve_sbox_path
  265.  
  266. . $SBOX_PATH/sbin/compatibility.sh
  267.  
  268. # use scratchbox's python
  269. PYTHON="$SBOX_PATH/host_shared/lib/ld.so --library-path $SBOX_PATH/host_shared/lib $SBOX_PATH/tools/bin/python"
  270.  
  271. #
  272. # parse arguments
  273. #
  274. while getopts d:e:pksh inst
  275. do
  276.   case $inst in
  277.     (d) INITIAL_DIR="$OPTARG" ;;
  278.     (e) MYENV="$OPTARG" ;;
  279.     (p) ECHO=true ;;
  280.     (s) STDIN=true ;;
  281.     (k) KEEPENV=true ;;
  282.     (*) usage ;;
  283.   esac
  284. done
  285. shift $(($OPTIND - 1))
  286. if [ "$1" = "-" ] ; then
  287.     STDIN=true
  288. fi
  289.  
  290.  
  291. sanity_check
  292. if [ -f $SBOX_PATH/.sudo_mode ]; then
  293.     sudo_mode
  294. else
  295.     normal_mode
  296. fi
  297. sboxify_environment
  298. if $KEEPENV; then
  299.   RUN="$CHROOT_CMD $SANDBOX /scratchbox/tools/bin/bash -l"
  300. else
  301.   RUN="eval eval $CHROOT_CMD $SANDBOX /scratchbox/tools/bin/env - \$SBENV /scratchbox/tools/bin/bash -l"
  302. fi
  303. initialize_sbox
  304.  
  305. trap true HUP
  306.  
  307. if [ $# -gt 0 -o "$STDIN" = true ] ; then
  308.     run_sbox_command "$@"
  309. else
  310.     run_sbox_shell
  311. fi
  312.  
  313. cleanup
  314.  
  315. trap true TERM
  316.  
  317. if [ $RETURN ] ; then
  318.     exit $RETURN
  319. fi
  320.  
  321. # kill the script process group (everything run from this script)
  322. kill -- -$$
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement