source: src-sh/warden/scripts/backend/startjail.sh @ 43c8ecf

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 43c8ecf was 43c8ecf, checked in by Kris Moore <kris@…>, 18 months ago

Fix a few typos

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