2892bcffd4287f698f4216d1284aa63098de88e0
[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 #Abort if chroot not mounted.
25 if [ ! -f "$CHROOT/var/lock/qmount-complete" ] ; then
26   echo "Nothing to do; chroot not mounted!"
27   exit 1
28 fi
29
30 echo "Closing the chroot..."
31
32 # Fremantle's fuser command is broken.
33 # We can either use Debian's one instead (as gfuser),
34 # or we can use the workaround: "cd /proc" first.
35
36 echo "...closing chroot apps..."
37
38 TEST1=`mount | grep " $CHROOT "`
39 if [ "x$TEST1" != "x" ] ; then
40   if [ -f "/bin/gfuser" ] ; then
41     gfuser -m "$CHROOT" -k
42   else
43     cd /proc
44     fuser -m "$CHROOT" -k
45   fi
46 else
47   if [ -f "/bin/gfuser" ] ; then
48     gfuser "$CHROOT" -k
49   else
50     cd /proc
51     fuser "$CHROOT" -k
52   fi
53 fi
54
55 echo "..Unmounting bound dirs..."
56
57 #Any external mounts
58
59 umount -fl $CHROOT/home/user/MyDocs
60 umount -fl $CHROOT/dev/pts
61 umount -fl $CHROOT/dev/shm
62
63 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
64 for MDRV in $MNTD ; do
65   echo "unmounting $MDRV"
66   umount -l "$MDRV"
67 done
68
69 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
70   rm "$CHROOT/var/lock/qmount-complete"
71 fi
72
73 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
74   rm "$CHROOT/var/lock/chroot-complete"
75 fi
76
77 /sbin/qumount $CHROOT
78
79 echo "chroot closed."
80 exit 0