source: src-sh/warden/scripts/backend/stopjail.sh @ 02d7a86

9.2-release
Last change on this file since 02d7a86 was 2c39b15, checked in by Kris Moore <kris@…>, 11 months ago

Remove experimental pbibox functionality, not going to be used.

  • Property mode set to 100755
File size: 5.0 KB
Line 
1#!/bin/sh
2# Script to stop a jail
3# Args $1 = jail-name
4#######################################################################
5
6# Source our functions
7PROGDIR="/usr/local/share/warden"
8
9# Source our variables
10. ${PROGDIR}/scripts/backend/functions.sh
11
12JAILNAME="${1}"
13if [ "${2}" = "FAST" ]
14then
15  FAST="Y"
16fi
17
18if [ -z "${JAILNAME}" ]
19then
20  echo "ERROR: No jail specified to delete!"
21  exit 5
22fi
23
24if [ -z "${JDIR}" ]
25then
26  echo "ERROR: JDIR is unset!!!!"
27  exit 5
28fi
29
30JAILDIR="${JDIR}/${JAILNAME}"
31
32if [ ! -d "${JAILDIR}" ]
33then
34  echo "ERROR: No jail located at ${JAILDIR}"
35  exit 5
36fi
37
38HOST="`cat ${JMETADIR}/host`"
39
40# Check if we need to enable vnet
41VIMAGEENABLE="NO"
42if [ -e "${JMETADIR}/vnet" ] ; then
43  VIMAGEENABLE="YES"
44fi
45
46IFACE=
47DEFAULT=0
48 
49# Make sure jail uses special interface if specified
50if [ -e "${JMETADIR}/iface" ] ; then
51  IFACE=`cat "${JMETADIR}/iface"`
52fi
53if [ -z "${IFACE}" ] ; then
54   IFACE=`get_default_interface`
55   DEFAULT=1
56fi
57if [ -z "${IFACE}" ] ; then
58  echo "ERROR: no interface specified and a default doesn't exist!"
59  exit 6
60fi
61
62# End of error checking, now shutdown this jail
63##################################################################
64
65echo -e "Stopping the jail...\c"
66
67# Get the JailID for this jail
68JID="`jls | grep ${JAILDIR}$ | tr -s " " | cut -d " " -f 2`"
69
70echo -e ".\c"
71
72# Check if we need umount x mnts
73if [ -e "${JMETADIR}/jail-portjail" ] ; then umountjailxfs ${JAILNAME} ; fi
74
75if [ "$VIMAGEENABLE" = "YES" ] ; then
76  jail_interfaces_down "${JID}"
77else
78  # Get list of IP4s for this jail
79  if [ -e "${JMETADIR}/ipv4" ] ; then
80    IP4S="`cat ${JMETADIR}/ipv4 | cut -d '/' -f 1`"
81  fi
82  if [ -e "${JMETADIR}/alias-ipv4" ] ; then
83    while read line
84    do
85      IP4S="${IP4S} `echo $line | cut -d '/' -f 1`"
86    done < ${JMETADIR}/alias-ipv4
87  fi
88
89  # Get list of IP6s for this jail
90  if [ -e "${JMETADIR}/ipv6" ] ; then
91    IP6S="`cat ${JMETADIR}/ipv6 | cut -d '/' -f 1`"
92  fi
93  if [ -e "${JMETADIR}/alias-ipv6" ] ; then
94    while read line
95    do
96      IP6S="${IP6S} `echo $line | cut -d '/' -f 1`"
97    done < ${JMETADIR}/alias-ipv6
98  fi
99 
100 
101  # Check if we need to remove the IP aliases from this jail
102  for _ip in $IP4S
103  do 
104    # See if active alias
105    ifconfig $IFACE | grep -q "${_ip}"
106    if [ $? -ne 0 ] ; then continue ; fi
107
108    ifconfig $IFACE inet -alias ${_ip}
109  done
110
111  for _ip in $IP6S
112  do 
113    # See if active alias
114    ifconfig $IFACE | grep -q "${_ip}"
115    if [ $? -ne 0 ] ; then continue ; fi
116
117    ifconfig $IFACE inet6 ${_ip} delete
118  done
119fi
120
121if [ -e "${JMETADIR}/jail-linux" ] ; then LINUXJAIL="YES" ; fi
122
123# Check for user-supplied mounts
124if [ -e "${JMETADIR}/fstab" ] ; then
125   echo "Unmounting user-supplied file-systems"
126   cp ${JMETADIR}/fstab /tmp/.wardenfstab.$$
127   sed -i '' "s|%%JAILDIR%%|${JAILDIR}|g" /tmp/.wardenfstab.$$
128   umount -a -F /tmp/.wardenfstab.$$
129   rm /tmp/.wardenfstab.$$
130fi
131
132if [ "$LINUXJAIL" = "YES" ] ; then
133  # If we have a custom stop script
134  if [ -e "${JMETADIR}/jail-stop" ] ; then
135    sCmd=`cat ${JMETADIR}/jail-stop`
136    echo "Stopping jail with: ${sCmd}"
137    if [ -n "${JID}" ] ; then
138      jexec ${JID} ${sCmd} 2>&1
139    fi
140  else
141    # Check for different init styles
142    if [ -e "${JAILDIR}/etc/init.d/rc" ] ; then
143      if [ -n "${JID}" ] ; then
144        jexec ${JID} /bin/sh /etc/init.d/rc 0 2>&1
145      fi
146    elif [ -e "${JAILDIR}/etc/rc" ] ; then
147      if [ -n "${JID}" ] ; then
148        jexec ${JID} /bin/sh /etc/rc 0 2>&1
149      fi
150    fi
151  fi
152  sleep 3
153
154  umount -f ${JAILDIR}/sys 2>/dev/null
155  umount -f ${JAILDIR}/dev/fd 2>/dev/null
156  umount -f ${JAILDIR}/dev 2>/dev/null
157  umount -f ${JAILDIR}/lib/init/rw 2>/dev/null
158else
159  # If we have a custom stop script
160  if [ -e "${JMETADIR}/jail-stop" ] ; then
161    if [ -n "${JID}" ] ; then
162      sCmd=`cat ${JMETADIR}/jail-stop`
163      echo "Stopping jail with: ${sCmd}"
164      jexec ${JID} ${sCmd} 2>&1
165    fi
166  else
167    if [ -n "${JID}" ] ; then
168      echo "Stopping jail with: /etc/rc.shutdown"
169      jexec ${JID} /bin/sh /etc/rc.shutdown >/dev/null 2>/dev/null
170    fi
171  fi
172fi
173
174umount -f ${JAILDIR}/dev >/dev/null 2>/dev/null
175
176echo -e ".\c"
177
178# Skip the time consuming portion if we are shutting down
179if [ "$FAST" != "Y" ]
180then
181
182# We asked nicely, so now kill the jail for sure
183killall -j ${JID} -TERM 2>/dev/null
184sleep 1
185killall -j ${JID} -KILL 2>/dev/null
186
187echo -e ".\c"
188
189# Check if we need to unmount the devfs in jail
190mount | grep "${JAILDIR}/dev" >/dev/null 2>/dev/null
191if [ "$?" = "0" ]
192then
193  # Setup a 60 second timer to try and umount devfs, since takes a bit
194  SEC="0"
195  while
196   i=1
197  do
198   sleep 2
199
200   # Try to unmount dev
201   umount -f "${JAILDIR}/dev" 2>/dev/null
202   if [ "$?" = "0" ]
203   then
204      break
205   fi
206
207   SEC="`expr $SEC + 2`"
208   echo -e ".\c"
209
210   if [ ${SEC} -gt 60 ]
211   then
212      break
213   fi
214
215  done
216fi
217
218# Check if we need to unmount any extra dirs
219mount | grep "${JAILDIR}/proc" >/dev/null 2>/dev/null
220if [ "$?" = "0" ]; then
221  umount -f "${JAILDIR}/proc"
222fi
223
224if [ -e "${JMETADIR}/jail-portjail" ] ; then
225  umountjailxfs
226fi
227
228fi # End of FAST check
229
230echo -e ".\c"
231
232if [ -n "${JID}" ] ; then
233  jail -r ${JID}
234fi
235
236echo -e "Done"
Note: See TracBrowser for help on using the repository browser.