f44d3dbdaac532cf28b6b5042abeb2ce11c54abf
[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    # missing in Fremantle...
92    # modprobe mbcache
93
94    if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
95      echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
96      IMGFS=ext2
97    fi
98
99    echo "Using $IMGFS file system"
100    if [ "$IMGFS" = ext3 ] ; then
101      #insmod "$MODULEPATH/jbd.ko" 2>/dev/null
102      modprobe jbd
103    fi
104
105    #insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
106    modprobe $IMGFS
107
108    if [ -d "/lib/modules/`uname -r`" ] ; then
109      MODULEPATH="/lib/modules/`uname -r`"
110    else
111      MODULEPATH=/lib/modules/2.6.28-omap1
112    fi
113
114    if [ "$LOOP" = "loop," ] ; then
115
116    # Check for dm-loop kernel module and dmlosetup command. 
117    # If found, then use dm-loop instead of regular loop.
118
119      if [ -f "$MODULEPATH/dm-loop.ko" ] && [ -f "/sbin/dmlosetup" ]; then
120
121        # use dm-loop
122
123        echo "mounting $IMGFILE on the turbo-loop ;)"
124        #insmod $MODULEPATH/dm-mod.ko 2>/dev/null
125        modprobe dm-mod
126        #insmod $MODULEPATH/dm-loop.ko 2>/dev/null
127        modprobe dm-loop
128
129        NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
130        if [ "x$NEXTLOOP" = "x"  ] ; then
131          NEXTLOOP=0
132        fi
133
134        DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
135        MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime,nobh,nodiratime 2>&1` 
136
137      else
138
139        # use "regular" loop
140
141        echo "mounting $IMGFILE on loop" >/dev/stderr
142        NEXTLOOP=""
143        if [ "x$NEXTLOOP" = "x"  ] ; then
144          NEXTLOOP=0
145        fi
146
147        DMLOMSG=""
148        MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime,nobh,nodiratime 2>&1` 
149
150      fi
151
152      if [ "$?" != 0 ] ; then
153        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
154        if [ ! -f "/usr/bin/gxmessage" ] ; then
155          echo $MSG1 >/dev/stderr
156        else
157          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
158        fi
159        exit 2
160      fi
161      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
162    else
163      echo "mounting device: $IMGFILE" >/dev/stderr
164      if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime,nobh,nodiratime ; then
165        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
166        if [ ! -f "/usr/bin/gxmessage" ] ; then
167          echo $MSG1 >/dev/stderr
168        else
169          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
170        fi
171        exit 3
172      fi
173
174    fi
175
176   else
177     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
178   fi 
179
180 #All set up. Set flag for next time...
181
182  if [ ! -d "$MNTPT/var/lock" ] ; then
183    mkdir -p "$MNTPT/var/lock"
184  fi
185
186  # Place any commands you wish to run the first time you mount
187  # into the $MNTPT/var/run/onmount.rc file
188
189   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
190     . "$MNTPT/var/run/onmount.rc"
191   fi
192
193  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
194  exit 0
195
196 fi
197
198 exit 1