2 # Sets up (if necessary) and chroots into a different environment.
3 # Expects root privileges, does not drop them.
5 # By Alan M Bruce (qole) with help from Benson Mitchell and Thomas Perl
7 # GPL licensed; keep code free!
9 # Hacked roughly to work with Fremantle. Help always appreciated. I can be found on talk.maemo.org.
11 # This script should have a wrapper to set up extra variables,
12 # OR, it can be run as a command:
13 # qmount <partition/file/'none'> <mountpoint>
15 if [ "`whoami`" != "root" ] ; then
16 echo "please run me as root!"
23 # echo qmount $IMGFILE $MNTPT
25 #Ensure that we have an image or partition to mount
27 if [ ! -f "$IMGFILE" ] && [ ! -b "$IMGFILE" ] ; then
28 MSG1=`printf "ERROR!\n\nThe image specified ($IMGFILE) does not exist or is neither\na regular nor a block special file.\n\nFirst parameter must be an image file or partition"`
29 if [ ! -f "/usr/bin/gxmessage" ] ; then
30 echo $MSG1 >/dev/stderr
32 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
37 #Ensure that we have a chroot directory to mount the image or partition on
39 if [ "x$MNTPT" = "x" ] || [ "x`echo $MNTPT | grep '/'`" = "x" ] ; then
40 MSG1=`printf "ERROR!\n\nNo chroot directory specified!\n\nSecond parameter must be chroot dir (eg. /debian)"`
41 if [ ! -f "/usr/bin/gxmessage" ] ; then
42 echo $MSG1 >/dev/stderr
44 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
49 # Strip off a trailing slash
50 LASTCHAR=`echo $MNTPT | cut -c ${#MNTPT}`
51 if [ "$LASTCHAR" = "/" ] ; then
52 echo "..stripping trailing slash..." >/dev/stderr
53 MNTPT=`echo $MNTPT | cut -c 0-$((${#MNTPT}-1))`
56 #Check to see if already mounted
57 if [ -f "$MNTPT/var/lock/qmount-complete" ] ; then
58 echo "$MNTPT has a qmount already!" >/dev/stderr
59 MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
60 if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
61 echo $MTDIMGFILE already mounted here! >/dev/stderr
62 MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
63 if [ ! -f "/usr/bin/gxmessage" ] ; then
64 echo $MSG1 >/dev/stderr
66 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
69 # Instead of failing, we could unmount instead...
73 echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
78 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
80 if [ "$IMGFILE" != "none" ] ; then
82 if [ -f "$IMGFILE" ] ; then
84 echo "using image file: $IMGFILE" >/dev/stderr
85 if [ "x$IMGFS" = x ] ; then
86 IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
87 echo "fs type is $IMGFS" >/dev/stderr
91 echo "using device: $IMGFILE" >/dev/stderr
92 PARTINFO="`blkid -s TYPE $IMGFILE`"
93 if [ "x$IMGFS" = x ] ; then
94 IMGFS=`echo $PARTINFO | awk '{print $NF}' | awk -F '=' '{print $NF}' | sed s/\"//g`
98 # missing in Fremantle...
101 if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
102 echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
106 echo "Using $IMGFS file system"
107 if [ "$IMGFS" = ext3 ] ; then
108 #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
112 #insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
115 if [ -d "/lib/modules/`uname -r`" ] ; then
116 MODULEPATH="/lib/modules/`uname -r`"
118 MODULEPATH=/lib/modules/2.6.28-omap1
121 if [ "$LOOP" = "loop," ] ; then
123 # Check for dm-loop kernel module and dmlosetup command.
124 # If found, then use dm-loop instead of regular loop.
126 if [ -f "$MODULEPATH/dm-loop.ko" ] && [ -f "/sbin/dmlosetup" ]; then
130 echo "mounting $IMGFILE on the turbo-loop ;)"
131 #insmod $MODULEPATH/dm-mod.ko 2>/dev/null
133 #insmod $MODULEPATH/dm-loop.ko 2>/dev/null
136 NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
137 if [ "x$NEXTLOOP" = "x" ] ; then
141 DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
142 MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime,nobh,nodiratime 2>&1`
148 echo "mounting $IMGFILE on loop" >/dev/stderr
149 NEXTLOOP=`mount | grep loop | tail -1 | awk '{print $1}' | awk -F "/" '{print $3}' | cut -c 5-6 | awk '{print $0+1}'`
150 if [ "x$NEXTLOOP" = "x" ] ; then
155 MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime,nobh,nodiratime 2>&1`
159 if [ "$?" != 0 ] ; then
160 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
161 if [ ! -f "/usr/bin/gxmessage" ] ; then
162 echo $MSG1 >/dev/stderr
164 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
168 echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
170 echo "mounting device: $IMGFILE" >/dev/stderr
171 if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime,nobh,nodiratime ; then
172 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
173 if [ ! -f "/usr/bin/gxmessage" ] ; then
174 echo $MSG1 >/dev/stderr
176 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
184 echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
187 #All set up. Set flag for next time...
189 if [ ! -d "$MNTPT/var/lock" ] ; then
190 mkdir -p "$MNTPT/var/lock"
193 # Place any commands you wish to run the first time you mount
194 # into the $MNTPT/var/run/onmount.rc file
196 if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
197 . "$MNTPT/var/run/onmount.rc"
200 echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"