Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- unset ISMOUNTED
- test -z "$ASUSER" && ASUSER="chronos"
- if test "$1" = "-u"
- then
- shift
- ASUSER="$1"
- shift
- fi
- if test -z "$2" -o -z "$1"
- then
- echo "Usage:
- $(basename "$0") chrootdir program <options>
- or
- $(basename "$0") -u user chrootdir program <options>
- "
- exit 1
- fi
- CHROOTDIR="`cd \"$1\" && pwd`"
- PROGRAM="$2"
- shift
- shift
- if ! test -d "$CHROOTDIR/proc"
- then
- echo "Directory does not exists"; exit 1
- fi
- function unmnt()
- {
- FOUND=1
- COUNT=1
- while test -n "$FOUND" -a $COUNT -lt 100
- do
- unset FOUND
- COUNT=$[$COUNT+1]
- for P in /proc/*/root
- do
- L=$(readlink $P)
- if test -n "$L"
- then
- if test "$L" = "$CHROOTDIR"
- then
- kill -9 "$(echo "$P" | cut -d '/' -f 3)"
- FOUND=1
- ISMOUNTED=1
- fi
- fi
- done
- done
- if test "$ISMOUNTED"
- then
- echo "Unmounting bindings..."
- umount -l "$CHROOTDIR/home" &>/dev/null
- umount -l "$CHROOTDIR/tmp" &>/dev/null
- umount -l "$CHROOTDIR/dev" &>/dev/null
- umount -l "$CHROOTDIR/sys" &>/dev/null
- umount -l "$CHROOTDIR/media" &>/dev/null
- umount -l "$CHROOTDIR/proc" &>/dev/null
- fi
- }
- if test "$PROGRAM" = "unmount" -o "$PROGRAM" = "umount" -o "$PROGRAM" = "kill"
- then
- test -n "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")" && ISMOUNTED=1
- unmnt
- exit
- fi
- if test -z "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")"
- then
- echo "Creating bind mounts.."
- mount -o rbind /home "$CHROOTDIR/home"
- mount -o rbind /tmp "$CHROOTDIR/tmp"
- mount -o rbind /dev "$CHROOTDIR/dev"
- mount -o rbind /sys "$CHROOTDIR/sys"
- mount -o rbind /media "$CHROOTDIR/media"
- mount -o rbind /proc "$CHROOTDIR/proc"
- ISMOUNTED=1
- if test -z "$(cat /proc/mounts | grep " $CHROOTDIR/proc ")"
- then
- echo "Failed mounting"
- unmnt
- exit 1
- fi
- fi
- "$CHROOTDIR/chrooted/prepare.sh" "$CHROOTDIR"
- chroot "$CHROOTDIR" /chrooted/start.sh "$ASUSER" "$PROGRAM" "$@"
- retval=$!
- exit $retval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement