Easy Chroot 0.3.1
[easy-deb-chroot] / fremantle / easy-chroot / .py2deb_build_folder / 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 # Strip off a trailing slash
50 LASTCHAR=`echo $MNTPT | cut -c ${#MNTPT}`
51 if [ "$LASTCHAR" = "/" ] ; then
52    echo "..stripping trailing slash..." >/dev/stderr
53    MNTPT=`echo $MNTPT | cut -c 0-$((${#MNTPT}-1))`
54 fi
55
56 #Check to see if already mounted
57 if [ -f "$MNTPT/var/lock/qmount-complete" ] ; then
58   echo "$MNTPT has a qmount already!" >/dev/stderr
59   MTDIMGFILE=`cat $MNTPT/var/lock/qmount-complete`
60   if [ "$IMGFILE" != "$MTDIMGFILE" ] ; then
61     echo $MTDIMGFILE already mounted here! >/dev/stderr
62     MSG1=`printf "Mount problem!\n\n$MTDIMGFILE already mounted on $MNTPT"`
63        if [ ! -f "/usr/bin/gxmessage" ] ; then
64          echo $MSG1 >/dev/stderr
65        else
66          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
67        fi
68     exit 9
69     # Instead of failing, we could unmount instead...
70     # echo Unmounting...
71     # closechroot $MNTPT
72   else
73     echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
74     exit 1
75   fi
76 fi 
77
78 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
79  echo "Mounting..."
80  if [ "$IMGFILE" != "none" ] ; then
81
82    if [ -f "$IMGFILE" ] ; then
83      LOOP=loop,
84      echo "using image file: $IMGFILE" >/dev/stderr
85      if [ "x$IMGFS" = x ] ; then
86        IMGFS=`echo $IMGFILE | awk -F '.' '{print $NF}'`
87        echo "fs type is $IMGFS" >/dev/stderr
88      fi
89    else
90      LOOP=
91      echo "using device: $IMGFILE" >/dev/stderr
92      PARTINFO="`blkid -s TYPE $IMGFILE`"
93      if [ "x$IMGFS" = x ] ; then
94        IMGFS=`echo $PARTINFO | awk '{print $NF}' | awk -F '=' '{print $NF}' | sed s/\"//g`
95      fi
96    fi
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 [ -d "/lib/modules/`uname -r`" ] ; then
116      MODULEPATH="/lib/modules/`uname -r`"
117    else
118      MODULEPATH=/lib/modules/2.6.28-omap1
119    fi
120
121    if [ "$LOOP" = "loop," ] ; then
122
123    # Check for dm-loop kernel module and dmlosetup command. 
124    # If found, then use dm-loop instead of regular loop.
125
126      if [ -f "$MODULEPATH/dm-loop.ko" ] && [ -f "/sbin/dmlosetup" ]; then
127
128        # use dm-loop
129
130        echo "mounting $IMGFILE on the turbo-loop ;)"
131        #insmod $MODULEPATH/dm-mod.ko 2>/dev/null
132        modprobe dm-mod
133        #insmod $MODULEPATH/dm-loop.ko 2>/dev/null
134        modprobe dm-loop
135
136        NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
137        if [ "x$NEXTLOOP" = "x"  ] ; then
138          NEXTLOOP=0
139        fi
140
141        DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
142        MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime,nobh,nodiratime 2>&1` 
143
144      else
145
146        # use "regular" loop
147
148        echo "mounting $IMGFILE on loop" >/dev/stderr
149        NEXTLOOP=`mount | grep loop | tail -1 | awk '{print $1}' | awk -F "/" '{print $3}' | cut -c 5-6 | awk '{print $0+1}'`
150        if [ "x$NEXTLOOP" = "x"  ] ; then
151          NEXTLOOP=0
152        fi
153
154        DMLOMSG=""
155        MNTMSG=`mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o loop,noatime,nobh,nodiratime 2>&1` 
156
157      fi
158
159      if [ "$?" != 0 ] ; then
160        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
161        if [ ! -f "/usr/bin/gxmessage" ] ; then
162          echo $MSG1 >/dev/stderr
163        else
164          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
165        fi
166        exit 2
167      fi
168      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
169    else
170      echo "mounting device: $IMGFILE" >/dev/stderr
171      if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime,nobh,nodiratime ; then
172        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
173        if [ ! -f "/usr/bin/gxmessage" ] ; then
174          echo $MSG1 >/dev/stderr
175        else
176          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
177        fi
178        exit 3
179      fi
180
181    fi
182
183   else
184     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
185   fi 
186
187 #All set up. Set flag for next time...
188
189  if [ ! -d "$MNTPT/var/lock" ] ; then
190    mkdir -p "$MNTPT/var/lock"
191  fi
192
193  # Place any commands you wish to run the first time you mount
194  # into the $MNTPT/var/run/onmount.rc file
195
196   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
197     . "$MNTPT/var/run/onmount.rc"
198   fi
199
200  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
201  exit 0
202
203 fi
204
205 exit 1