Easy Chroot 0.3.1
[easy-deb-chroot] / fremantle / easy-chroot / .py2deb_build_folder / easy-chroot / src / sbin / qchroot
diff --git a/fremantle/easy-chroot/.py2deb_build_folder/easy-chroot/src/sbin/qchroot b/fremantle/easy-chroot/.py2deb_build_folder/easy-chroot/src/sbin/qchroot
new file mode 100755 (executable)
index 0000000..8955351
--- /dev/null
@@ -0,0 +1,163 @@
+#!/bin/sh
+# Sets up (if necessary) and chroots into a different environment.
+# Expects root privileges, does not drop them. 
+
+# By Alan M Bruce (qole) with help from Benson Mitchell and Thomas Perl
+#
+# GPL licensed; keep code free!
+
+# This script should have a wrapper to set up extra variables,
+# OR, it can be run as a command:
+# ezchroot <part/file/'none'> <chroot dir> <command> <params...>
+
+if [ "`whoami`" != "root" ] ; then
+  echo "please run me as root!"
+  exit 9
+fi
+
+IMGFILE=$1
+shift 1
+
+CHROOT=$1
+shift 1
+
+# echo ezchroot $IMGFILE $CHROOT $*
+
+# Strip off a trailing slash
+LASTCHAR=`echo $CHROOT | cut -c ${#CHROOT}`
+if [ "$LASTCHAR" = "/" ] ; then
+   echo "..stripping trailing slash..." >/dev/stderr
+   CHROOT=`echo $CHROOT | cut -c 0-$((${#CHROOT}-1))`
+fi
+
+qmount $IMGFILE $CHROOT
+MOUNTSUCCESS=$?
+
+if [ "$MOUNTSUCCESS" != "1" ] && [ "$MOUNTSUCCESS" != "0" ] ; then
+  echo Cancelling chroot...
+  exit $MOUNTSUCCESS
+fi
+
+if [ "$MOUNTSUCCESS" = "0" ] ; then
+  #Make the tablet's devices available to the chroot
+  echo . >/dev/stderr
+  mount -o bind /dev "$CHROOT/dev"
+  mount -o bind /proc "$CHROOT/proc"
+
+  #Gentoo wiki says this will make X work
+  echo .. >/dev/stderr
+  mount -t devpts none "$CHROOT/dev/pts"
+  mount -o bind /tmp "$CHROOT/tmp"
+
+  #Open e-mail attachments, etc
+  mount -o bind /var/tmp "$CHROOT/var/tmp"
+
+  #ArchLinux suggestions
+  mount -o bind /dev/shm "$CHROOT/dev/shm"
+  mount -o bind /sys "$CHROOT/sys"
+
+  #Any external devices
+  echo ... >/dev/stderr
+  MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`
+
+  for MDRV in $MNTD ; do
+    if [ ! -d "$CHROOT$MDRV" ] ; then
+      mkdir -p "$CHROOT$MDRV"
+    fi
+    mount -o bind "$MDRV" "$CHROOT$MDRV"
+  done
+
+  #Mount the user's home dir
+  echo .... >/dev/stderr
+  #mount -o bind /home/user "$CHROOT/home/user"
+
+  # Do it the Fremantle way.
+  mount /dev/mmcblk0p2 "$CHROOT/home"
+  mount /dev/mmcblk0p1 "$CHROOT/home/user/MyDocs"
+  
+  #Make DBus work
+  mount -o bind /var/run/dbus "$CHROOT/var/run/dbus"
+  mount -o bind /var/lib/dbus "$CHROOT/var/lib/dbus"
+
+  #Make pulseaudio work
+  mount -o bind /var/run/pulse "$CHROOT/var/run/pulse"
+
+  #Speed hacks: lower the priority of processes
+  #renice 0 `pidof mmcqd`
+  #renice 20 `pidof trackerd`
+
+  # Sync the chroot if requested...
+  if [ -f /home/user/.synchroot ] ; then 
+    /sbin/synchroot $CHROOT
+    rm /home/user/.synchroot
+  fi
+
+  # Place any commands you wish to run the first time you chroot
+  # into the /var/run/onfirstchroot-ext.rc file (inside your rootfs)
+
+  if [ -f "$CHROOT/var/run/onfirstchroot-ext.rc" ] ; then
+    . "$CHROOT/var/run/onfirstchroot-ext.rc"
+  fi
+
+  # Place any commands you wish to run from inside the chroot 
+  # the first time you chroot into the /var/run/onfirstchroot.rc
+  # file (inside your rootfs)
+
+  if [ -f "$CHROOT/var/run/onfirstchroot.rc" ] ; then
+    chroot $CHROOT "/var/run/onfirstchroot.rc"
+  fi
+
+fi
+
+# Place any commands you wish to run every time you chroot
+# into the /var/run/onchroot-ext.rc file (inside your rootfs)
+
+if [ -f "$CHROOT/var/run/onchroot-ext.rc" ] ; then
+  . "$CHROOT/var/run/onchroot-ext.rc"
+fi
+
+# Place any commands you wish to run from inside the chroot
+# every time you chroot into the /var/run/onchroot.rc
+# file (inside your rootfs)
+
+if [ -f "$CHROOT/var/run/onchroot.rc" ] ; then
+  chroot $CHROOT "/var/run/onchroot.rc"
+fi
+
+#All set up. Set flag for next time...
+
+if [ ! -d "$CHROOT/var/lock" ] ; then
+  mkdir -p "$CHROOT/var/lock"
+fi
+
+trap "rm -f $CHROOT/var/lock/chroot-complete ; echo -ne '\033]0;osso_xterm\007' ; exit" INT TERM EXIT
+echo $IMGFILE $@ > "$CHROOT/var/lock/chroot-complete"
+
+#Custom prompt and xterm title. Reduces confusion.
+CHRLABEL=`blkid -s LABEL $IMGFILE | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
+
+#If the file is mounted on loop, use that label.
+if [ "x$CHRLABEL" = "x" ] ; then
+
+  # first find the loop
+  LOOPNO=`mount | grep loop | grep $CHROOT | awk '{print $1}'`
+
+  if [ "x$LOOPNO" != "x" ] ; then
+     CHRLABEL=`blkid -s LABEL $LOOPNO | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
+  fi
+fi
+
+if [ "x$CHRLABEL" = "x" ] ; then
+  CHRLABEL=chroot
+fi
+
+echo -ne "\033]0;$CHRLABEL\007" >/dev/stderr
+export PS1="[\u@$CHRLABEL: \w]"
+
+#Actually chroot
+echo "Everything set up, running chroot..." >/dev/stderr
+chroot $CHROOT "$@"
+
+#All done, reset.
+exit 0
+