Added py2deb scripts for fremantle easy-deb-chroot and diablo easy-chroot.
[easy-deb-chroot] / fremantle / easy-deb-chroot / thp / sbin / qchroot
1 #!/bin/sh
2 # Sets up (if necessary) and chroots into a different environment.
3 # Expects root privileges, does not drop them. 
4
5 # By Alan M Bruce (qole) with help from Benson Mitchell
6 #
7 # GPL licensed; keep code free!
8
9 # This script should have a wrapper to set up extra variables,
10 # OR, it can be run as a command:
11 # ezchroot <part/file/'none'> <chroot dir> <command> <params...>
12
13 if [ "`whoami`" != "root" ] ; then
14   echo "please run me as root!"
15   exit 9
16 fi
17
18 IMGFILE=$1
19 shift 1
20
21 CHROOT=$1
22 shift 1
23
24 # echo ezchroot $IMGFILE $CHROOT $*
25
26 qmount $IMGFILE $CHROOT
27 MOUNTSUCCESS=$?
28
29 if [ "$MOUNTSUCCESS" != "1" ] && [ "$MOUNTSUCCESS" != "0" ] ; then
30   echo Cancelling chroot...
31   exit $MOUNTSUCCESS
32 fi
33
34 if [ "$MOUNTSUCCESS" = "0" ] ; then
35   #Make the tablet's devices available to the chroot
36   echo . >/dev/stderr
37   mount -o bind /dev "$CHROOT/dev"
38   mount -o bind /proc "$CHROOT/proc"
39
40   #Gentoo wiki says this will make X work
41   echo .. >/dev/stderr
42   mount -t devpts none "$CHROOT/dev/pts"
43   mount -o bind /tmp "$CHROOT/tmp"
44
45   #Open e-mail attachments, etc
46   mount -o bind /var/tmp "$CHROOT/var/tmp"
47
48   #Any external devices
49   echo ... >/dev/stderr
50   MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`
51
52   for MDRV in $MNTD ; do
53     if [ ! -d "$CHROOT$MDRV" ] ; then
54       mkdir -p "$CHROOT$MDRV"
55     fi
56     mount -o bind "$MDRV" "$CHROOT$MDRV"
57   done
58
59   #Mount the user's home dir
60   echo .... >/dev/stderr
61   mount -o bind /home/user "$CHROOT/home/user"
62
63   #Make DBus work
64   mount -o bind /var/run/dbus "$CHROOT/var/run/dbus"
65
66   #Speed hacks: lower the priority of processes
67   renice 0 `pidof mmcqd`
68   renice 20 `pidof metalayer-crawler`
69
70   # Sync the chroot if requested...
71   if [ -f /home/user/.synchroot ] ; then 
72     /sbin/synchroot $CHROOT
73     rm /home/user/.synchroot
74   fi
75
76   # Place any commands you wish to run the first time you chroot
77   # into the /var/run/onfirstchroot-ext.rc file (inside your rootfs)
78
79   if [ -f "$CHROOT/var/run/onfirstchroot-ext.rc" ] ; then
80     . "$CHROOT/var/run/onfirstchroot-ext.rc"
81   fi
82
83   # Place any commands you wish to run from inside the chroot 
84   # the first time you chroot into the /var/run/onfirstchroot.rc
85   # file (inside your rootfs)
86
87   if [ -f "$CHROOT/var/run/onfirstchroot.rc" ] ; then
88     chroot $CHROOT "/var/run/onfirstchroot.rc"
89   fi
90
91 fi
92
93 # Place any commands you wish to run every time you chroot
94 # into the /var/run/onchroot-ext.rc file (inside your rootfs)
95
96 if [ -f "$CHROOT/var/run/onchroot-ext.rc" ] ; then
97   . "$CHROOT/var/run/onchroot-ext.rc"
98 fi
99
100 # Place any commands you wish to run from inside the chroot
101 # every time you chroot into the /var/run/onchroot.rc
102 # file (inside your rootfs)
103
104 if [ -f "$CHROOT/var/run/onchroot.rc" ] ; then
105   chroot $CHROOT "/var/run/onchroot.rc"
106 fi
107
108 #All set up. Set flag for next time...
109
110 if [ ! -d "$CHROOT/var/lock" ] ; then
111   mkdir -p "$CHROOT/var/lock"
112 fi
113
114 trap "rm -f $CHROOT/var/lock/chroot-complete ; echo -ne '\033]0;osso_xterm\007' ; exit" INT TERM EXIT
115 echo $IMGFILE $@ > "$CHROOT/var/lock/chroot-complete"
116
117 #Custom prompt and xterm title. Reduces confusion.
118 CHRLABEL=`blkid -s LABEL $IMGFILE | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
119 if [ "x$CHRLABEL" = "x" ] ; then
120   CHRLABEL=chroot
121 fi
122 echo -ne "\033]0;$CHRLABEL\007" >/dev/stderr
123 export PS1="[\u@$CHRLABEL: \w]"
124
125 #Actually chroot
126 echo "Everything set up, running chroot..." >/dev/stderr
127 chroot $CHROOT "$@"
128
129 #All done, reset.
130 exit 0
131