cdb2c896fb304ad9427595b5b294d20ffbd53418
[easy-deb-chroot] / fremantle / easy-chroot / .py2deb_build_folder / 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 # Fremantle's fuser command is broken.
40 # We can either use Debian's one instead (as gfuser),
41 # or we can use the workaround: "cd /proc" first.
42
43 echo "...closing chroot apps..."
44
45 TEST1=`mount | grep " $CHROOT "`
46 if [ "x$TEST1" != "x" ] ; then
47   if [ -f "/bin/gfuser" ] ; then
48     gfuser -m "$CHROOT" -k
49   else
50     cd /proc
51     fuser -m "$CHROOT" -k
52   fi
53 else
54   if [ -f "/bin/gfuser" ] ; then
55     gfuser "$CHROOT" -k
56   else
57     cd /proc
58     fuser "$CHROOT" -k
59   fi
60 fi
61
62 echo "..Unmounting bound dirs..."
63
64 #Any external mounts
65
66 umount -fl $CHROOT/home/user/MyDocs
67 umount -fl $CHROOT/dev/pts
68 umount -fl $CHROOT/dev/shm
69
70 MNTD=`cat /proc/mounts | grep " $CHROOT/" | awk '{print $2}'`
71 for MDRV in $MNTD ; do
72   echo "unmounting $MDRV"
73   umount -l "$MDRV"
74 done
75
76 if [ -f "$CHROOT/var/lock/qmount-complete" ] ; then
77   rm "$CHROOT/var/lock/qmount-complete"
78 fi
79
80 if [ -f "$CHROOT/var/lock/chroot-complete" ] ; then
81   rm "$CHROOT/var/lock/chroot-complete"
82 fi
83
84 /sbin/qumount $CHROOT
85
86 echo "chroot closed."
87 exit 0