9a928021cd162b853be5ecada498c2fde30ae1d5
[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    # missing in Fremantle...
99    # modprobe mbcache
100
101    if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
102      echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
103      IMGFS=ext2
104    fi
105
106    echo "Using $IMGFS file system"
107    if [ "$IMGFS" = ext3 ] ; then
108      #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
109      modprobe jbd
110    fi
111
112    #insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
113    modprobe $IMGFS
114
115    if [ "$LOOP" = "loop," ] ; then
116     #echo "mounting $IMGFILE on the turbo-loop ;)"
117     echo "mounting $IMGFILE on loop" >/dev/stderr
118      #insmod /lib/modules/2.6.21-omap1/dm-mod.ko 2>/dev/null
119      #insmod /lib/modules/2.6.21-omap1/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      NEXTLOOP=""
122      if [ "x$NEXTLOOP" = "x"  ] ; then
123        NEXTLOOP=0
124      fi
125      #DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
126      DMLOMSG=""
127      #MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime 2>&1` 
128      MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime 2>&1` 
129      if [ "$?" != 0 ] ; then
130        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
131        if [ ! -f "/usr/bin/gxmessage" ] ; then
132          echo $MSG1 >/dev/stderr
133        else
134          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
135        fi
136        exit 2
137      fi
138      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
139    else
140      echo "mounting device: $IMGFILE" >/dev/stderr
141      if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime ; then
142        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
143        if [ ! -f "/usr/bin/gxmessage" ] ; then
144          echo $MSG1 >/dev/stderr
145        else
146          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
147        fi
148        exit 3
149      fi
150
151    fi
152
153   else
154     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
155   fi 
156
157 #All set up. Set flag for next time...
158
159  if [ ! -d "$MNTPT/var/lock" ] ; then
160    mkdir -p "$MNTPT/var/lock"
161  fi
162
163  # Place any commands you wish to run the first time you mount
164  # into the $MNTPT/var/run/onmount.rc file
165
166   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
167     . "$MNTPT/var/run/onmount.rc"
168   fi
169
170  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
171  exit 0
172
173 fi
174
175 exit 1