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 #Check to see if already mounted
50 if [ -f "$MNTPT/var/lock/qmount-complete" ] ; then
51 echo "$MNTPT has a qmount already!" >/dev/stderr
52 MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
53 if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
54 echo $MTDIMGFILE already mounted here! >/dev/stderr
55 MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
56 if [ ! -f "/usr/bin/gxmessage" ] ; then
57 echo $MSG1 >/dev/stderr
59 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
62 # Instead of failing, we could unmount instead...
66 echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
71 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
73 if [ "$IMGFILE" != "none" ] ; then
75 if [ -f "$IMGFILE" ] ; then
77 echo "using image file: $IMGFILE" >/dev/stderr
78 if [ "x$IMGFS" = x ] ; then
79 IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
80 echo "fs type is $IMGFS" >/dev/stderr
84 echo "using device: $IMGFILE" >/dev/stderr
85 PARTINFO="`blkid -s TYPE $IMGFILE`"
86 if [ "x$IMGFS" = x ] ; then
87 IMGFS=`echo $PARTINFO | awk '{print $NF}' | awk -F '=' '{print $NF}' | sed s/\"//g`
91 #if [ -d "/mnt/initfs/lib/modules/`uname -r`" ] ; then
92 # MODULEPATH="/mnt/initfs/lib/modules/`uname -r`"
94 # MODULEPATH=/mnt/initfs/lib/modules/2.6.21-omap1
96 #insmod "$MODULEPATH/mbcache.ko" 2>/dev/null
100 if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
101 echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
105 echo "Using $IMGFS file system"
106 if [ "$IMGFS" = ext3 ] ; then
107 #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
111 #insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
114 if [ "$LOOP" = "loop," ] ; then
115 #echo "mounting $IMGFILE on the turbo-loop ;)"
116 echo "mounting $IMGFILE on the slow Fremantle-loop ;)" >/dev/stderr
117 #insmod /lib/modules/2.6.21-omap1/dm-mod.ko 2>/dev/null
118 #insmod /lib/modules/2.6.21-omap1/dm-loop.ko 2>/dev/null
119 #NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
121 if [ "x$NEXTLOOP" = "x" ] ; then
124 #DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
126 #MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime 2>&1`
127 MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime 2>&1`
128 if [ "$?" != 0 ] ; then
129 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
130 if [ ! -f "/usr/bin/gxmessage" ] ; then
131 echo $MSG1 >/dev/stderr
133 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
137 echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
139 echo "mounting device: $IMGFILE" >/dev/stderr
140 if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime ; then
141 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
142 if [ ! -f "/usr/bin/gxmessage" ] ; then
143 echo $MSG1 >/dev/stderr
145 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
153 echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
156 #All set up. Set flag for next time...
158 if [ ! -d "$MNTPT/var/lock" ] ; then
159 mkdir -p "$MNTPT/var/lock"
162 # Place any commands you wish to run the first time you mount
163 # into the $MNTPT/var/run/onmount.rc file
165 if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
166 . "$MNTPT/var/run/onmount.rc"
169 echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"