Skip to content

Instantly share code, notes, and snippets.

@rsdy
Created May 26, 2012 15:56
Show Gist options
  • Save rsdy/2794408 to your computer and use it in GitHub Desktop.
Save rsdy/2794408 to your computer and use it in GitHub Desktop.
enter a development chroot and run a command
#!/bin/ksh
## configuration
CHROOT=$HOME/sid-debootstrap
USER=rsdy
USER_HOME=/home/$USER
WORKDIR=$USER_HOME/build
TERM=rxvt-unicode
PATH_EXTEND='$HOME/.cabal/bin'
BIND_LIST="/dev /proc /sys"
## halt. hammerzeit.
MOUNTDIR=$CHROOT/$WORKDIR
ENV="TERM=$TERM; \
HOME=$USER_HOME; \
PATH=$PATH_EXTEND:\$PATH; \
cd $WORKDIR;"
COMMAND="$ENV ${@:-bash}"
bind_if_not_mounted()
{
params=($@)
grep -qv ${params[${#params[*]} -1]} /proc/mounts && sudo mount -o bind $@
}
bind_mount_list()
{
for i in $BIND_LIST; do
bind_if_not_mounted "$i" "$CHROOT/$i"
done
}
unbind_mount_list()
{
for i in $BIND_LIST; do sudo umount "$CHROOT/$i"; done
}
bind_mount_list
bind_if_not_mounted $(pwd) "$MOUNTDIR"
sudo chroot --userspec="$USER:$USER" "$CHROOT" /bin/bash -c "$COMMAND"
sleep 1 # this is obviously the perfect way to handle race conditions that may occur
sudo umount "$MOUNTDIR"
unbind_mount_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment