Advertisement
Guest User

chroot script for chromeos chrooting

a guest
Dec 5th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. unset ISMOUNTED
  4. test -z "$ASUSER" && ASUSER="chronos"
  5. if test "$1" = "-u"
  6. then
  7.   shift
  8.   ASUSER="$1"
  9.   shift
  10. fi
  11.  
  12. if test -z "$2" -o -z "$1"
  13. then
  14.   echo "Usage:
  15.  $(basename "$0") chrootdir program <options>
  16.    or
  17.  $(basename "$0") -u user chrootdir program <options>
  18. "
  19.   exit 1
  20. fi
  21.  
  22. CHROOTDIR="`cd \"$1\" && pwd`"
  23. PROGRAM="$2"
  24.  
  25. shift
  26. shift
  27.  
  28. if ! test -d "$CHROOTDIR/proc"
  29. then
  30.   echo "Directory does not exists"; exit 1
  31. fi
  32.  
  33. function unmnt()
  34. {
  35.   FOUND=1
  36.   COUNT=1
  37.   while test -n "$FOUND" -a $COUNT -lt 100
  38.   do
  39.     unset FOUND
  40.     COUNT=$[$COUNT+1]
  41.     for P in /proc/*/root
  42.     do
  43.       L=$(readlink $P)
  44.       if test -n "$L"
  45.       then
  46.         if test "$L" = "$CHROOTDIR"
  47.         then
  48.           kill -9 "$(echo "$P" | cut -d '/' -f 3)"
  49.           FOUND=1
  50.           ISMOUNTED=1
  51.         fi
  52.       fi
  53.     done
  54.   done
  55.   if test "$ISMOUNTED"
  56.   then
  57.      echo "Unmounting bindings..."
  58.      umount -l "$CHROOTDIR/home" &>/dev/null
  59.      umount -l "$CHROOTDIR/tmp" &>/dev/null
  60.      umount -l "$CHROOTDIR/dev" &>/dev/null
  61.      umount -l "$CHROOTDIR/sys" &>/dev/null
  62.      umount -l "$CHROOTDIR/media" &>/dev/null
  63.      umount -l "$CHROOTDIR/proc" &>/dev/null
  64.   fi
  65. }
  66.  
  67. if test "$PROGRAM" = "unmount" -o "$PROGRAM" = "umount" -o "$PROGRAM" = "kill"
  68. then
  69.   test -n "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")" && ISMOUNTED=1
  70.   unmnt
  71.   exit
  72. fi
  73.  
  74. if test -z "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")"
  75. then
  76.   echo "Creating bind mounts.."
  77.   mount -o rbind /home "$CHROOTDIR/home"
  78.   mount -o rbind /tmp "$CHROOTDIR/tmp"
  79.   mount -o rbind /dev "$CHROOTDIR/dev"
  80.   mount -o rbind /sys "$CHROOTDIR/sys"
  81.   mount -o rbind /media "$CHROOTDIR/media"
  82.   mount -o rbind /proc "$CHROOTDIR/proc"
  83.   ISMOUNTED=1
  84.  
  85.   if test -z "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")"
  86.   then
  87.     echo "Failed mounting"
  88.     unmnt
  89.     exit 1
  90.   fi
  91. fi
  92.  
  93. "$CHROOTDIR/chrooted/prepare.sh" "$CHROOTDIR"
  94. chroot "$CHROOTDIR" /chrooted/start.sh "$ASUSER" "$PROGRAM" "$@"
  95. retval=$!
  96.  
  97. exit $retval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement