Trying to upload all related projects
[easy-deb-chroot] / fremantle / easy-deb-chroot / thp / 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
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      insmod /lib/modules/2.6.21-omap1/dm-mod.ko 2>/dev/null
111      insmod /lib/modules/2.6.21-omap1/dm-loop.ko 2>/dev/null
112      NEXTLOOP=`ls -l /dev/dm-* 2>/dev/null | tail -1 | awk '{print $NF}' | awk -F '-' '{print $NF+1}'`
113      if [ "x$NEXTLOOP" = "x"  ] ; then
114        NEXTLOOP=0
115      fi
116      DMLOMSG=`dmlosetup loop$NEXTLOOP "$IMGFILE" 2>&1`
117      MNTMSG=`mount -t "$IMGFS" /dev/dm-$NEXTLOOP "$MNTPT" -o noatime 2>&1` 
118      if [ "$?" != 0 ] ; then
119        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on loop$NEXTLOOP\n\n$DMLOMSG\n$MNTMSG"`
120        if [ ! -f "/usr/bin/gxmessage" ] ; then
121          echo $MSG1 >/dev/stderr
122        else
123          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
124        fi
125        exit 2
126      fi
127      echo ...$IMGFILE mounted on loop$NEXTLOOP >/dev/stderr
128    else
129      echo "mounting device: $IMGFILE" >/dev/stderr
130      if ! mount -t "$IMGFS" "$IMGFILE" "$MNTPT" -o ${LOOP}noatime ; then
131        MSG1=`printf "Mount failure!\n\n$IMGFILE failed to mount on $MNTPT"`
132        if [ ! -f "/usr/bin/gxmessage" ] ; then
133          echo $MSG1 >/dev/stderr
134        else
135          gxmessage -center -alignbuttons center -buttons GTK_STOCK_OK:0 -geometry 680x250 -title "EZ-CHROOT ERROR" "$MSG1"
136        fi
137        exit 3
138      fi
139
140    fi
141
142   else
143     echo "Not mounting any filesystem, chroot is $MNTPT" >/dev/stderr
144   fi 
145
146 #All set up. Set flag for next time...
147
148  if [ ! -d "$MNTPT/var/lock" ] ; then
149    mkdir -p "$MNTPT/var/lock"
150  fi
151
152  # Place any commands you wish to run the first time you mount
153  # into the $MNTPT/var/run/onmount.rc file
154
155   if [ -f "$MNTPT/var/run/onmount.rc" ] ; then
156     . "$MNTPT/var/run/onmount.rc"
157   fi
158
159  echo $IMGFILE > "$MNTPT/var/lock/qmount-complete"
160  exit 0
161
162 fi
163
164 exit 1