eda7e9e334bc12639922395d3f3802c266400013
[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 # Using Debian's one instead.
34
35 echo "...closing chroot apps..."
36
37 TEST1=`mount | grep " $CHROOT "`
38 if [ "x$TEST1" != "x" ] ; then
39   if [ -f "/bin/gfuser" ] ; then
40     gfuser -m "$CHROOT" -k
41   else
42     fuser -m "$CHROOT" -k
43   fi
44 else
45   if [ -f "/bin/gfuser" ] ; then
46     gfuser "$CHROOT" -k
47   else
48     fuser "$CHROOT" -k
49   fi
50 fi
51
52 echo "..Unmounting bound dirs..."
53
54 #Any external mounts
55
56 umount -fl $CHROOT/home/user/MyDocs
57 umount -fl $CHROOT/dev/pts
58 umount -fl $CHROOT/dev/shm
59
60 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
61 for MDRV in $MNTD ; do
62   echo "unmounting $MDRV"
63   umount -l "$MDRV"
64 done
65
66 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
67   rm "$CHROOT/var/lock/qmount-complete"
68 fi
69
70 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
71   rm "$CHROOT/var/lock/chroot-complete"
72 fi
73
74 /sbin/qumount $CHROOT
75
76 echo "chroot closed."
77 exit 0