7ef9b9f3a2cc8be26817e5e446fd5262b93c0481
[easy-deb-chroot] / diablo / 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 umount -fl $CHROOT/dev/pts
45
46 #Any external mounts
47 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
48 for MDRV in $MNTD ; do
49   echo "unmounting $MDRV"
50   umount -l "$MDRV"
51 done
52
53 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
54   rm "$CHROOT/var/lock/qmount-complete"
55 fi
56
57 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
58   rm "$CHROOT/var/lock/chroot-complete"
59 fi
60
61 /sbin/qumount $CHROOT
62
63 echo "chroot closed."
64 exit 0