source: src-sh/warden/scripts/backend/startjail.sh @ 738f740

9.1-release9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 738f740 was 738f740, checked in by Kris Moore <kris@…>, 12 months ago

Allow us to use %%JAILDIR%% in the user-supplied fstab, will make it more
portable when moving jails between systems

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