799ae5bfa181049f56f47815df2c912799716d18
[easy-deb-chroot] / fremantle / 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 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   #ArchLinux suggestions
49   mount -o bind /dev/shm "$CHROOT/dev/shm"
50   mount -o bind /sys "$CHROOT/sys"
51
52   #Any external devices
53   echo ... >/dev/stderr
54   MNTD=`cat /proc/mounts | grep ' /media/' | awk '{print $2}'`
55
56   for MDRV in $MNTD ; do
57     if [ ! -d "$CHROOT$MDRV" ] ; then
58       mkdir -p "$CHROOT$MDRV"
59     fi
60     mount -o bind "$MDRV" "$CHROOT$MDRV"
61   done
62
63   #Mount the user's home dir
64   echo .... >/dev/stderr
65   #mount -o bind /home/user "$CHROOT/home/user"
66
67   # Do it the Fremantle way.
68   mount /dev/mmcblk0p2 "$CHROOT/home"
69   mount /dev/mmcblk0p1 "$CHROOT/home/user/MyDocs"
70   
71   #Make DBus work
72   mount -o bind /var/run/dbus "$CHROOT/var/run/dbus"
73   mount -o bind /var/lib/dbus "$CHROOT/var/lib/dbus"
74
75   #Make pulseaudio work
76   mount -o bind /var/run/pulse "$CHROOT/var/run/pulse"
77
78   #Speed hacks: lower the priority of processes
79   #renice 0 `pidof mmcqd`
80   #renice 20 `pidof trackerd`
81
82   # Sync the chroot if requested...
83   if [ -f /home/user/.synchroot ] ; then 
84     /sbin/synchroot $CHROOT
85     rm /home/user/.synchroot
86   fi
87
88   # Place any commands you wish to run the first time you chroot
89   # into the /var/run/onfirstchroot-ext.rc file (inside your rootfs)
90
91   if [ -f "$CHROOT/var/run/onfirstchroot-ext.rc" ] ; then
92     . "$CHROOT/var/run/onfirstchroot-ext.rc"
93   fi
94
95   # Place any commands you wish to run from inside the chroot 
96   # the first time you chroot into the /var/run/onfirstchroot.rc
97   # file (inside your rootfs)
98
99   if [ -f "$CHROOT/var/run/onfirstchroot.rc" ] ; then
100     chroot $CHROOT "/var/run/onfirstchroot.rc"
101   fi
102
103 fi
104
105 # Place any commands you wish to run every time you chroot
106 # into the /var/run/onchroot-ext.rc file (inside your rootfs)
107
108 if [ -f "$CHROOT/var/run/onchroot-ext.rc" ] ; then
109   . "$CHROOT/var/run/onchroot-ext.rc"
110 fi
111
112 # Place any commands you wish to run from inside the chroot
113 # every time you chroot into the /var/run/onchroot.rc
114 # file (inside your rootfs)
115
116 if [ -f "$CHROOT/var/run/onchroot.rc" ] ; then
117   chroot $CHROOT "/var/run/onchroot.rc"
118 fi
119
120 #All set up. Set flag for next time...
121
122 if [ ! -d "$CHROOT/var/lock" ] ; then
123   mkdir -p "$CHROOT/var/lock"
124 fi
125
126 trap "rm -f $CHROOT/var/lock/chroot-complete ; echo -ne '\033]0;osso_xterm\007' ; exit" INT TERM EXIT
127 echo $IMGFILE $@ > "$CHROOT/var/lock/chroot-complete"
128
129 #If the file is mounted on loop, use that label.
130
131 #Custom prompt and xterm title. Reduces confusion.
132 CHRLABEL=`blkid -s LABEL $IMGFILE | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
133
134 #If the file is mounted on loop, use that label.
135 if [ "x$CHRLABEL" = "x" ] ; then
136   CHRLABEL=`blkid -s LABEL /dev/loop0 | cut -d' ' -f2 | cut -d'=' -f2 | sed 's/"//g'`
137 fi
138
139 if [ "x$CHRLABEL" = "x" ] ; then
140   CHRLABEL=chroot
141 fi
142
143 echo -ne "\033]0;$CHRLABEL\007" >/dev/stderr
144 export PS1="[\u@$CHRLABEL: \w]"
145
146 #Actually chroot
147 echo "Everything set up, running chroot..." >/dev/stderr
148 chroot $CHROOT "$@"
149
150 #All done, reset.
151 exit 0
152