Some fixes and cleanup of Fremantle easy-chroot scripts
[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 #
33
34 TEST1=`mount | grep " $CHROOT "`
35 if [ "x$TEST1" != "x" ] ; then
36   echo "...Killing chroot apps..."
37   fuser -m "$CHROOT" -k
38 else
39   fuser "$CHROOT" -k
40 fi
41
42 echo "..Unmounting bound dirs..."
43
44 #Any external mounts
45
46 umount -fl $CHROOT/home/user/MyDocs
47 umount -fl $CHROOT/dev/pts
48
49 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
50 for MDRV in $MNTD ; do
51   echo "unmounting $MDRV"
52   umount -l "$MDRV"
53 done
54
55 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
56   rm "$CHROOT/var/lock/qmount-complete"
57 fi
58
59 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
60   rm "$CHROOT/var/lock/chroot-complete"
61 fi
62
63 /sbin/qumount $CHROOT
64
65 echo "chroot closed."
66 exit 0