Diablo easy-chroot now depending on dm-loop pkgs and building via autobuilder.
[easy-deb-chroot] / diablo / 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 # 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>
12
13 if [ "`whoami`" != "root" ] ; then
14   echo "please run me as root!"
15   exit 9
16 fi
17
18 IMGFILE=$1
19 MNTPT=$2
20
21 # echo qmount $IMGFILE $MNTPT
22
23 #Ensure that we have an image or partition to mount
24
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
29   else
30     gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
31   fi
32   exit 9
33 fi
34
35 #Ensure that we have a chroot directory to mount the image or partition on
36
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
41   else
42     gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
43   fi
44   exit 9
45 fi
46
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
56        else
57          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
58        fi
59     exit 9
60     # Instead of failing, we could unmount instead...
61     # echo Unmounting...
62     # closechroot $MNTPT
63   else
64     echo $MTDIMGFILE already mounted on $MNTPT... >/dev/stderr
65     exit 1
66   fi
67 fi 
68
69 if [ ! -f "$MNTPT/var/lock/qmount-complete" ] ; then
70  echo "Mounting..." >/dev/stderr
71  if [ "$IMGFILE" != "none" ] ; then
72
73    if [ -f "$IMGFILE" ] ; then
74      LOOP=loop,
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
79      fi
80    else
81      LOOP=
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`
86      fi
87    fi
88
89    if [ -d "/mnt/initfs/lib/modules/`uname -r`" ] ; then
90      MODULEPATH="/mnt/initfs/lib/modules/`uname -r`"
91    else
92      MODULEPATH=/mnt/initfs/lib/modules/2.6.21-omap1
93    fi
94    insmod "$MODULEPATH/mbcache.ko" 2>/dev/null
95
96    if [ "$IMGFS" != "ext3" ] && [ "$IMGFS" != "ext2" ] ; then
97      echo "Don't know $IMGFS: Using ext2 file system" >/dev/stderr
98      IMGFS=ext2
99    fi
100
101    echo "Using $IMGFS file system" >/dev/stderr
102    if [ "$IMGFS" = ext3 ] ; then
103      insmod "$MODULEPATH/jbd.ko" 2>/dev/null
104    fi
105
106    insmod "$MODULEPATH/$IMGFS.ko" 2>/dev/null
107
108    if [ "$LOOP" = "loop," ] ; then
109     echo "mounting $IMGFILE on the turbo-loop ;)" >/dev/stderr
110
111     if [ -d "/lib/modules/`uname -r`/extra" ] ; then
112       DMODULEPATH="/lib/modules/`uname -r`/extra"
113     else
114       DMODULEPATH=/lib/modules/2.6.21-omap1/extra
115     fi
116
117     insmod $DMODULEPATH/dm-mod.ko 2>/dev/null
118     insmod $DMODULEPATH/dm-loop.ko 2>/dev/null
119
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
122        NEXTLOOP=0
123      fi
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
130        else
131          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
132        fi
133        exit 2
134      fi
135      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
136    else
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
142        else
143          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
144        fi
145        exit 3
146      fi
147
148    fi
149
150   else
151     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
152   fi 
153
154 #All set up. Set flag for next time...
155
156  if [ ! -d "$MNTPT/var/lock" ] ; then
157    mkdir -p "$MNTPT/var/lock"
158  fi
159
160  # Place any commands you wish to run the first time you mount
161  # into the $MNTPT/var/run/onmount.rc file
162
163   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
164     . "$MNTPT/var/run/onmount.rc"
165   fi
166
167  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
168  exit 0
169
170 fi
171
172 exit 1