89553511d8e5899a3b10bc610e4801dd7862a8dd
[easy-deb-chroot] / fremantle / easy-chroot / .py2deb_build_folder / easy-chroot / src / 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 and Thomas Perl
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 # Strip off a trailing slash
27 LASTCHAR=`echo $CHROOT | cut -c ${#CHROOT}`
28 if [ "$LASTCHAR" = "/" ] ; then
29    echo "..stripping trailing slash..." >/dev/stderr
30    CHROOT=`echo $CHROOT | cut -c 0-$((${#CHROOT}-1))`
31 fi
32
33 qmount $IMGFILE $CHROOT
34 MOUNTSUCCESS=$?
35
36 if [ "$MOUNTSUCCESS" != "1" ] && [ "$MOUNTSUCCESS" != "0" ] ; then
37   echo Cancelling chroot...
38   exit $MOUNTSUCCESS
39 fi
40
41 if [ "$MOUNTSUCCESS" = "0" ] ; then
42   #Make the tablet's devices available to the chroot
43   echo . >/dev/stderr
44   mount -o bind /dev "$CHROOT/dev"
45   mount -o bind /proc "$CHROOT/proc"
46
47   #Gentoo wiki says this will make X work
48   echo .. >/dev/stderr
49   mount -t devpts none "$CHROOT/dev/pts"
50   mount -o bind /tmp "$CHROOT/tmp"
51
52   #Open e-mail attachments, etc
53   mount -o bind /var/tmp "$CHROOT/var/tmp"
54
55   #ArchLinux suggestions
56   mount -o bind /dev/shm "$CHROOT/dev/shm"
57   mount -o bind /sys "$CHROOT/sys"
58
59   #Any external devices
60   echo ... >/dev/stderr
61   MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`
62
63   for MDRV in $MNTD ; do
64     if [ ! -d "$CHROOT$MDRV" ] ; then
65       mkdir -p "$CHROOT$MDRV"
66     fi
67     mount -o bind "$MDRV" "$CHROOT$MDRV"
68   done
69
70   #Mount the user's home dir
71   echo .... >/dev/stderr
72   #mount -o bind /home/user "$CHROOT/home/user"
73
74   # Do it the Fremantle way.
75   mount /dev/mmcblk0p2 "$CHROOT/home"
76   mount /dev/mmcblk0p1 "$CHROOT/home/user/MyDocs"
77   
78   #Make DBus work
79   mount -o bind /var/run/dbus "$CHROOT/var/run/dbus"
80   mount -o bind /var/lib/dbus "$CHROOT/var/lib/dbus"
81
82   #Make pulseaudio work
83   mount -o bind /var/run/pulse "$CHROOT/var/run/pulse"
84
85   #Speed hacks: lower the priority of processes
86   #renice 0 `pidof mmcqd`
87   #renice 20 `pidof trackerd`
88
89   # Sync the chroot if requested...
90   if [ -f /home/user/.synchroot ] ; then 
91     /sbin/synchroot $CHROOT
92     rm /home/user/.synchroot
93   fi
94
95   # Place any commands you wish to run the first time you chroot
96   # into the /var/run/onfirstchroot-ext.rc file (inside your rootfs)
97
98   if [ -f "$CHROOT/var/run/onfirstchroot-ext.rc" ] ; then
99     . "$CHROOT/var/run/onfirstchroot-ext.rc"
100   fi
101
102   # Place any commands you wish to run from inside the chroot 
103   # the first time you chroot into the /var/run/onfirstchroot.rc
104   # file (inside your rootfs)
105
106   if [ -f "$CHROOT/var/run/onfirstchroot.rc" ] ; then
107     chroot $CHROOT "/var/run/onfirstchroot.rc"
108   fi
109
110 fi
111
112 # Place any commands you wish to run every time you chroot
113 # into the /var/run/onchroot-ext.rc file (inside your rootfs)
114
115 if [ -f "$CHROOT/var/run/onchroot-ext.rc" ] ; then
116   . "$CHROOT/var/run/onchroot-ext.rc"
117 fi
118
119 # Place any commands you wish to run from inside the chroot
120 # every time you chroot into the /var/run/onchroot.rc
121 # file (inside your rootfs)
122
123 if [ -f "$CHROOT/var/run/onchroot.rc" ] ; then
124   chroot $CHROOT "/var/run/onchroot.rc"
125 fi
126
127 #All set up. Set flag for next time...
128
129 if [ ! -d "$CHROOT/var/lock" ] ; then
130   mkdir -p "$CHROOT/var/lock"
131 fi
132
133 trap "rm -f $CHROOT/var/lock/chroot-complete ; echo -ne '\033]0;osso_xterm\007' ; exit" INT TERM EXIT
134 echo $IMGFILE $@ > "$CHROOT/var/lock/chroot-complete"
135
136 #Custom prompt and xterm title. Reduces confusion.
137 CHRLABEL=`blkid -s LABEL $IMGFILE | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
138
139 #If the file is mounted on loop, use that label.
140 if [ "x$CHRLABEL" = "x" ] ; then
141
142   # first find the loop
143   LOOPNO=`mount | grep loop | grep $CHROOT | awk '{print $1}'`
144
145   if [ "x$LOOPNO" != "x" ] ; then
146      CHRLABEL=`blkid -s LABEL $LOOPNO | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
147   fi
148 fi
149
150 if [ "x$CHRLABEL" = "x" ] ; then
151   CHRLABEL=chroot
152 fi
153
154 echo -ne "\033]0;$CHRLABEL\007" >/dev/stderr
155 export PS1="[\u@$CHRLABEL: \w]"
156
157 #Actually chroot
158 echo "Everything set up, running chroot..." >/dev/stderr
159 chroot $CHROOT "$@"
160
161 #All done, reset.
162 exit 0
163