source: src-sh/warden/scripts/backend/startjail.sh @ 5cb55b5

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 5cb55b5 was 5cb55b5, checked in by John Hixson <john@…>, 16 months ago

Remove debug statement

  • Property mode set to 100755
File size: 9.5 KB
Line 
1#/bin/sh
2# Script to startup 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}"
13
14if [ -z "${JAILNAME}" ]
15then
16  echo "ERROR: No jail specified to start!"
17  exit 5
18fi
19
20if [ -z "${JDIR}" ]
21then
22  echo "ERROR: JDIR is unset!!!!"
23  exit 5
24fi
25
26JAILDIR="${JDIR}/${JAILNAME}"
27
28if [ ! -d "${JAILDIR}" ]
29then
30  echo "ERROR: No jail located at ${JAILDIR}"
31  exit 5
32fi
33
34# Make sure the jail is NOT already running
35jls | grep ${JAILDIR}$ >/dev/null 2>/dev/null
36if [ "$?" = "0" ]
37then
38  echo "ERROR: Jail appears to be running already!"
39  exit 6
40fi
41
42IFACE=
43DEFAULT=0
44
45# Make sure jail uses special interface if specified
46if [ -e "${JMETADIR}/iface" ] ; then
47  IFACE=`cat "${JMETADIR}/iface"`
48fi
49if [ -z "${IFACE}" ] ; then
50   IFACE=`get_default_interface`
51   DEFAULT=1
52fi
53if [ -z "${IFACE}" ] ; then
54  echo "ERROR: no interface specified and a default doesn't exist!"
55  exit 6
56fi
57
58MTU=`ifconfig ${IFACE} | head -1 | sed -E 's/.*mtu ([0-9]+)/\1/g'`
59
60GATEWAY4=
61if [ -e "${JMETADIR}/defaultrouter-ipv4" ] ; then
62  GATEWAY4=`cat "${JMETADIR}/defaultrouter-ipv4"`
63fi
64GATEWAY6=
65if [ -e "${JMETADIR}/defaultrouter-ipv6" ] ; then
66  GATEWAY6=`cat "${JMETADIR}/defaultrouter-ipv6"`
67fi
68
69BRIDGEIP4=
70if [ -e "${JMETADIR}/bridge-ipv4" ] ; then
71  BRIDGEIP4=`cat "${JMETADIR}/bridge-ipv4"`
72fi
73
74BRIDGEIPS4=
75if [ -e "${JMETADIR}/alias-bridge-ipv4" ] ; then
76  while read line
77  do
78    BRIDGEIPS4="${BRIDGEIPS4} $line" 
79  done < ${JMETADIR}/alias-bridge-ipv4
80fi
81
82BRIDGEIP6=
83if [ -e "${JMETADIR}/bridge-ipv6" ] ; then
84  BRIDGEIP6=`cat "${JMETADIR}/bridge-ipv6"`
85fi
86
87BRIDGEIPS6=
88if [ -e "${JMETADIR}/alias-bridge-ipv6" ] ; then
89  while read line
90  do
91    BRIDGEIPS6="${BRIDGEIPS6} $line" 
92  done < ${JMETADIR}/alias-bridge-ipv6
93fi
94
95set_warden_metadir
96
97if [ -e "${JMETADIR}/jail-linux" ] ; then
98   LINUXJAIL="YES"
99fi
100
101HOST="`cat ${JMETADIR}/host`"
102
103if is_symlinked_mountpoint ${JAILDIR}/dev; then
104   echo "${JAILDIR}/dev has symlink as parent, not mounting"
105else
106   mount -t devfs devfs "${JAILDIR}/dev"
107fi
108
109if [ "$LINUXJAIL" = "YES" ] ; then
110  # Linux Jail
111  if is_symlinked_mountpoint ${JAILDIR}/proc; then
112     echo "${JAILDIR}/proc has symlink as parent, not mounting"
113  else
114     mount -t linprocfs linproc "${JAILDIR}/proc"
115  fi
116  if is_symlinked_mountpoint ${JAILDIR}/dev/fd; then
117     echo "${JAILDIR}/dev/fd has symlink as parent, not mounting"
118  else
119     mount -t fdescfs null "${JAILDIR}/dev/fd"
120  fi
121  if is_symlinked_mountpoint ${JAILDIR}/sys; then
122     echo "${JAILDIR}/sys has symlink as parent, not mounting"
123  else
124     mount -t linsysfs linsys "${JAILDIR}/sys"
125  fi
126  if [ -e "${JAILDIR}/lib/init/rw" ] ; then
127    if is_symlinked_mountpoint ${JAILDIR}/lib/init/rw; then
128       echo "${JAILDIR}/lib/init/rw has symlink as parent, not mounting"
129    else
130       mount -t tmpfs tmpfs "${JAILDIR}/lib/init/rw"
131    fi
132  fi
133else
134  # FreeBSD Jail
135  if is_symlinked_mountpoint ${JAILDIR}/proc; then
136     echo "${JAILDIR}/proc has symlink as parent, not mounting"
137  else
138     mount -t procfs proc "${JAILDIR}/proc"
139  fi
140
141  if [ -e "${JMETADIR}/jail-portjail" ] ; then mountjailxfs ${JAILNAME} ; fi
142fi
143
144# Check for user-supplied mounts
145if [ -e "${JMETADIR}/fstab" ] ; then
146   echo "Mounting user-supplied file-systems"
147   mount -a -F ${JMETADIR}/fstab
148fi
149
150IP4=
151if [ -e "${JMETADIR}/ipv4" ] ; then
152  IP4=`cat "${JMETADIR}/ipv4"`
153fi
154
155IPS4=
156if [ -e "${JMETADIR}/alias-ipv4" ] ; then
157  while read line
158  do
159    IPS4="${IPS4} $line" 
160  done < ${JMETADIR}/alias-ipv4
161fi
162
163IP6=
164if [ -e "${JMETADIR}/ipv6" ] ; then
165  IP6=`cat "${JMETADIR}/ipv6"`
166fi
167
168IPS6=
169if [ -e "${JMETADIR}/alias-ipv6" ] ; then
170  while read line
171  do
172    IPS6="${IPS6} $line" 
173  done < ${JMETADIR}/alias-ipv6
174fi
175
176BRIDGE=
177
178# See if we need to create a new bridge, or use an existing one
179_bridges=`get_bridge_interfaces`
180if [ -n "${_bridges}" ] ; then
181   for _bridge in ${_bridges}
182   do
183      _members=`get_bridge_members ${_bridge}`
184      for _member in ${_members}
185      do
186         if [ "${_member}" = "${IFACE}" ] ; then
187            BRIDGE=${_bridge}
188            break
189         fi
190      done
191      if [ -n "${BRIDGE}" ] ; then
192         break
193      fi
194   done
195fi
196
197if [ -z "${BRIDGE}" ] ; then
198   BRIDGE=`ifconfig bridge create mtu ${MTU}`
199fi
200if [ -n "${IFACE}" ] ; then
201   if ! is_bridge_member "${BRIDGE}" "${IFACE}" ; then
202      ifconfig ${BRIDGE} addm ${IFACE}
203   fi
204fi
205
206# create epair for vimage jail
207EPAIRA=`ifconfig epair create mtu ${MTU}`
208ifconfig ${EPAIRA} up
209
210EPAIRB=`echo ${EPAIRA}|sed -E "s/([0-9])a$/\1b/g"`
211ifconfig ${BRIDGE} addm ${EPAIRA} up
212
213if [ -n "${BRIDGEIP4}" ] ; then
214   if ! ipv4_configured "${BRIDGE}" ; then
215      ifconfig ${BRIDGE} inet "${BRIDGEIP4}"
216
217   elif ! ipv4_address_configured "${BRIDGE}" "${BRIDGEIP4}" ; then
218      ifconfig ${BRIDGE} inet alias "${BRIDGEIP4}"
219   fi
220fi
221if [ -n "${BRIDGEIPS4}" ] ; then
222   for _ip in ${BRIDGEIPS4}
223   do
224      if ! ipv4_address_configured "${BRIDGE}" "${_ip}" ; then
225         ifconfig ${BRIDGE} inet alias "${_ip}"
226      fi
227   done
228fi
229
230if [ -n "${BRIDGEIP6}" ] ; then
231   if ! ipv6_configured "${BRIDGE}" ; then
232      ifconfig ${BRIDGE} inet6 "${BRIDGEIP6}"
233
234   elif ! ipv6_address_configured "${BRIDGE}" "${BRIDGEIP6}" ; then
235      ifconfig ${BRIDGE} inet6 alias "${BRIDGEIP6}"
236   fi
237fi
238if [ -n "${BRIDGEIPS6}" ] ; then
239   for _ip in ${BRIDGEIPS6}
240   do
241      if ! ipv6_address_configured "${BRIDGE}" "${_ip}" ; then
242         ifconfig ${BRIDGE} inet6 alias "${_ip}"
243      fi
244   done
245fi
246
247jFlags=""
248# Grab any additional jail flags
249if [ -e "${JMETADIR}/jail-flags" ] ; then
250  jFlags=`cat ${JMETADIR}/jail-flags`
251fi
252
253# Start the jail now
254echo "jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet"
255jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet
256if [ $? -ne 0 ] ; then
257   echo "ERROR: Failed starting jail with above command..."
258   umountjailxfs "${JAILNAME}"
259   exit 1
260fi
261
262JID="`jls | grep ${JAILDIR}$ | tr -s " " | cut -d " " -f 2`"
263
264# Move epairb into jail
265ifconfig ${EPAIRB} vnet ${JID}
266
267# Configure the IPv4 addresses
268if [ -n "${IP4}" ] ; then
269   jexec ${JID} ifconfig ${EPAIRB} inet "${IP4}"
270fi
271for ip4 in ${IPS4}
272do
273   ipv4_configured ${EPAIRB} ${JID}
274   if [ "$?" = "0" ] ; then
275      if ! ipv4_address_configured "${EPAIRB}" "${ip4}" "${JID}" ; then
276         jexec ${JID} ifconfig ${EPAIRB} inet alias ${ip4}
277      fi
278   else
279      jexec ${JID} ifconfig ${EPAIRB} inet ${ip4}
280   fi
281done
282
283# Configure the IPv6 addresses
284if [ -n "${IP6}" ] ; then
285   jexec ${JID} ifconfig ${EPAIRB} inet6 "${IP4}"
286fi
287for ip6 in ${IPS6}
288do
289   ipv6_configured ${EPAIRB} ${JID}
290   if [ "$?" = "0" ] ; then
291      if ! ipv6_address_configured "${EPAIRB}" "${ip6}" "${JID}" ; then
292         jexec ${JID} ifconfig ${EPAIRB} inet6 alias ${ip6}
293      fi
294   else
295      jexec ${JID} ifconfig ${EPAIRB} inet6 ${ip6}
296   fi
297done
298
299#
300# Configure default IPv4 gateway
301#
302if [ -n "${GATEWAY4}" ] ; then
303   jexec ${JID} route add -inet default ${GATEWAY4}
304
305#
306# No defaultrouter configured for IPv4, so if bridge IP address was
307# configured, we set the default router to that IP.
308#
309elif [ -n "${BRIDGEIP4}" ] ; then
310   get_ip_and_netmask "${BRIDGEIP4}"
311   jexec ${JID} route add -inet default ${JIP}
312fi
313
314#
315# Configure default IPv6 gateway
316#
317if [ -n "${GATEWAY6}" ] ; then
318   jexec ${JID} route add -inet6 default ${GATEWAY6}
319
320#
321# No defaultrouter configured for IPv6, so if bridge IP address was
322# configured, we set the default router to that IP.
323#
324elif [ -n "${BRIDGEIP6}" ] ; then
325   get_ip_and_netmask "${BRIDGEIP6}"
326   jexec ${JID} route add -inet6 default ${JIP}
327fi
328
329#
330# Set ourself to be a jail router with NAT. Don't
331# use PF since it will panic the box when used
332# with VIMAGE.
333#
334ip_forwarding=`sysctl -n net.inet.ip.forwarding`
335if [ "${ip_forwarding}" = "0" ] ; then
336   sysctl net.inet.ip.forwarding=1
337fi
338
339ip6_forwarding=`sysctl -n net.inet6.ip6.forwarding`
340if [ "${ip6_forwarding}" = "0" ] ; then
341   sysctl net.inet6.ip6.forwarding=1
342fi
343
344firewall_enable=`egrep '^firewall_enable' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
345firewall_type=`egrep '^firewall_type' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
346
347if [ "${firewall_enable}" != "YES" -o "${firewall_type}" != "open" ] ; then
348   tmp_rcconf=`mktemp /tmp/.wdn.XXXXXX`
349   egrep -v '^firewall_(enable|type)' /etc/rc.conf >> "${tmp_rcconf}"
350
351   cat<<__EOF__>>"${tmp_rcconf}"
352firewall_enable="YES"
353firewall_type="open"
354__EOF__
355
356   if [ -s "${tmp_rcconf}" ] ; then
357      cp /etc/rc.conf /var/tmp/rc.conf.bak
358      mv "${tmp_rcconf}" /etc/rc.conf
359      if [ "$?" != "0" ] ; then
360         mv /var/tmp/rc.conf.bak /etc/rc.conf
361      fi
362   fi
363   /etc/rc.d/ipfw forcerestart
364fi
365
366instance=`get_ipfw_nat_instance "${IFACE}"`
367if [ -z "${instance}" ] ; then
368   priority=`get_ipfw_nat_priority`
369   instance=`get_ipfw_nat_instance`
370
371   ipfw "${priority}" add nat "${instance}" all from any to any
372   ipfw nat "${instance}" config if "${IFACE}" reset
373fi
374
375if [ "$LINUXJAIL" = "YES" ] ; then
376  # If we have a custom start script
377  if [ -e "${JMETADIR}/jail-start" ] ; then
378    sCmd=`cat ${JMETADIR}/jail-start`
379    echo "Starting jail with: ${sCmd}"
380    jexec ${JID} ${sCmd} 2>&1
381  else
382    # Check for different init styles
383    if [ -e "${JAILDIR}/etc/init.d/rc" ] ; then
384      jexec ${JID} /bin/sh /etc/init.d/rc 3 2>&1
385    elif [ -e "${JAILDIR}/etc/rc" ] ; then
386      jexec ${JID} /bin/sh /etc/rc 3 2>&1
387    fi
388  fi
389else
390  # If we have a custom start script
391  if [ -e "${JMETADIR}/jail-start" ] ; then
392    sCmd=`cat ${JMETADIR}/jail-start`
393    echo "Starting jail with: ${sCmd}"
394    jexec ${JID} ${sCmd} 2>&1
395  else
396    echo "Starting jail with: /etc/rc"
397    jexec ${JID} /bin/sh /etc/rc 2>&1
398  fi
399fi
Note: See TracBrowser for help on using the repository browser.