This is the version in the Maemo repositories. Sorry for not updating sooner.
[easy-deb-chroot] / fremantle / easy-chroot / src / sbin / closechroot
1 #!/bin/sh
2 #Close a mounted chroot; this means killing all the chroot apps and unmounting the bound directories.
3
4 # By Alan M Bruce (qole)
5 #
6 # GPL licensed; keep code free!
7
8 if [ "`whoami`" != "root" ] ; then
9   echo "please run me as root!"
10   exit 9
11 fi
12
13 #Try to get the chroot location from the first parameter
14 CHROOT=$1
15
16 #Try to get the chroot location from the config file... 
17 if [ "x$CHROOT" = x ] ; then
18   #Pull in the config, if possible...
19   [ -f /home/user/.chroot ] && . /home/user/.chroot
20   #Still not set? Set to default
21   [ "x$CHROOT" != x ] || CHROOT=/debian
22 fi
23
24 # Strip off a trailing slash
25 LASTCHAR=`echo $CHROOT | cut -c ${#CHROOT}`
26 if [ "$LASTCHAR" = "/" ] ; then
27    echo "..stripping trailing slash..." >/dev/stderr
28    CHROOT=`echo $CHROOT | cut -c 0-$((${#CHROOT}-1))`
29 fi
30
31 #Abort if chroot not mounted.
32 if [ ! -f "$CHROOT/var/lock/qmount-complete" ] ; then
33   echo "Nothing to do; chroot not mounted!"
34   exit 1
35 fi
36
37 echo "Closing the chroot..."
38
39   # Place any commands you wish to run before you close the chroot
40   # into the /var/run/onclosechroot-ext.rc file (inside your rootfs)
41
42   if [ -f "$CHROOT/var/run/onclosechroot-ext.rc" ] ; then
43     . "$CHROOT/var/run/onclosechroot-ext.rc"
44   fi
45
46   # Place any commands you wish to run from inside the chroot 
47   # before you close the chroot into the /var/run/onclosechroot.rc
48   # file (inside your rootfs)
49
50   if [ -f "$CHROOT/var/run/onclosechroot.rc" ] ; then
51     chroot $CHROOT "/var/run/onclosechroot.rc"
52   fi
53
54
55 # Fremantle's fuser command is broken.
56 # We can either use Debian's one instead (as gfuser),
57 # or we can use the workaround: "cd /proc" first.
58
59 echo "...closing chroot apps..."
60
61 TEST1=`mount | grep " $CHROOT "`
62 MAPPER=`mount | grep "/dev/mapper"`
63 if [ "x$TEST1" != "x" ] && [ "x$MAPPER" == "x" ] ; then
64   if [ -f "/bin/gfuser" ] ; then
65     gfuser -m "$CHROOT" -k
66   else
67     cd /proc
68     fuser -m "$CHROOT" -k
69   fi
70 else
71   if [ -f "/bin/gfuser" ] ; then
72     gfuser "$CHROOT" -k
73   else
74     cd /proc
75     fuser "$CHROOT" -k
76   fi
77 fi
78
79 echo "..Unmounting bound dirs..."
80
81 #Any external mounts
82
83 umount -fl $CHROOT/home/user/MyDocs
84 umount -fl $CHROOT/dev/pts
85 umount -fl $CHROOT/dev/shm
86
87 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
88 for MDRV in $MNTD ; do
89   echo "unmounting $MDRV"
90   umount -l "$MDRV"
91 done
92
93 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
94   rm "$CHROOT/var/lock/qmount-complete"
95 fi
96
97 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
98   rm "$CHROOT/var/lock/chroot-complete"
99 fi
100
101 /sbin/qumount $CHROOT
102
103 echo "chroot closed."
104 exit 0