source: src-sh/warden/scripts/backend/startjail.sh @ 17235d6

9.1-release9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 17235d6 was 17235d6, checked in by Kris Moore <kris@…>, 21 months ago

Merge changes from John Hixson and FreeNAS work

  • Property mode set to 100755
File size: 9.4 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
144IP4=
145if [ -e "${JMETADIR}/ipv4" ] ; then
146  IP4=`cat "${JMETADIR}/ipv4"`
147fi
148
149IPS4=
150if [ -e "${JMETADIR}/alias-ipv4" ] ; then
151  while read line
152  do
153    IPS4="${IPS4} $line" 
154  done < ${JMETADIR}/alias-ipv4
155fi
156
157IP6=
158if [ -e "${JMETADIR}/ipv6" ] ; then
159  IP6=`cat "${JMETADIR}/ipv6"`
160fi
161
162IPS6=
163if [ -e "${JMETADIR}/alias-ipv6" ] ; then
164  while read line
165  do
166    IPS6="${IPS6} $line" 
167  done < ${JMETADIR}/alias-ipv6
168fi
169
170BRIDGE=
171
172# See if we need to create a new bridge, or use an existing one
173_bridges=`get_bridge_interfaces`
174if [ -n "${_bridges}" ] ; then
175   for _bridge in ${_bridges}
176   do
177      _members=`get_bridge_members ${_bridge}`
178      for _member in ${_members}
179      do
180         if [ "${_member}" = "${IFACE}" ] ; then
181            BRIDGE=${_bridge}
182            break
183         fi
184      done
185      if [ -n "${BRIDGE}" ] ; then
186         break
187      fi
188   done
189fi
190
191if [ -z "${BRIDGE}" ] ; then
192   BRIDGE=`ifconfig bridge create mtu ${MTU}`
193fi
194if [ -n "${IFACE}" ] ; then
195   if ! is_bridge_member "${BRIDGE}" "${IFACE}" ; then
196      ifconfig ${BRIDGE} addm ${IFACE}
197   fi
198fi
199
200# create epair for vimage jail
201EPAIRA=`ifconfig epair create mtu ${MTU}`
202ifconfig ${EPAIRA} up
203
204EPAIRB=`echo ${EPAIRA}|sed -E "s/([0-9])a$/\1b/g"`
205ifconfig ${BRIDGE} addm ${EPAIRA} up
206
207if [ -n "${BRIDGEIP4}" ] ; then
208   if ! ipv4_configured "${BRIDGE}" ; then
209      ifconfig ${BRIDGE} inet "${BRIDGEIP4}"
210
211   elif ! ipv4_address_configured "${BRIDGE}" "${BRIDGEIP4}" ; then
212      ifconfig ${BRIDGE} inet alias "${BRIDGEIP4}"
213   fi
214fi
215if [ -n "${BRIDGEIPS4}" ] ; then
216   for _ip in ${BRIDGEIPS4}
217   do
218      if ! ipv4_address_configured "${BRIDGE}" "${_ip}" ; then
219         ifconfig ${BRIDGE} inet alias "${_ip}"
220      fi
221   done
222fi
223
224if [ -n "${BRIDGEIP6}" ] ; then
225   if ! ipv6_configured "${BRIDGE}" ; then
226      ifconfig ${BRIDGE} inet6 "${BRIDGEIP6}"
227
228   elif ! ipv6_address_configured "${BRIDGE}" "${BRIDGEIP6}" ; then
229      ifconfig ${BRIDGE} inet6 alias "${BRIDGEIP6}"
230   fi
231fi
232if [ -n "${BRIDGEIPS6}" ] ; then
233   for _ip in ${BRIDGEIPS6}
234   do
235      if ! ipv6_address_configured "${BRIDGE}" "${_ip}" ; then
236         ifconfig ${BRIDGE} inet6 alias "${_ip}"
237      fi
238   done
239fi
240
241jFlags=""
242# Grab any additional jail flags
243if [ -e "${JMETADIR}/jail-flags" ] ; then
244  jFlags=`cat ${JMETADIR}/jail-flags`
245fi
246
247# Start the jail now
248echo "jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet"
249jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet
250if [ $? -ne 0 ] ; then
251   echo "ERROR: Failed starting jail with above command..."
252   umountjailxfs "${JAILNAME}"
253   exit 1
254fi
255
256JID="`jls | grep ${JAILDIR}$ | tr -s " " | cut -d " " -f 2`"
257
258# Move epairb into jail
259ifconfig ${EPAIRB} vnet ${JID}
260
261# Configure the IPv4 addresses
262if [ -n "${IP4}" ] ; then
263   jexec ${JID} ifconfig ${EPAIRB} inet "${IP4}"
264fi
265for ip4 in ${IPS4}
266do
267   ipv4_configured ${EPAIRB} ${JID}
268   if [ "$?" = "0" ] ; then
269      if ! ipv4_address_configured "${EPAIRB}" "${ip4}" "${JID}" ; then
270         jexec ${JID} ifconfig ${EPAIRB} inet alias ${ip4}
271      fi
272   else
273      jexec ${JID} ifconfig ${EPAIRB} inet ${ip4}
274   fi
275done
276
277# Configure the IPv6 addresses
278if [ -n "${IP6}" ] ; then
279   jexec ${JID} ifconfig ${EPAIRB} inet6 "${IP4}"
280fi
281for ip6 in ${IPS6}
282do
283   ipv6_configured ${EPAIRB} ${JID}
284   if [ "$?" = "0" ] ; then
285      if ! ipv6_address_configured "${EPAIRB}" "${ip6}" "${JID}" ; then
286         jexec ${JID} ifconfig ${EPAIRB} inet6 alias ${ip6}
287      fi
288   else
289      jexec ${JID} ifconfig ${EPAIRB} inet6 ${ip6}
290   fi
291done
292
293#
294# Configure default IPv4 gateway
295#
296if [ -n "${GATEWAY4}" ] ; then
297   jexec ${JID} route add -inet default ${GATEWAY4}
298
299#
300# No defaultrouter configured for IPv4, so if bridge IP address was
301# configured, we set the default router to that IP.
302#
303elif [ -n "${BRIDGEIP4}" ] ; then
304   get_ip_and_netmask "${BRIDGEIP4}"
305   jexec ${JID} route add -inet default ${JIP}
306fi
307
308#
309# Configure default IPv6 gateway
310#
311if [ -n "${GATEWAY6}" ] ; then
312   jexec ${JID} route add -inet6 default ${GATEWAY6}
313
314#
315# No defaultrouter configured for IPv6, so if bridge IP address was
316# configured, we set the default router to that IP.
317#
318elif [ -n "${BRIDGEIP6}" ] ; then
319   get_ip_and_netmask "${BRIDGEIP6}"
320   jexec ${JID} route add -inet6 default ${JIP}
321fi
322
323#
324# Set ourself to be a jail router with NAT. Don't
325# use PF since it will panic the box when used
326# with VIMAGE.
327#
328ip_forwarding=`sysctl -n net.inet.ip.forwarding`
329if [ "${ip_forwarding}" = "0" ] ; then
330   sysctl net.inet.ip.forwarding=1
331fi
332
333ip6_forwarding=`sysctl -n net.inet6.ip6.forwarding`
334if [ "${ip6_forwarding}" = "0" ] ; then
335   sysctl net.inet6.ip6.forwarding=1
336fi
337
338firewall_enable=`egrep '^firewall_enable' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
339firewall_type=`egrep '^firewall_type' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
340
341if [ "${firewall_enable}" != "YES" -o "${firewall_type}" != "open" ] ; then
342   tmp_rcconf=`mktemp /tmp/.wdn.XXXXXX`
343   egrep -v '^firewall_(enable|type)' /etc/rc.conf >> "${tmp_rcconf}"
344
345   cat<<__EOF__>>"${tmp_rcconf}"
346firewall_enable="YES"
347firewall_type="open"
348__EOF__
349
350   if [ -s "${tmp_rcconf}" ] ; then
351      cp /etc/rc.conf /var/tmp/rc.conf.bak
352      mv "${tmp_rcconf}" /etc/rc.conf
353      if [ "$?" != "0" ] ; then
354         mv /var/tmp/rc.conf.bak /etc/rc.conf
355      fi
356   fi
357   /etc/rc.d/ipfw forcerestart
358fi
359
360instance=`get_ipfw_nat_instance "${IFACE}"`
361if [ -z "${instance}" ] ; then
362echo "NAT IS NULL"
363   priority=`get_ipfw_nat_priority`
364   instance=`get_ipfw_nat_instance`
365
366   ipfw "${priority}" add nat "${instance}" all from any to any
367   ipfw nat "${instance}" config if "${IFACE}" reset
368fi
369
370if [ "$LINUXJAIL" = "YES" ] ; then
371  # If we have a custom start script
372  if [ -e "${JMETADIR}/jail-start" ] ; then
373    sCmd=`cat ${JMETADIR}/jail-start`
374    echo "Starting jail with: ${sCmd}"
375    jexec ${JID} ${sCmd} 2>&1
376  else
377    # Check for different init styles
378    if [ -e "${JAILDIR}/etc/init.d/rc" ] ; then
379      jexec ${JID} /bin/sh /etc/init.d/rc 3 2>&1
380    elif [ -e "${JAILDIR}/etc/rc" ] ; then
381      jexec ${JID} /bin/sh /etc/rc 3 2>&1
382    fi
383  fi
384else
385  # If we have a custom start script
386  if [ -e "${JMETADIR}/jail-start" ] ; then
387    sCmd=`cat ${JMETADIR}/jail-start`
388    echo "Starting jail with: ${sCmd}"
389    jexec ${JID} ${sCmd} 2>&1
390  else
391    echo "Starting jail with: /etc/rc"
392    jexec ${JID} /bin/sh /etc/rc 2>&1
393  fi
394fi
Note: See TracBrowser for help on using the repository browser.