d9e0238fb4d9dfcbc6f25d2347c5fdeb03794288
[easy-deb-chroot] / fremantle / easy-chroot / src / sbin / qmount
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 # Hacked roughly to work with Fremantle. Help always appreciated. I can be found on talk.maemo.org.
10
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>
14
15 if [ "`whoami`" != "root" ] ; then
16   echo "please run me as root!"
17   exit 9
18 fi
19
20 IMGFILE=$1
21 MNTPT=$2
22
23 # echo qmount $IMGFILE $MNTPT
24
25 #Ensure that we have an image or partition to mount
26
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
31   else
32     gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
33   fi
34   exit 9
35 fi
36
37 #Ensure that we have a chroot directory to mount the image or partition on
38
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
43   else
44     gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
45   fi
46   exit 9
47 fi
48
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
58        else
59          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
60        fi
61     exit 9
62     # Instead of failing, we could unmount instead...
63     # echo Unmounting...
64     # closechroot $MNTPT
65   else
66     echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
67     exit 1
68   fi
69 fi 
70
71 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
72  echo "Mounting..."
73  if [ "$IMGFILE" != "none" ] ; then
74
75    if [ -f "$IMGFILE" ] ; then
76      LOOP=loop,
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
81      fi
82    else
83      LOOP=
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`
88      fi
89    fi
90
91    #if [ -d "/mnt/initfs/lib/modules/`uname -r`" ] ; then
92    #  MODULEPATH="/mnt/initfs/lib/modules/`uname -r`"
93    #else
94    #  MODULEPATH=/mnt/initfs/lib/modules/2.6.21-omap1
95    #fi
96    #insmod "$MODULEPATH/mbcache.ko" 2>/dev/null
97
98    modprobe mbcache
99
100    if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
101      echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
102      IMGFS=ext2
103    fi
104
105    echo "Using $IMGFS file system"
106    if [ "$IMGFS" = ext3 ] ; then
107      #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
108      modprobe jbd
109    fi
110
111    #insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
112    modprobe $IMGFS
113
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}'`
120      NEXTLOOP=""
121      if [ "x$NEXTLOOP" = "x"  ] ; then
122        NEXTLOOP=0
123      fi
124      #DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
125      DMLOMSG=""
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
132        else
133          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
134        fi
135        exit 2
136      fi
137      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
138    else
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
144        else
145          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
146        fi
147        exit 3
148      fi
149
150    fi
151
152   else
153     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
154   fi 
155
156 #All set up. Set flag for next time...
157
158  if [ ! -d "$MNTPT/var/lock" ] ; then
159    mkdir -p "$MNTPT/var/lock"
160  fi
161
162  # Place any commands you wish to run the first time you mount
163  # into the $MNTPT/var/run/onmount.rc file
164
165   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
166     . "$MNTPT/var/run/onmount.rc"
167   fi
168
169  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
170  exit 0
171
172 fi
173
174 exit 1