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

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since a68c384 was a68c384, checked in by Kris Moore <kris@…>, 11 months ago

Add some extra checks when we import old jails -> new format

Make sure IPV4 ends up with a /24 at the end

Also check at jail startup, in case somehow user imported jail with wrong config

  • Property mode set to 100755
File size: 9.9 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 $IPV4 | grep -q '/' 
159  if [ $? -ne 0 ] ; then
160     IPV4="${IPV4}/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 $IPV6 | grep -q '/' 
177  if [ $? -ne 0 ] ; then
178     IPV6="${IPV6}/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   jexec ${JID} ifconfig ${EPAIRB} inet "${IP4}"
284fi
285for ip4 in ${IPS4}
286do
287   ipv4_configured ${EPAIRB} ${JID}
288   if [ "$?" = "0" ] ; then
289      if ! ipv4_address_configured "${EPAIRB}" "${ip4}" "${JID}" ; then
290         jexec ${JID} ifconfig ${EPAIRB} inet alias ${ip4}
291      fi
292   else
293      jexec ${JID} ifconfig ${EPAIRB} inet ${ip4}
294   fi
295done
296
297# Configure the IPv6 addresses
298if [ -n "${IP6}" ] ; then
299   jexec ${JID} ifconfig ${EPAIRB} inet6 "${IP4}"
300fi
301for ip6 in ${IPS6}
302do
303   ipv6_configured ${EPAIRB} ${JID}
304   if [ "$?" = "0" ] ; then
305      if ! ipv6_address_configured "${EPAIRB}" "${ip6}" "${JID}" ; then
306         jexec ${JID} ifconfig ${EPAIRB} inet6 alias ${ip6}
307      fi
308   else
309      jexec ${JID} ifconfig ${EPAIRB} inet6 ${ip6}
310   fi
311done
312
313#
314# Configure default IPv4 gateway
315#
316if [ -n "${GATEWAY4}" ] ; then
317   jexec ${JID} route add -inet default ${GATEWAY4}
318
319#
320# No defaultrouter configured for IPv4, so if bridge IP address was
321# configured, we set the default router to that IP.
322#
323elif [ -n "${BRIDGEIP4}" ] ; then
324   get_ip_and_netmask "${BRIDGEIP4}"
325   jexec ${JID} route add -inet default ${JIP}
326fi
327
328#
329# Configure default IPv6 gateway
330#
331if [ -n "${GATEWAY6}" ] ; then
332   jexec ${JID} route add -inet6 default ${GATEWAY6}
333
334#
335# No defaultrouter configured for IPv6, so if bridge IP address was
336# configured, we set the default router to that IP.
337#
338elif [ -n "${BRIDGEIP6}" ] ; then
339   get_ip_and_netmask "${BRIDGEIP6}"
340   jexec ${JID} route add -inet6 default ${JIP}
341fi
342
343#
344# Set ourself to be a jail router with NAT. Don't
345# use PF since it will panic the box when used
346# with VIMAGE.
347#
348ip_forwarding=`sysctl -n net.inet.ip.forwarding`
349if [ "${ip_forwarding}" = "0" ] ; then
350   sysctl net.inet.ip.forwarding=1
351fi
352
353ip6_forwarding=`sysctl -n net.inet6.ip6.forwarding`
354if [ "${ip6_forwarding}" = "0" ] ; then
355   sysctl net.inet6.ip6.forwarding=1
356fi
357
358firewall_enable=`egrep '^firewall_enable' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
359firewall_type=`egrep '^firewall_type' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
360
361if [ "${firewall_enable}" != "YES" -o "${firewall_type}" != "open" ] ; then
362   tmp_rcconf=`mktemp /tmp/.wdn.XXXXXX`
363   egrep -v '^firewall_(enable|type)' /etc/rc.conf >> "${tmp_rcconf}"
364
365   cat<<__EOF__>>"${tmp_rcconf}"
366firewall_enable="YES"
367firewall_type="open"
368__EOF__
369
370   if [ -s "${tmp_rcconf}" ] ; then
371      cp /etc/rc.conf /var/tmp/rc.conf.bak
372      mv "${tmp_rcconf}" /etc/rc.conf
373      if [ "$?" != "0" ] ; then
374         mv /var/tmp/rc.conf.bak /etc/rc.conf
375      fi
376   fi
377   /etc/rc.d/ipfw forcerestart
378fi
379
380instance=`get_ipfw_nat_instance "${IFACE}"`
381if [ -z "${instance}" ] ; then
382   priority=`get_ipfw_nat_priority`
383   instance=`get_ipfw_nat_instance`
384
385   ipfw "${priority}" add nat "${instance}" all from any to any
386   ipfw nat "${instance}" config if "${IFACE}" reset
387fi
388
389if [ "$LINUXJAIL" = "YES" ] ; then
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    # Check for different init styles
397    if [ -e "${JAILDIR}/etc/init.d/rc" ] ; then
398      jexec ${JID} /bin/sh /etc/init.d/rc 3 2>&1
399    elif [ -e "${JAILDIR}/etc/rc" ] ; then
400      jexec ${JID} /bin/sh /etc/rc 3 2>&1
401    fi
402  fi
403else
404  # If we have a custom start script
405  if [ -e "${JMETADIR}/jail-start" ] ; then
406    sCmd=`cat ${JMETADIR}/jail-start`
407    echo "Starting jail with: ${sCmd}"
408    jexec ${JID} ${sCmd} 2>&1
409  else
410    echo "Starting jail with: /etc/rc"
411    jexec ${JID} /bin/sh /etc/rc 2>&1
412  fi
413fi
Note: See TracBrowser for help on using the repository browser.