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 # This script should have a wrapper to set up extra variables,
10 # OR, it can be run as a command:
11 # qmount <part/file/'none'> <mountpoint>
13 if [ "`whoami`" != "root" ] ; then
14 echo "please run me as root!"
21 # echo qmount $IMGFILE $MNTPT
23 #Ensure that we have an image or partition to mount
25 if [ ! -f "$IMGFILE" ] && [ ! -b "$IMGFILE" ] ; then
26 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"`
27 if [ ! -f "/usr/bin/gxmessage" ] ; then
28 echo $MSG1 >/dev/stderr
30 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
35 #Ensure that we have a chroot directory to mount the image or partition on
37 if [ "x$MNTPT" = "x" ] || [ "x`echo $MNTPT | grep '/'`" = "x" ] ; then
38 MSG1=`printf "ERROR!\n\nNo chroot directory specified!\n\nSecond parameter must be chroot dir (eg. /debian)"`
39 if [ ! -f "/usr/bin/gxmessage" ] ; then
40 echo $MSG1 >/dev/stderr
42 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
47 #Check to see if already mounted
48 if [ -f "$MNTPT/var/lock/qmount-complete" ] ; then
49 echo "$MNTPT has a qmount already!" >/dev/stderr
50 MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
51 if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
52 echo $MTDIMGFILE already mounted here! >/dev/stderr
53 MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
54 if [ ! -f "/usr/bin/gxmessage" ] ; then
55 echo $MSG1 >/dev/stderr
57 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
60 # Instead of failing, we could unmount instead...
64 echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
69 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
70 echo "Mounting..." >/dev/stderr
71 if [ "$IMGFILE" != "none" ] ; then
73 if [ -f "$IMGFILE" ] ; then
75 echo "using image file: $IMGFILE" >/dev/stderr
76 if [ "x$IMGFS" = x ] ; then
77 IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
78 echo "fs type is $IMGFS" >/dev/stderr
82 echo "using device: $IMGFILE" >/dev/stderr
83 PARTINFO="`blkid -s TYPE $IMGFILE`"
84 if [ "x$IMGFS" = x ] ; then
85 IMGFS=`echo $PARTINFO | awk '{print $NF}' | awk -F '=' '{print $NF}' | sed s/\"//g`
89 if [ -d "/mnt/initfs/lib/modules/`uname -r`" ] ; then
90 MODULEPATH="/mnt/initfs/lib/modules/`uname -r`"
92 MODULEPATH=/mnt/initfs/lib/modules/2.6.21-omap1
94 insmod "$MODULEPATH/mbcache.ko" 2>/dev/null
96 if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
97 echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
101 echo "Using $IMGFS file system" >/dev/stderr
102 if [ "$IMGFS" = ext3 ] ; then
103 insmod "$MODULEPATH/jbd.ko" 2>/dev/null
106 insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
108 if [ "$LOOP" = "loop," ] ; then
109 echo "mounting $IMGFILE on the turbo-loop ;)" >/dev/stderr
111 if [ -d "/lib/modules/`uname -r`/extra" ] ; then
112 DMODULEPATH="/lib/modules/`uname -r`/extra"
114 DMODULEPATH=/lib/modules/2.6.21-omap1/extra
117 insmod $DMODULEPATH/dm-mod.ko 2>/dev/null
118 insmod $DMODULEPATH/dm-loop.ko 2>/dev/null
120 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`
125 MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime 2>&1`
126 if [ "$?" != 0 ] ; then
127 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
128 if [ ! -f "/usr/bin/gxmessage" ] ; then
129 echo $MSG1 >/dev/stderr
131 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
135 echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
137 echo "mounting device: $IMGFILE" >/dev/stderr
138 if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime ; then
139 MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
140 if [ ! -f "/usr/bin/gxmessage" ] ; then
141 echo $MSG1 >/dev/stderr
143 gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
151 echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
154 #All set up. Set flag for next time...
156 if [ ! -d "$MNTPT/var/lock" ] ; then
157 mkdir -p "$MNTPT/var/lock"
160 # Place any commands you wish to run the first time you mount
161 # into the $MNTPT/var/run/onmount.rc file
163 if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
164 . "$MNTPT/var/run/onmount.rc"
167 echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"